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

SemaphoreAspect

public class SemaphoreAspect extends Object
comment
author
Bill Burke

Fields Summary
protected SemaphoreLocked
props
Constructors Summary
public SemaphoreAspect(SemaphoreLocked locked)

      this.props = locked;
   
Methods Summary
public java.lang.Objectacquire(org.jboss.aop.joinpoint.Invocation invocation)

      SemaphoredObject obj = (SemaphoredObject)invocation.getTargetObject();

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

      try
      {
         return invocation.invokeNext();
      }
      finally
      {
         obj.getSemaphore().release(props.permits());
      }
   
protected voidblockIndefinately(java.util.concurrent.Semaphore semaphore)

      try
      {
         semaphore.acquire(props.permits());
      }
      catch (InterruptedException e)
      {
         throw new LockAcquisitionFailureException("Failed to acquire permit for @Semaphored class", e);
      }
   
protected voidtryLock(java.util.concurrent.Semaphore semaphore)

      if (!semaphore.tryAcquire(props.permits()))
      {
         throw new LockAcquisitionFailureException("Failed to acquire permit for @Semaphored class");
      }
   
protected voidtryLockWithTimeout(java.util.concurrent.Semaphore semaphore)

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