FileDocCategorySizeDatePackage
SemaphoredMethodAspect.javaAPI DocJBoss 4.2.12821Fri Jul 13 21:02:36 BST 2007org.jboss.aspects.concurrent

SemaphoredMethodAspect

public class SemaphoredMethodAspect extends Object
comment
author
Bill Burke

Fields Summary
protected Semaphore
semaphore
protected SemaphoredMethod
props
Constructors Summary
public SemaphoredMethodAspect(SemaphoredMethod m)

      props = m;
      semaphore = new Semaphore(m.permits(), m.isFair());
   
Methods Summary
public java.lang.Objectacquire(org.jboss.aop.joinpoint.Invocation invocation)

      if (props.timeout() == -1)
      {
            blockIndefinately();
      }
      else if (props.timeout() == 0)
      {
         tryLock();
      }
      else
      {
         tryLockWithTimeout();
      }

      try
      {
         return invocation.invokeNext();
      }
      finally
      {
         semaphore.release();
      }
   
protected voidblockIndefinately()

      try
      {
         semaphore.acquire();
      }
      catch (InterruptedException e)
      {
         throw new LockAcquisitionFailureException("Failed to acquire permit for @SemaphoredMethod", e);
      }
   
protected voidtryLock()

      if (!semaphore.tryAcquire())
      {
         throw new LockAcquisitionFailureException("Failed to acquire permit for @SemaphoredMethod");
      }
   
protected voidtryLockWithTimeout()

      try
      {
         if (!semaphore.tryAcquire(props.permits(), props.timeout(), props.unit()))
         {
            throw new LockAcquisitionFailureException("Failed to acquire permit for @SemaphoredMethod.  Timeout reached.");
         }
      }
      catch (InterruptedException e)
      {
         throw new LockAcquisitionFailureException("Failed to acquire permit for @Semaphored class", e);
      }