Package org.jboss.ejb

Examples of org.jboss.ejb.EntityEnterpriseContext


   // Protected -----------------------------------------------------

   protected void unableToPassivateDueToCtxLock(EnterpriseContext ctx, boolean passivateAfterCommit)
   {
      EntityEnterpriseContext ectx = (EntityEnterpriseContext)ctx;
      ectx.setPassivateAfterCommit(passivateAfterCommit);
      ConfigurationMetaData config = m_container.getBeanMetaData().getContainerConfiguration();
      if(!config.isStoreNotFlushed() && ectx.hasTxSynchronization())
      {
         ectx.setTxAssociation(GlobalTxEntityMap.PREVENT_SYNC);
      }
   }
View Full Code Here


   public Object invokeHome(Invocation mi)
      throws Exception
   {
      // Get context
      EntityContainer container = (EntityContainer) getContainer();
      EntityEnterpriseContext ctx = (EntityEnterpriseContext) container.getInstancePool().get();
      ctx.setTxAssociation(GlobalTxEntityMap.NOT_READY);
      InstancePool pool = container.getInstancePool();

      // Pass it to the method invocation
      mi.setEnterpriseContext(ctx);
  
      // Give it the transaction
      ctx.setTransaction(mi.getTransaction());
  
      // Set the current security information
      ctx.setPrincipal(mi.getPrincipal());

      AllowedOperationsAssociation.pushInMethodFlag(IN_EJB_HOME);
     
      // Invoke through interceptors
     
      Object obj = null;
      Exception exception = null;

      try
      {
         obj = getNext().invokeHome(mi);
         
         // Is the context now with an identity? in which case we need to insert
         if (ctx.getId() != null)
         {
            BeanLock lock = container.getLockManager().getLock(ctx.getCacheKey());
            lock.sync(); // lock all access to BeanLock
            try
            {
               // Check there isn't a context already in the cache
               // e.g. commit-option B where the entity was
               // created then removed externally
               InstanceCache cache = container.getInstanceCache();
               cache.remove(ctx.getCacheKey());
      
               // marcf: possible race on creation and usage
               // insert instance in cache,
               cache.insert(ctx);
            }
            finally
            {
               lock.releaseSync();
               container.getLockManager().removeLockRef(ctx.getCacheKey());
            }
            
            // we are all done            
            return obj;
         }
      }
      catch (Exception e)
      {
         exception = e;
      }
      finally
      {
         AllowedOperationsAssociation.popInMethodFlag();
      }

      ctx.setTransaction(null);
      // EntityCreateInterceptor will access ctx if it is not null and call postCreate
      mi.setEnterpriseContext(null);
     
      // if we get to here with a null exception then our invocation is
      // just a home invocation. Return our instance to the instance pool  
View Full Code Here

      // The key
      Object key = mi.getId();

      // The context
      EntityEnterpriseContext ctx;
      try
      {
         ctx = (EntityEnterpriseContext) container.getInstanceCache().get(key);
      }
      catch (NoSuchObjectException e)
      {
         if (mi.isLocal())
            throw new NoSuchObjectLocalException(e.getMessage());
         else
            throw e;
      }
      catch (EJBException e)
      {
         throw e;
      }
      catch (RemoteException e)
      {
         throw e;
      }
      catch (Exception e)
      {
         InvocationType type = mi.getType();
         boolean isLocal = (type == InvocationType.LOCAL || type == InvocationType.LOCALHOME);
         if (isLocal)
            throw new EJBException("Unable to get an instance from the pool/cache", e);
         else
            throw new RemoteException("Unable to get an intance from the pool/cache", e);
      }

      if (trace) log.trace("Begin invoke, key=" + key);

      // Associate transaction, in the new design the lock already has the transaction from the
      // previous interceptor

      // Don't set the transction if a read-only method.  With a read-only method, the ctx can be shared
      // between multiple transactions.
      Transaction tx = mi.getTransaction();
      if (!container.isReadOnly())
      {
         Method method = mi.getMethod();
         if (method == null ||
            !container.getBeanMetaData().isMethodReadOnly(method.getName()))
         {
            ctx.setTransaction(tx);
         }
      }

      // Set the current security information
      ctx.setPrincipal(mi.getPrincipal());
      // Set the JACC EnterpriseBean PolicyContextHandler data
      EnterpriseBeanPolicyContextHandler.setEnterpriseBean(ctx.getInstance());

      // Set context on the method invocation
      mi.setEnterpriseContext(ctx);

      if (ejbTimeout.equals(mi.getMethod()))
         AllowedOperationsAssociation.pushInMethodFlag(IN_EJB_TIMEOUT);
      else
         AllowedOperationsAssociation.pushInMethodFlag(IN_BUSINESS_METHOD);

      Throwable exceptionThrown = null;
      boolean discardContext = false;
      try
      {
         Object obj = getNext().invoke(mi);
         return obj;
      }
      catch (RemoteException e)
      {
         exceptionThrown = e;
         discardContext = true;
         throw e;
      }
      catch (RuntimeException e)
      {
         exceptionThrown = e;
         discardContext = true;
         throw e;
      }
      catch (Error e)
      {
         exceptionThrown = e;
         discardContext = true;
         throw e;
      }
      catch (Exception e)
      {
         exceptionThrown = e;
         throw e;
      }
      catch (Throwable e)
      {
         exceptionThrown = e;
         discardContext = true;
         throw new NestedRuntimeException(e);
      }
      finally
      {
         AllowedOperationsAssociation.popInMethodFlag();

         // Make sure we clear the transaction on an error before synchronization.
         // But avoid a race with a transaction rollback on a synchronization
         // that may have moved the context onto a different transaction
         if (exceptionThrown != null && tx != null)
         {
            Transaction ctxTx = ctx.getTransaction();
            if (tx.equals(ctxTx) && ctx.hasTxSynchronization() == false)
               ctx.setTransaction(null);
         }

         // If an exception has been thrown,
         if (exceptionThrown != null &&
            // if tx, the ctx has been registered in an InstanceSynchronization.
            // that will remove the context, so we shouldn't.
            // if no synchronization then we need to do it by hand
            // But not for application exceptions
            !ctx.hasTxSynchronization() && discardContext)
         {
            // Discard instance
            // EJB 1.1 spec 12.3.1
            container.getInstanceCache().remove(key);

            if (trace) log.trace("Ending invoke, exceptionThrown, ctx=" + ctx, exceptionThrown);
         }
         else if (ctx.getId() == null)
         {
            // The key from the Invocation still identifies the right cachekey
            container.getInstanceCache().remove(key);

            if (trace) log.trace("Ending invoke, cache removal, ctx=" + ctx);
View Full Code Here

      }

      org.jboss.ejb.plugins.cmp.jdbc.bridge.CMRMessage msg = ((CMRInvocation)mi).getCmrMessage();

      // We are going to work with the context a lot
      EntityEnterpriseContext ctx = (EntityEnterpriseContext)mi.getEnterpriseContext();
      JDBCCMRFieldBridge2 cmrField = (JDBCCMRFieldBridge2)mi.getArguments()[0];

      if(org.jboss.ejb.plugins.cmp.jdbc.bridge.CMRMessage.ADD_RELATION == msg)
      {
         Object relatedId = mi.getArguments()[1];
         if(log.isTraceEnabled())
         {
            log.trace("Add relation: field=" + cmrField.getFieldName() +
               " id=" + ctx.getId() +
               " relatedId=" + relatedId);
         }

         cmrField.addRelatedId(ctx, relatedId);
      }
      else if(org.jboss.ejb.plugins.cmp.jdbc.bridge.CMRMessage.REMOVE_RELATION == msg)
      {
         // call removeRelation
         Object relatedId = mi.getArguments()[1];
         if(log.isTraceEnabled())
         {
            log.trace("Remove relation: field=" + cmrField.getFieldName() +
               " id=" + ctx.getId() +
               " relatedId=" + relatedId);
         }

         cmrField.removeRelatedId(ctx, relatedId);
      }
View Full Code Here

         // Not a relationship message. Invoke down the chain
         return getNext().invoke(mi);
      }

      // We are going to work with the context a lot
      EntityEnterpriseContext ctx = (EntityEnterpriseContext)mi.getEnterpriseContext();
      JDBCCMRFieldBridge cmrField = (JDBCCMRFieldBridge)mi.getArguments()[0];

      if(CMRMessage.GET_RELATED_ID == relationshipMessage)
      {
         // call getRelateId
         if(log.isTraceEnabled())
         {
            log.trace("Getting related id: field=" + cmrField.getFieldName() + " id=" + ctx.getId());
         }
         return cmrField.getRelatedId(ctx);

      }
      else if(CMRMessage.ADD_RELATION == relationshipMessage)
      {
         // call addRelation
         Object relatedId = mi.getArguments()[1];
         if(log.isTraceEnabled())
         {
            log.trace("Add relation: field=" + cmrField.getFieldName() +
               " id=" + ctx.getId() +
               " relatedId=" + relatedId);
         }

         cmrField.addRelation(ctx, relatedId);

         return null;

      }
      else if(CMRMessage.REMOVE_RELATION == relationshipMessage)
      {
         // call removeRelation
         Object relatedId = mi.getArguments()[1];
         if(log.isTraceEnabled())
         {
            log.trace("Remove relation: field=" + cmrField.getFieldName() +
               " id=" + ctx.getId() +
               " relatedId=" + relatedId);
         }

         cmrField.removeRelation(ctx, relatedId);
View Full Code Here

   }

   protected EnterpriseContext create(Object instance)
      throws Exception
   {
      return new EntityEnterpriseContext(instance, getContainer());
   }
View Full Code Here

   public Object invokeHome(Invocation mi)
      throws Exception
   {
      // Get context
      EntityContainer ec = (EntityContainer) getContainer();
      EntityEnterpriseContext ctx = (EntityEnterpriseContext) ec.getInstancePool().get();

    // Pass it to the method invocation
      mi.setEnterpriseContext(ctx);

      // Give it the transaction
      ctx.setTransaction(mi.getTransaction());

      // Set the current security information
      ctx.setPrincipal(mi.getPrincipal());

      AllowedOperationsAssociation.pushInMethodFlag(IN_EJB_HOME);

      Object result;
      try
      {
         // Invoke through interceptors
         result = getNext().invokeHome(mi);
      }
      finally
      {
         AllowedOperationsAssociation.popInMethodFlag();
      }
     
      // No id, means we can put the context back in the pool
      if (ctx.getId() == null)
      {
         ctx.setTransaction(null);
         ec.getInstancePool().free(ctx);
      }
     
      // We are done
      return result;
View Full Code Here

   {

      // The key
      Object key = mi.getId();

      EntityEnterpriseContext ctx = null;
      EntityContainer ec = (EntityContainer) container;
      if (mi.getTransaction() != null)
      {
         //ctx = ec.getTxEntityMap().getCtx(mi.getTransaction(), key);
      }
      if (ctx == null)
      {
         InstancePool pool = ec.getInstancePool();
         try
         {
            ctx = (EntityEnterpriseContext) pool.get();
         }
         catch (EJBException e)
         {
            throw e;
         }
         catch (RemoteException e)
         {
            throw e;
         }
         catch (Exception e)
         {
            InvocationType type = mi.getType();
            boolean isLocal = (type == InvocationType.LOCAL || type == InvocationType.LOCALHOME);
            if (isLocal)
               throw new EJBException("Unable to get an instance from the pool", e);
            else
               throw new RemoteException("Unable to get an intance from the pool", e);
         }
         ctx.setCacheKey(key);
         ctx.setId(key);
         EntityPersistenceManager pm = ec.getPersistenceManager();
         pm.activateEntity(ctx);
      }

      boolean trace = log.isTraceEnabled();
      if( trace ) log.trace("Begin invoke, key="+key);

      // Associate transaction, in the new design the lock already has the transaction from the
      // previous interceptor
      ctx.setTransaction(mi.getTransaction());

      // Set the current security information
      ctx.setPrincipal(mi.getPrincipal());
      // Set the JACC EnterpriseBean PolicyContextHandler data
      EnterpriseBeanPolicyContextHandler.setEnterpriseBean(ctx.getInstance());

      // Set context on the method invocation
      mi.setEnterpriseContext(ctx);

      if (ejbTimeout.equals(mi.getMethod()))
View Full Code Here

      }
   }

   public Object invokeHome(Invocation mi) throws Exception
   {
      EntityEnterpriseContext ctx = (EntityEnterpriseContext)mi.getEnterpriseContext();
      Transaction tx = mi.getTransaction();

      Object rtn = getNext().invokeHome(mi);

      // An anonymous context was sent in, so if it has an id it is a real instance now
      if(ctx.getId() != null)
      {

         // it doesn't need to be read, but it might have been changed from the db already.
         ctx.setValid(true);

         if(tx != null)
         {
            BeanLock lock = container.getLockManager().getLock(ctx.getCacheKey());
            try
            {
               lock.schedule(mi);
               register(ctx, tx); // Set tx
               lock.endInvocation(mi);
View Full Code Here

   }

   public Object invoke(Invocation mi) throws Exception
   {
      // We are going to work with the context a lot
      EntityEnterpriseContext ctx = (EntityEnterpriseContext)mi.getEnterpriseContext();

      // The Tx coming as part of the Method Invocation
      Transaction tx = mi.getTransaction();

      if(log.isTraceEnabled())
         log.trace("invoke called for ctx " + ctx + ", tx=" + tx);

      if(!ctx.isValid())
      {
         container.getPersistenceManager().loadEntity(ctx);
         ctx.setValid(true);
      }

      // mark the context as read only if this is a readonly method and the context
      // was not already readonly
      boolean didSetReadOnly = false;
      if(!ctx.isReadOnly() &&
         (container.isReadOnly() ||
         container.getBeanMetaData().isMethodReadOnly(mi.getMethod())))
      {
         ctx.setReadOnly(true);
         didSetReadOnly = true;
      }

      // So we can go on with the invocation

      // Invocation with a running Transaction
      try
      {
         if(tx != null && tx.getStatus() != Status.STATUS_NO_TRANSACTION)
         {
            // readonly does not synchronize, lock or belong with transaction.
            boolean isReadOnly = container.isReadOnly();
            if(isReadOnly == false)
            {
               Method method = mi.getMethod();
               if(method != null)
                  isReadOnly = container.getBeanMetaData().isMethodReadOnly(method.getName());
            }
            try
            {
               if(isReadOnly == false)
               {
                  // register the wrapper with the transaction monitor (but only
                  // register once). The transaction demarcation will trigger the
                  // storage operations
                  register(ctx, tx);
               }

               //Invoke down the chain
               Object retVal = getNext().invoke(mi);

               // Register again as a finder in the middle of a method
               // will de-register this entity, and then the rest of the method can
               // change fields which will never be stored
               if(isReadOnly == false)
               {
                  // register the wrapper with the transaction monitor (but only
                  // register once). The transaction demarcation will trigger the
                  // storage operations
                  register(ctx, tx);
               }

               // return the return value
               return retVal;
            }
            finally
            {
               // We were read-only and the context wasn't already synchronized, tidyup the cache
               if(isReadOnly && ctx.hasTxSynchronization() == false)
               {
                  switch(commitOption)
                  {
                     // Keep instance active, but invalidate state
                     case ConfigurationMetaData.B_COMMIT_OPTION:
                        // Invalidate state (there might be other points of entry)
                        ctx.setValid(false);
                        break;

                        // Invalidate everything AND Passivate instance
                     case ConfigurationMetaData.C_COMMIT_OPTION:
                        try
                        {
                           // FIXME: We cannot passivate here, because previous
                           // interceptors work with the context, in particular
                           // the re-entrance interceptor is doing lock counting
                           // Just remove it from the cache
                           if(ctx.getId() != null)
                              container.getInstanceCache().remove(ctx.getId());
                        }
                        catch(Exception e)
                        {
                           log.debug("Exception releasing context", e);
                        }
                        break;
                  }
               }
            }
         }
         else
         {
            // No tx
            try
            {
               Object result = getNext().invoke(mi);

               // Store after each invocation -- not on exception though, or removal
               // And skip reads too ("get" methods)
               if(ctx.getId() != null && !container.isReadOnly())
               {
                  container.invokeEjbStore(ctx);
                  container.storeEntity(ctx);
               }

               return result;
            }
            catch(Exception e)
            {
               // Exception - force reload on next call
               ctx.setValid(false);
               throw e;
            }
            finally
            {
               switch(commitOption)
               {
                  // Keep instance active, but invalidate state
                  case ConfigurationMetaData.B_COMMIT_OPTION:
                     // Invalidate state (there might be other points of entry)
                     ctx.setValid(false);
                     break;

                     // Invalidate everything AND Passivate instance
                  case ConfigurationMetaData.C_COMMIT_OPTION:
                     try
                     {
                        // Do not call release if getId() is null.  This means that
                        // the entity has been removed from cache.
                        // release will schedule a passivation and this removed ctx
                        // could be put back into the cache!
                        // This is necessary because we have no lock, we
                        // don't want to return an instance to the pool that is
                        // being used
                        if(ctx.getId() != null)
                           container.getInstanceCache().remove(ctx.getId());
                     }
                     catch(Exception e)
                     {
                        log.debug("Exception releasing context", e);
                     }
                     break;
               }
            }
         }
      }
      finally
      {
         // if we marked the context as read only we need to reset it
         if(didSetReadOnly)
         {
            ctx.setReadOnly(false);
         }
      }
   }
View Full Code Here

TOP

Related Classes of org.jboss.ejb.EntityEnterpriseContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.