1 22 package org.jboss.web.tomcat.tc5.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 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 e) 168 { 169 throw new ServletException ("Error invoking cached connection manager", e); 170 } 171 } 172 173 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 server = MBeanServerLocator.locateJBoss(); 194 ccm = (CachedConnectionManager) server.getAttribute(new ObjectName (ccmName), "Instance"); 195 tm = (TransactionManager ) server.getAttribute(new ObjectName (tmName), "TransactionManager"); 196 } 197 catch (Exception e) 198 { 199 throw new LifecycleException(e); 200 } 201 202 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 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 ex) 237 { 238 log.error("Failed to rollback", ex); 239 } 240 case Status.STATUS_PREPARED: 242 String 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 ignored) 254 { 255 } 256 257 String msg = "Application error: " + servletName + " did not complete its transaction"; 258 log.error(msg); 259 } 260 } 261 } 262 | Popular Tags |