KickJava   Java API By Example, From Geeks To Geeks.

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


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.InvocationTargetException JavaDoc;
25 import javax.naming.NamingException JavaDoc;
26 import javax.ejb.RemoteHome JavaDoc;
27 import org.jboss.annotation.ejb.RemoteBinding;
28 import org.jboss.aop.Advisor;
29 import org.jboss.aop.AspectManager;
30 import org.jboss.aop.Dispatcher;
31 import org.jboss.aop.advice.AdviceStack;
32 import org.jboss.aspects.remoting.Remoting;
33 import org.jboss.ejb3.JBossProxy;
34 import org.jboss.ejb3.ProxyFactory;
35 import org.jboss.ejb3.ProxyFactoryHelper;
36 import org.jboss.ejb3.remoting.RemoteProxyFactory;
37 import org.jboss.logging.Logger;
38 import org.jboss.naming.Util;
39 import org.jboss.remoting.InvokerLocator;
40
41 /**
42  * Comment
43  *
44  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
45  * @version $Revision: 56518 $
46  */

47 public class StatefulRemoteProxyFactory extends BaseStatefulProxyFactory implements RemoteProxyFactory
48 {
49    private static final Logger log = Logger.getLogger(StatefulRemoteProxyFactory.class);
50
51    private RemoteBinding binding;
52    private InvokerLocator locator;
53
54    public void setRemoteBinding(RemoteBinding binding)
55    {
56       this.binding = binding;
57    }
58
59    protected Class JavaDoc[] getInterfaces()
60    {
61       Class JavaDoc[] interfaces;
62
63       Class JavaDoc[] remoteInterfaces = ProxyFactoryHelper.getRemoteInterfaces(container);
64          interfaces = new Class JavaDoc[remoteInterfaces.length + 2];
65
66       System.arraycopy(remoteInterfaces, 0, interfaces, 0, remoteInterfaces.length);
67       interfaces[remoteInterfaces.length] = JBossProxy.class;
68       interfaces[remoteInterfaces.length + 1] = javax.ejb.EJBObject JavaDoc.class;
69       return interfaces;
70    }
71
72    protected void initializeJndiName()
73    {
74       jndiName = ProxyFactoryHelper.getRemoteJndiName(container, binding);
75    }
76
77    public void init() throws Exception JavaDoc
78    {
79       super.init();
80       String JavaDoc clientBindUrl = ProxyFactoryHelper.getClientBindUrl(binding);
81       locator = new InvokerLocator(clientBindUrl);
82    }
83
84    public void start() throws Exception JavaDoc
85    {
86       init();
87
88       super.start();
89       Class JavaDoc[] interfaces = {ProxyFactory.class};
90       Object JavaDoc factoryProxy = Remoting.createPojiProxy(jndiName + PROXY_FACTORY_NAME, interfaces, ProxyFactoryHelper.getClientBindUrl(binding));
91       try
92       {
93          Util.rebind(container.getInitialContext(), jndiName + PROXY_FACTORY_NAME, factoryProxy);
94       }
95       catch (NamingException JavaDoc e)
96       {
97          NamingException JavaDoc namingException = new NamingException JavaDoc("Could not bind stateful remote proxy with ejb name " + container.getEjbName() + " into JNDI under jndiName: " + container.getInitialContext().getNameInNamespace() + "/" + jndiName + PROXY_FACTORY_NAME);
98          namingException.setRootCause(e);
99          throw namingException;
100       }
101       Dispatcher.singleton.registerTarget(jndiName + PROXY_FACTORY_NAME, this);
102
103       StatefulContainer statefulContainer = (StatefulContainer) container;
104       RemoteHome JavaDoc remoteHome = (RemoteHome JavaDoc) statefulContainer.resolveAnnotation(RemoteHome JavaDoc.class);
105       if (remoteHome != null)
106       {
107          Object JavaDoc homeProxy = createHomeProxy(remoteHome.value());
108          Util.rebind(container.getInitialContext(), jndiName + "Home", homeProxy);
109       }
110    }
111
112    public void stop() throws Exception JavaDoc
113    {
114       super.stop();
115       Util.unbind(container.getInitialContext(), jndiName + PROXY_FACTORY_NAME);
116       Dispatcher.singleton.unregisterTarget(jndiName + PROXY_FACTORY_NAME);
117       StatefulContainer statefulContainer = (StatefulContainer) container;
118       RemoteHome JavaDoc remoteHome = (RemoteHome JavaDoc) statefulContainer.resolveAnnotation(RemoteHome JavaDoc.class);
119       if (remoteHome != null)
120       {
121          Util.unbind(container.getInitialContext(), jndiName + "Home");
122       }
123    }
124
125
126    public Object JavaDoc createHomeProxy(Class JavaDoc homeInterface)
127    {
128       try
129       {
130          Object JavaDoc containerId = container.getObjectName().getCanonicalName();
131          String JavaDoc stackName = "StatefulSessionClientInterceptors";
132          if (binding.interceptorStack() != null && !binding.interceptorStack().equals(""))
133          {
134             stackName = binding.interceptorStack();
135          }
136          AdviceStack stack = AspectManager.instance().getAdviceStack(stackName);
137          if (stack == null) throw new RuntimeException JavaDoc("unable to find interceptor stack: " + stackName);
138          StatefulHomeRemoteProxy proxy = new StatefulHomeRemoteProxy(containerId, stack.createInterceptors((Advisor) container, null), locator);
139
140
141          setEjb21Objects(proxy);
142          Class JavaDoc[] intfs = {homeInterface};
143          return java.lang.reflect.Proxy.newProxyInstance(container.getBeanClass().getClassLoader(), intfs, proxy);
144       }
145       catch (IllegalArgumentException JavaDoc e)
146       {
147          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
148
}
149    }
150    public Object JavaDoc createProxy()
151    {
152       try
153       {
154          Object JavaDoc containerId = container.getObjectName().getCanonicalName();
155          String JavaDoc stackName = "StatefulSessionClientInterceptors";
156          if (binding.interceptorStack() != null && !binding.interceptorStack().equals(""))
157          {
158             stackName = binding.interceptorStack();
159          }
160          AdviceStack stack = AspectManager.instance().getAdviceStack(stackName);
161          if (stack == null) throw new RuntimeException JavaDoc("unable to find interceptor stack: " + stackName);
162          StatefulRemoteProxy proxy = new StatefulRemoteProxy(containerId, stack.createInterceptors((Advisor) container, null), locator);
163
164
165          setEjb21Objects(proxy);
166          Object JavaDoc[] args = {proxy};
167          return proxyConstructor.newInstance(args);
168       }
169       catch (InstantiationException JavaDoc e)
170       {
171          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
172
}
173       catch (IllegalAccessException JavaDoc e)
174       {
175          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
176
}
177       catch (IllegalArgumentException JavaDoc e)
178       {
179          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
180
}
181       catch (InvocationTargetException JavaDoc e)
182       {
183          throw new RuntimeException JavaDoc(e.getTargetException()); //To change body of catch statement use Options | File Templates.
184
}
185    }
186
187    protected StatefulHandleImpl getHandle()
188    {
189       StatefulHandleImpl handle = new StatefulHandleImpl();
190       RemoteBinding remoteBinding = (RemoteBinding) advisor.resolveAnnotation(RemoteBinding.class);
191       if (remoteBinding != null)
192          handle.jndiName = remoteBinding.jndiBinding();
193
194       return handle;
195    }
196
197    public Object JavaDoc createProxy(Object JavaDoc id)
198    {
199       try
200       {
201          Object JavaDoc containerId = container.getObjectName().getCanonicalName();
202          String JavaDoc stackName = "StatefulSessionClientInterceptors";
203          if (binding.interceptorStack() != null && !binding.interceptorStack().equals(""))
204          {
205             stackName = binding.interceptorStack();
206          }
207          AdviceStack stack = AspectManager.instance().getAdviceStack(stackName);
208          StatefulRemoteProxy proxy = new StatefulRemoteProxy(containerId, stack.createInterceptors((Advisor) container, null), locator, id);
209          setEjb21Objects(proxy);
210          Object JavaDoc[] args = {proxy};
211          return proxyConstructor.newInstance(args);
212       }
213       catch (InstantiationException JavaDoc e)
214       {
215          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
216
}
217       catch (IllegalAccessException JavaDoc e)
218       {
219          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
220
}
221       catch (IllegalArgumentException JavaDoc e)
222       {
223          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
224
}
225       catch (InvocationTargetException JavaDoc e)
226       {
227          throw new RuntimeException JavaDoc(e.getTargetException()); //To change body of catch statement use Options | File Templates.
228
}
229    }
230
231
232 }
233
Popular Tags