KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > web > tomcat > tc6 > session > SessionBasedClusteredSession


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.web.tomcat.tc6.session;
23
24 import java.io.IOException JavaDoc;
25 import java.io.ObjectInput JavaDoc;
26 import java.io.ObjectOutput JavaDoc;
27 import java.util.Map JavaDoc;
28
29
30 /**
31  * Implementation of a clustered session for the JBossCacheManager. The replication granularity
32  * level is session based; that is, we replicate per whole session object.
33  * We use JBossCache for our internal replicated data store.
34  * The internal structure in JBossCache is as follows:
35  * <pre>
36  * /JSESSION
37  * /hostname
38  * /web_app_path (path + session id is unique)
39  * /id Map(id, session)
40  * (VERSION_KEY, version) // Used for version tracking. version is an Integer.
41  * </pre>
42  * <p/>
43  * Note that the isolation level of the cache dictates the
44  * concurrency behavior.</p>
45  *
46  * @author Ben Wang
47  * @author Brian Stansberry
48  *
49  * @version $Revision: 56542 $
50  */

51 class SessionBasedClusteredSession
52    extends JBossCacheClusteredSession
53 {
54    static final long serialVersionUID = 3200976125245487256L;
55
56    /**
57     * Descriptive information describing this Session implementation.
58     */

59    protected static final String JavaDoc info = "SessionBasedClusteredSession/1.0";
60
61    public SessionBasedClusteredSession(JBossCacheManager manager)
62    {
63       super(manager);
64    }
65
66    // ---------------------------------------------- Overridden Public Methods
67

68    /**
69     * Return a string representation of this object.
70     */

71    public String JavaDoc toString()
72    {
73       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
74       sb.append("SessionBasedClusteredSession[");
75       sb.append(super.toString());
76       sb.append("]");
77       return (sb.toString());
78
79    }
80
81    public void removeMyself()
82    {
83       proxy_.removeSession(realId);
84    }
85
86    public void removeMyselfLocal()
87    {
88       proxy_.removeSessionLocal(realId);
89    }
90
91    // ----------------------------------------------HttpSession Public Methods
92

93    /**
94     * Does nothing -- all attributes are populated already
95     */

96    protected void populateAttributes()
97    {
98       // no-op
99
}
100
101    protected Object JavaDoc getJBossInternalAttribute(String JavaDoc name)
102    {
103       Object JavaDoc result = attributes.get(name);
104
105       // Do dirty check even if result is null, as w/ SET_AND_GET null
106
// still makes us dirty (ensures timely replication w/o using ACCESS)
107
if (isGetDirty(result))
108       {
109          sessionAttributesDirty();
110       }
111
112       return result;
113
114    }
115
116    protected Object JavaDoc removeJBossInternalAttribute(String JavaDoc name,
117                                                  boolean localCall,
118                                                  boolean localOnly)
119    {
120       if (localCall)
121          sessionAttributesDirty();
122       return attributes.remove(name);
123    }
124
125    protected Map JavaDoc getJBossInternalAttributes()
126    {
127       return attributes;
128    }
129
130    protected Object JavaDoc setJBossInternalAttribute(String JavaDoc name, Object JavaDoc value)
131    {
132       sessionAttributesDirty();
133       return attributes.put(name, value);
134    }
135
136    /**
137     * Overrides the superclass version by additionally reading the
138     * attributes map.
139     *
140     * <p>
141     * This method is deliberately public so it can be used to reset
142     * the internal state of a session object using serialized
143     * contents replicated from another JVM via JBossCache.
144     * </p>
145     *
146     * @see org.jboss.web.tomcat.tc6.session.ClusteredSession#readExternal(java.io.ObjectInput)
147     */

148    public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc
149    {
150       synchronized (this)
151       {
152          // Let superclass read in everything but the attribute map
153
super.readExternal(in);
154       
155          attributes = (Map JavaDoc) in.readObject();
156       }
157    }
158
159    /**
160     * Overrides the superclass version by appending the attributes map. Does
161     * not write any attributes whose names are found in
162     * {@link ClusteredSession#excludedAttributes}.
163     *
164     * @see org.jboss.web.tomcat.tc6.session.ClusteredSession#writeExternal(java.io.ObjectOutput)
165     */

166    public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
167    {
168       synchronized (this)
169       {
170          // Let superclass write out everything but the attribute map
171
super.writeExternal(out);
172          
173          // Don't replicate any excluded attributes
174
Map JavaDoc excluded = removeExcludedAttributes(attributes);
175         
176          out.writeObject(attributes);
177         
178          // Restore any excluded attributes
179
if (excluded != null)
180             attributes.putAll(excluded);
181       }
182    
183    }
184
185    protected void update(ClusteredSession replicated)
186    {
187       SessionBasedClusteredSession other = (SessionBasedClusteredSession) replicated;
188       
189       super.update(other);
190       
191       attributes = other.attributes;
192    }
193    
194
195    /**
196     * Overrides the superclass version to return <code>true</code> if either
197     * the metadata or the attributes are dirty.
198     */

199    public boolean getReplicateSessionBody()
200    {
201       return isSessionDirty() || getExceedsMaxUnreplicatedInterval();
202    }
203
204 }
205
Popular Tags