1 22 package org.jboss.ejb.plugins; 23 24 import java.rmi.RemoteException ; 25 import java.lang.reflect.Method ; 26 27 import org.jboss.ejb.Container; 28 import org.jboss.invocation.Invocation; 29 import org.jboss.invocation.InvocationKey; 30 import org.jboss.invocation.InvocationType; 31 import org.jboss.ejb.EnterpriseContext; 32 import org.jboss.ejb.StatelessSessionContainer; 33 import org.jboss.ejb.InstancePool; 34 import org.jboss.ejb.AllowedOperationsAssociation; 35 import org.jboss.ejb.StatelessSessionEnterpriseContext; 36 37 import javax.ejb.EJBException ; 38 import javax.ejb.TimedObject ; 39 import javax.ejb.Timer ; 40 41 50 public class StatelessSessionInstanceInterceptor 51 extends AbstractInterceptor 52 { 53 55 57 protected StatelessSessionContainer container; 58 59 61 62 protected static final Method ejbTimeout; 63 static 64 { 65 try 66 { 67 ejbTimeout = TimedObject .class.getMethod("ejbTimeout", new Class []{Timer .class}); 68 } 69 catch (Exception e) 70 { 71 throw new ExceptionInInitializerError (e); 72 } 73 } 74 75 77 79 public void setContainer(final Container container) 80 { 81 super.setContainer(container); 82 this.container = (StatelessSessionContainer)container; 83 } 84 85 87 public Object invokeHome(final Invocation mi) throws Exception 88 { 89 InstancePool pool = container.getInstancePool(); 90 StatelessSessionEnterpriseContext ctx = null; 91 try 92 { 93 ctx = (StatelessSessionEnterpriseContext) pool.get(); 95 mi.setEnterpriseContext(ctx); 96 return getNext().invokeHome(mi); 98 } 99 finally 100 { 101 mi.setEnterpriseContext(null); 102 if( ctx != null ) 104 pool.free(ctx); 105 } 106 107 } 108 109 public Object invoke(final Invocation mi) throws Exception 110 { 111 InstancePool pool = container.getInstancePool(); 113 StatelessSessionEnterpriseContext ctx = null; 114 try 115 { 116 ctx = (StatelessSessionEnterpriseContext) pool.get(); 117 } 118 catch (EJBException e) 119 { 120 throw e; 121 } 122 catch (RemoteException e) 123 { 124 throw e; 125 } 126 catch (Exception e) 127 { 128 InvocationType type = mi.getType(); 129 boolean isLocal = (type == InvocationType.LOCAL || type == InvocationType.LOCALHOME); 130 if (isLocal) 131 throw new EJBException ("Unable to get an instance from the pool", e); 132 else 133 throw new RemoteException ("Unable to get an intance from the pool", e); 134 } 135 136 ctx.setPrincipal(mi.getPrincipal()); 138 EnterpriseBeanPolicyContextHandler.setEnterpriseBean(ctx.getInstance()); 140 141 mi.setEnterpriseContext(ctx); 143 144 Object msgContext = mi.getValue(InvocationKey.SOAP_MESSAGE_CONTEXT); 146 147 if (ejbTimeout.equals(mi.getMethod())) 149 { 150 AllowedOperationsAssociation.pushInMethodFlag(IN_EJB_TIMEOUT); 151 } 152 153 else if (msgContext != null) 155 { 156 if (msgContext instanceof javax.xml.rpc.handler.MessageContext ) 157 ctx.setMessageContext((javax.xml.rpc.handler.MessageContext )msgContext); 158 159 AllowedOperationsAssociation.pushInMethodFlag(IN_SERVICE_ENDPOINT_METHOD); 160 } 161 162 else 164 { 165 AllowedOperationsAssociation.pushInMethodFlag(IN_BUSINESS_METHOD); 166 } 167 168 try 171 { 172 Object obj = getNext().invoke(mi); 173 return obj; 174 175 } 176 catch (RuntimeException e) { 178 mi.setEnterpriseContext(null); 179 throw e; 180 } 181 catch (RemoteException e) { 183 mi.setEnterpriseContext(null); 184 throw e; 185 } 186 catch (Error e) { 188 mi.setEnterpriseContext(null); 189 throw e; 190 } 191 finally 192 { 193 AllowedOperationsAssociation.popInMethodFlag(); 194 195 if (mi.getEnterpriseContext() != null) 197 { 198 pool.free(((EnterpriseContext) mi.getEnterpriseContext())); 199 } 200 else 201 { 202 pool.discard(ctx); 203 } 204 } 205 } 206 } 207 | Popular Tags |