1 25 26 package org.objectweb.easybeans.container.session.stateful; 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.EasyBeansSFSB; 37 import org.objectweb.easybeans.api.pool.PoolException; 38 import org.objectweb.easybeans.container.session.SessionFactory; 39 import org.objectweb.easybeans.log.JLog; 40 import org.objectweb.easybeans.log.JLogFactory; 41 import org.objectweb.easybeans.pool.JPool; 42 import org.objectweb.easybeans.pool.PoolEntryStatistics; 43 import org.objectweb.easybeans.pool.PoolFactory; 44 import org.objectweb.easybeans.rpc.JEJBResponse; 45 import org.objectweb.easybeans.rpc.api.EJBResponse; 46 import org.objectweb.easybeans.rpc.api.RPCException; 47 48 52 public class StatefulSessionFactory extends SessionFactory<EasyBeansSFSB> implements PoolFactory<EasyBeansSFSB, Long > { 53 54 57 private static JLog logger = JLogFactory.getLog(StatefulSessionFactory.class); 58 59 62 private long idCount = 0L; 63 64 70 public StatefulSessionFactory(final String className, final EZBContainer container) throws FactoryException { 71 super(className, container); 72 JPool<EasyBeansSFSB, Long > pool = new JPool<EasyBeansSFSB, Long >(this); 73 pool.setAllowSharedInstance(false); 75 setPool(pool); 76 } 77 78 84 public boolean isMatching(final EasyBeansSFSB bean, final Long clue) { 85 logger.debug("Called with bean = {0} and beanId = {1}", bean, clue); 86 Long beanId = bean.getEasyBeansStatefulID(); 87 boolean val = (beanId.equals(clue)); 88 logger.debug("Found id = {0} and will return = {1}", beanId, Boolean.valueOf(val)); 89 return val; 90 } 91 92 98 public boolean validate(final EasyBeansSFSB object, final PoolEntryStatistics stats) { 99 return true; 100 } 101 102 107 @Override 108 protected synchronized Long getId(final Long beanId) { 109 Long newId = beanId; 110 if (newId == null) { 112 idCount++; 113 newId = Long.valueOf(idCount); 114 } 115 return newId; 116 } 117 118 124 @Override 125 public synchronized EasyBeansSFSB create(final Long clue) throws PoolException { 126 EasyBeansSFSB bean = super.create(clue); 128 129 return bean; 130 } 131 132 138 @Override 139 protected synchronized EasyBeansSFSB getBean(final Long beanId) throws IllegalArgumentException { 140 EasyBeansSFSB bean = null; 141 try { 142 bean = getPool().get(beanId); 143 } catch (PoolException e) { 144 throw new IllegalArgumentException ("Cannot get element in the pool", e); 145 } 146 logger.debug("Set for bean {0} the Id = {1}", bean, beanId); 147 bean.setEasyBeansStatefulID(beanId); 148 return bean; 149 } 150 151 155 @Override 156 public void remove(final EasyBeansSFSB instance) { 157 super.remove(instance); 158 instance.setEasyBeansRemoved(true); 159 } 160 161 168 @Override 169 public EJBResponse localCall(final long hash, final Object [] methodArgs, final Long beanId) { 170 Long id = getId(beanId); 171 172 EJBResponse ejbResponse = new JEJBResponse(); 174 ejbResponse.setBeanId(id); 175 176 EasyBeansSFSB bean = null; 177 try { 178 bean = getBean(id); 179 } catch (IllegalArgumentException e) { 180 ejbResponse.setRPCException(new RPCException("Cannot get element in the pool", e)); 181 return ejbResponse; 182 } catch (NoSuchEJBException e) { 183 ejbResponse.setRPCException(new RPCException("Bean has been removed", e)); 184 return ejbResponse; 185 } 186 187 Method m = getHashes().get(Long.valueOf(hash)); 188 189 if (m == null) { 190 ejbResponse.setRPCException(new RPCException("Cannot find method called on the bean '" + getClassName() + "'.")); 191 return ejbResponse; 192 } 193 194 Object value = null; 195 196 ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader(); 198 Thread.currentThread().setContextClassLoader(getContainer().getClassLoader()); 199 synchronized (bean) { 200 try { 201 value = m.invoke(bean, methodArgs); 202 } catch (IllegalArgumentException e) { 203 ejbResponse.setRPCException(new RPCException(e)); 204 } catch (IllegalAccessException e) { 205 ejbResponse.setRPCException(new RPCException(e)); 206 } catch (InvocationTargetException e) { 207 Throwable cause = e.getCause(); 208 RPCException rpcException = new RPCException(cause); 209 ApplicationException applicationException = getBeanInfo().getApplicationExceptions().get( 211 cause.getClass().getName()); 212 if (applicationException != null) { 213 rpcException.setApplicationException(); 214 } 215 ejbResponse.setRPCException(rpcException); 216 } finally { 217 Thread.currentThread().setContextClassLoader(oldClassLoader); 218 try { 220 getPool().release(bean); 221 } catch (PoolException e) { 222 ejbResponse.setRPCException(new RPCException("cannot release bean", e)); 223 } 224 225 if (bean.getEasyBeansRemoved()) { 228 ejbResponse.setRemoved(true); 229 } 230 231 } 232 } 233 ejbResponse.setValue(value); 234 return ejbResponse; 235 236 } 237 } 238 | Popular Tags |