1 25 26 package org.objectweb.easybeans.container.session.stateless; 27 28 import java.lang.reflect.InvocationTargetException ; 29 import java.lang.reflect.Method ; 30 31 import javax.ejb.ApplicationException ; 32 import javax.ejb.NoSuchEJBException ; 33 34 import org.objectweb.easybeans.api.EZBContainer; 35 import org.objectweb.easybeans.api.FactoryException; 36 import org.objectweb.easybeans.api.bean.EasyBeansSLSB; 37 import org.objectweb.easybeans.api.pool.PoolException; 38 import org.objectweb.easybeans.container.session.SessionFactory; 39 import org.objectweb.easybeans.pool.JPool; 40 import org.objectweb.easybeans.pool.PoolEntryStatistics; 41 import org.objectweb.easybeans.pool.PoolFactory; 42 import org.objectweb.easybeans.rpc.JEJBResponse; 43 import org.objectweb.easybeans.rpc.api.EJBResponse; 44 import org.objectweb.easybeans.rpc.api.RPCException; 45 46 50 public class StatelessSessionFactory extends SessionFactory<EasyBeansSLSB> implements 51 PoolFactory<EasyBeansSLSB, Long > { 52 53 59 public StatelessSessionFactory(final String className, final EZBContainer container) throws FactoryException { 60 super(className, container); 61 setPool(new JPool<EasyBeansSLSB, Long >(this)); 62 } 63 64 70 public boolean isMatching(final EasyBeansSLSB object, final Long clue) { 71 return true; 74 } 75 76 82 public boolean validate(final EasyBeansSLSB object, final PoolEntryStatistics stats) { 83 return true; 84 } 85 86 91 @Override 92 protected Long getId(final Long beanId) { 93 return null; 94 } 95 96 102 @Override 103 protected EasyBeansSLSB getBean(final Long beanId) throws IllegalArgumentException { 104 try { 105 return getPool().get(); 106 } catch (PoolException e) { 107 throw new IllegalArgumentException ("Cannot get element in the pool", e); 108 } 109 } 110 111 118 @Override 119 public EJBResponse localCall(final long hash, final Object [] methodArgs, final Long beanId) { 120 121 EJBResponse ejbResponse = new JEJBResponse(); 123 124 EasyBeansSLSB bean = null; 125 try { 126 bean = getBean(null); 127 } catch (IllegalArgumentException e) { 128 ejbResponse.setRPCException(new RPCException("Cannot get element in the pool", e)); 129 return ejbResponse; 130 } catch (NoSuchEJBException e) { 131 ejbResponse.setRPCException(new RPCException("Bean has been removed", e)); 132 return ejbResponse; 133 } 134 135 Method m = getHashes().get(Long.valueOf(hash)); 136 137 if (m == null) { 138 ejbResponse.setRPCException(new RPCException("Cannot find method called on the bean '" + getClassName() + "'.")); 139 return ejbResponse; 140 } 141 142 Object value = null; 143 144 ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader(); 146 Thread.currentThread().setContextClassLoader(getContainer().getClassLoader()); 147 148 try { 149 value = m.invoke(bean, methodArgs); 150 } catch (IllegalArgumentException e) { 151 ejbResponse.setRPCException(new RPCException(e)); 152 } catch (IllegalAccessException e) { 153 ejbResponse.setRPCException(new RPCException(e)); 154 } catch (InvocationTargetException e) { 155 Throwable cause = e.getCause(); 156 RPCException rpcException = new RPCException(cause); 157 ApplicationException applicationException = getBeanInfo().getApplicationExceptions().get(cause.getClass().getName()); 159 if (applicationException != null) { 160 rpcException.setApplicationException(); 161 } 162 ejbResponse.setRPCException(rpcException); 163 } finally { 164 Thread.currentThread().setContextClassLoader(oldClassLoader); 165 try { 167 getPool().release(bean); 168 } catch (PoolException e) { 169 ejbResponse.setRPCException(new RPCException("cannot release bean", e)); 170 } 171 172 } 173 ejbResponse.setValue(value); 174 return ejbResponse; 175 176 } 177 178 } 179 | Popular Tags |