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  package org.caleigo.security;
19  
20  import java.util.*;
21  
22  /*** Interface for session classes
23   *
24   * @author  tobias
25   */
26  public interface ISession 
27  {
28      /*** Returns the ID for this session instance.
29       * @return Session ID.
30       */    
31      public int getSessionID();
32      /*** Returns the UserInfo corresponding to this session.
33       * @return The <I>UserInfo</I>.
34       */    
35      public UserInfo getUserInfo();
36      /*** Returns the created time when this session was instantiated.
37       * @return Time in milliseconds.
38       */
39      public long getCreatedTime();
40      /*** Returns the time when this session was last accessed.
41       * @return Time in millisecond.
42       */
43      public long getLastAccessTime();
44      /*** Updates the time when the session was last accessed.
45       * @param time The time when the session was accessed.
46       */
47      public void updateLastAccessTime();
48      /*** This method is used to check if the session is valid.
49       * @return Returns true if the session is valid.
50       */
51      public boolean isValid();
52      /*** This method is used to retrieve data which is stored for this session
53       * instance. 
54       * @param key the key used to identify specific data.
55       * @return The data stored for the key.
56       */
57      public Object getAttribute(String key);
58      /*** This method is used to store session specific data. 
59       * @param key The key used to identify the data.
60       * @param attribute The data to be stored.
61       */
62      public void setAttribute(String key, Object attribute);
63  
64      public static class DefaultSession implements ISession
65      {
66          // Member variables ----------------------------------------------------
67          private int sessionID;
68          private HashMap attributes = new HashMap();
69          private long createdTime;
70          private long lastAccessTime;
71          private UserInfo userInfo;
72          
73          // Constructors --------------------------------------------------------
74          
75          public DefaultSession(UserInfo userInfo, int sessionID)
76          {
77              this.sessionID      = sessionID;
78              this.createdTime    = System.currentTimeMillis();
79              this.lastAccessTime = createdTime;
80              this.userInfo       = userInfo;
81          }
82          
83          public Object getAttribute(String key)
84          {
85              if (key != null)
86                  return attributes.get(key);
87              else
88                  return null;
89          }
90          
91          public long getCreatedTime()
92          {
93              return createdTime;
94          }
95          
96          public long getLastAccessTime()
97          {
98              return lastAccessTime;
99          }
100         
101         public void updateLastAccessTime()
102         {
103             lastAccessTime = System.currentTimeMillis();
104         }
105 
106         public int getSessionID()
107         {
108             return sessionID;
109         }
110         
111         public UserInfo getUserInfo()
112         {
113             return userInfo;
114         }
115         
116         public boolean isValid()
117         {
118             return true;
119         }
120         
121         public void setAttribute(String key, Object attribute)
122         {
123             if (key != null)
124             {
125                 if (attribute != null)
126                     attributes.put(key, attribute);
127                 else
128                     attributes.remove(key);
129             }
130         }
131     }
132 }