KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > cluster > session > ReplicatedSession


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

17
18 package org.apache.catalina.cluster.session;
19
20 /**
21  * Title: Tomcat Session Replication for Tomcat 4.0 <BR>
22  * Description: A very simple straight forward implementation of
23  * session replication of servers in a cluster.<BR>
24  * This session replication is implemented "live". By live
25  * I mean, when a session attribute is added into a session on Node A
26  * a message is broadcasted to other messages and setAttribute is called on the replicated
27  * sessions.<BR>
28  * A full description of this implementation can be found under
29  * <href="http://www.filip.net/tomcat/">Filip's Tomcat Page</a><BR>
30  *
31  * Copyright: See apache license
32  * Company: www.filip.net
33  * @author <a HREF="mailto:mail@filip.net">Filip Hanik</a>
34  * @version 1.0 for TC 4.0
35  * Description:<BR>
36  * The ReplicatedSession class is a simple extension of the StandardSession class
37  * It overrides a few methods (setAttribute, removeAttribute, expire, access) and has
38  * hooks into the InMemoryReplicationManager to broadcast and receive events from the cluster.<BR>
39  * This class inherits the readObjectData and writeObject data methods from the StandardSession
40  * and does not contain any serializable elements in addition to the inherited ones from the StandardSession
41  *
42  */

