1 22 package org.jboss.web.tomcat.tc6.jca; 23 24 import java.io.IOException ; 25 import java.util.HashSet ; 26 import java.util.Set ; 27 28 import javax.management.MBeanServer ; 29 import javax.management.ObjectName ; 30 import javax.resource.ResourceException ; 31 import javax.servlet.ServletException ; 32 import javax.transaction.Status ; 33 import javax.transaction.SystemException ; 34 import javax.transaction.TransactionManager ; 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 54 public class CachedConnectionValve extends ValveBase implements Lifecycle 55 { 56 59 private static final Logger log = Logger.getLogger(CachedConnectionValve.class); 60 61 64 private static final String info = "CachedConnectionValve/1.0"; 65 66 69 protected LifecycleSupport support = new LifecycleSupport(this); 70 71 74 protected String ccmName; 75 76 79 protected CachedConnectionManager ccm; 80 81 84 protected String tmName; 85 86 89 protected TransactionManager tm; 90 91 94 protected Set unsharableResources = new HashSet (); 95 96 101 public CachedConnectionValve() 102 { 103 super(); 104 } 105 106 109 public String getInfo() 110 { 111 return info; 112 } 113 114 117 public String getCachedConnectionManagerObjectName() 118 { 119 return ccmName; 120 } 121 122 125 public void setCachedConnectionManagerObjectName(String ccmName) 126 { 127 this.ccmName = ccmName; 128 } 129 130 133 public String getTransactionManagerObjectName() 134 { 135 return tmName; 136 } 137 138 141 public void setTransactionManagerObjectName(String tmName) 142 { 143 this.tmName = tmName; 144 } 145 146 public void invoke(Request request, Response response) throws IOException , ServletException 147 { 148 if(ccm == null) 149 throw new IllegalStateException ("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 e) 171 { 172 throw new ServletException ("Error invoking cached connection manager", e); 173 } 174 } 175 176 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 server = MBeanServerLocator.locateJBoss(); 197 ccm = (CachedConnectionManager) server.getAttribute(new ObjectName (ccmName), "Instance"); 198 tm = (TransactionManager ) server.getAttribute(new ObjectName (tmName), "TransactionManager"); 199 } 200 catch (Exception e) 201 { 202 throw new LifecycleException(e); 203 } 204 205 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 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 ex) 240 { 241 log.error("Failed to rollback", ex); 242 } 243 case Status.STATUS_PREPARED: 245 String 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 ignored) 257 { 258 } 259 260 String msg = "Application error: " + servletName + " did not complete its transaction"; 261 log.error(msg); 262 } 263 } 264 } 265 | Popular Tags |