1 19 package org.objectweb.carol.cmi; 20 21 import java.lang.reflect.Constructor ; 22 import java.lang.reflect.InvocationTargetException ; 23 import java.lang.reflect.Method ; 24 import java.rmi.NoSuchObjectException ; 25 import java.rmi.Remote ; 26 import java.rmi.RemoteException ; 27 import java.rmi.server.ObjID ; 28 import java.rmi.server.RemoteRef ; 29 import java.rmi.server.RemoteStub ; 30 31 import javax.rmi.CORBA.PortableRemoteObjectDelegate ; 32 33 import org.objectweb.carol.rmi.multi.JrmpPRODelegate; 34 35 class SunLowerOrb { 36 private static Class liveref; 37 private static Constructor liveref_cons; 38 private static Class usref; 39 private static Constructor usref_cons; 40 private static Method usref_export; 41 private static Class tcpep; 42 private static Constructor tcpep_cons; 43 private static Constructor liveref_cons2; 44 private static Class uref; 45 private static Constructor uref_cons; 46 private static boolean init = false; 47 48 static { 49 try { 50 liveref = Class.forName("sun.rmi.transport.LiveRef"); 51 Class [] p0 = { ObjID .class, int.class }; 52 liveref_cons = liveref.getConstructor(p0); 53 usref = Class.forName("sun.rmi.server.UnicastServerRef"); 54 Class [] p1 = { liveref }; 55 usref_cons = usref.getConstructor(p1); 56 Class [] p2 = { Remote .class, Object .class, boolean.class }; 57 usref_export = usref.getMethod("exportObject", p2); 58 tcpep = Class.forName("sun.rmi.transport.tcp.TCPEndpoint"); 59 Class [] p3 = { String .class, int.class }; 60 tcpep_cons = tcpep.getConstructor(p3); 61 Class ep = Class.forName("sun.rmi.transport.Endpoint"); 62 Class [] p4 = { ObjID .class, ep, boolean.class }; 63 liveref_cons2 = liveref.getConstructor(p4); 64 uref = Class.forName("sun.rmi.server.UnicastRef"); 65 Class [] p5 = { liveref }; 66 uref_cons = uref.getConstructor(p5); 67 init = true; 68 } catch (ClassNotFoundException e) { 69 } catch (SecurityException e) { 71 } catch (NoSuchMethodException e) { 73 } 75 } 76 77 public static boolean isValid() { 78 return init; 79 } 80 81 public static Remote export(Remote obj, int port, ObjID id) throws IllegalArgumentException , InstantiationException , IllegalAccessException , InvocationTargetException { 82 Object [] p0 = { id, new Integer (port) }; 83 Object lr = liveref_cons.newInstance(p0); 84 Object [] p1 = { lr }; 85 Object usr = usref_cons.newInstance(p1); 86 Object [] p2 = { obj, null, new Boolean (true) }; 87 Object ret = usref_export.invoke(usr, p2); 88 return (Remote )ret; 89 } 90 91 public static RemoteRef getRemoteRef(String host, int port, ObjID id) throws IllegalArgumentException , InstantiationException , IllegalAccessException , InvocationTargetException { 92 Object [] p0 = { host, new Integer (port) }; 93 Object ep = tcpep_cons.newInstance(p0); 94 Object [] p1 = { id, ep, new Boolean (false) }; 95 Object ref = liveref_cons2.newInstance(p1); 96 Object [] p2 = { ref }; 97 Object rr = uref_cons.newInstance(p2); 98 return (RemoteRef )rr; 99 } 100 } 101 102 103 class GcjLowerOrb { 104 private static boolean init = false; 105 106 static { 107 try { 108 } catch (SecurityException e) { 110 } 112 } 113 114 public static boolean isValid() { 115 return init; 116 } 117 118 public static Remote export(Remote obj, int port, ObjID id) throws IllegalArgumentException , InstantiationException , IllegalAccessException , InvocationTargetException { 119 return null; 120 } 121 122 public static RemoteRef getRemoteRef(String host, int port, ObjID id) throws IllegalArgumentException , InstantiationException , IllegalAccessException , InvocationTargetException { 123 return null; 124 } 125 } 126 127 128 public class LowerOrb { 129 private static String prefix = "rmi:"; 130 public static final int DEFAULT_CREG_PORT = 1099; 131 public static final int REG_ID = 0xC2C91901; 132 private static ObjID id = new ObjID (REG_ID); 133 private static PortableRemoteObjectDelegate rmi = new JrmpPRODelegate(true); 134 135 public static Remote toStub(Remote obj) throws NoSuchObjectException { 136 return rmi.toStub(obj); 137 } 138 139 public static void exportObject(Remote obj) throws RemoteException { 140 rmi.exportObject(obj); 141 } 142 143 public static void unexportObject(Remote obj) throws NoSuchObjectException { 144 rmi.unexportObject(obj); 145 } 146 147 public static PortableRemoteObjectDelegate getPRODelegate() { 148 return rmi; 149 } 150 151 public static Remote exportRegistry(Remote obj, int port) 152 throws RemoteException { 153 158 try { 159 if (SunLowerOrb.isValid()) { 160 return SunLowerOrb.export(obj, port, id); 161 } else if (GcjLowerOrb.isValid()) { 162 return GcjLowerOrb.export(obj, port, id); 163 } else { 164 throw new RemoteException ("Don't know how to export registry : ORB specific"); 165 } 166 } catch (InvocationTargetException e) { 167 Throwable t = e.getTargetException(); 168 if (t instanceof RemoteException ) { 169 throw (RemoteException )t; 170 } else { 171 throw new RemoteException ("Unexpected exception", t); 172 } 173 } catch (RemoteException e) { 174 throw e; 175 } catch (Exception e) { 176 throw new RemoteException ("Unexpected exception", e); 177 } 178 } 179 180 private static Class [] stubConsParamTypes = { RemoteRef .class }; 181 182 public static Remote getRegistryStub( 183 String className, 184 String host, 185 int port) 186 throws RemoteException { 187 if (port <= 0) 188 throw new RemoteException ("Invalid port no " + port); 189 RemoteRef rr; 190 try { 191 196 if (SunLowerOrb.isValid()) { 197 rr = SunLowerOrb.getRemoteRef(host, port, id); 198 } else if (GcjLowerOrb.isValid()) { 199 rr = GcjLowerOrb.getRemoteRef(host, port, id); 200 } else { 201 throw new RemoteException ("Don't know how to build a stub : ORB specific"); 202 } 203 Class stubcl = Class.forName(className + "_Stub"); 204 Object [] p0 = { rr }; 205 Constructor cons = stubcl.getConstructor(stubConsParamTypes); 206 return (RemoteStub ) cons.newInstance(p0); 207 } catch (InvocationTargetException e) { 208 Throwable t = e.getTargetException(); 209 if (t instanceof RemoteException ) { 210 throw (RemoteException )t; 211 } else { 212 throw new RemoteException ("Unexpected exception", t); 213 } 214 } catch (RemoteException e) { 215 throw e; 216 } catch (Exception e) { 217 throw new RemoteException ("Unexpected exception", e); 218 } 219 } 220 } 221 | Popular Tags |