Package org.jboss.ejb

Examples of org.jboss.ejb.BeanLock


   protected void tryToPassivate(EnterpriseContext instance)
   {
      Object id = instance.getId();
      if(id != null)
      {
         BeanLock lock = container.getLockManager().getLock(id);
         try
         {
            lock.sync();
            if(canPassivate(instance))
            {
               try
               {
                  remove(id);
                  EntityEnterpriseContext entity = (EntityEnterpriseContext) instance;
                  container.getPersistenceManager().passivateEntity(entity);
                  container.getInstancePool().free(instance);
               }
               catch(Exception ignored)
               {
                  log.warn("failed to passivate, id=" + id, ignored);
               }
            }
            else
            {
               log.warn("Unable to passivate due to ctx lock, id=" + id);
            }
         }
         finally
         {
            lock.releaseSync();
            container.getLockManager().removeLockRef(id);
         }
      }
   }
View Full Code Here


    */
   protected void tryToPassivate(EnterpriseContext ctx, boolean passivateAfterCommit)
   {
      Object id = ctx.getId();
      if (id == null) return;
      BeanLock lock = getContainer().getLockManager().getLock(id);
      boolean lockedBean = false;
      try
      {
         /* If this is a BeanLockExt only attempt the lock as the call to
         remove is going to have to acquire the cache lock, but this may already
         be held since this method is called by passivation policies without
         the cache lock. This can lead to a deadlock as in the case of a size based
         eviction during a cache get attempts to lock the bean that has been
         locked by an age based background thread as seen in bug 987389 on
         sourceforge.
         */
         if( lock instanceof BeanLockExt )
         {
            BeanLockExt lock2 = (BeanLockExt) lock;
            lockedBean = lock2.attemptSync();
            if( lockedBean == false )
            {
               unableToPassivateDueToCtxLock(ctx, passivateAfterCommit);
               return;
            }
         }
         else
         {
            // Use the blocking sync
            lock.sync();
            lockedBean = true;
         }

         if (canPassivate(ctx))
         {
            ClassLoader ccl = SecurityActions.getContextClassLoader();
            SecurityActions.setContextClassLoader(getContainer().getClassLoader());
            try
            {
               remove(id);
               passivate(ctx);
               freeContext(ctx);
            }
            catch (Exception ignored)
            {
               log.warn("failed to passivate, id="+id, ignored);
            }
            finally
            {
               SecurityActions.setContextClassLoader(ccl);
            }
         }
         else
         {
            // Touch the entry to make it MRU
            synchronized (getCacheLock())
            {
               getCache().get(id);
            }

            unableToPassivateDueToCtxLock(ctx, passivateAfterCommit);
         }
      }
      finally
      {
         if( lockedBean )
            lock.releaseSync();
         getContainer().getLockManager().removeLockRef(id);
      }
   }
View Full Code Here

      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();
               }
            }
         }
      }
      finally
      {
         container.getLockManager().removeLockRef(lock.getId());
         if(!callerRunAsIdentityPresent && pushSecurityContext)
           SecurityActions.clearSecurityContext();
         EnterpriseBeanPolicyContextHandler.setEnterpriseBean(null);
      }
   }
View Full Code Here

   protected void tryToPassivate(EnterpriseContext instance)
   {
      Object id = instance.getId();
      if(id != null)
      {
         BeanLock lock = container.getLockManager().getLock(id);
         try
         {
            lock.sync();
            if(canPassivate(instance))
            {
               try
               {
                  remove(id);
                  EntityEnterpriseContext entity = (EntityEnterpriseContext) instance;
                  container.getPersistenceManager().passivateEntity(entity);
                  container.getInstancePool().free(instance);
               }
               catch(Exception ignored)
               {
                  log.warn("failed to passivate, id=" + id, ignored);
               }
            }
            else
            {
               log.warn("Unable to passivate due to ctx lock, id=" + id);
            }
         }
         finally
         {
            lock.releaseSync();
            container.getLockManager().removeLockRef(id);
         }
      }
   }
View Full Code Here

         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;
