KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > session > SimpleSession


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.axis.session;
18
19 import java.util.Enumeration JavaDoc;
20 import java.util.Hashtable JavaDoc;
21
22 /**
23  * A trivial session implementation.
24  *
25  * @author Glen Daniels (gdaniels@apache.org)
26  */

27 public class SimpleSession implements Session
28 {
29     private Hashtable JavaDoc rep = null;
30     
31     /** Inactivity timeout (in seconds).
32      * Not used yet.
33      */

34     private int timeout = -1;
35     private long lastTouched;
36     
37     /**
38      * Default constructor - set lastTouched to now
39      */

40     public SimpleSession()
41     {
42         lastTouched = System.currentTimeMillis();
43     }
44                           
45     /** Get a property from the session
46      *
47      * @param key the name of the property desired.
48      */

49     public Object JavaDoc get(String JavaDoc key)
50     {
51         if (rep == null)
52             return null;
53         lastTouched = System.currentTimeMillis();
54         return rep.get(key);
55     }
56     
57     /** Set a property in the session
58      *
59      * @param key the name of the property to set.
60      * @param value the value of the property.
61      */

62     public void set(String JavaDoc key, Object JavaDoc value)
63     {
64         synchronized (this) {
65             if (rep == null)
66                 rep = new Hashtable JavaDoc();
67         }
68         lastTouched = System.currentTimeMillis();
69         rep.put(key, value);
70     }
71     
72     /** Remove a property from the session
73      *
74      * @param key the name of the property desired.
75      */

76     public void remove(String JavaDoc key)
77     {
78         if (rep != null)
79             rep.remove(key);
80         lastTouched = System.currentTimeMillis();
81     }
82
83     /**
84      * Get an enumeration of the keys in this session
85      */

86     public Enumeration JavaDoc getKeys() {
87         if (rep != null)
88             return rep.keys();
89         return null;
90     }
91
92     /** Set the session's time-to-live.
93      *
94      * This is implementation-specific, but basically should be the #
95      * of seconds of inactivity which will cause the session to time
96      * out and invalidate. "inactivity" is implementation-specific.
97      */

98     public void setTimeout(int timeout)
99     {
100         this.timeout = timeout;
101     }
102     
103     public int getTimeout()
104     {
105         return timeout;
106     }
107
108     /**
109      * "Touch" the session (mark it recently used)
110      */

111     public void touch() {
112         lastTouched = System.currentTimeMillis();
113     }
114
115     /**
116      * invalidate the session
117      */

118     public void invalidate() {
119         rep = null;
120         lastTouched = System.currentTimeMillis();
121         timeout = -1;
122     }
123     
124     public long getLastAccessTime()
125     {
126         return lastTouched;
127     }
128
129     /**
130      * Get an Object suitable for synchronizing the session. This method
131      * exists because different session implementations might provide
132      * different ways of getting at shared data. For a simple hashtable-
133      * based session, this would just be the hashtable, but for sessions
134      * which use database connections, etc. it might be an object wrapping
135      * a table ID or somesuch.
136      */

137     public synchronized Object JavaDoc getLockObject() {
138         if (rep == null) {
139             rep = new Hashtable JavaDoc();
140         }
141         return rep;
142     }
143 }
144
Popular Tags