KickJava   Java API By Example, From Geeks To Geeks.

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


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.ejb.LocalHome JavaDoc;
26 import javax.naming.NamingException JavaDoc;
27 import org.jboss.annotation.ejb.LocalBinding;
28 import org.jboss.ejb3.EJBContainer;
29 import org.jboss.ejb3.JBossProxy;
30 import org.jboss.ejb3.NonSerializableFactory;
31 import org.jboss.ejb3.ProxyFactoryHelper;
32
33
34 /**
35  * Comment
36  *
37  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
38  * @version $Revision: 57744 $
39  */

40 public class StatefulLocalProxyFactory extends BaseStatefulProxyFactory
41 {
42    protected Class JavaDoc[] getInterfaces()
43    {
44       Class JavaDoc[] interfaces;
45
46       Class JavaDoc[] localInterfaces = ProxyFactoryHelper.getLocalInterfaces(container);
47       interfaces = new Class JavaDoc[localInterfaces.length + 2];
48       System.arraycopy(localInterfaces, 0, interfaces, 0, localInterfaces.length);
49       interfaces[localInterfaces.length] = JBossProxy.class;
50       interfaces[localInterfaces.length + 1] = javax.ejb.EJBLocalObject JavaDoc.class;
51       return interfaces;
52    }
53
54    protected void initializeJndiName()
55    {
56       jndiName = ProxyFactoryHelper.getLocalJndiName(container);
57    }
58
59    public void start() throws Exception JavaDoc
60    {
61       super.start();
62
63       try
64       {
65          NonSerializableFactory.rebind(container.getInitialContext(), jndiName + PROXY_FACTORY_NAME, this);
66       }
67       catch (NamingException JavaDoc e)
68       {
69          NamingException JavaDoc namingException = new NamingException JavaDoc("Could not bind stateful local proxy with ejb name " + container.getEjbName() + " into JNDI under jndiName: " + container.getInitialContext().getNameInNamespace() + "/" + jndiName + PROXY_FACTORY_NAME);
70          namingException.setRootCause(e);
71          throw namingException;
72       }
73
74       LocalHome JavaDoc localHome = (LocalHome JavaDoc) ((EJBContainer) container).resolveAnnotation(LocalHome JavaDoc.class);
75       if (localHome != null)
76       {
77          Class JavaDoc[] interfaces = {localHome.value()};
78          Object JavaDoc homeProxy = java.lang.reflect.Proxy.newProxyInstance(container.getBeanClass().getClassLoader(),
79                                                                      interfaces, new StatefulLocalHomeProxy(container));
80          NonSerializableFactory.rebind(container.getInitialContext(), jndiName + "Home", homeProxy);
81       }
82    }
83
84    public void stop() throws Exception JavaDoc
85    {
86       super.stop();
87       NonSerializableFactory.unbind(container.getInitialContext(), jndiName + PROXY_FACTORY_NAME);
88       LocalHome JavaDoc localHome = (LocalHome JavaDoc) ((EJBContainer) container).resolveAnnotation(LocalHome JavaDoc.class);
89       if (localHome != null)
90       {
91          NonSerializableFactory.unbind(container.getInitialContext(), jndiName + "Home");
92       }
93    }
94
95    public Object JavaDoc createProxy()
96    {
97       try
98       {
99          StatefulContainer sfsb = (StatefulContainer) container;
100          StatefulBeanContext ctx = sfsb.getCache().create();
101          ctx.inUse = false;
102          Object JavaDoc id = ctx.getId();
103          Object JavaDoc[] args = {new StatefulLocalProxy(container, id)};
104          return proxyConstructor.newInstance(args);
105       }
106       catch (InstantiationException JavaDoc e)
107       {
108          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
109
}
110       catch (IllegalAccessException JavaDoc e)
111       {
112          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
113
}
114       catch (IllegalArgumentException JavaDoc e)
115       {
116          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
117
}
118       catch (InvocationTargetException JavaDoc e)
119       {
120          throw new RuntimeException JavaDoc(e.getTargetException()); //To change body of catch statement use Options | File Templates.
121
}
122    }
123
124    public Object JavaDoc createProxy(Object JavaDoc id)
125    {
126       try
127       {
128          StatefulContainer sfsb = (StatefulContainer) container;
129          Object JavaDoc[] args = {new StatefulLocalProxy(container, id)};
130          return proxyConstructor.newInstance(args);
131       }
132       catch (InstantiationException JavaDoc e)
133       {
134          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
135
}
136       catch (IllegalAccessException JavaDoc e)
137       {
138          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
139
}
140       catch (IllegalArgumentException JavaDoc e)
141       {
142          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
143
}
144       catch (InvocationTargetException JavaDoc e)
145       {
146          throw new RuntimeException JavaDoc(e.getTargetException()); //To change body of catch statement use Options | File Templates.
147
}
148    }
149    public Object JavaDoc createProxy(Class JavaDoc[] initTypes, Object JavaDoc[] initValues)
150    {
151       try
152       {
153          StatefulContainer sfsb = (StatefulContainer) container;
154          Object JavaDoc id = sfsb.createSession(initTypes, initValues);
155          Object JavaDoc[] args = {new StatefulLocalProxy(container, id)};
156          return proxyConstructor.newInstance(args);
157       }
158       catch (InstantiationException JavaDoc e)
159       {
160          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
161
}
162       catch (IllegalAccessException JavaDoc e)
163       {
164          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
165
}
166       catch (IllegalArgumentException JavaDoc e)
167       {
168          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
169
}
170       catch (InvocationTargetException JavaDoc e)
171       {
172          throw new RuntimeException JavaDoc(e.getTargetException()); //To change body of catch statement use Options | File Templates.
173
}
174    }
175
176    protected StatefulHandleImpl getHandle()
177    {
178       StatefulHandleImpl handle = new StatefulHandleImpl();
179       LocalBinding remoteBinding = (LocalBinding) advisor.resolveAnnotation(LocalBinding.class);
180       if (remoteBinding != null)
181          handle.jndiName = remoteBinding.jndiBinding();
182
183       return handle;
184    }
185 }
186
Popular Tags