View Javadoc

1   /* (c) Copyright 2003 Caleigo AB, All rights reserved. 
2    * 
3    * This library is free software; you can redistribute it and/or
4    * modify it under the terms of the GNU Lesser General Public
5    * License as published by the Free Software Foundation; either
6    * version 2.1 of the License, or (at your option) any later version.
7    * 
8    * This library is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11   * Lesser General Public License for more details.
12   * 
13   * You should have received a copy of the GNU Lesser General Public
14   * License along with this library; if not, write to the Free Software
15   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16   *  
17   */
18  
19  package org.caleigo.security;
20  
21  import java.util.*;
22  import java.lang.reflect.*;
23  
24  import org.caleigo.toolkit.log.*;
25  
26  /*** <Description for SessionHandler>
27   *
28   * @author  Mattias Hagstrand
29   * @version 1.00
30   *
31   *//*
32    *
33    * WHEN        WHO               WHY & WHAT
34    * -----------------------------------------------------------------------------
35    * 2002-10-20  Mattias Hagstrand    Creation
36    */
37  public class SessionHandler
38  {
39      // Class members -----------------------------------------------------------
40      private static SessionHandler sInstance;
41      private static Class mSessionClass;
42      
43      // Static methods ----------------------------------------------------------
44      public static SessionHandler getInstance()
45      {
46          if (sInstance == null)
47              sInstance = new SessionHandler();
48          return sInstance;
49      }
50      
51      // Data members ------------------------------------------------------------
52      private int mCurrentSessionID;
53      protected HashMap mSessions;
54      
55      // Constructors ------------------------------------------------------------
56      private SessionHandler()
57      {
58          mSessions = new HashMap(20);
59          setSessionClass(ISession.DefaultSession.class);
60      }
61      
62      // Action methods ----------------------------------------------------------
63      
64      /*** Creates a new session and associates a session objetc with it.
65       */
66      public synchronized void createSession(UserInfo userInfo)
67      {
68          int sessionID = this.generateSessionID();
69          mSessions.put(new Integer(sessionID), createSession(userInfo, sessionID));
70          userInfo.setSessionID(sessionID);
71      }
72      
73      public synchronized void destroySession(int sessionID)
74      {
75          mSessions.remove(new Integer(sessionID));
76      }
77      
78      /*** Returns an ISessesion object for the provided session id, or <code>null</code>
79       * if no valid session exists. Note that invoker of this method should not
80       * cache the session object since it may be invalidated at any time.
81       */
82      public synchronized ISession getSession(int sessionID)
83      {
84          ISession session = (ISession) mSessions.get(new Integer(sessionID));
85          if (session != null && session.isValid())
86              return session;
87              
88          return null;
89      }
90      
91      public synchronized boolean isValidSession(UserInfo userInfo, int sessionID)
92      {
93          ISession session = (ISession) mSessions.get(new Integer(sessionID));
94          if (session != null &&
95              session.isValid() &&
96              session.getUserInfo().getUserID().compareTo(userInfo.getUserID()) == 0 &&
97              session.getUserInfo().getSessionID() == sessionID)
98              return true;
99          return false;
100     }
101     
102     public synchronized void setSessionClass(Class sessionClass)
103     {
104         if ( !ISession.class.isAssignableFrom(sessionClass) ) 
105             throw new IllegalArgumentException("The class " + sessionClass + 
106                 " does not implement ISession");
107         mSessionClass = sessionClass;
108     }
109     
110     // Help methods ------------------------------------------------------------
111     protected synchronized int generateSessionID()
112     {
113         return ++mCurrentSessionID;
114     }
115     
116     protected ISession createSession(UserInfo userInfo, int sessionID)
117     {
118         try
119         {
120             Constructor constructor = mSessionClass.getConstructor(new Class[] {UserInfo.class, Integer.TYPE});
121             ISession session = (ISession) constructor.newInstance(new Object[] {userInfo, new Integer(sessionID)});
122             return session;
123         }
124         catch (Exception e)
125         {
126             Log.printError(this, "Could not create session", e);
127             throw new RuntimeException("Error while creating a session object", e);
128         }
129     }
130 }