Version 5
     
    /*
     * Asynchronous Engine .
     * 
     * Copyright (C) 2004 Claude Hussenet
     * Contact :  [mailto:ch@claudehussenet.com]
     * 
     * Distributable under LGPL license.
     * See terms of license at gnu.org.
     */
    package org.jboss.aop.asynchronous.aspects.jboss;
    import org.jboss.aop.asynchronous.aspects.AsynchronousFacade;
    import org.jboss.aop.asynchronous.aspects.AsynchronousFacadeImpl;
    import org.jboss.aop.asynchronous.aspects.Utils;
    import org.jboss.aop.asynchronous.concurrent.ThreadManagerFactory;
    import org.jboss.aop.joinpoint.MethodInvocation;
    import org.jboss.aop.joinpoint.Invocation;
    import org.jboss.aop.joinpoint.ConstructorInvocation;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    /**
     * @version <tt>$Revision: 1.0 $</tt>
     * @author  [mailto:ch@claudehussenet.com]{Claude Hussenet Independent Consultant}
     */
    public class AsynchronousAspect {
         static private final Log log = LogFactory.getLog(AsynchronousAspect.class);
         private AsynchronousInvokeTask asynchronousInvokeTask =
              new AsynchronousInvokeTask();
         public AsynchronousAspect() {}
         public Object execute(MethodInvocation invocation) throws Throwable {
              AsynchronousFacade asynchronousResult = null;
              if (invocation.targetObject != null)
                   asynchronousResult = (AsynchronousFacade)invocation.targetObject;
              else {
                   Object[] tObject = invocation.getArguments();
                   for (int i = 0; i < tObject.length; i++) {
                        Object object = tObject+;
                        if (object instanceof AsynchronousFacade) {
                             asynchronousResult = (AsynchronousFacade)object;
                             break;
                        }
                   }
                   if (asynchronousResult == null)
                        asynchronousResult = new AsynchronousFacadeImpl();
              }
              if (invocation.getMetaData("asynchronous", "timeout") != null)
                   asynchronousResult.setTimeout(
                        Long.parseLong(
                             (String)invocation.getMetaData("asynchronous", "timeout")));
              if (invocation.getMetaData("asynchronous", "id") != null)
                   asynchronousResult.setId(
                        (String)invocation.getMetaData("asynchronous", "id"));
              asynchronousResult.setAsynchronousTask(
                   ThreadManagerFactory.getThreadManager().process(
                        ThreadManagerFactory.createNewThreadManagerRequest(
                             asynchronousResult.getId(),
                             new InvokeTaskInputParameters(
                                  (MethodInvocation)invocation.copy()),
                             asynchronousInvokeTask,
                             asynchronousResult.getTimeout())));
              Class returnType = invocation.getActualMethod().getReturnType();
              return Utils.returnInitObject(returnType);
         }
    }