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.ejb3.service;
023:
024: import java.lang.reflect.Method;
025: import java.lang.reflect.Proxy;
026: import org.jboss.aop.Dispatcher;
027: import org.jboss.aop.advice.Interceptor;
028: import org.jboss.aop.joinpoint.MethodInvocation;
029: import org.jboss.aop.util.MethodHashing;
030: import org.jboss.aop.util.PayloadKey;
031: import org.jboss.aspects.asynch.AsynchMixin;
032: import org.jboss.aspects.asynch.AsynchProvider;
033: import org.jboss.aspects.remoting.InvokeRemoteInterceptor;
034: import org.jboss.ejb3.JBossProxy;
035: import org.jboss.ejb3.ProxyUtils;
036: import org.jboss.ejb3.asynchronous.AsynchronousInterceptor;
037: import org.jboss.remoting.InvokerLocator;
038:
039: /**
040: * @author <a href="mailto:kabir.khan@jboss.org">Kabir Khan</a>
041: * @version $Revision: 60233 $
042: */
043: public class ServiceRemoteProxy extends
044: org.jboss.ejb3.remoting.BaseRemoteProxy {
045: private static final long serialVersionUID = 306994045720155142L;
046:
047: protected InvokerLocator uri;
048: AsynchProvider provider;
049:
050: public ServiceRemoteProxy(Object containerId,
051: Interceptor[] interceptors, InvokerLocator uri) {
052: super (containerId, interceptors);
053: this .uri = uri;
054: }
055:
056: public ServiceRemoteProxy(AsynchProvider provider,
057: Object containerId, Interceptor[] interceptors,
058: InvokerLocator uri) {
059: super (containerId, interceptors);
060: this .uri = uri;
061: this .provider = provider;
062: }
063:
064: protected ServiceRemoteProxy() {
065: }
066:
067: public Object invoke(Object proxy, Method method, Object[] args)
068: throws Throwable {
069: if (method.getDeclaringClass() == AsynchProvider.class) {
070: return provider.getFuture();
071: }
072:
073: long hash = MethodHashing.calculateHash(method);
074: Object ret = ProxyUtils.handleCallLocally((JBossProxy) proxy,
075: this , method, args);
076: if (ret != null) {
077: return ret;
078: }
079:
080: MethodInvocation sri = new MethodInvocation(interceptors, hash,
081: method, method, null);
082: sri.setArguments(args);
083: sri.setInstanceResolver(metadata);
084: sri.getMetaData().addMetaData(Dispatcher.DISPATCHER,
085: Dispatcher.OID, containerId, PayloadKey.AS_IS);
086: sri.getMetaData().addMetaData(InvokeRemoteInterceptor.REMOTING,
087: InvokeRemoteInterceptor.INVOKER_LOCATOR, uri,
088: PayloadKey.AS_IS);
089: sri.getMetaData().addMetaData(InvokeRemoteInterceptor.REMOTING,
090: InvokeRemoteInterceptor.SUBSYSTEM, "AOP",
091: PayloadKey.AS_IS);
092:
093: if (provider != null) {
094: sri.getMetaData().addMetaData(
095: AsynchronousInterceptor.ASYNCH,
096: AsynchronousInterceptor.INVOKE_ASYNCH, "YES",
097: PayloadKey.AS_IS);
098: }
099: return sri.invokeNext();
100: }
101:
102: public Object getAsynchronousProxy(Object proxy) {
103: Class[] infs = proxy.getClass().getInterfaces();
104: if (!ProxyUtils.isAsynchronous(infs)) {
105: Class[] interfaces = ProxyUtils
106: .addAsynchProviderInterface(infs);
107: AsynchMixin mixin = new AsynchMixin();
108: Interceptor[] newInterceptors = ProxyUtils
109: .addAsynchProxyInterceptor(mixin, interceptors);
110: ServiceRemoteProxy handler = new ServiceRemoteProxy(mixin,
111: containerId, newInterceptors, uri);
112: return Proxy.newProxyInstance(Thread.currentThread()
113: .getContextClassLoader(), interfaces, handler);
114: }
115:
116: //I was already asynchronous
117: return proxy;
118: }
119:
120: public String toString() {
121: return containerId.toString();
122: }
123: }
|