KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > stateful > StatefulRemoteProxy


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.ejb3.stateful;
23
24 import java.lang.reflect.Method JavaDoc;
25 import java.lang.reflect.Proxy JavaDoc;
26 import javax.ejb.EJBMetaData JavaDoc;
27 import javax.ejb.Handle JavaDoc;
28 import javax.ejb.HomeHandle JavaDoc;
29 import org.jboss.aop.Dispatcher;
30 import org.jboss.aop.advice.Interceptor;
31 import org.jboss.aop.util.MethodHashing;
32 import org.jboss.aop.util.PayloadKey;
33 import org.jboss.aspects.asynch.AsynchMixin;
34 import org.jboss.aspects.asynch.AsynchProvider;
35 import org.jboss.aspects.remoting.InvokeRemoteInterceptor;
36 import org.jboss.ejb3.ProxyUtils;
37 import org.jboss.ejb3.asynchronous.AsynchronousInterceptor;
38 import org.jboss.logging.Logger;
39 import org.jboss.remoting.InvokerLocator;
40 import org.jboss.util.id.GUID;
41
42 /**
43  * Comment
44  *
45  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
46  * @version $Revision: 40600 $
47  */

48 public class StatefulRemoteProxy extends org.jboss.ejb3.session.BaseSessionRemoteProxy
49 {
50    private static final Logger log = Logger.getLogger(StatefulRemoteProxy.class);
51    
52    protected InvokerLocator uri;
53    AsynchProvider provider;
54
55    private Handle JavaDoc handle;
56    private HomeHandle JavaDoc homeHandle;
57    private EJBMetaData JavaDoc ejbMetaData;
58
59    public StatefulRemoteProxy(Object JavaDoc containerId, Interceptor[] interceptors, InvokerLocator uri)
60    {
61       super(containerId, interceptors);
62       this.uri = uri;
63    }
64
65    public StatefulRemoteProxy(Object JavaDoc containerId, Interceptor[] interceptors, InvokerLocator uri, Object JavaDoc id)
66    {
67       super(containerId, interceptors);
68       this.uri = uri;
69       this.id = id;
70    }
71
72    public StatefulRemoteProxy(AsynchProvider provider, Object JavaDoc containerId, Interceptor[] interceptors, InvokerLocator uri)
73    {
74       super(containerId, interceptors);
75       this.uri = uri;
76       this.provider = provider;
77    }
78
79    protected StatefulRemoteProxy()
80    {
81    }
82    
83    public void setHandle(StatefulHandleImpl handle)
84    {
85       this.handle = handle;
86       handle.id = id;
87    }
88    
89    public void setHomeHandle(HomeHandle JavaDoc homeHandle)
90    {
91       this.homeHandle = homeHandle;
92    }
93    
94    public void setEjbMetaData(EJBMetaData JavaDoc ejbMetaData)
95    {
96       this.ejbMetaData = ejbMetaData;
97    }
98
99    public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args)
100            throws Throwable JavaDoc
101    {
102       if (method.getDeclaringClass() == AsynchProvider.class)
103       {
104          return provider.getFuture();
105       }
106
107       long hash = MethodHashing.calculateHash(method);
108       Object JavaDoc ret = ProxyUtils.handleCallLocally(hash, proxy, this, method, args);
109       if (ret != null)
110       {
111          return ret;
112       }
113       
114       ret = handleEjb21CallLocally(method, args);
115       if (ret != null)
116       {
117          return ret;
118       }
119
120       StatefulRemoteInvocation sri = new StatefulRemoteInvocation(interceptors, hash, method, method, null, id);
121       sri.setArguments(args);
122       sri.setInstanceResolver(metadata);
123       sri.getMetaData().addMetaData(Dispatcher.DISPATCHER, Dispatcher.OID, containerId, PayloadKey.AS_IS);
124       sri.getMetaData().addMetaData(InvokeRemoteInterceptor.REMOTING, InvokeRemoteInterceptor.INVOKER_LOCATOR, uri, PayloadKey.AS_IS);
125       sri.getMetaData().addMetaData(InvokeRemoteInterceptor.REMOTING, InvokeRemoteInterceptor.SUBSYSTEM, "AOP", PayloadKey.AS_IS);
126
127       if (provider != null)
128       {
129          sri.getMetaData().addMetaData(AsynchronousInterceptor.ASYNCH, AsynchronousInterceptor.INVOKE_ASYNCH, "YES", PayloadKey.AS_IS);
130       }
131       try
132       {
133          Object JavaDoc rtn = sri.invokeNext();
134          // if this is first invocation then container passes back actual ID
135
if (id == null)
136          {
137             id = sri.getResponseAttachment(StatefulConstants.NEW_ID);
138          }
139          return rtn;
140       }
141       catch (ForwardId forward)
142       {
143          // if this is first invocation then container passes back actual ID
144
// The ForwardId exception is needed if 1st operation throws an exception
145
id = forward.getId();
146          throw forward.getCause();
147       }
148    }
149
150    public Object JavaDoc getAsynchronousProxy(Object JavaDoc proxy)
151    {
152       Class JavaDoc[] infs = proxy.getClass().getInterfaces();
153       if (!ProxyUtils.isAsynchronous(infs))
154       {
155          Class JavaDoc[] interfaces = ProxyUtils.addAsynchProviderInterface(infs);
156          AsynchMixin mixin = new AsynchMixin();
157          Interceptor[] newInterceptors = ProxyUtils.addAsynchProxyInterceptor(mixin, interceptors);
158          StatefulRemoteProxy handler = new StatefulRemoteProxy(mixin, containerId, newInterceptors, uri);
159          return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), interfaces, handler);
160       }
161
162       //I was already asynchronous
163
return proxy;
164    }
165
166    public String JavaDoc toString()
167    {
168       if (id != null)
169       {
170          return containerId.toString() + ":" + id.toString();
171       }
172       else
173       {
174          //If the proxy has not been used yet, create a temporary id
175
GUID guid = new GUID();
176          return containerId.toString() + ":" + guid.toString();
177       }
178    }
179    
180    private Object JavaDoc handleEjb21CallLocally(Method JavaDoc method, Object JavaDoc[] args)
181    {
182       if (method.equals(ProxyUtils.GET_HOME_HANDLE))
183       {
184          return homeHandle;
185       } else if (method.equals(ProxyUtils.GET_EJB_METADATA))
186       {
187          return ejbMetaData;
188       } else if (method.equals(ProxyUtils.GET_HANDLE))
189       {
190          return handle;
191       }
192       
193       return null;
194    }
195 }
196
Popular Tags