package org.jboss.ejb;
import org.jboss.invocation.Invocation;
import org.jboss.invocation.MarshalledInvocation;
import org.jboss.metadata.SessionMetaData;
import javax.ejb.EJBHome;
import javax.ejb.EJBLocalHome;
import javax.ejb.EJBMetaData;
import javax.ejb.EJBObject;
import javax.ejb.Handle;
import javax.ejb.HomeHandle;
import javax.ejb.RemoveException;
import javax.ejb.TimedObject;
import javax.ejb.Timer;
import javax.management.ObjectName;
import java.lang.reflect.Method;
import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
public abstract class SessionContainer extends Container
{
protected Map homeMapping;
protected Map beanMapping;
protected Interceptor interceptor;
protected Class serviceEndpoint;
protected InstancePool instancePool;
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 LocalProxyFactory getLocalProxyFactory()
{
return localProxyFactory;
}
public void addInterceptor(Interceptor in)
{
if (interceptor == null)
{
interceptor = in;
}
else
{
Interceptor current = interceptor;
while (current.getNext() != null)
{
current = current.getNext();
}
current.setNext(in);
}
}
public Interceptor getInterceptor()
{
return interceptor;
}
public Class getServiceEndpoint()
{
return serviceEndpoint;
}
protected void createService() throws Exception
{
ClassLoader oldCl = SecurityActions.getContextClassLoader();
SecurityActions.setContextClassLoader(getClassLoader());
try
{
if (metaData.getHome() != null)
homeInterface = classLoader.loadClass(metaData.getHome());
if (metaData.getRemote() != null)
remoteInterface = classLoader.loadClass(metaData.getRemote());
if (((SessionMetaData) metaData).getServiceEndpoint() != null)
{
serviceEndpoint =
classLoader.loadClass(((SessionMetaData) metaData).getServiceEndpoint());
}
super.createService();
checkCoherency();
setupBeanMapping();
setupHomeMapping();
setupMarshalledInvocationMapping();
createInvokers();
createInstanceCache();
createInstancePool();
createPersistenceManager();
createInterceptors();
}
finally
{
SecurityActions.setContextClassLoader(oldCl);
}
}
protected abstract void setupHomeMapping() throws Exception;
protected void setUpBeanMappingImpl(Map map, Method[] methods, String declaringClass)
throws NoSuchMethodException
{
for (int i = 0; i < methods.length; i++)
{
Method m = methods[i];
if (m.getDeclaringClass().getName().equals(declaringClass) == false)
{
try
{
Method beanMethod = beanClass.getMethod(m.getName(), m.getParameterTypes());
map.put(m, beanMethod);
}
catch (NoSuchMethodException ex)
{
throw new NoSuchMethodException("Not found in bean class: " + m);
}
if (log.isDebugEnabled())
log.debug("Mapped " + m.getName() + " HASH " + m.hashCode() + "to " + map.get(m));
}
else
{
try
{
Method containerMethod = getClass().getMethod(m.getName(), new Class[]{Invocation.class});
map.put(m, containerMethod);
}
catch (NoSuchMethodException e)
{
throw new NoSuchMethodException("Not found in container class: " + m);
}
if (log.isDebugEnabled())
log.debug("Mapped Container method " + m.getName() + " HASH " + m.hashCode());
}
}
}
protected void setupBeanMapping() throws NoSuchMethodException
{
Map map = new HashMap();
if (remoteInterface != null)
{
Method[] m = remoteInterface.getMethods();
setUpBeanMappingImpl(map, m, "javax.ejb.EJBObject");
}
if (localInterface != null)
{
Method[] m = localInterface.getMethods();
setUpBeanMappingImpl(map, m, "javax.ejb.EJBLocalObject");
}
if (TimedObject.class.isAssignableFrom(beanClass))
{
Method[] m = new Method[]{TimedObject.class.getMethod("ejbTimeout", new Class[]{Timer.class})};
setUpBeanMappingImpl(map, m, "javax.ejb.Timer");
}
if (serviceEndpoint != null)
{
Method[] m = serviceEndpoint.getMethods();
setUpBeanMappingImpl(map, m, "java.rmi.Remote");
}
beanMapping = map;
}
protected void setupMarshalledInvocationMapping() throws Exception
{
if (homeInterface != null)
{
Method[] m = homeInterface.getMethods();
for (int i = 0; i < m.length; i++)
{
marshalledInvocationMapping.put(new Long(MarshalledInvocation.calculateHash(m[i])), m[i]);
}
}
if (remoteInterface != null)
{
Method[] m = remoteInterface.getMethods();
for (int j = 0; j < m.length; j++)
{
marshalledInvocationMapping.put(new Long(MarshalledInvocation.calculateHash(m[j])), m[j]);
}
}
Method getEJBObjectMethod =
Class.forName("javax.ejb.Handle").getMethod("getEJBObject",
new Class[0]);
marshalledInvocationMapping.put(new Long(MarshalledInvocation.calculateHash(getEJBObjectMethod)), getEJBObjectMethod);
}
protected void checkCoherency() throws Exception
{
if (metaData.isClustered())
{
boolean clusteredProxyFactoryFound = false;
Iterator it = proxyFactories.keySet().iterator();
while (it.hasNext())
{
String invokerBinding = (String) it.next();
EJBProxyFactory ci = (EJBProxyFactory) proxyFactories.get(invokerBinding);
if (ci instanceof org.jboss.proxy.ejb.ClusterProxyFactory)
clusteredProxyFactoryFound = true;
}
if (!clusteredProxyFactoryFound)
{
log.warn("*** EJB '"
+ this.metaData.getEjbName()
+ "' deployed as CLUSTERED but not a single clustered-invoker is bound to container ***");
}
}
}
protected void createInstancePool() throws Exception
{
try
{
ObjectName containerName = super.getJmxName();
Hashtable props = containerName.getKeyPropertyList();
props.put("plugin", "pool");
ObjectName poolName = new ObjectName(containerName.getDomain(), props);
server.registerMBean(instancePool, poolName);
}
catch (Throwable t)
{
log.debug("Failed to register pool as mbean", t);
}
instancePool.create();
}
protected void createInstanceCache() throws Exception
{
}
protected void createInvokers() throws Exception
{
for (Iterator it = proxyFactories.keySet().iterator(); it.hasNext();)
{
String invokerBinding = (String) it.next();
EJBProxyFactory ci = (EJBProxyFactory) proxyFactories.get(invokerBinding);
ci.create();
}
}
protected void createInterceptors() throws Exception
{
Interceptor in = interceptor;
while (in != null)
{
in.setContainer(this);
in.create();
in = in.getNext();
}
}
protected void createPersistenceManager() throws Exception
{
}
protected void startService() throws Exception
{
ClassLoader oldCl = SecurityActions.getContextClassLoader();
SecurityActions.setContextClassLoader(getClassLoader());
try
{
super.startService();
startInvokers();
startInstanceCache();
startInstancePool();
startPersistenceManager();
startInterceptors();
}
finally
{
SecurityActions.setContextClassLoader(oldCl);
}
}
protected void startPersistenceManager() throws Exception
{
}
protected void startInstanceCache() throws Exception
{
}
protected void startInvokers() throws Exception
{
for (Iterator it = proxyFactories.keySet().iterator(); it.hasNext();)
{
String invokerBinding = (String) it.next();
EJBProxyFactory ci = (EJBProxyFactory) proxyFactories.get(invokerBinding);
ci.start();
}
}
protected void startInstancePool() throws Exception
{
instancePool.start();
}
protected void startInterceptors() throws Exception
{
Interceptor in = interceptor;
while (in != null)
{
in.start();
in = in.getNext();
}
}
protected void stopService() throws Exception
{
ClassLoader oldCl = SecurityActions.getContextClassLoader();
SecurityActions.setContextClassLoader(getClassLoader());
try
{
super.stopService();
stopInvokers();
stopInstanceCache();
stopInstancePool();
stopPersistenceManager();
stopInterceptors();
}
finally
{
SecurityActions.setContextClassLoader(oldCl);
}
}
protected void stopInterceptors()
{
Interceptor in = interceptor;
while (in != null)
{
in.stop();
in = in.getNext();
}
}
protected void stopPersistenceManager()
{
}
protected void stopInstancePool()
{
instancePool.stop();
}
protected void stopInstanceCache()
{
}
protected void stopInvokers()
{
for (Iterator it = proxyFactories.keySet().iterator(); it.hasNext();)
{
String invokerBinding = (String) it.next();
EJBProxyFactory ci = (EJBProxyFactory) proxyFactories.get(invokerBinding);
ci.stop();
}
}
protected void destroyService() throws Exception
{
ClassLoader oldCl = SecurityActions.getContextClassLoader();
SecurityActions.setContextClassLoader(getClassLoader());
try
{
destroyInvokers();
destroyInstanceCache();
destroyInstancePool();
destroyPersistenceManager();
destroyInterceptors();
destroyMarshalledInvocationMapping();
super.destroyService();
}
finally
{
SecurityActions.setContextClassLoader(oldCl);
}
}
protected void destroyMarshalledInvocationMapping()
{
MarshalledInvocation.removeHashes(homeInterface);
MarshalledInvocation.removeHashes(remoteInterface);
}
protected void destroyInterceptors()
{
Interceptor in = interceptor;
while (in != null)
{
in.destroy();
in.setContainer(null);
in = in.getNext();
}
}
protected void destroyPersistenceManager()
{
}
protected void destroyInstancePool()
{
instancePool.destroy();
instancePool.setContainer(null);
try
{
ObjectName containerName = super.getJmxName();
Hashtable props = containerName.getKeyPropertyList();
props.put("plugin", "pool");
ObjectName poolName = new ObjectName(containerName.getDomain(), props);
server.unregisterMBean(poolName);
}
catch (Throwable ignore)
{
}
}
protected void destroyInstanceCache()
{
}
protected void destroyInvokers()
{
for (Iterator it = proxyFactories.keySet().iterator(); it.hasNext();)
{
String invokerBinding = (String) it.next();
EJBProxyFactory ci = (EJBProxyFactory) proxyFactories.get(invokerBinding);
ci.destroy();
ci.setContainer(null);
}
}
public Object internalInvokeHome(Invocation mi) throws Exception
{
Method method = mi.getMethod();
if (method != null && method.getName().equals("remove"))
{
Object arg = mi.getArguments()[0];
if (arg instanceof Handle)
{
if (arg == null)
throw new RemoteException("Null handle");
Handle handle = (Handle) arg;
EJBObject ejbObject = handle.getEJBObject();
ejbObject.remove();
return null;
}
else
throw new RemoveException("EJBHome.remove(Object) not allowed for session beans");
}
return getInterceptor().invokeHome(mi);
}
public Object internalInvoke(Invocation mi) throws Exception
{
return getInterceptor().invoke(mi);
}
public Handle getHandle(Invocation mi) throws RemoteException
{
return null;
}
public Object getPrimaryKey(Invocation mi) throws RemoteException
{
return getPrimaryKey();
}
public Object getPrimaryKey() throws RemoteException
{
throw new RemoteException("Call to getPrimaryKey not allowed on session bean");
}
public EJBHome getEJBHome(Invocation mi) throws RemoteException
{
EJBProxyFactory ci = getProxyFactory();
if (ci == null)
{
String msg = "No ProxyFactory, check for ProxyFactoryFinderInterceptor";
throw new IllegalStateException(msg);
}
return (EJBHome) ci.getEJBHome();
}
public boolean isIdentical(Invocation mi) throws RemoteException
{
EJBProxyFactory ci = getProxyFactory();
if (ci == null)
{
String msg = "No ProxyFactory, check for ProxyFactoryFinderInterceptor";
throw new IllegalStateException(msg);
}
return ci.isIdentical(this, mi);
}
public EJBMetaData getEJBMetaDataHome(Invocation mi) throws RemoteException
{
return getEJBMetaDataHome();
}
public EJBMetaData getEJBMetaDataHome() throws RemoteException
{
EJBProxyFactory ci = getProxyFactory();
if (ci == null)
{
String msg = "No ProxyFactory, check for ProxyFactoryFinderInterceptor";
throw new IllegalStateException(msg);
}
return ci.getEJBMetaData();
}
public HomeHandle getHomeHandleHome(Invocation mi) throws RemoteException
{
return getHomeHandleHome();
}
public HomeHandle getHomeHandleHome() throws RemoteException
{
EJBProxyFactory ci = getProxyFactory();
if (ci == null)
{
String msg = "No ProxyFactory, check for ProxyFactoryFinderInterceptor";
throw new IllegalStateException(msg);
}
EJBHome home = (EJBHome) ci.getEJBHome();
return home.getHomeHandle();
}
public EJBLocalHome getEJBLocalHome(Invocation mi)
{
return localProxyFactory.getEJBLocalHome();
}
protected Map getHomeMapping()
{
return homeMapping;
}
protected Map getBeanMapping()
{
return beanMapping;
}
}