View Full Code Here

    */
   protected void tryToPassivate(EnterpriseContext ctx, boolean passivateAfterCommit)
   {
      Object id = ctx.getId();
      if (id == null) return;
      BeanLock lock = getContainer().getLockManager().getLock(id);
      boolean lockedBean = false;
      try
      {
         /* If this is a BeanLockExt only attempt the lock as the call to
         remove is going to have to acquire the cache lock, but this may already
         be held since this method is called by passivation policies without
         the cache lock. This can lead to a deadlock as in the case of a size based
         eviction during a cache get attempts to lock the bean that has been
         locked by an age based background thread as seen in bug 987389 on
         sourceforge.
         */
         if( lock instanceof BeanLockExt )
         {
            BeanLockExt lock2 = (BeanLockExt) lock;
            lockedBean = lock2.attemptSync();
            if( lockedBean == false )
            {
               unableToPassivateDueToCtxLock(ctx, passivateAfterCommit);
               return;
            }
         }
         else
         {
            // Use the blocking sync
            lock.sync();
            lockedBean = true;
         }

         if (canPassivate(ctx))
         {
            try
            {
               remove(id);
               passivate(ctx);
               freeContext(ctx);
            }
            catch (Exception ignored)
            {
               log.warn("failed to passivate, id="+id, ignored);
            }
         }
         else
         {
            // Touch the entry to make it MRU
            synchronized (getCacheLock())
            {
               getCache().get(id);
            }

            unableToPassivateDueToCtxLock(ctx, passivateAfterCommit);
         }
      }
      finally
      {
         if( lockedBean )
            lock.releaseSync();
         getContainer().getLockManager().removeLockRef(id);
      }
   }
View Full Code Here

         // 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);
            }
            finally
            {
               container.getLockManager().removeLockRef(lock.getId());
            }
         }
      }
      return rtn;
   }
View Full Code Here

      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();
               }
            }
         }
      }
      finally
      {
         container.getLockManager().removeLockRef(lock.getId());
         if(!callerRunAsIdentityPresent && pushSecurityContext)
           SecurityActions.clearSecurityContext();
         EnterpriseBeanPolicyContextHandler.setEnterpriseBean(null);
      }
   }
View Full Code Here

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

      // The lock.
      BeanLock lock ;
 
      boolean threadIsScheduled = false;
      boolean trace = log.isTraceEnabled();
 
      if( trace ) log.trace("Begin invoke, key="+key);
  
 
      lock = container.getLockManager().getLock(key);
      try
      {
  
         lock.schedule(mi);
  
         try {
   
            return getNext().invoke(mi);
         }
  
         finally
         {
   
            // we are done with the method, decrease the count, if it reaches 0 it will wake up
            // the next thread
            lock.sync();
            lock.endInvocation(mi);
            lock.releaseSync();
         }
      }
      finally
      {
  
View Full Code Here

/*     */   protected void tryToPassivate(EnterpriseContext instance)
/*     */   {
/* 160 */     Object id = instance.getId();
/* 161 */     if (id != null)
/*     */     {
/* 163 */       BeanLock lock = this.container.getLockManager().getLock(id);
/*     */       try
/*     */       {
/* 166 */         lock.sync();
/* 167 */         if (canPassivate(instance))
/*     */         {
/*     */           try
/*     */           {
/* 171 */             remove(id);
/* 172 */             EntityEnterpriseContext entity = (EntityEnterpriseContext)instance;
/* 173 */             this.container.getPersistenceManager().passivateEntity(entity);
/* 174 */             this.container.getInstancePool().free(instance);
/*     */           }
/*     */           catch (Exception ignored)
/*     */           {
/* 178 */             log.warn("failed to passivate, id=" + id, ignored);
/*     */           }
/*     */         }
/*     */         else
/*     */         {
/* 183 */           log.warn("Unable to passivate due to ctx lock, id=" + id);
/*     */         }
/*     */       }
/*     */       finally
/*     */       {
/* 188 */         lock.releaseSync();
/* 189 */         this.container.getLockManager().removeLockRef(id);
/*     */       }
/*     */     }
/*     */   }
View Full Code Here

TOP

Related Classes of org.jboss.ejb.BeanLock

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.