User: mnf999 Date: 01/12/18 21:32:43 Modified: src/main/org/jboss/ejb StatelessSessionContainer.java Log: move to the new invocation layer Revision Changes Path 1.30 +217 -176 jboss/src/main/org/jboss/ejb/StatelessSessionContainer.java Index: StatelessSessionContainer.java =================================================================== RCS file: /cvsroot/jboss/jboss/src/main/org/jboss/ejb/StatelessSessionContainer.java,v retrieving revision 1.29 retrieving revision 1.30 diff -u -r1.29 -r1.30 --- StatelessSessionContainer.java 2001/11/24 20:43:22 1.29 +++ StatelessSessionContainer.java 2001/12/19 05:32:43 1.30 @@ -1,9 +1,9 @@ /* - * JBoss, the OpenSource J2EE webOS - * - * Distributable under LGPL license. - * See terms of license at gnu.org. - */ +* JBoss, the OpenSource J2EE webOS +* +* Distributable under LGPL license. +* See terms of license at gnu.org. +*/ package org.jboss.ejb; import java.lang.reflect.InvocationTargetException; @@ -23,85 +23,90 @@ import javax.ejb.RemoveException; import javax.ejb.EJBException; +import org.jboss.invocation.Invocation; +import org.jboss.invocation.MarshalledInvocation; + /** - * The container for <em>stateless</em> session beans. - * - * @author <a href="mailto:[EMAIL PROTECTED]">Rickard Öberg</a> - * @author <a href="mailto:[EMAIL PROTECTED]">Marc Fleury</a> - * @author <a href="mailto:[EMAIL PROTECTED]">Daniel OConnor</a> - * @version $Revision: 1.29 $ - */ +* The container for <em>stateless</em> session beans. +* +* @author <a href="mailto:[EMAIL PROTECTED]">Rickard Öberg</a> +* @author <a href="mailto:[EMAIL PROTECTED]">Marc Fleury</a> +* @author <a href="mailto:[EMAIL PROTECTED]">Daniel OConnor</a> +* @version $Revision: 1.30 $ +* <p><b>2001219 marc fleury</b> +* <ul> +* <li> move to the new invocation layer and Invocation object +* </ul> +*/ public class StatelessSessionContainer - extends Container - implements ContainerInvokerContainer, InstancePoolContainer +extends Container +implements ContainerInvokerContainer, InstancePoolContainer { // Constants ----------------------------------------------------- - + // Attributes ---------------------------------------------------- - - /** This is the Home interface class */ - protected Class homeInterface; - - /** This is the Remote interface class */ - protected Class remoteInterface; - + /** - * These are the mappings between the home interface methods and the - * container methods. - */ + * These are the mappings between the home interface methods and the + * container methods. + */ protected Map homeMapping; + /** - * These are the mappings between the remote interface methods and the - * bean methods. - */ + * These are the mappings between the remote interface methods and the + * bean methods. + */ protected Map beanMapping; + /** This is the container invoker for this container */ protected ContainerInvoker containerInvoker; + /** This is the instancepool that is to be used */ protected InstancePool instancePool; + /** - * This is the first interceptor in the chain. The last interceptor must - * be provided by the container itself - */ + * This is the first interceptor in the chain. The last interceptor must + * be provided by the container itself + */ protected Interceptor interceptor; // Static -------------------------------------------------------- // Constructors -------------------------------------------------- - + // Public -------------------------------------------------------- - + public void setContainerInvoker(ContainerInvoker ci) { if (ci == null) throw new IllegalArgumentException("Null invoker"); - + this.containerInvoker = ci; ci.setContainer(this); } - + public ContainerInvoker getContainerInvoker() { return containerInvoker; } - + public LocalContainerInvoker getLocalContainerInvoker() { return localContainerInvoker; } - + public void setInstancePool(InstancePool ip) { if (ip == null) throw new IllegalArgumentException("Null pool"); - + this.instancePool = ip; ip.setContainer(this); } - + public InstancePool getInstancePool() { return instancePool; } - + public void addInterceptor(Interceptor in) { if (interceptor == null) @@ -114,86 +119,89 @@ { current = current.getNext(); } - + current.setNext(in); } } - + public Interceptor getInterceptor() { return interceptor; } - + public Class getHomeClass() { return homeInterface; } - + public Class getRemoteClass() { return remoteInterface; } - - + + // Container implementation -------------------------------------- - - public void init() throws Exception + + public void create() throws Exception { // Associate thread with classloader ClassLoader oldCl = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(getClassLoader()); - + // Acquire classes from CL if (metaData.getHome() != null) homeInterface = classLoader.loadClass(metaData.getHome()); if (metaData.getRemote() != null) remoteInterface = classLoader.loadClass(metaData.getRemote()); - + // Call default init - super.init(); - + super.create(); + // Map the bean methods setupBeanMapping(); - + // Map the home methods setupHomeMapping(); - + + // Map the interfaces to Long + setupMarshalledInvocationMapping(); + // Initialize pool - instancePool.init(); - + instancePool.create(); + // Init container invoker if (containerInvoker != null) - containerInvoker.init(); - + containerInvoker.create(); + // Initialize the interceptor by calling the chain Interceptor in = interceptor; while (in != null) { in.setContainer(this); - in.init(); + in.create(); in = in.getNext(); } - + // Reset classloader Thread.currentThread().setContextClassLoader(oldCl); } - + public void start() throws Exception { // Associate thread with classloader ClassLoader oldCl = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(getClassLoader()); - + // Call default start super.start(); - + // Start container invoker if (containerInvoker != null) containerInvoker.start(); - + // Start the instance pool instancePool.start(); - + // Start all interceptors in the chain Interceptor in = interceptor; while (in != null) @@ -201,27 +209,27 @@ in.start(); in = in.getNext(); } - + // Reset classloader Thread.currentThread().setContextClassLoader(oldCl); } - + public void stop() { // Associate thread with classloader ClassLoader oldCl = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(getClassLoader()); - + // Call default stop super.stop(); - + // Stop container invoker if (containerInvoker != null) containerInvoker.stop(); - + // Stop the instance pool instancePool.stop(); - + // Stop all interceptors in the chain Interceptor in = interceptor; while (in != null) @@ -229,27 +237,27 @@ in.stop(); in = in.getNext(); } - + // Reset classloader Thread.currentThread().setContextClassLoader(oldCl); } - + public void destroy() { // Associate thread with classloader ClassLoader oldCl = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(getClassLoader()); - + // Call default destroy super.destroy(); - + // Destroy container invoker if (containerInvoker != null) containerInvoker.destroy(); - + // Destroy the pool instancePool.destroy(); - + // Destroy all the interceptors in the chain Interceptor in = interceptor; while (in != null) @@ -257,159 +265,161 @@ in.destroy(); in = in.getNext(); } - + // Reset classloader Thread.currentThread().setContextClassLoader(oldCl); } - - public Object invokeHome(MethodInvocation mi) throws Exception + + public Object invokeHome(Invocation mi) throws Exception { + return getInterceptor().invokeHome(mi); } - + /** - * This method does invocation interpositioning of tx and security, - * retrieves the instance from an object table, and invokes the method - * on the particular instance - */ - public Object invoke(MethodInvocation mi) - throws Exception + * This method does invocation interpositioning of tx and security, + * retrieves the instance from an object table, and invokes the method + * on the particular instance + */ + public Object invoke(Invocation mi) + throws Exception { + // Invoke through interceptors return getInterceptor().invoke(mi); } - + // EJBObject implementation -------------------------------------- - + /** - * No-op. - */ - public void remove(MethodInvocation mi) - throws RemoteException, RemoveException + * No-op. + */ + public void remove(Invocation mi) + throws RemoteException, RemoveException { //TODO } - + /** - * @return Always null - */ - public Handle getHandle(MethodInvocation mi) - throws RemoteException + * @return Always null + */ + public Handle getHandle(Invocation mi) + throws RemoteException { // TODO return null; } - + /** - * @return Always null - */ - public Object getPrimaryKey(MethodInvocation mi) - throws RemoteException + * @return Always null + */ + public Object getPrimaryKey(Invocation mi) + throws RemoteException { // TODO return null; } - - public EJBHome getEJBHome(MethodInvocation mi) - throws RemoteException + + public EJBHome getEJBHome(Invocation mi) + throws RemoteException { if (containerInvoker == null) throw new IllegalStateException(); - - return containerInvoker.getEJBHome(); + + return (EJBHome) containerInvoker.getEJBHome(); } - + /** - * @return Always false - */ - public boolean isIdentical(MethodInvocation mi) - throws RemoteException + * @return Always false + */ + public boolean isIdentical(Invocation mi) + throws RemoteException { return false; // TODO } - + // EJBLocalObject implementation - - public EJBLocalHome getEJBLocalHome(MethodInvocation mi) + + public EJBLocalHome getEJBLocalHome(Invocation mi) { return localContainerInvoker.getEJBLocalHome(); } - + // EJBLocalHome implementation - + public EJBLocalObject createLocalHome() - throws CreateException + throws CreateException { if (localContainerInvoker == null) throw new IllegalStateException(); return localContainerInvoker.getStatelessSessionEJBLocalObject(); } - + /** - * No-op. - */ + * No-op. + */ public void removeLocalHome(Object primaryKey) { // todo } - + // EJBHome implementation ---------------------------------------- - + public EJBObject createHome() - throws RemoteException, CreateException + throws RemoteException, CreateException { if (containerInvoker == null) throw new IllegalStateException(); - + Object obj = containerInvoker.getStatelessSessionEJBObject(); return (EJBObject)obj; } - + /** - * No-op. - */ + * No-op. + */ public void removeHome(Handle handle) - throws RemoteException, RemoveException + throws RemoteException, RemoveException { // TODO } - + /** - * No-op. - */ + * No-op. + */ public void removeHome(Object primaryKey) - throws RemoteException, RemoveException + throws RemoteException, RemoveException { // TODO } - + /** - * @return Always null. - */ + * @return Always null. + */ public EJBMetaData getEJBMetaDataHome() - throws RemoteException + throws RemoteException { // TODO return null; } - + /** - * @return Always null. - */ + * @return Always null. + */ public HomeHandle getHomeHandleHome() - throws RemoteException + throws RemoteException { // TODO return null; } - + // Protected ---------------------------------------------------- - + protected void setupHomeMapping() - throws NoSuchMethodException + throws NoSuchMethodException { Map map = new HashMap(); - + if (homeInterface != null) { Method[] m = homeInterface.getMethods(); @@ -430,12 +440,12 @@ map.put(m[i], getClass().getMethod(m[i].getName()+"LocalHome", m[i].getParameterTypes())); } } - + homeMapping = map; } - + private void setUpBeanMappingImpl( Map map, Method[] m, String declaringClass ) - throws NoSuchMethodException + throws NoSuchMethodException { for (int i = 0; i < m.length; i++) { @@ -451,7 +461,7 @@ { // Implemented by container log.debug("Mapped Container method "+m[i].getName() +" HASH "+m[i].hashCode()); - map.put(m[i], getClass().getMethod(m[i].getName(), new Class[] { MethodInvocation.class })); + map.put(m[i], getClass().getMethod(m[i].getName(), new Class[] { Invocation.class })); } catch (NoSuchMethodException e) { log.error(m[i].getName() + " in bean has not been mapped", e); @@ -459,12 +469,12 @@ } } } - + protected void setupBeanMapping() - throws NoSuchMethodException + throws NoSuchMethodException { Map map = new HashMap(); - + if (remoteInterface != null) { Method[] m = remoteInterface.getMethods(); @@ -475,39 +485,70 @@ Method[] m = localInterface.getMethods(); setUpBeanMappingImpl( map, m, "javax.ejb.EJBLocalObject" ); } - + beanMapping = map; } - + + protected void setupMarshalledInvocationMapping() + { + try + {// Create method mappings for container invoker + Method [] m = homeInterface.getMethods(); + for (int i = 0 ; i<m.length ; i++) + { + marshalledInvocationMapping.put( new Long(MarshalledInvocation.calculateHash(m[i])), m[i]); + } + m = remoteInterface.getMethods(); + for (int j = 0 ; j<m.length ; j++) + { + marshalledInvocationMapping.put( new Long(MarshalledInvocation.calculateHash(m[j])), m[j]); + } + + // Get the getEJBObjectMethod + Method getEJBObjectMethod = Class.forName("javax.ejb.Handle").getMethod("getEJBObject", new Class[0]); + + // Hash it + marshalledInvocationMapping.put(new Long(MarshalledInvocation.calculateHash(getEJBObjectMethod)),getEJBObjectMethod); + } + catch (Exception e) + { + e.printStackTrace(); + log.error("could not load methods", e); + } + } + + Interceptor createContainerInterceptor() { return new ContainerInterceptor(); } - + /** - * This is the last step before invocation - all interceptors are done - */ + * This is the last step before invocation - all interceptors are done + */ class ContainerInterceptor - implements Interceptor + implements Interceptor { public void setContainer(Container con) {} - + public void setNext(Interceptor interceptor) {} - + public Interceptor getNext() { return null; } - - public void init() {} - + + public void create() {} + public void start() {} - + public void stop() {} - + public void destroy() {} - - public Object invokeHome(MethodInvocation mi) - throws Exception + + public Object invokeHome(Invocation mi) + throws Exception { + Method m = (Method)homeMapping.get(mi.getMethod()); + try { return m.invoke(StatelessSessionContainer.this, mi.getArguments()); @@ -528,16 +569,16 @@ throw (Error)ex; } } - - public Object invoke(MethodInvocation mi) - throws Exception + + public Object invoke(Invocation mi) + throws Exception { //wire the transaction on the context, this is how the instance remember the tx - if (mi.getEnterpriseContext().getTransaction() == null) mi.getEnterpriseContext().setTransaction(mi.getTransaction()); + if (((EnterpriseContext) mi.getEnterpriseContext()).getTransaction() == null) ((EnterpriseContext) mi.getEnterpriseContext()).setTransaction(mi.getTransaction()); // Get method and instance to invoke upon Method m = (Method)beanMapping.get(mi.getMethod()); - + //If we have a method that needs to be done by the container (EJBObject methods) if (m.getDeclaringClass().equals(StatelessSessionContainer.class)) { @@ -565,7 +606,7 @@ // Invoke and handle exceptions try { - return m.invoke(mi.getEnterpriseContext().getInstance(), mi.getArguments()); + return m.invoke(((EnterpriseContext) mi.getEnterpriseContext()).getInstance(), mi.getArguments()); } catch (IllegalAccessException e) { // Throw this as a bean exception...(?) @@ -587,7 +628,7 @@ // StatisiticsProvider implementation ------------------------------------ public Map retrieveStatistic() { - return null; + return null; } public void resetStatistic() {
_______________________________________________ Jboss-development mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/jboss-development