KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > proxy > ejb > handle > StatelessHandleImpl


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.proxy.ejb.handle;
23
24 import java.rmi.ServerException JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import java.io.ObjectStreamField JavaDoc;
27 import java.io.ObjectInputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.ObjectOutputStream JavaDoc;
30 import java.util.Hashtable JavaDoc;
31
32 import javax.naming.InitialContext JavaDoc;
33 import javax.ejb.Handle JavaDoc;
34 import javax.ejb.EJBObject JavaDoc;
35 import javax.ejb.EJBHome JavaDoc;
36
37 import org.jboss.naming.NamingContextFactory;
38
39 /**
40  * An EJB stateless session bean handle.
41  *
42  * @author <a HREF="mailto:marc.fleury@jboss.org">Marc Fleury</a>
43  * @author Scott.Stark@jboss.org
44  * @version $Revision: 37459 $
45  */

46 public class StatelessHandleImpl
47       implements Handle JavaDoc
48 {
49    /** Serial Version Identifier. */
50    static final long serialVersionUID = 3811452873535097661L;
51    private static final ObjectStreamField JavaDoc[] serialPersistentFields =
52       new ObjectStreamField JavaDoc[]
53    {
54       new ObjectStreamField JavaDoc("jndiName", String JavaDoc.class),
55       new ObjectStreamField JavaDoc("jndiEnv", Hashtable JavaDoc.class)
56    };
57
58    /** The JNDI name of the home inteface binding */
59    private String JavaDoc jndiName;
60    /** The JNDI env in effect when the home handle was created */
61    private Hashtable JavaDoc jndiEnv;
62
63    // Constructors --------------------------------------------------
64

65    /**
66     * Construct a <tt>StatelessHandleImpl</tt>.
67     *
68     * @param handle The initial context handle that will be used
69     * to restore the naming context or null to use
70     * a fresh InitialContext object.
71     * @param name JNDI name.
72     */

73    public StatelessHandleImpl(String JavaDoc jndiName)
74    {
75       this.jndiName = jndiName;
76       this.jndiEnv = (Hashtable JavaDoc) NamingContextFactory.lastInitialContextEnv.get();
77    }
78
79    // Public --------------------------------------------------------
80

81    public EJBObject JavaDoc getEJBObject()
82          throws ServerException JavaDoc
83    {
84       try
85       {
86          InitialContext JavaDoc ic = null;
87          if( jndiEnv != null )
88             ic = new InitialContext JavaDoc(jndiEnv);
89          else
90             ic = new InitialContext JavaDoc();
91          EJBHome JavaDoc home = (EJBHome JavaDoc) ic.lookup(jndiName);
92          Class JavaDoc type = home.getClass();
93          Method JavaDoc method = type.getMethod("create", new Class JavaDoc[0]);
94          return (EJBObject JavaDoc) method.invoke(home, new Object JavaDoc[0]);
95       }
96       catch (Exception JavaDoc e)
97       {
98          throw new ServerException JavaDoc("Could not get EJBObject", e);
99       }
100    }
101
102    /**
103     * @return the jndi name
104     */

105    public String JavaDoc getJNDIName()
106    {
107       return jndiName;
108    }
109
110    private void readObject(ObjectInputStream JavaDoc ois)
111       throws IOException JavaDoc, ClassNotFoundException JavaDoc
112    {
113       ObjectInputStream.GetField JavaDoc getField = ois.readFields();
114       jndiName = (String JavaDoc) getField.get("jndiName", null);
115       jndiEnv = (Hashtable JavaDoc) getField.get("jndiEnv", null);
116    }
117
118    private void writeObject(ObjectOutputStream JavaDoc oos)
119       throws IOException JavaDoc
120    {
121       ObjectOutputStream.PutField JavaDoc putField = oos.putFields();
122       putField.put("jndiName", jndiName);
123       putField.put("jndiEnv", jndiEnv);
124       oos.writeFields();
125    }
126 }
127
Popular Tags