1 8 9 package mx4j.remote.resolver.iiop; 10 11 import java.io.IOException ; 12 import java.net.MalformedURLException ; 13 import java.util.Map ; 14 import java.util.Properties ; 15 16 import javax.management.remote.JMXServiceURL ; 17 import javax.management.remote.rmi.RMIIIOPServerImpl ; 18 import javax.management.remote.rmi.RMIServer ; 19 import javax.management.remote.rmi.RMIServerImpl ; 20 import javax.rmi.CORBA.Stub ; 21 import javax.rmi.PortableRemoteObject ; 22 23 import mx4j.remote.resolver.rmi.RMIResolver; 24 import org.omg.CORBA.ORB ; 25 import org.omg.CORBA.BAD_OPERATION ; 26 27 32 public class IIOPResolver extends RMIResolver 33 { 34 private static final String IOR_CONTEXT = "/ior/"; 35 36 private ORB orb; 37 38 39 42 43 protected RMIServer decodeStub(JMXServiceURL url, Map environment) throws IOException 44 { 45 String path = url.getURLPath(); 46 String ior = IOR_CONTEXT; 47 if (path.startsWith(ior)) 48 { 49 String encoded = path.substring(ior.length()); 50 ORB orb = getORB(environment); 51 Object object = orb.string_to_object(encoded); 52 return narrowRMIServerStub(object); 53 } 54 throw new MalformedURLException ("Unsupported binding: " + url); 55 } 56 57 protected RMIServer narrowRMIServerStub(Object stub) 58 { 59 return (RMIServer )PortableRemoteObject.narrow(stub, RMIServer .class); 60 } 61 62 public Object bindClient(Object client, Map environment) throws IOException 63 { 64 Stub stub = (Stub )client; 65 ORB orb = null; 66 try 67 { 68 orb = stub._orb(); 69 } 70 catch (BAD_OPERATION x) 71 { 72 } 74 75 if (orb == null) 76 { 77 orb = getORB(environment); 78 stub.connect(orb); 79 } 80 return stub; 81 } 82 83 86 87 protected RMIServerImpl createRMIServer(JMXServiceURL url, Map environment) throws IOException 88 { 89 return new RMIIIOPServerImpl (environment); 90 } 91 92 protected String encodeStub(RMIServerImpl rmiServer, Map environment) throws IOException 93 { 94 Stub stub = (Stub )bindClient(rmiServer.toStub(), environment); 95 String ior = orb.object_to_string(stub); 96 return IOR_CONTEXT + ior; 97 } 98 99 103 private synchronized ORB getORB(Map environment) 104 { 105 if (orb == null) 106 { 107 Properties props = new Properties (); 108 props.putAll(environment); 109 orb = ORB.init((String [])null, props); 110 } 111 return orb; 112 } 113 114 protected boolean isEncodedForm(JMXServiceURL url) 115 { 116 String path = url.getURLPath(); 117 if (path != null && path.startsWith(IOR_CONTEXT)) return true; 118 return super.isEncodedForm(url); 119 } 120 121 protected void destroyServer(Object server, Map environment) throws IOException 122 { 123 if (orb != null) 124 { 125 orb.shutdown(true); 126 orb.destroy(); 127 } 128 } 129 } 130 | Popular Tags |