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 java.io.ObjectInput;
027: import java.io.IOException;
028: import java.io.ObjectOutput;
029: import org.jboss.aspects.asynch.AsynchMixin;
030: import org.jboss.aspects.asynch.AsynchProvider;
031: import org.jboss.aspects.asynch.FutureHolder;
032: import org.jboss.ejb3.Container;
033: import org.jboss.ejb3.JBossProxy;
034: import org.jboss.ejb3.LocalProxy;
035: import org.jboss.ejb3.ProxyUtils;
036:
037: /**
038: * @author <a href="mailto:kabir.khan@jboss.org">Kabir Khan</a>
039: * @version $Revision: 60233 $
040: */
041: public class ServiceLocalProxy extends LocalProxy {
042: private static final long serialVersionUID = -4023671606878355605L;
043:
044: AsynchProvider provider;
045:
046: public ServiceLocalProxy() {
047: }
048:
049: public ServiceLocalProxy(Container container) {
050: super (container);
051: }
052:
053: public ServiceLocalProxy(AsynchProvider provider,
054: Container container) {
055: super (container);
056: this .provider = provider;
057: }
058:
059: public Object invoke(Object proxy, Method method, Object[] args)
060: throws Throwable {
061: if (method.getDeclaringClass() == AsynchProvider.class) {
062: return provider.getFuture();
063: }
064:
065: Object ret = ProxyUtils.handleCallLocally((JBossProxy) proxy,
066: this , method, args);
067: if (ret != null) {
068: return ret;
069: }
070:
071: ServiceContainer sc = (ServiceContainer) container;
072: return sc.localInvoke(method, args, (FutureHolder) provider);
073: }
074:
075: public Object getAsynchronousProxy(Object proxy) {
076: Class[] infs = proxy.getClass().getInterfaces();
077: if (!ProxyUtils.isAsynchronous(infs)) {
078: Class[] interfaces = ProxyUtils
079: .addAsynchProviderInterface(infs);
080: AsynchMixin mixin = new AsynchMixin();
081: ServiceLocalProxy handler = new ServiceLocalProxy(mixin,
082: container);
083: return Proxy.newProxyInstance(Thread.currentThread()
084: .getContextClassLoader(), interfaces, handler);
085: }
086:
087: //I was already asynchronous
088: return proxy;
089: }
090:
091: //@Override
092: public void readExternal(ObjectInput in) throws IOException,
093: ClassNotFoundException {
094: super .readExternal(in);
095: provider = (AsynchProvider) in.readObject();
096: }
097:
098: //@Override
099: public void writeExternal(ObjectOutput out) throws IOException {
100: super .writeExternal(out);
101: out.writeObject(provider);
102: }
103:
104: public String toString() {
105: return container.getEjbName().toString();
106: }
107:
108: }
|