KickJava   Java API By Example, From Geeks To Geeks.

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

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

67    public StatelessHandleImpl()
68    {
69       
70    }
71    
72    /**
73     * Construct a <tt>StatelessHandleImpl</tt>.
74     *
75     * @param handle The initial context handle that will be used
76     * to restore the naming context or null to use
77     * a fresh InitialContext object.
78     * @param name JNDI name.
79     */

80    public StatelessHandleImpl(String JavaDoc jndiName)
81    {
82       this.jndiName = jndiName;
83       this.jndiEnv = (Hashtable JavaDoc) NamingContextFactory.lastInitialContextEnv.get();
84    }
85
86    // Public --------------------------------------------------------
87

88    public EJBObject JavaDoc getEJBObject() throws RemoteException JavaDoc
89    {
90       try
91       {
92          InitialContext JavaDoc ic = null;
93          if( jndiEnv != null )
94             ic = new InitialContext JavaDoc(jndiEnv);
95          else
96             ic = new InitialContext JavaDoc();
97     
98          Proxy JavaDoc proxy = (Proxy JavaDoc) ic.lookup(jndiName);
99
100          return (EJBObject JavaDoc) proxy;
101       }
102       catch (Throwable JavaDoc t)
103       {
104          t.printStackTrace();
105          throw new RemoteException JavaDoc("Error during getEJBObject", t);
106       }
107    }
108
109    /**
110     * @return the jndi name
111     */

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