KickJava   Java API By Example, From Geeks To Geeks.

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