/*
 * JBoss, the OpenSource J2EE webOS
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jboss.aspects.concurrent;

import org.jboss.aop.advice.AspectFactory;
import org.jboss.aop.Advisor;
import org.jboss.aop.InstanceAdvisor;
import org.jboss.aop.joinpoint.Joinpoint;
import org.jboss.aop.joinpoint.MethodJoinpoint;

/**
 * comment
 *
 * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
 */
public class MutexAspectFactory implements AspectFactory
{
   public Object createPerVM()
   {
      throw new IllegalStateException("THIS SCOPE NOT USABLE");
   }

   public Object createPerClass(Advisor advisor)
   {
      throw new IllegalStateException("THIS SCOPE NOT USABLE");
   }

   public Object createPerInstance(Advisor advisor, InstanceAdvisor instanceAdvisor)
   {
      throw new IllegalStateException("THIS SCOPE NOT USABLE");
   }

   public Object createPerJoinpoint(Advisor advisor, Joinpoint jp)
   {
      MethodJoinpoint mj = (MethodJoinpoint)jp;
      MutexLocked props = (MutexLocked)advisor.resolveAnnotation(mj.getMethod(), MutexLocked.class);
      if (props.timeout() < 0)
      {
         return new MutexAspect.BlockingMutex();
      }
      else if (props.timeout() == 0)
      {
         return new MutexAspect.TryLockMutex();
      }
      else
      {
         return new MutexAspect.TimeoutMutex(props);
      }
   }

   public Object createPerJoinpoint(Advisor advisor, InstanceAdvisor instanceAdvisor, Joinpoint jp)
   {
      throw new IllegalStateException("THIS SCOPE NOT USABLE");
   }

   public String getName()
   {
      return MutexAspectFactory.class.getName();
   }
}
