KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aspects > asynch > AsynchAspect


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.aspects.asynch;
23
24 import java.io.Serializable JavaDoc;
25
26 import org.jboss.aop.Advisor;
27 import org.jboss.aop.Dispatcher;
28 import org.jboss.aop.InstanceAdvised;
29 import org.jboss.aop.joinpoint.MethodInvocation;
30 import org.jboss.aop.proxy.ProxyFactory;
31 import org.jboss.aspects.NullOrZero;
32 import org.jboss.aspects.remoting.InvokeRemoteInterceptor;
33 import org.jboss.aspects.remoting.Remoting;
34 import org.jboss.remoting.InvokerLocator;
35 import org.jboss.util.id.GUID;
36
37 /**
38  * Comment
39  *
40  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
41  * @version $Revision: 37406 $
42  */

43 public class AsynchAspect
44 {
45    public static final String JavaDoc FUTURE = "ASYNCH_FUTURE";
46
47    private static GUID futureClassGUID = new GUID();
48    private static Class JavaDoc[] futureIntf = {Future.class};
49    private static Class JavaDoc[] futureDynamicIntf = {Future.class, Serializable JavaDoc.class, InstanceAdvised.class};
50    private Advisor advisor;
51    protected ExecutorAbstraction executor;
52    private boolean generateFutureProxy = true;
53
54    public Advisor getAdvisor()
55    {
56       return advisor;
57    }
58
59    public void setAdvisor(Advisor advisor)
60    {
61       this.advisor = advisor;
62       Class JavaDoc executorClass = null;
63       AsynchExecutor executorAnnotation = (AsynchExecutor) advisor.resolveAnnotation(AsynchExecutor.class);
64       if (executorAnnotation == null)
65       {
66          executorClass = ThreadPoolExecutor.class;
67       }
68       else
69       {
70          executorClass = executorAnnotation.value();
71       }
72
73       try
74       {
75          executor = (ExecutorAbstraction) executorClass.newInstance();
76       }
77       catch (InstantiationException JavaDoc e)
78       {
79          throw new RuntimeException JavaDoc(e);
80       }
81       catch (IllegalAccessException JavaDoc e)
82       {
83          throw new RuntimeException JavaDoc(e);
84       }
85       executor.setAdvisor(advisor);
86
87    }
88
89    public void setGenerateDynamicProxy(boolean gen)
90    {
91       this.generateFutureProxy = gen;
92    }
93    
94    public Object JavaDoc execute(MethodInvocation invocation) throws Throwable JavaDoc
95    {
96       RemotableFuture future = executor.execute(invocation);
97
98       InvokerLocator locator = (InvokerLocator) invocation.getMetaData(InvokeRemoteInterceptor.REMOTING, InvokeRemoteInterceptor.INVOKER_LOCATOR);
99
100       // this is a remote invocation so just stuff the future within the response
101
if (locator != null)
102       {
103          setupRemoteFuture(invocation, future, locator);
104       }
105       else
106       {
107          setupLocalFuture(invocation, future);
108       }
109
110       return NullOrZero.nullOrZero(invocation.getMethod().getReturnType());
111    }
112    
113    protected void setupRemoteFuture(MethodInvocation invocation, RemotableFuture future, InvokerLocator locator)throws Exception JavaDoc
114    {
115       GUID futureGUID = new GUID();
116       Dispatcher.singleton.registerTarget(futureGUID, future);
117       InstanceAdvised ia = (generateProxy()) ?
118             ProxyFactory.createInterfaceProxy(futureClassGUID, Future.class.getClassLoader(), futureIntf) :
119                (InstanceAdvised)FutureInvocationHandler.createFutureProxy(futureClassGUID, Future.class.getClassLoader(), futureDynamicIntf);
120       Remoting.makeRemotable(ia, locator, futureGUID);
121       future.setRemoteObjectID(futureGUID);
122       invocation.addResponseAttachment(FUTURE, ia);
123    }
124    
125    /**
126     * Default behaviour is to generate a proxy using aop.
127     * This can be overridden, e.g. by the EJB3 AsynchronousInterceptor to avoid dependencies on javassist for EJB3 clients
128     */

129    protected boolean generateProxy()
130    {
131       return generateFutureProxy;
132    }
133    
134    protected void setupLocalFuture(MethodInvocation invocation, Future future)
135    {
136       FutureHolder provider = (FutureHolder) invocation.getTargetObject();
137       provider.setFuture(future);
138       invocation.addResponseAttachment(FUTURE, future);
139    }
140
141 }
142
Popular Tags