001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.proxy.ejb.handle;
023:
024: import java.lang.reflect.Method;
025: import java.rmi.RemoteException;
026: import java.rmi.ServerException;
027: import java.io.IOException;
028: import java.io.ObjectStreamField;
029: import java.io.ObjectInputStream;
030: import java.io.ObjectOutputStream;
031: import java.util.Hashtable;
032:
033: import javax.naming.InitialContext;
034:
035: import javax.ejb.Handle;
036: import javax.ejb.EJBObject;
037: import javax.ejb.EJBHome;
038: import org.jboss.naming.NamingContextFactory;
039:
040: /**
041: * An EJB entity bean handle implementation.
042: *
043: * @author <a href="mailto:marc.fleury@jboss.org">Marc Fleury</a>.
044: * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
045: * @author Scott.Stark@jboss.org
046: * @version $Revision: 57209 $
047: */
048: public class EntityHandleImpl implements Handle {
049: // Constants -----------------------------------------------------
050:
051: /** Serial Version Identifier. */
052: static final long serialVersionUID = -132866169652666721L;
053: private static final ObjectStreamField[] serialPersistentFields = new ObjectStreamField[] {
054: new ObjectStreamField("id", Object.class),
055: new ObjectStreamField("jndiName", String.class),
056: new ObjectStreamField("jndiEnv", Hashtable.class) };
057:
058: /** The primary key of the entity bean. */
059: private Object id;
060: /** The JNDI name of the home inteface binding */
061: private String jndiName;
062: /** The JNDI env in effect when the home handle was created */
063: private Hashtable jndiEnv;
064:
065: // Constructors --------------------------------------------------
066:
067: /**
068: * Construct a <tt>EntityHandleImpl</tt>.
069: *
070: * @param jndiName JNDI name.
071: * @param id Primary key of the entity.
072: */
073: public EntityHandleImpl(String jndiName, Object id) {
074: this .jndiName = jndiName;
075: this .id = id;
076: this .jndiEnv = (Hashtable) NamingContextFactory.lastInitialContextEnv
077: .get();
078: }
079:
080: // Public --------------------------------------------------------
081:
082: /**
083: * Handle implementation.
084: *
085: * @return <tt>EJBObject</tt> reference.
086: *
087: * @throws ServerException Could not get EJBObject.
088: * @throws RemoteException
089: */
090: public EJBObject getEJBObject() throws RemoteException {
091:
092: try {
093: InitialContext ic = null;
094: if (jndiEnv != null)
095: ic = new InitialContext(jndiEnv);
096: else
097: ic = new InitialContext();
098: EJBHome home = (EJBHome) ic.lookup(jndiName);
099: Class type = home.getClass();
100: Method method = type.getMethod("findByPrimaryKey",
101: new Class[] { home.getEJBMetaData()
102: .getPrimaryKeyClass() });
103:
104: // call findByPrimary on the target
105: return (EJBObject) method.invoke(home, new Object[] { id });
106: } catch (Exception e) {
107: throw new ServerException("Could not get EJBObject", e);
108: }
109: }
110:
111: /**
112: * @return the primary key
113: */
114: public Object getID() {
115: return id;
116: }
117:
118: /**
119: * @return the jndi name
120: */
121: public String getJNDIName() {
122: return jndiName;
123: }
124:
125: private void readObject(ObjectInputStream ois) throws IOException,
126: ClassNotFoundException {
127: ObjectInputStream.GetField getField = ois.readFields();
128: id = getField.get("id", null);
129: jndiName = (String) getField.get("jndiName", null);
130: jndiEnv = (Hashtable) getField.get("jndiEnv", null);
131: }
132:
133: private void writeObject(ObjectOutputStream oos) throws IOException {
134: ObjectOutputStream.PutField putField = oos.putFields();
135: putField.put("id", id);
136: putField.put("jndiName", jndiName);
137: putField.put("jndiEnv", jndiEnv);
138: oos.writeFields();
139: }
140:
141: }
|