Package org.jboss.ejb

Examples of org.jboss.ejb.InstancePool


      // Invocation on the handle, we don't need a bean instance
      if (getEJBObject.equals(mi.getMethod()))
         return getNext().invokeHome(mi);

      // get a new context from the pool (this is a home method call)
      InstancePool pool = container.getInstancePool();
      EnterpriseContext ctx = pool.get();

      // set the context on the Invocation
      mi.setEnterpriseContext(ctx);
     
      // It is a new context for sure so we can lock it
      ctx.lock();
     
      // Set the current security information
      /**
       * JBAS-3976: Setting principal on the context has been moved to a separate interceptor
       */
      AllowedOperationsAssociation.pushInMethodFlag(IN_EJB_HOME);

      try
      {
         // Invoke through interceptors
         return getNext().invokeHome(mi);
      }
      finally
      {
         synchronized (ctx)
         {
            AllowedOperationsAssociation.popInMethodFlag();

            // Release the lock
            ctx.unlock();
           
            // Still free? Not free if create() was called successfully
            if (ctx.getId() == null)
            {
               pool.free(ctx);
            }
         }
      }
   }
View Full Code Here


  
   public Object invoke(Invocation mi)
   throws Exception
   {
      InstanceCache cache = container.getInstanceCache();
      InstancePool pool = container.getInstancePool();
      Object methodID = mi.getId();
      EnterpriseContext ctx = null;
     
      BeanLock lock = container.getLockManager().getLock(methodID);
      boolean callerRunAsIdentityPresent = SecurityActions.peekRunAsIdentity() != null;
      boolean pushSecurityContext = SecurityActions.getSecurityContext() == null;
      try
      {
         /* The security context must be established before the cache
         lookup because the activation of a session should have the caller's
         security context as ejbActivate is allowed to call other secured
         resources. Since the pm makes the ejbActivate call, we need to
         set the caller's security context. The only reason this shows up for
         stateful session is that we moved the SecurityInterceptor to after
         the instance interceptor to allow security exceptions to result in
         invalidation of the session. This may be too literal an interpretation
         of the ejb spec requirement that runtime exceptions should invalidate
         the session.
          */
         if(!callerRunAsIdentityPresent && pushSecurityContext)
         {
            AuthenticationManager am = container.getSecurityManager();
            String securityDomain = SecurityConstants.DEFAULT_APPLICATION_POLICY;
            if(am != null)
               securityDomain = am.getSecurityDomain();
            SecurityActions.createAndSetSecurityContext(mi.getPrincipal(), mi.getCredential(),
                  securityDomain , null);
            //SecurityActions.pushSubjectContext(mi.getPrincipal(), mi.getCredential(), null);
         }

         lock.sync();
         try
         {          
            // Get context
            try
            {
               ctx = cache.get(methodID);
            }
            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);
            }
           
            // Associate it with the method invocation
            mi.setEnterpriseContext(ctx);
            // Set the JACC EnterpriseBean PolicyContextHandler data
            EnterpriseBeanPolicyContextHandler.setEnterpriseBean(ctx.getInstance());

            // BMT beans will lock and replace tx no matter what, CMT do work on transaction
            boolean isBMT = ((SessionMetaData)container.getBeanMetaData()).isBeanManagedTx();
            if (isBMT == false)
            {
              
               // Do we have a running transaction with the context
               if (ctx.getTransaction() != null &&
               // And are we trying to enter with another transaction
               !ctx.getTransaction().equals(mi.getTransaction()))
               {
                  // Calls must be in the same transaction
                  StringBuffer msg = new StringBuffer("Application Error: " +
                     "tried to enter Stateful bean with different tx context");
                  msg.append(", contextTx: " + ctx.getTransaction());
                  msg.append(", methodTx: " + mi.getTransaction());
                  throw new EJBException(msg.toString());
               }

               //If the instance will participate in a new transaction we register a sync for it
               if (ctx.getTransaction() == null && mi.getTransaction() != null)
               {
                  register(ctx, mi.getTransaction(), lock);
               }
            }
           
            if (!ctx.isLocked())
            {
              
               //take it!
               ctx.lock();
            }
            else
            {
               if (!isCallAllowed(mi))
               {
                  // Concurent calls are not allowed
                  throw new EJBException("Application Error: no concurrent " +
                        "calls on stateful beans");
               }
               else
               {
                  ctx.lock();
               }
            }
         }
         finally
         {
            lock.releaseSync();
         }

         // Set the current security information
         /**
          * JBAS-3976: Setting principal on the context has been moved to a separate interceptor
          */

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

         boolean validContext = true;
         try
         {
            // Invoke through interceptors
            Object ret = getNext().invoke(mi);
            return ret;
         }
         catch (RemoteException e)
         {
            // Discard instance
            cache.remove(methodID);
            pool.discard(ctx);
            validContext = false;

            throw e;
         }
         catch (RuntimeException e)
         {
            // Discard instance
            cache.remove(methodID);
            pool.discard(ctx);
            validContext = false;

            throw e;
         }
         catch (Error e)
         {
            // Discard instance
            cache.remove(methodID);
            pool.discard(ctx);
            validContext = false;

            throw e;
         }
         finally
         {
            AllowedOperationsAssociation.popInMethodFlag();

            if (validContext)
            {
               // Still a valid instance
               lock.sync();
               try
               {

                  // release it
                  ctx.unlock();
                 
                  // if removed, remove from cache
                  if (ctx.getId() == null)
                  {
                     // Remove from cache
                     cache.remove(methodID);
                     pool.free(ctx);
                  }
               }
               finally
               {
                  lock.releaseSync();
View Full Code Here

   // Interceptor implementation --------------------------------------
  
   public Object invokeHome(final Invocation mi) throws Exception
   {
      InstancePool pool = container.getInstancePool();
      StatelessSessionEnterpriseContext ctx = null;
      try
      {
         // Acquire an instance in case the ejbCreate throws a CreateException 
         ctx = (StatelessSessionEnterpriseContext) pool.get();
         mi.setEnterpriseContext(ctx);
         // Dispatch the method to the container
         return getNext().invokeHome(mi);
      }
      finally
      {
         mi.setEnterpriseContext(null);
         // If an instance was created, return it to the pool
         if( ctx != null )
            pool.free(ctx);
      }

   }
View Full Code Here

   }

   public Object invoke(final Invocation mi) throws Exception
   {
      // Get context
      InstancePool pool = container.getInstancePool();
      StatelessSessionEnterpriseContext ctx = null;
      try
      {
         ctx = (StatelessSessionEnterpriseContext) 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);
      }


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

      // Use this context
      mi.setEnterpriseContext(ctx);

      // JAXRPC/JAXWS message context
      Object msgContext = mi.getValue(InvocationKey.SOAP_MESSAGE_CONTEXT);

      // Timer invocation
      if (ejbTimeout.equals(mi.getMethod()))
      {
         AllowedOperationsAssociation.pushInMethodFlag(IN_EJB_TIMEOUT);
      }

      // Service Endpoint invocation
      else if (msgContext != null)
      {
         if (msgContext instanceof javax.xml.rpc.handler.MessageContext)
            ctx.setMessageContext((javax.xml.rpc.handler.MessageContext)msgContext);

         AllowedOperationsAssociation.pushInMethodFlag(IN_SERVICE_ENDPOINT_METHOD);
      }

      // Business Method Invocation
      else
      {
         AllowedOperationsAssociation.pushInMethodFlag(IN_BUSINESS_METHOD);
      }

      // There is no need for synchronization since the instance is always fresh also there should
      // never be a tx associated with the instance.
      try
      {
         Object obj = getNext().invoke(mi);
         return obj;

      }
      catch (RuntimeException e) // Instance will be GC'ed at MI return
      {
         mi.setEnterpriseContext(null);
         throw e;
      }
      catch (RemoteException e) // Instance will be GC'ed at MI return
      {
         mi.setEnterpriseContext(null);
         throw e;
      }
      catch (Error e) // Instance will be GC'ed at MI return
      {
         mi.setEnterpriseContext(null);
         throw e;
      }
      finally
      {
         AllowedOperationsAssociation.popInMethodFlag();
         EnterpriseBeanPolicyContextHandler.setEnterpriseBean(null);
        
         // Return context
         if (mi.getEnterpriseContext() != null)
         {
            pool.free(((EnterpriseContext) mi.getEnterpriseContext()));
         }
         else
         {
            pool.discard(ctx);
         }
      }
   }
View Full Code Here

   public Object invoke(final Invocation mi)
         throws Exception
   {
      // Get context
      MessageDrivenContainer mdc = (MessageDrivenContainer) container;
      InstancePool pool = mdc.getInstancePool();
      EnterpriseContext ctx = null;
      try
      {
         ctx = pool.get();
      }
      catch (EJBException e)
      {
         throw e;
      }
      catch (Exception e)
      {
         throw new EJBException("Unable to get an instance from the pool", e);
      }

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

      // Use this context
      mi.setEnterpriseContext(ctx);
      // Set the JACC EnterpriseBean PolicyContextHandler data
      EnterpriseBeanPolicyContextHandler.setEnterpriseBean(ctx.getInstance());

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

      // There is no need for synchronization since the instance is always
      // fresh also there should never be a tx associated with the instance.
      try
      {
         // Invoke through interceptors
         Object obj = getNext().invoke(mi);
         return obj;
      }
      catch (RuntimeException e) // Instance will be GC'ed at MI return
      {
         mi.setEnterpriseContext(null);
         throw e;
      }
      catch (RemoteException e) // Instance will be GC'ed at MI return
      {
         mi.setEnterpriseContext(null);
         throw e;
      }
      catch (Error e) // Instance will be GC'ed at MI return
      {
         mi.setEnterpriseContext(null);
         throw e;
      }
      finally
      {
         AllowedOperationsAssociation.popInMethodFlag();
         EnterpriseBeanPolicyContextHandler.setEnterpriseBean(null);
        
         // Return context
         if (mi.getEnterpriseContext() != null)
         {
            pool.free((EnterpriseContext) mi.getEnterpriseContext());
         }
         else
         {
            pool.discard(ctx);
         }
      }
   }
View Full Code Here

   {
      // 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  
      if (exception == null)
      {
         container.getInstancePool().free(ctx);
         return obj;
      }
     
      if (exception instanceof RuntimeException)
      {
         // if we get to here with a RuntimeException, we have a system exception.
         // EJB 2.1 section 18.3.1 says we need to discard our instance.
         pool.discard(ctx);
      }
      else
      {
         // if we get to here with an Exception, we have an application exception.
         // EJB 2.1 section 18.3.1 says we can keep the instance. We need to return
         // our instance to the instance pool so we dont get a memory leak. 
         pool.free(ctx);        
      }
     
      throw exception;
   }
View Full Code Here

      {
         //ctx = ec.getTxEntityMap().getCtx(mi.getTransaction(), key);
      }
      if (ctx == null)
      {
         InstancePool pool = ec.getInstancePool();
         try
         {
            ctx = (EntityEnterpriseContext) pool.get();
         }
         catch (EJBException e)
         {
            throw e;
         }
View Full Code Here

      // Invocation on the handle, we don't need a bean instance
      if (getEJBObject.equals(mi.getMethod()))
         return getNext().invokeHome(mi);

      // get a new context from the pool (this is a home method call)
      InstancePool pool = container.getInstancePool();
      EnterpriseContext ctx = pool.get();

      // set the context on the Invocation
      mi.setEnterpriseContext(ctx);
     
      // It is a new context for sure so we can lock it
      ctx.lock();
     
      // Set the current security information
      /**
       * JBAS-3976: Setting principal on the context has been moved to a separate interceptor
       */
      AllowedOperationsAssociation.pushInMethodFlag(IN_EJB_HOME);

      try
      {
         // Invoke through interceptors
         return getNext().invokeHome(mi);
      }
      finally
      {
         synchronized (ctx)
         {
            AllowedOperationsAssociation.popInMethodFlag();

            // Release the lock
            ctx.unlock();
           
            // Still free? Not free if create() was called successfully
            if (ctx.getId() == null)
            {
               pool.free(ctx);
            }
         }
      }
   }
View Full Code Here

  
   public Object invoke(Invocation mi)
   throws Exception
   {
      InstanceCache cache = container.getInstanceCache();
      InstancePool pool = container.getInstancePool();
      Object methodID = mi.getId();
      EnterpriseContext ctx = null;
     
      BeanLock lock = container.getLockManager().getLock(methodID);
      boolean callerRunAsIdentityPresent = SecurityActions.peekRunAsIdentity() != null;
      boolean pushSecurityContext = SecurityActions.getSecurityContext() == null;
      try
      {
         /* The security context must be established before the cache
         lookup because the activation of a session should have the caller's
         security context as ejbActivate is allowed to call other secured
         resources. Since the pm makes the ejbActivate call, we need to
         set the caller's security context. The only reason this shows up for
         stateful session is that we moved the SecurityInterceptor to after
         the instance interceptor to allow security exceptions to result in
         invalidation of the session. This may be too literal an interpretation
         of the ejb spec requirement that runtime exceptions should invalidate
         the session.
          */
         if(!callerRunAsIdentityPresent && pushSecurityContext)
         {
            AuthenticationManager am = container.getSecurityManager();
            String securityDomain = SecurityConstants.DEFAULT_APPLICATION_POLICY;
            if(am != null)
               securityDomain = am.getSecurityDomain();
            SecurityActions.createAndSetSecurityContext(mi.getPrincipal(), mi.getCredential(),
                  securityDomain , null);
            //SecurityActions.pushSubjectContext(mi.getPrincipal(), mi.getCredential(), null);
         }

         lock.sync();
         try
         {          
            // Get context
            try
            {
               ctx = cache.get(methodID);
            }
            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);
            }
           
            // Associate it with the method invocation
            mi.setEnterpriseContext(ctx);
            // Set the JACC EnterpriseBean PolicyContextHandler data
            EnterpriseBeanPolicyContextHandler.setEnterpriseBean(ctx.getInstance());

            // BMT beans will lock and replace tx no matter what, CMT do work on transaction
            boolean isBMT = ((SessionMetaData)container.getBeanMetaData()).isBeanManagedTx();
            if (isBMT == false)
            {
              
               // Do we have a running transaction with the context
               if (ctx.getTransaction() != null &&
               // And are we trying to enter with another transaction
               !ctx.getTransaction().equals(mi.getTransaction()))
               {
                  // Calls must be in the same transaction
                  StringBuffer msg = new StringBuffer("Application Error: " +
                     "tried to enter Stateful bean with different tx context");
                  msg.append(", contextTx: " + ctx.getTransaction());
                  msg.append(", methodTx: " + mi.getTransaction());
                  throw new EJBException(msg.toString());
               }

               //If the instance will participate in a new transaction we register a sync for it
               if (ctx.getTransaction() == null && mi.getTransaction() != null)
               {
                  register(ctx, mi.getTransaction(), lock);
               }
            }
           
            if (!ctx.isLocked())
            {
              
               //take it!
               ctx.lock();
            }
            else
            {
               if (!isCallAllowed(mi))
               {
                  // Concurent calls are not allowed
                  throw new EJBException("Application Error: no concurrent " +
                        "calls on stateful beans");
               }
               else
               {
                  ctx.lock();
               }
            }
         }
         finally
         {
            lock.releaseSync();
         }

         // Set the current security information
         /**
          * JBAS-3976: Setting principal on the context has been moved to a separate interceptor
          */

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

         boolean validContext = true;
         try
         {
            // Invoke through interceptors
            Object ret = getNext().invoke(mi);
            return ret;
         }
         catch (RemoteException e)
         {
            // Discard instance
            cache.remove(methodID);
            pool.discard(ctx);
            validContext = false;

            throw e;
         }
         catch (RuntimeException e)
         {
            // Discard instance
            cache.remove(methodID);
            pool.discard(ctx);
            validContext = false;

            throw e;
         }
         catch (Error e)
         {
            // Discard instance
            cache.remove(methodID);
            pool.discard(ctx);
            validContext = false;

            throw e;
         }
         finally
         {
            AllowedOperationsAssociation.popInMethodFlag();

            if (validContext)
            {
               // Still a valid instance
               lock.sync();
               try
               {

                  // release it
                  ctx.unlock();
                 
                  // if removed, remove from cache
                  if (ctx.getId() == null)
                  {
                     // Remove from cache
                     cache.remove(methodID);
                     pool.free(ctx);
                  }
               }
               finally
               {
                  lock.releaseSync();
View Full Code Here

/*     */   {
/* 124 */     if (getEJBObject.equals(mi.getMethod())) {
/* 125 */       return getNext().invokeHome(mi);
/*     */     }
/*     */
/* 128 */     InstancePool pool = this.container.getInstancePool();
/* 129 */     EnterpriseContext ctx = pool.get();
/*     */
/* 132 */     mi.setEnterpriseContext(ctx);
/*     */
/* 135 */     ctx.lock();
/*     */
/* 142 */     AllowedOperationsAssociation.pushInMethodFlag(IN_EJB_HOME);
/*     */     try
/*     */     {
/* 147 */       localObject1 = getNext().invokeHome(mi);
/*     */     }
/*     */     finally
/*     */     {
/* 151 */       synchronized (ctx)
/*     */       {
/*     */         Object localObject1;
/* 153 */         AllowedOperationsAssociation.popInMethodFlag();
/*     */
/* 156 */         ctx.unlock();
/*     */
/* 159 */         if (ctx.getId() == null)
/*     */         {
/* 161 */           pool.free(ctx);
/*     */         }
/*     */       }
/*     */     }
/*     */   }
View Full Code Here

TOP

Related Classes of org.jboss.ejb.InstancePool

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.