KickJava   Java API By Example, From Geeks To Geeks.

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


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.Constructor JavaDoc;
25 import java.lang.reflect.InvocationHandler JavaDoc;
26 import javax.naming.Context JavaDoc;
27 import javax.naming.Name JavaDoc;
28 import javax.naming.NamingException JavaDoc;
29 import javax.naming.RefAddr JavaDoc;
30 import javax.naming.Reference JavaDoc;
31 import javax.naming.StringRefAddr JavaDoc;
32 import org.jboss.ejb3.JndiProxyFactory;
33 import org.jboss.ejb3.ProxyFactory;
34 import org.jboss.logging.Logger;
35 import org.jboss.naming.Util;
36
37 /**
38  * Comment
39  *
40  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
41  * @version $Revision: 56518 $
42  */

43 public abstract class BaseStatefulProxyFactory extends org.jboss.ejb3.session.BaseSessionProxyFactory implements ProxyFactory
44 {
45    private static final Logger log = Logger.getLogger(BaseStatefulProxyFactory.class);
46
47    protected Class JavaDoc proxyClass;
48    protected Constructor JavaDoc proxyConstructor;
49    protected Context JavaDoc proxyFactoryContext;
50    protected String JavaDoc jndiName;
51
52    public static final String JavaDoc PROXY_FACTORY_NAME = "StatefulProxyFactory";
53
54    public void init() throws Exception JavaDoc
55    {
56       initializeJndiName();
57       Class JavaDoc[] interfaces = getInterfaces();
58       Class JavaDoc proxyClass = java.lang.reflect.Proxy.getProxyClass(container.getBeanClass().getClassLoader(), interfaces);
59       final Class JavaDoc[] constructorParams =
60               {InvocationHandler JavaDoc.class};
61       proxyConstructor = proxyClass.getConstructor(constructorParams);
62    }
63
64    public void start() throws Exception JavaDoc
65    {
66       init();
67
68       Context JavaDoc ctx = container.getInitialContext();
69       Name JavaDoc name = ctx.getNameParser("").parse(jndiName);
70       ctx = Util.createSubcontext(ctx, name.getPrefix(name.size() - 1));
71       String JavaDoc atom = name.get(name.size() - 1);
72       RefAddr JavaDoc refAddr = new StringRefAddr JavaDoc(JndiProxyFactory.FACTORY, jndiName + PROXY_FACTORY_NAME);
73       Reference JavaDoc ref = new Reference JavaDoc("java.lang.Object", refAddr, JndiProxyFactory.class.getName(), null);
74       try
75       {
76          Util.rebind(ctx, atom, ref);
77       } catch (NamingException JavaDoc e)
78       {
79          NamingException JavaDoc namingException = new NamingException JavaDoc("Could not bind stateful proxy with ejb name " + container.getEjbName() + " into JNDI under jndiName: " + ctx.getNameInNamespace() + "/" + atom);
80          namingException.setRootCause(e);
81          throw namingException;
82       }
83    }
84
85    public void stop() throws Exception JavaDoc
86    {
87       Util.unbind(container.getInitialContext(), jndiName);
88    }
89
90    protected abstract Class JavaDoc[] getInterfaces();
91
92    protected abstract void initializeJndiName();
93 }
94
Popular Tags