FileDocCategorySizeDatePackage
CachedConnectionInterceptor.javaAPI DocJBoss 4.2.19918Fri Jul 13 21:01:18 BST 2007org.jboss.resource.connectionmanager

CachedConnectionInterceptor

public class CachedConnectionInterceptor extends org.jboss.ejb.plugins.AbstractInterceptor implements org.jboss.ejb.EntityPersistenceManager
CachedConnectionInterceptor
author
David Jencks
author
Erwin Guib
author
Adrian Brock
version
$Revision: 57189 $

Fields Summary
private final CachedConnectionManager
ccm
private final Logger
log
private org.jboss.ejb.Container
container
private org.jboss.ejb.EntityPersistenceManager
pm
private Set
unsharableResources
Constructors Summary
public CachedConnectionInterceptor()


      
   
      try
      {
         MBeanServer server = MBeanServerLocator.locateJBoss();
         ccm = (CachedConnectionManager) server.getAttribute(CachedConnectionManagerMBean.OBJECT_NAME, "Instance");
      }
      catch (Exception e)
      {
         JMXExceptionDecoder.rethrow(e);
         throw e;
      }
   
Methods Summary
public voidactivateEntity(org.jboss.ejb.EntityEnterpriseContext instance)

      pm.activateEntity(instance);
   
public java.lang.ObjectcreateBeanClassInstance()

      return pm.createBeanClassInstance();
   
public voidcreateEntity(java.lang.reflect.Method m, java.lang.Object[] args, org.jboss.ejb.EntityEnterpriseContext instance)

      pm.createEntity(m, args, instance);
   
public java.util.CollectionfindEntities(java.lang.reflect.Method finderMethod, java.lang.Object[] args, org.jboss.ejb.EntityEnterpriseContext instance, org.jboss.ejb.GenericEntityObjectFactory factory)

      return pm.findEntities(finderMethod, args, instance, factory);
   
public java.lang.ObjectfindEntity(java.lang.reflect.Method finderMethod, java.lang.Object[] args, org.jboss.ejb.EntityEnterpriseContext instance, org.jboss.ejb.GenericEntityObjectFactory factory)

      return pm.findEntity(finderMethod, args, instance, factory);
   
public org.jboss.ejb.ContainergetContainer()

      return container;
   
public org.jboss.ejb.EntityPersistenceManagergetDelegatePersistenceManager()
Return the real EntityPersistenceManager to which this interceptor delegates.

return
the real EntityPersistenceManager

      return pm;
   
public java.lang.Objectinvoke(org.jboss.invocation.Invocation mi)

      Object key = ((EnterpriseContext) mi.getEnterpriseContext()).getInstance();
      try
      {
         ccm.pushMetaAwareObject(key, unsharableResources);
         try
         {
            return getNext().invoke(mi);
         }
         finally
         {
            ccm.popMetaAwareObject(unsharableResources);
         }
      }
      catch (ResourceException e)
      {
         InvocationType type = mi.getType();
         boolean isLocal = (type == InvocationType.LOCAL || type == InvocationType.LOCALHOME);
         if (isLocal)
            throw new EJBException("Resource problem during invoke", e);
         else
            throw new RemoteException("Resource problem during invoke", e);
      }
   
public voidinvokeEjbStore(org.jboss.ejb.EntityEnterpriseContext ctx)

      Object key = ctx.getInstance();
      try
      {
         ccm.pushMetaAwareObject(key, unsharableResources);
         try
         {
            pm.invokeEjbStore(ctx);
         }
         finally
         {
            ccm.popMetaAwareObject(unsharableResources);
         }
      }
      catch (ResourceException e)
      {
         throw new RemoteException("Could not store!: ", e);
      }
   
public java.lang.ObjectinvokeHome(org.jboss.invocation.Invocation mi)

      EnterpriseContext ctx = (EnterpriseContext) mi.getEnterpriseContext();
      if (ctx == null)
         return getNext().invokeHome(mi);
      else
      {
         Object key = ctx.getInstance();
         try
         {
            ccm.pushMetaAwareObject(key, unsharableResources);
            try
            {
               return getNext().invokeHome(mi);
            }
            finally
            {
               ccm.popMetaAwareObject(unsharableResources);
            }
         }
         catch (ResourceException e)
         {
            InvocationType type = mi.getType();
            boolean isLocal = (type == InvocationType.LOCAL || type == InvocationType.LOCALHOME);
            if (isLocal)
               throw new EJBException("Resource problem during invokeHome", e);
            else
               throw new RemoteException("Resource problem during invokeHome", e);
         }
      }
   
public booleanisModified(org.jboss.ejb.EntityEnterpriseContext ctx)

      return pm.isModified(ctx);
   
public booleanisStoreRequired(org.jboss.ejb.EntityEnterpriseContext instance)

      return pm.isStoreRequired(instance);
   
public voidloadEntity(org.jboss.ejb.EntityEnterpriseContext instance)

      pm.loadEntity(instance);
   
public voidpassivateEntity(org.jboss.ejb.EntityEnterpriseContext instance)

      pm.passivateEntity(instance);
   
public voidpostCreateEntity(java.lang.reflect.Method m, java.lang.Object[] args, org.jboss.ejb.EntityEnterpriseContext instance)

      pm.postCreateEntity(m, args, instance);
   
public voidremoveEntity(org.jboss.ejb.EntityEnterpriseContext instance)

      pm.removeEntity(instance);
   
public voidsetContainer(org.jboss.ejb.Container container)

      this.container = container;
   
public voidstart()

      log.debug("start called in CachedConnectionInterceptor");
      if (container == null)
      {
         log.warn("container is null, can't steal persistence manager");
         return;
      }
      if (container instanceof EntityContainer)
      {
         EntityContainer ec = (EntityContainer) container;

         if (ec.getPersistenceManager() == null)
         {
            log.info("no persistence manager in container!");
            return;
         }
         if (ec.getPersistenceManager() == this)
         {
            log.info(" persistence manager in container already set!");
            return;
         }
         pm = ec.getPersistenceManager();
         ec.setPersistenceManager(this);
      }

      // get the JNDI names for all resources that are referenced "Unshareable"
      BeanMetaData bmd = container.getBeanMetaData();
      ApplicationMetaData appMetaData = bmd.getApplicationMetaData();
      ResourceRefMetaData resRefMetaData;
      String jndiName;

      for (Iterator iter = bmd.getResourceReferences(); iter.hasNext();)
      {
         resRefMetaData = (ResourceRefMetaData) iter.next();
         jndiName = resRefMetaData.getJndiName();
         if (jndiName == null)
         {
            jndiName = appMetaData.getResourceByName(resRefMetaData.getResourceName());
         }
         if (jndiName != null && resRefMetaData.isShareable() == false)
         {
            int i = jndiName.indexOf(':");
            if (jndiName.charAt(i + 1) == '/")
            {
               i++;
            }
            unsharableResources.add(jndiName.substring(i + 1));
         }
      }

   
public voidstop()

      if (container != null && pm != null && ((EntityContainer) container).getPersistenceManager() == this)
      {
         ((EntityContainer) container).setPersistenceManager(pm);
         pm = null;
      }
      unsharableResources.clear();
   
public voidstoreEntity(org.jboss.ejb.EntityEnterpriseContext ctx)

      Object key = ctx.getInstance();
      try
      {
         ccm.pushMetaAwareObject(key, unsharableResources);
         try
         {
            pm.storeEntity(ctx);
         }
         finally
         {
            ccm.popMetaAwareObject(unsharableResources);
         }
      }
      catch (ResourceException e)
      {
         throw new RemoteException("Could not store!: ", e);
      }