1 22 package org.jboss.proxy.ejb.handle; 23 24 import java.lang.reflect.Method ; 25 import java.rmi.RemoteException ; 26 import java.rmi.ServerException ; 27 import java.io.IOException ; 28 import java.io.ObjectStreamField ; 29 import java.io.ObjectInputStream ; 30 import java.io.ObjectOutputStream ; 31 import java.util.Hashtable ; 32 33 import javax.naming.InitialContext ; 34 35 import javax.ejb.Handle ; 36 import javax.ejb.EJBObject ; 37 import javax.ejb.EJBHome ; 38 import org.jboss.naming.NamingContextFactory; 39 40 48 public class EntityHandleImpl 49 implements Handle 50 { 51 53 54 static final long serialVersionUID = -132866169652666721L; 55 private static final ObjectStreamField [] serialPersistentFields = 56 new ObjectStreamField [] 57 { 58 new ObjectStreamField ("id", Object .class), 59 new ObjectStreamField ("jndiName", String .class), 60 new ObjectStreamField ("jndiEnv", Hashtable .class) 61 }; 62 63 64 private Object id; 65 66 private String jndiName; 67 68 private Hashtable jndiEnv; 69 70 72 81 public EntityHandleImpl(String jndiName, Object id) 82 { 83 this.jndiName = jndiName; 84 this.id = id; 85 this.jndiEnv = (Hashtable ) NamingContextFactory.lastInitialContextEnv.get(); 86 } 87 88 90 98 public EJBObject getEJBObject() throws RemoteException 99 { 100 101 try 102 { 103 InitialContext ic = null; 104 if( jndiEnv != null ) 105 ic = new InitialContext (jndiEnv); 106 else 107 ic = new InitialContext (); 108 EJBHome home = (EJBHome ) ic.lookup(jndiName); 109 Class type = home.getClass(); 110 Method method = type.getMethod("findByPrimaryKey", new Class []{home.getEJBMetaData().getPrimaryKeyClass()}); 111 112 return (EJBObject ) method.invoke(home, new Object []{id}); 114 } 115 catch (Exception e) 116 { 117 throw new ServerException ("Could not get EJBObject", e); 118 } 119 } 120 121 124 public Object getID() 125 { 126 return id; 127 } 128 129 132 public String getJNDIName() 133 { 134 return jndiName; 135 } 136 137 private void readObject(ObjectInputStream ois) 138 throws IOException , ClassNotFoundException 139 { 140 ObjectInputStream.GetField getField = ois.readFields(); 141 id = getField.get("id", null); 142 jndiName = (String ) getField.get("jndiName", null); 143 jndiEnv = (Hashtable ) getField.get("jndiEnv", null); 144 } 145 146 private void writeObject(ObjectOutputStream oos) 147 throws IOException 148 { 149 ObjectOutputStream.PutField 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 |