KickJava   Java API By Example, From Geeks To Geeks.

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


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

48 public class EntityHandleImpl
49       implements Handle JavaDoc
50 {
51    // Constants -----------------------------------------------------
52

53    /** Serial Version Identifier. */
54    static final long serialVersionUID = -132866169652666721L;
55    private static final ObjectStreamField JavaDoc[] serialPersistentFields =
56       new ObjectStreamField JavaDoc[]
57    {
58       new ObjectStreamField JavaDoc("id", Object JavaDoc.class),
59       new ObjectStreamField JavaDoc("jndiName", String JavaDoc.class),
60       new ObjectStreamField JavaDoc("jndiEnv", Hashtable JavaDoc.class)
61    };
62
63    /** The primary key of the entity bean. */
64    private Object JavaDoc id;
65    /** The JNDI name of the home inteface binding */
66    private String JavaDoc jndiName;
67    /** The JNDI env in effect when the home handle was created */
68    private Hashtable JavaDoc jndiEnv;
69
70    // Constructors --------------------------------------------------
71

72    /**
73     * Construct a <tt>EntityHandleImpl</tt>.
74     *
75     * @param state The initial context state 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     * @param id Primary key of the entity.
80     */

81    public EntityHandleImpl(String JavaDoc jndiName, Object JavaDoc id)
82    {
83       this.jndiName = jndiName;
84       this.id = id;
85       this.jndiEnv = (Hashtable JavaDoc) NamingContextFactory.lastInitialContextEnv.get();
86    }
87
88    // Public --------------------------------------------------------
89

90    /**
91     * Handle implementation.
92     *
93     * @return <tt>EJBObject</tt> reference.
94     *
95     * @throws ServerException Could not get EJBObject.
96     * @throws RemoteException
97     */

98    public EJBObject JavaDoc getEJBObject() throws RemoteException JavaDoc
99    {
100
101       try
102       {
103          InitialContext JavaDoc ic = null;
104          if( jndiEnv != null )
105             ic = new InitialContext JavaDoc(jndiEnv);
106          else
107             ic = new InitialContext JavaDoc();
108          EJBHome JavaDoc home = (EJBHome JavaDoc) ic.lookup(jndiName);
109          Class JavaDoc type = home.getClass();
110          Method JavaDoc method = type.getMethod("findByPrimaryKey", new Class JavaDoc[]{home.getEJBMetaData().getPrimaryKeyClass()});
111
112          // call findByPrimary on the target
113
return (EJBObject JavaDoc) method.invoke(home, new Object JavaDoc[]{id});
114       }
115       catch (Exception JavaDoc e)
116       {
117          throw new ServerException JavaDoc("Could not get EJBObject", e);
118       }
119    }
120
121    /**
122     * @return the primary key
123     */

124    public Object JavaDoc getID()
125    {
126       return id;
127    }
128
129    /**
130     * @return the jndi name
131     */

132    public String JavaDoc getJNDIName()
133    {
134       return jndiName;
135    }
136
137    private void readObject(ObjectInputStream JavaDoc ois)
138       throws IOException JavaDoc, ClassNotFoundException JavaDoc
139    {
140       ObjectInputStream.GetField JavaDoc getField = ois.readFields();
141       id = getField.get("id", null);
142       jndiName = (String JavaDoc) getField.get("jndiName", null);
143       jndiEnv = (Hashtable JavaDoc) getField.get("jndiEnv", null);
144    }
145
146    private void writeObject(ObjectOutputStream JavaDoc oos)
147       throws IOException JavaDoc
148    {
149       ObjectOutputStream.PutField JavaDoc putField = oos.putFields();
150       putField.put("id", id);
151       putField.put("jndiName", jndiName);
152       putField.put("jndiEnv", jndiEnv);
153       oos.writeFields();
154    }
155
156 }
157
Popular Tags