1 7 8 package java.rmi.activation; 9 10 import java.io.IOException ; 11 import java.io.InvalidObjectException ; 12 import java.lang.reflect.InvocationHandler ; 13 import java.lang.reflect.Proxy ; 14 import java.rmi.MarshalledObject ; 15 import java.rmi.Remote ; 16 import java.rmi.RemoteException ; 17 import java.rmi.UnmarshalException ; 18 import java.rmi.server.RemoteObject ; 19 import java.rmi.server.RemoteObjectInvocationHandler ; 20 import java.rmi.server.RemoteRef ; 21 import java.rmi.server.RemoteStub ; 22 import java.rmi.server.UID ; 23 24 51 public class ActivationID implements java.io.Serializable { 52 55 private transient Activator activator; 56 57 60 private transient UID uid = new UID (); 61 62 63 private static final long serialVersionUID = -4608673054848209235L; 64 65 76 public ActivationID(Activator activator) { 77 this.activator = activator; 78 } 79 80 92 public Remote activate(boolean force) 93 throws ActivationException , UnknownObjectException , RemoteException 94 { 95 try { 96 MarshalledObject mobj = 97 (MarshalledObject ) activator.activate(this, force); 98 return (Remote ) mobj.get(); 99 } catch (RemoteException e) { 100 throw e; 101 } catch (IOException e) { 102 throw new UnmarshalException ("activation failed", e); 103 } catch (ClassNotFoundException e) { 104 throw new UnmarshalException ("activation failed", e); 105 } 106 107 } 108 109 116 public int hashCode() { 117 return uid.hashCode(); 118 } 119 120 132 public boolean equals(Object obj) { 133 if (obj instanceof ActivationID ) { 134 ActivationID id = (ActivationID ) obj; 135 return (uid.equals(id.uid) && activator.equals(id.activator)); 136 } else { 137 return false; 138 } 139 } 140 141 186 private void writeObject(java.io.ObjectOutputStream out) 187 throws IOException , ClassNotFoundException 188 { 189 out.writeObject(uid); 190 191 RemoteRef ref; 192 if (activator instanceof RemoteObject ) { 193 ref = ((RemoteObject ) activator).getRef(); 194 } else if (Proxy.isProxyClass(activator.getClass())) { 195 InvocationHandler handler = Proxy.getInvocationHandler(activator); 196 if (!(handler instanceof RemoteObjectInvocationHandler )) { 197 throw new InvalidObjectException ( 198 "unexpected invocation handler"); 199 } 200 ref = ((RemoteObjectInvocationHandler ) handler).getRef(); 201 202 } else { 203 throw new InvalidObjectException ("unexpected activator type"); 204 } 205 out.writeUTF(ref.getRefClass(out)); 206 ref.writeExternal(out); 207 } 208 209 245 private void readObject(java.io.ObjectInputStream in) 246 throws IOException , ClassNotFoundException 247 { 248 uid = (UID )in.readObject(); 249 250 try { 251 Class refClass = Class.forName(RemoteRef.packagePrefix + "." + 252 in.readUTF()); 253 RemoteRef ref = (RemoteRef ) refClass.newInstance(); 254 ref.readExternal(in); 255 activator = (Activator ) 256 Proxy.newProxyInstance(null, 257 new Class []{ Activator .class }, 258 new RemoteObjectInvocationHandler (ref)); 259 260 } catch (InstantiationException e) { 261 throw (IOException ) 262 new InvalidObjectException ( 263 "Unable to create remote reference").initCause(e); 264 } catch (IllegalAccessException e) { 265 throw (IOException ) 266 new InvalidObjectException ( 267 "Unable to create remote reference").initCause(e); 268 } 269 } 270 } 271 | Popular Tags |