KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > stateless > StatelessRemoteProxyFactory


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

44 public class StatelessRemoteProxyFactory extends BaseStatelessProxyFactory implements RemoteProxyFactory
45 {
46    private static final Logger log = Logger.getLogger(StatelessRemoteProxyFactory.class);
47
48    protected RemoteBinding binding;
49    protected InvokerLocator locator;
50
51    public void setRemoteBinding(RemoteBinding binding)
52    {
53       this.binding = binding;
54    }
55
56    protected Class JavaDoc[] getInterfaces()
57    {
58       Class JavaDoc[] interfaces;
59
60       Class JavaDoc[] remoteInterfaces = ProxyFactoryHelper.getRemoteInterfaces(container);
61       interfaces = new Class JavaDoc[remoteInterfaces.length + 2];
62
63       System.arraycopy(remoteInterfaces, 0, interfaces, 0, remoteInterfaces.length);
64       interfaces[remoteInterfaces.length] = JBossProxy.class;
65       interfaces[remoteInterfaces.length + 1] = javax.ejb.EJBObject JavaDoc.class;
66
67       return interfaces;
68    }
69
70    protected void initializeJndiName()
71    {
72       jndiName = ProxyFactoryHelper.getRemoteJndiName(container, binding);
73    }
74
75    public void init() throws Exception JavaDoc
76    {
77       super.init();
78       String JavaDoc clientBindUrl = ProxyFactoryHelper.getClientBindUrl(binding);
79       locator = new InvokerLocator(clientBindUrl);
80    }
81
82    public void start() throws Exception JavaDoc
83    {
84       super.start();
85       StatelessContainer statelessContainer = (StatelessContainer) container;
86       RemoteHome JavaDoc remoteHome = (RemoteHome JavaDoc) statelessContainer.resolveAnnotation(RemoteHome JavaDoc.class);
87       if (remoteHome != null)
88       {
89          Object JavaDoc homeProxy = createHomeProxy(remoteHome.value());
90          try
91          {
92             Util.rebind(container.getInitialContext(), jndiName + "Home", homeProxy);
93          }
94          catch (NamingException JavaDoc e)
95          {
96             NamingException JavaDoc namingException = new NamingException JavaDoc("Could not bind stateless home proxy with ejb name " + container.getEjbName() + " into JNDI under jndiName: " + container.getInitialContext().getNameInNamespace() + "/" + jndiName + "Home");
97             namingException.setRootCause(e);
98             throw namingException;
99          }
100
101       }
102    }
103
104    public void stop() throws Exception JavaDoc
105    {
106       super.stop();
107       StatelessContainer statelessContainer = (StatelessContainer) container;
108       RemoteHome JavaDoc remoteHome = (RemoteHome JavaDoc) statelessContainer.resolveAnnotation(RemoteHome JavaDoc.class);
109       if (remoteHome != null)
110       {
111          Util.unbind(container.getInitialContext(), jndiName + "Home");
112       }
113    }
114
115    protected StatelessHandleImpl getHandle()
116    {
117       StatelessHandleImpl handle = new StatelessHandleImpl();
118       RemoteBinding remoteBinding = (RemoteBinding) advisor.resolveAnnotation(RemoteBinding.class);
119       if (remoteBinding != null)
120          handle.jndiName = remoteBinding.jndiBinding() ;
121
122       return handle;
123    }
124
125    public Object JavaDoc createHomeProxy(Class JavaDoc homeInterface)
126    {
127       try
128       {
129          Object JavaDoc containerId = container.getObjectName().getCanonicalName();
130          ;
131          String JavaDoc stackName = "StatelessSessionClientInterceptors";
132          if (binding.interceptorStack() != null && !binding.interceptorStack().equals(""))
133          {
134             stackName = binding.interceptorStack();
135          }
136          AdviceStack stack = AspectManager.instance().getAdviceStack(stackName);
137          StatelessRemoteProxy proxy = new StatelessRemoteProxy(containerId, stack.createInterceptors((Advisor) container, null), locator);
138          setEjb21Objects(proxy);
139          Class JavaDoc[] interfaces = {homeInterface};
140          return java.lang.reflect.Proxy.newProxyInstance(container.getBeanClass().getClassLoader(), interfaces, proxy);
141       }
142       catch (IllegalArgumentException JavaDoc e)
143       {
144          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
145
}
146    }
147
148    public Object JavaDoc createProxy()
149    {
150 // try
151
{
152          Object JavaDoc containerId = container.getObjectName().getCanonicalName();
153          ;
154          String JavaDoc stackName = "StatelessSessionClientInterceptors";
155          if (binding.interceptorStack() != null && !binding.interceptorStack().equals(""))
156          {
157             stackName = binding.interceptorStack();
158          }
159          AdviceStack stack = AspectManager.instance().getAdviceStack(stackName);
160          StatelessRemoteProxy proxy = new StatelessRemoteProxy(containerId, stack.createInterceptors((Advisor) container, null), locator);
161          setEjb21Objects(proxy);
162          /*
163          Object[] args = {proxy};
164          return proxyConstructor.newInstance(args);
165          */

166          return constructProxy(proxy);
167       }
168       /*
169       catch (InstantiationException e)
170       {
171          throw new RuntimeException(e); //To change body of catch statement use Options | File Templates.
172       }
173       catch (IllegalAccessException e)
174       {
175          throw new RuntimeException(e); //To change body of catch statement use Options | File Templates.
176       }
177       catch (IllegalArgumentException e)
178       {
179          throw new RuntimeException(e); //To change body of catch statement use Options | File Templates.
180       }
181       catch (InvocationTargetException e)
182       {
183          throw new RuntimeException(e.getTargetException()); //To change body of catch statement use Options | File Templates.
184       }
185       */

186    }
187
188 }
189
Popular Tags