1 22 package org.jboss.proxy.ejb.handle; 23 24 import java.io.IOException ; 25 import java.io.ObjectInputStream ; 26 import java.io.ObjectOutputStream ; 27 28 import javax.ejb.EJBHome ; 29 import javax.ejb.EJBObject ; 30 import javax.ejb.spi.HandleDelegate ; 31 import javax.naming.InitialContext ; 32 import javax.naming.NamingException ; 33 import javax.rmi.PortableRemoteObject ; 34 import javax.rmi.CORBA.Stub ; 35 36 import org.jboss.util.NestedRuntimeException; 37 import org.omg.CORBA.BAD_OPERATION ; 38 import org.omg.CORBA.ORB ; 39 import org.omg.CORBA.portable.Delegate ; 40 import org.omg.CORBA.portable.ObjectImpl ; 41 42 58 public class HandleDelegateImpl 59 implements HandleDelegate 60 { 61 public static HandleDelegate getDelegate() 62 { 63 try 64 { 65 InitialContext ctx = new InitialContext (); 66 return (HandleDelegate ) ctx.lookup("java:comp/HandleDelegate"); 67 } 68 catch (NamingException e) 69 { 70 throw new NestedRuntimeException(e); 71 } 72 } 73 74 76 public void writeEJBObject(EJBObject ejbObject, ObjectOutputStream oostream) 77 throws IOException 78 { 79 oostream.writeObject(ejbObject); 80 } 81 82 public EJBObject readEJBObject(ObjectInputStream oistream) 83 throws IOException , ClassNotFoundException 84 { 85 Object ejbObject = oistream.readObject(); 86 reconnect(ejbObject); 87 return (EJBObject ) PortableRemoteObject.narrow(ejbObject, EJBObject .class); 88 } 89 90 public void writeEJBHome(EJBHome ejbHome, ObjectOutputStream oostream) 91 throws IOException 92 { 93 oostream.writeObject(ejbHome); 94 } 95 96 public EJBHome readEJBHome(ObjectInputStream oistream) 97 throws IOException , ClassNotFoundException 98 { 99 Object ejbHome = oistream.readObject(); 100 reconnect(ejbHome); 101 return (EJBHome ) PortableRemoteObject.narrow(ejbHome, EJBHome .class); 102 } 103 104 protected void reconnect(Object object) throws IOException 105 { 106 if (object instanceof ObjectImpl ) 107 { 108 try 109 { 110 ObjectImpl objectImpl = (ObjectImpl ) object; 112 objectImpl._get_delegate(); 113 } 114 catch (BAD_OPERATION e) 115 { 116 try 117 { 118 Stub stub = (Stub ) object; 120 ORB orb = (ORB ) new InitialContext ().lookup("java:comp/ORB"); 121 stub.connect(orb); 122 } 123 catch (NamingException ne) 124 { 125 throw new IOException ("Unable to lookup java:comp/ORB"); 126 } 127 } 128 } 129 else 130 throw new IOException ("Not an ObjectImpl " + object.getClass().getName()); 131 } 132 } 133 | Popular Tags |