KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.servlet.ServletException JavaDoc;
26 import javax.transaction.TransactionManager JavaDoc;
27
28 import org.apache.catalina.Lifecycle;
29 import org.apache.catalina.LifecycleException;
30 import org.apache.catalina.LifecycleListener;
31 import org.apache.catalina.connector.Request;
32 import org.apache.catalina.connector.Response;
33 import org.apache.catalina.util.LifecycleSupport;
34 import org.apache.catalina.valves.ValveBase;
35 import org.jboss.logging.Logger;
36
37 /**
38  * This Valve handles batch replication mode. It uses the cache tm to performa batch replication.
39  *
40  * @author Ben Wang
41  * @version $Revision: 58581 $
42  */

43 public class BatchReplicationClusteredSessionValve extends ValveBase implements Lifecycle
44 {
45    private static Logger log_ = Logger.getLogger(BatchReplicationClusteredSessionValve.class);
46
47    // The info string for this Valve
48
private static final String JavaDoc info = "BatchReplicationClusteredSessionValve/1.0";
49
50    // Valve-lifecycle_ helper object
51
protected LifecycleSupport support = new LifecycleSupport(this);
52
53    protected JBossCacheManager manager_;
54
55    /**
56     * Create a new Valve.
57     *
58     */

59    public BatchReplicationClusteredSessionValve(AbstractJBossManager manager)
60    {
61       super();
62       manager_ = (JBossCacheManager)manager;
63    }
64
65    /**
66     * Get information about this Valve.
67     */

68    public String JavaDoc getInfo()
69    {
70       return info;
71    }
72
73    /**
74     * Valve-chain handler method.
75     * This method gets called when the request goes through the Valve-chain. Our session replication mechanism replicates the
76     * session after request got through the servlet code.
77     *
78     * @param request The request object associated with this request.
79     * @param response The response object associated with this request.
80     */

81    public void invoke(Request request, Response response) throws IOException JavaDoc, ServletException JavaDoc
82    {
83       // Note: we use specfically the tm in cache.
84
TransactionManager JavaDoc tm = manager_.getCacheService().getTransactionManager();
85       if(tm == null)
86       {
87          throw new RuntimeException JavaDoc("BatchReplicationClusteredSessionValve.invoke(): Obtain null tm");
88       }
89
90       // Before we start a tx, get the session. If this is a failover
91
// situation, this will cause data gravitation, which will occur
92
// thus outside of the scope of the tx we are about to start.
93
// JBossCacheManager will ensure the gravitation is in its own tx
94
request.getSession(false);
95       
96       // Start a new transaction, we need transaction so all the replication are sent in batch.
97
try
98       {
99          tm.begin();
100          
101          // let the servlet invocation go through
102
getNext().invoke(request, response);
103          
104          log_.trace("Ready to commit batch replication for field level granularity");
105          
106          tm.commit();
107       }
108       catch (Exception JavaDoc e)
109       {
110          try
111          {
112             tm.rollback();
113          }
114          catch (Exception JavaDoc exn)
115          {
116             log_.error("Caught exception rolling back transaction", exn);
117          }
118          // We will need to alert Tomcat of this exception.
119
throw new RuntimeException JavaDoc("JBossCacheManager.processSessionRepl(): failed to replicate session.", e);
120       }
121    }
122
123    // Lifecylce-interface
124
public void addLifecycleListener(LifecycleListener listener)
125    {
126       support.addLifecycleListener(listener);
127    }
128
129    public void removeLifecycleListener(LifecycleListener listener)
130    {
131       support.removeLifecycleListener(listener);
132    }
133
134    public LifecycleListener[] findLifecycleListeners()
135    {
136       return support.findLifecycleListeners();
137    }
138
139    public void start() throws LifecycleException
140    {
141       support.fireLifecycleEvent(START_EVENT, this);
142    }
143
144    public void stop() throws LifecycleException
145    {
146       support.fireLifecycleEvent(STOP_EVENT, this);
147    }
148
149 }
150
Popular Tags