43 import org.apache.catalina.Manager;
44 import java.io.IOException JavaDoc;
45 import java.io.ObjectInputStream JavaDoc;
46 import java.io.ObjectOutputStream JavaDoc;
47 import java.security.Principal JavaDoc;
48
49 public class ReplicatedSession extends org.apache.catalina.session.StandardSession
50 implements org.apache.catalina.cluster.ClusterSession{
51
52     private transient Manager mManager = null;
53     protected boolean isDirty = false;
54     private transient long lastAccessWasDistributed = System.currentTimeMillis();
55     private boolean isPrimarySession=true;
56     
57
58     public ReplicatedSession(Manager manager) {
59         super(manager);
60         mManager = manager;
61     }
62
63
64     public boolean isDirty()
65     {
66         return isDirty;
67     }
68
69     public void setIsDirty(boolean dirty)
70     {
71         isDirty = dirty;
72     }
73
74
75     public void setLastAccessWasDistributed(long time) {
76         lastAccessWasDistributed = time;
77     }
78
79     public long getLastAccessWasDistributed() {
80         return lastAccessWasDistributed;
81     }
82
83
84     public void removeAttribute(String JavaDoc name) {
85         setIsDirty(true);
86         super.removeAttribute(name);
87     }
88
89     /**
90      * see parent description,
91      * plus we also notify other nodes in the cluster
92      */

93     public void removeAttribute(String JavaDoc name, boolean notify) {
94         setIsDirty(true);
95         super.removeAttribute(name,notify);
96     }
97
98
99     /**
100      * Sets an attribute and notifies the other nodes in the cluster
101      */

102     public void setAttribute(String JavaDoc name, Object JavaDoc value)
103     {
104         if ( value == null ) {
105           removeAttribute(name);
106           return;
107         }
108         if (!(value instanceof java.io.Serializable JavaDoc))
109             throw new java.lang.IllegalArgumentException JavaDoc("Value for attribute "+name+" is not serializable.");
110         setIsDirty(true);
111         super.setAttribute(name,value);
112     }
113
114     public void setMaxInactiveInterval(int interval) {
115         setIsDirty(true);
116         super.setMaxInactiveInterval(interval);
117     }
118
119
120     /**
121      * Sets the manager for this session
122      * @param mgr - the servers InMemoryReplicationManager
123      */

124     public void setManager(SimpleTcpReplicationManager mgr)
125     {
126         mManager = mgr;
127         super.setManager(mgr);
128     }
129
130
131     /**
132      * Set the authenticated Principal that is associated with this Session.
133      * This provides an <code>Authenticator</code> with a means to cache a
134      * previously authenticated Principal, and avoid potentially expensive
135      * <code>Realm.authenticate()</code> calls on every request.
136      *
137      * @param principal The new Principal, or <code>null</code> if none
138      */

139     public void setPrincipal(Principal JavaDoc principal) {
140         super.setPrincipal(principal);
141         setIsDirty(true);
142     }
143
144     public void expire() {
145         SimpleTcpReplicationManager mgr =(SimpleTcpReplicationManager)getManager();
146         mgr.sessionInvalidated(getId());
147         setIsDirty(true);
148         super.expire();
149     }
150
151     public void invalidate() {
152         SimpleTcpReplicationManager mgr =(SimpleTcpReplicationManager)getManager();
153         mgr.sessionInvalidated(getId());
154         setIsDirty(true);
155         super.invalidate();
156     }
157
158
159     /**
160      * Read a serialized version of the contents of this session object from
161      * the specified object input stream, without requiring that the
162      * StandardSession itself have been serialized.
163      *
164      * @param stream The object input stream to read from
165      *
166      * @exception ClassNotFoundException if an unknown class is specified
167      * @exception IOException if an input/output error occurs
168      */

169     public void readObjectData(ObjectInputStream JavaDoc stream)
170         throws ClassNotFoundException JavaDoc, IOException JavaDoc {
171
172         super.readObjectData(stream);
173
174     }
175
176
177     /**
178      * Write a serialized version of the contents of this session object to
179      * the specified object output stream, without requiring that the
180      * StandardSession itself have been serialized.
181      *
182      * @param stream The object output stream to write to
183      *
184      * @exception IOException if an input/output error occurs
185      */

186     public void writeObjectData(ObjectOutputStream JavaDoc stream)
187         throws IOException JavaDoc {
188
189         super.writeObjectData(stream);
190
191     }
192     
193     public void setId(String JavaDoc id, boolean tellNew) {
194
195         if ((this.id != null) && (manager != null))
196             manager.remove(this);
197
198         this.id = id;
199
200         if (manager != null)
201             manager.add(this);
202         if (tellNew) tellNew();
203     }
204     
205     
206
207
208
209
210
211
212     /**
213      * returns true if this session is the primary session, if that is the
214      * case, the manager can expire it upon timeout.
215      */

216     public boolean isPrimarySession() {
217         return isPrimarySession;
218     }
219
220     /**
221      * Sets whether this is the primary session or not.
222      * @param primarySession Flag value
223      */

224     public void setPrimarySession(boolean primarySession) {
225         this.isPrimarySession=primarySession;
226     }
227
228
229
230
231     /**
232      * Implements a log method to log through the manager
233      */

234     protected void log(String JavaDoc message) {
235
236         if ((mManager != null) && (mManager instanceof SimpleTcpReplicationManager)) {
237             ((SimpleTcpReplicationManager) mManager).log.debug("ReplicatedSession: " + message);
238         } else {
239             System.out.println("ReplicatedSession: " + message);
240         }
241
242     }
243
244     protected void log(String JavaDoc message, Throwable JavaDoc x) {
245
246         if ((mManager != null) && (mManager instanceof SimpleTcpReplicationManager)) {
247             ((SimpleTcpReplicationManager) mManager).log.error("ReplicatedSession: " + message,x);
248         } else {
249             System.out.println("ReplicatedSession: " + message);
250             x.printStackTrace();
251         }
252
253     }
254
255     public String JavaDoc toString() {
256         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("ReplicatedSession id=");
257         buf.append(getId()).append(" ref=").append(super.toString()).append("\n");
258         java.util.Enumeration JavaDoc e = getAttributeNames();
259         while ( e.hasMoreElements() ) {
260             String JavaDoc name = (String JavaDoc)e.nextElement();
261             Object JavaDoc value = getAttribute(name);
262             buf.append("\tname=").append(name).append("; value=").append(value).append("\n");
263         }
264         buf.append("\tLastAccess=").append(getLastAccessedTime()).append("\n");
265         return buf.toString();
266     }
267     public int getAccessCount() {
268         return accessCount;
269     }
270     public void setAccessCount(int accessCount) {
271         this.accessCount = accessCount;
272     }
273     public long getLastAccessedTime() {
274         return lastAccessedTime;
275     }
276     public void setLastAccessedTime(long lastAccessedTime) {
277         this.lastAccessedTime = lastAccessedTime;
278     }
279     public long getThisAccessedTime() {
280         return thisAccessedTime;
281     }
282     public void setThisAccessedTime(long thisAccessedTime) {
283         this.thisAccessedTime = thisAccessedTime;
284     }
285
286 }
287
Popular Tags