1 22 package org.jboss.aspects.asynch; 23 24 import java.io.Serializable ; 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 43 public class AsynchAspect 44 { 45 public static final String FUTURE = "ASYNCH_FUTURE"; 46 47 private static GUID futureClassGUID = new GUID(); 48 private static Class [] futureIntf = {Future.class}; 49 private static Class [] futureDynamicIntf = {Future.class, Serializable .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 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 e) 78 { 79 throw new RuntimeException (e); 80 } 81 catch (IllegalAccessException e) 82 { 83 throw new RuntimeException (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 execute(MethodInvocation invocation) throws Throwable 95 { 96 RemotableFuture future = executor.execute(invocation); 97 98 InvokerLocator locator = (InvokerLocator) invocation.getMetaData(InvokeRemoteInterceptor.REMOTING, InvokeRemoteInterceptor.INVOKER_LOCATOR); 99 100 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 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 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 |