001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.aspects.asynch;
023:
024: import java.io.ObjectStreamException;
025: import java.io.Serializable;
026: import java.lang.reflect.InvocationHandler;
027: import java.lang.reflect.Method;
028: import java.util.HashMap;
029: import java.util.Map;
030:
031: import org.jboss.aop.ClassInstanceAdvisor;
032: import org.jboss.aop.InstanceAdvisor;
033: import org.jboss.aop.advice.Interceptor;
034: import org.jboss.aop.instrument.Untransformable;
035: import org.jboss.aop.joinpoint.MethodInvocation;
036: import org.jboss.aop.proxy.Proxy;
037: import org.jboss.aop.util.MethodHashing;
038: import org.jboss.util.id.GUID;
039:
040: /**
041: * An invocation handler for the Future interface using dynamic proxies. It is an alternative to
042: * having generated proxies for use with EJB 3, avoiding client relying on javassist in the
043: * EJB 3 client proxies
044: *
045: * @author <a href="kabir.khan@jboss.com">Kabir Khan</a>
046: * @version $Revision$
047: */
048: public class FutureInvocationHandler extends Proxy implements
049: InvocationHandler, Untransformable, Serializable {
050: private static final long serialVersionUID = -2343948303742422382L;
051:
052: private Map methodMap = new HashMap();
053:
054: public FutureInvocationHandler() {
055: // FIXME FutureInvocationHandler constructor
056: super ();
057: }
058:
059: public static Object createFutureProxy(GUID guid,
060: ClassLoader loader, Class[] interfaces) throws Exception {
061: FutureInvocationHandler ih = new FutureInvocationHandler();
062: ih.instanceAdvisor = new ClassInstanceAdvisor();
063: ih.mixins = null;
064: ih.interfaces = interfaces;
065: ih.guid = guid;
066: return java.lang.reflect.Proxy.newProxyInstance(loader,
067: interfaces, ih);
068: }
069:
070: public Object invoke(Object proxy, Method method, Object[] args)
071: throws Throwable {
072: if (method.getName().equals("_getInstanceAdvisor")) {
073: return _getInstanceAdvisor();
074: } else if (method.getName().equals("_setInstanceAdvisor")
075: && method.getParameterTypes().length == 1
076: && method.getParameterTypes()[0]
077: .equals(InstanceAdvisor.class)) {
078: _setInstanceAdvisor((InstanceAdvisor) args[0]);
079: return null;
080: }
081:
082: Interceptor[] interceptors = instanceAdvisor.getInterceptors();
083: long hash = MethodHashing.calculateHash(method);
084: MethodInvocation invocation = new MethodInvocation(
085: interceptors, hash, method, method, null);
086: invocation.setInstanceResolver(instanceAdvisor.getMetaData());
087: invocation.setArguments(args);
088: return invocation.invokeNext();
089: }
090:
091: /**
092: * Override Proxy implementation so we get default behaviour.
093: * Reason is to avoid client dependencies on javassist in EJB 3 asynchronous proxies
094: */
095: public Object writeReplace() throws ObjectStreamException {
096: return this ;
097: }
098:
099: public Map getMethodMap() {
100: //I don't think we need to populate this for now
101: return methodMap;
102: }
103:
104: }
|