KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > web > tomcat > tc6 > jca > CachedConnectionValve


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.jca;
23
24 import java.io.IOException JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import javax.management.MBeanServer JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30 import javax.resource.ResourceException JavaDoc;
31 import javax.servlet.ServletException JavaDoc;
32 import javax.transaction.Status JavaDoc;
33 import javax.transaction.SystemException JavaDoc;
34 import javax.transaction.TransactionManager JavaDoc;
35
36 import org.apache.catalina.Lifecycle;
37 import org.apache.catalina.LifecycleException;
38 import org.apache.catalina.LifecycleListener;
39 import org.apache.catalina.Wrapper;
40 import org.apache.catalina.connector.Request;
41 import org.apache.catalina.connector.Response;
42 import org.apache.catalina.valves.ValveBase;
43 import org.apache.catalina.util.LifecycleSupport;
44 import org.jboss.logging.Logger;
45 import org.jboss.mx.util.MBeanServerLocator;
46 import org.jboss.resource.connectionmanager.CachedConnectionManager;
47
48 /**
49  * This valve checks for unclosed connections on a servlet request
50  *
51  * @author <a HREF="mailto:adrian@jboss.com">Adrian Brock</a>
52  * @version $Revision: 56732 $
53  */

54 public class CachedConnectionValve extends ValveBase implements Lifecycle
55 {
56    /**
57     * The log
58     */

59    private static final Logger log = Logger.getLogger(CachedConnectionValve.class);
60
61    /**
62     * The info string for this Valve
63     */

64    private static final String JavaDoc info = "CachedConnectionValve/1.0";
65
66    /**
67     * Valve-lifecycle helper object
68     */

69    protected LifecycleSupport support = new LifecycleSupport(this);
70
71    /**
72     * The object name of the cached connection manager
73     */

74    protected String JavaDoc ccmName;
75
76    /**
77     * The cached connection manager
78     */

79    protected CachedConnectionManager ccm;
80
81    /**
82     * The object name of the transaction manager service
83     */

84    protected String JavaDoc tmName;
85
86    /**
87     * The transaction manager
88     */

89    protected TransactionManager JavaDoc tm;
90
91    /**
92     * The unshareable resources
93     */

94    protected Set JavaDoc unsharableResources = new HashSet JavaDoc();
95
96    /**
97     * Create a new valve
98     *
99     * @param ccm the cached connection manager for the valve
100     */

101    public CachedConnectionValve()
102    {
103       super();
104    }
105
106    /**
107     * Get information about this Valve.
108     */

109    public String JavaDoc getInfo()
110    {
111       return info;
112    }
113
114    /**
115     * Get the cached connection manager object name
116     */

117    public String JavaDoc getCachedConnectionManagerObjectName()
118    {
119       return ccmName;
120    }
121
122    /**
123     * Set the cached connection manager object name
124     */

125    public void setCachedConnectionManagerObjectName(String JavaDoc ccmName)
126    {
127       this.ccmName = ccmName;
128    }
129
130    /**
131     * Get the transaction manager object name
132     */

133    public String JavaDoc getTransactionManagerObjectName()
134    {
135       return tmName;
136    }
137
138    /**
139     * Set the transaction manager object name
140     */

141    public void setTransactionManagerObjectName(String JavaDoc tmName)
142    {
143       this.tmName = tmName;
144    }
145
146    public void invoke(Request request, Response response) throws IOException JavaDoc, ServletException JavaDoc
147    {
148       if(ccm == null)
149          throw new IllegalStateException JavaDoc("Please uncomment the dependency on CachedConnectionManager"
150                + " in META-INF/jboss-service.xml of jbossweb-tomcatxxx.sar");
151       try
152       {
153          ccm.pushMetaAwareObject(this, unsharableResources);
154          try
155          {
156             getNext().invoke(request, response);
157          }
158          finally
159          {
160             try
161             {
162                ccm.popMetaAwareObject(unsharableResources);
163             }
164             finally
165             {
166                checkTransactionComplete(request);
167             }
168          }
169       }
170       catch (ResourceException JavaDoc e)
171       {
172          throw new ServletException JavaDoc("Error invoking cached connection manager", e);
173       }
174    }
175
176    // Lifecycle-interface
177
public void addLifecycleListener(LifecycleListener listener)
178    {
179       support.addLifecycleListener(listener);
180    }
181
182    public void removeLifecycleListener(LifecycleListener listener)
183    {
184       support.removeLifecycleListener(listener);
185    }
186
187    public LifecycleListener[] findLifecycleListeners()
188    {
189       return support.findLifecycleListeners();
190    }
191
192    public void start() throws LifecycleException
193    {
194       try
195       {
196          MBeanServer JavaDoc server = MBeanServerLocator.locateJBoss();
197          ccm = (CachedConnectionManager) server.getAttribute(new ObjectName JavaDoc(ccmName), "Instance");
198          tm = (TransactionManager JavaDoc) server.getAttribute(new ObjectName JavaDoc(tmName), "TransactionManager");
199       }
200       catch (Exception JavaDoc e)
201       {
202          throw new LifecycleException(e);
203       }
204       
205       // TODO unshareable resources
206
support.fireLifecycleEvent(START_EVENT, this);
207    }
208
209    public void stop() throws LifecycleException
210    {
211       support.fireLifecycleEvent(STOP_EVENT, this);
212       unsharableResources.clear();
213    }
214
215    protected void checkTransactionComplete(Request request)
216    {
217       int status = Status.STATUS_NO_TRANSACTION;
218
219       try
220       {
221          status = tm.getStatus();
222       }
223       catch (SystemException JavaDoc ex)
224       {
225          log.error("Failed to get status", ex);
226       }
227
228       switch (status)
229       {
230          case Status.STATUS_ACTIVE:
231          case Status.STATUS_COMMITTING:
232          case Status.STATUS_MARKED_ROLLBACK:
233          case Status.STATUS_PREPARING:
234          case Status.STATUS_ROLLING_BACK:
235             try
236             {
237                tm.rollback();
238             }
239             catch (Exception JavaDoc ex)
240             {
241                log.error("Failed to rollback", ex);
242             }
243             // fall through...
244
case Status.STATUS_PREPARED:
245             String JavaDoc servletName = "<Unknown>";
246             try
247             {
248                Wrapper servlet = request.getWrapper();
249                if (servlet != null)
250                {
251                   servletName = servlet.getName();
252                   if (servlet.getJspFile() != null)
253                      servletName = servlet.getJspFile();
254                }
255             }
256             catch (Throwable JavaDoc ignored)
257             {
258             }
259
260             String JavaDoc msg = "Application error: " + servletName + " did not complete its transaction";
261             log.error(msg);
262       }
263    }
264 }
265
Popular Tags