KickJava   Java API By Example, From Geeks To Geeks.

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


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.ObjectStreamException JavaDoc;
25 import java.io.Serializable JavaDoc;
26 import java.lang.reflect.InvocationHandler JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import org.jboss.aop.ClassInstanceAdvisor;
32 import org.jboss.aop.InstanceAdvisor;
33 import org.jboss.aop.advice.Interceptor;
34 import org.jboss.aop.instrument.Untransformable;
35 import org.jboss.aop.joinpoint.MethodInvocation;
36 import org.jboss.aop.proxy.Proxy;
37 import org.jboss.aop.util.MethodHashing;
38 import org.jboss.util.id.GUID;
39
40 /**
41  * An invocation handler for the Future interface using dynamic proxies. It is an alternative to
42  * having generated proxies for use with EJB 3, avoiding client relying on javassist in the
43  * EJB 3 client proxies
44  *
45  * @author <a HREF="kabir.khan@jboss.com">Kabir Khan</a>
46  * @version $Revision$
47  */

48 public class FutureInvocationHandler extends Proxy implements InvocationHandler JavaDoc, Untransformable, Serializable JavaDoc
49 {
50    private static final long serialVersionUID = -2343948303742422382L;
51    
52    private Map JavaDoc methodMap = new HashMap JavaDoc();
53
54    public FutureInvocationHandler()
55    {
56       // FIXME FutureInvocationHandler constructor
57
super();
58    }
59
60    public static Object JavaDoc createFutureProxy(GUID guid, ClassLoader JavaDoc loader, Class JavaDoc[] interfaces) throws Exception JavaDoc
61    {
62       FutureInvocationHandler ih = new FutureInvocationHandler();
63       ih.instanceAdvisor = new ClassInstanceAdvisor();
64       ih.mixins = null;
65       ih.interfaces = interfaces;
66       ih.guid = guid;
67       return java.lang.reflect.Proxy.newProxyInstance(loader, interfaces, ih);
68    }
69
70    
71    public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc
72    {
73       if (method.getName().equals("_getInstanceAdvisor"))
74       {
75          return _getInstanceAdvisor();
76       }
77       else if (method.getName().equals("_setInstanceAdvisor") &&
78             method.getParameterTypes().length == 1 && method.getParameterTypes()[0].equals(InstanceAdvisor.class))
79       {
80          _setInstanceAdvisor((InstanceAdvisor)args[0]);
81          return null;
82       }
83       
84       Interceptor[] interceptors = instanceAdvisor.getInterceptors();
85       long hash = MethodHashing.calculateHash(method);
86       MethodInvocation invocation = new MethodInvocation(interceptors, hash, method, method, null);
87       invocation.setInstanceResolver(instanceAdvisor.getMetaData());
88       invocation.setArguments(args);
89       return invocation.invokeNext();
90    }
91    
92
93    /**
94     * Override Proxy implementation so we get default behaviour.
95     * Reason is to avoid client dependencies on javassist in EJB 3 asynchronous proxies
96     */

97    public Object JavaDoc writeReplace() throws ObjectStreamException JavaDoc
98    {
99       return this;
100    }
101
102    public Map JavaDoc getMethodMap()
103    {
104       //I don't think we need to populate this for now
105
return methodMap;
106    }
107    
108    
109
110 }
111
Popular Tags