KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > web > tomcat > tc5 > session > ClusteredSessionValve


1 /*
2  * JBoss, the OpenSource WebOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.web.tomcat.tc5.session;
8
9 import java.io.IOException JavaDoc;
10 import javax.servlet.http.HttpSession JavaDoc;
11 import javax.servlet.ServletException JavaDoc;
12
13 import org.apache.catalina.*;
14 import org.apache.catalina.connector.Request;
15 import org.apache.catalina.connector.Response;
16 import org.apache.catalina.util.LifecycleSupport;
17 import org.apache.catalina.valves.ValveBase;
18
19 /**
20  * This Valve detects all sessions that were used in a request. All sessions are given to a snapshot
21  * manager that handles the distribution of modified sessions.
22  * <p/>
23  * TOMCAT 4.1.12 UPDATE: Added findLifecycleListeners() to comply with the latest
24  * Lifecycle interface.
25  *
26  * @author Thomas Peuss <jboss@peuss.de>
27  * @version $Revision: 1.4.2.1 $
28  */

29 public class ClusteredSessionValve extends ValveBase implements Lifecycle
30 {
31    // The info string for this Valve
32
private static final String JavaDoc info = "ClusteredSessionValve/1.0";
33
34    // The SnapshotManager that is associated with this Valve
35
protected SnapshotManager snapshot;
36
37    // Valve-lifecycle_ helper object
38
protected LifecycleSupport support = new LifecycleSupport(this);
39
40    // store the request and response object for parts of the clustering code that
41
// have no direct access to this objects
42
protected static ThreadLocal JavaDoc requestThreadLocal = new ThreadLocal JavaDoc();
43    protected static ThreadLocal JavaDoc responseThreadLocal = new ThreadLocal JavaDoc();
44
45    /**
46     * Create a new Valve.
47     *
48     * @param snapshot The SnapshotManager associated with this Valve
49     */

50    public ClusteredSessionValve(SnapshotManager snapshot)
51    {
52       super();
53       this.snapshot = snapshot;
54    }
55
56    /**
57     * Get information about this Valve.
58     */

59    public String JavaDoc getInfo()
60    {
61       return info;
62    }
63
64    /**
65     * Valve-chain handler method.
66     * This method gets called when the request goes through the Valve-chain. Our session replication mechanism replicates the
67     * session after request got through the servlet code.
68     *
69     * @param request The request object associated with this request.
70     * @param response The response object associated with this request.
71     * @param context The context of the Valve-invocation.
72     */

73    public void invoke(Request request, Response response) throws IOException JavaDoc, ServletException JavaDoc
74    {
75       // Store the request and response object for the clustering code that has no direct access to
76
// this objects
77
requestThreadLocal.set(request);
78       responseThreadLocal.set(response);
79
80       // let the servlet invokation go through
81
getNext().invoke(request, response);
82
83       // --> We are now after the servlet invokation
84

85       // Get the session
86
HttpSession JavaDoc session = request.getSession(false);
87
88       if (session != null && session.getId() != null)
89       {
90          // tell the snapshot manager that this session was modified
91
snapshot.snapshot(session.getId());
92       }
93
94       // remove references to the request and response object so that the garbace collector can
95
// kill them
96
requestThreadLocal.set(null);
97       responseThreadLocal.set(null);
98    }
99
100    // Lifecylce-interface
101
public void addLifecycleListener(LifecycleListener listener)
102    {
103       support.addLifecycleListener(listener);
104    }
105
106    public void removeLifecycleListener(LifecycleListener listener)
107    {
108       support.removeLifecycleListener(listener);
109    }
110
111    public LifecycleListener[] findLifecycleListeners()
112    {
113       return support.findLifecycleListeners();
114    }
115
116    public void start() throws LifecycleException
117    {
118       snapshot.start();
119       support.fireLifecycleEvent(START_EVENT, this);
120    }
121
122    public void stop() throws LifecycleException
123    {
124       support.fireLifecycleEvent(STOP_EVENT, this);
125       snapshot.stop();
126    }
127
128 }
129
Popular Tags