1 8 9 package mx4j.remote.resolver.iiop; 10 11 import java.io.IOException ; 12 import java.net.MalformedURLException ; 13 import java.util.Iterator ; 14 import java.util.Map ; 15 import java.util.Properties ; 16 17 import javax.management.remote.JMXServiceURL ; 18 import javax.management.remote.rmi.RMIIIOPServerImpl ; 19 import javax.management.remote.rmi.RMIServer ; 20 import javax.management.remote.rmi.RMIServerImpl ; 21 import javax.rmi.CORBA.Stub ; 22 import javax.rmi.PortableRemoteObject ; 23 24 import org.omg.CORBA.BAD_OPERATION ; 25 import org.omg.CORBA.ORB ; 26 27 30 public class Resolver extends mx4j.remote.resolver.rmi.Resolver 31 { 32 private static final String IOR_CONTEXT = "/ior/"; 33 34 private ORB orb; 35 private static final String ORB_KEY = "java.naming.corba.orb"; 36 37 38 41 protected RMIServer lookupStubInJNDI(JMXServiceURL url, Map environment) throws IOException 42 { 43 checkORB(environment); 44 return super.lookupStubInJNDI(url, environment); 45 } 46 47 protected RMIServer decodeStub(JMXServiceURL url, Map environment) throws IOException 48 { 49 String path = url.getURLPath(); 50 String ior = IOR_CONTEXT; 51 if (path.startsWith(ior)) 52 { 53 String encoded = path.substring(ior.length()); 54 ORB orb = getORB(environment); 55 Object object = orb.string_to_object(encoded); 56 return narrowRMIServerStub(object); 57 } 58 throw new MalformedURLException ("Unsupported binding: " + url); 59 } 60 61 protected RMIServer narrowRMIServerStub(Object stub) 62 { 63 return (RMIServer )PortableRemoteObject.narrow(stub, RMIServer .class); 64 } 65 66 public Object bindClient(Object client, Map environment) throws IOException 67 { 68 Stub stub = (Stub )client; 69 ORB orb = null; 70 try 71 { 72 orb = stub._orb(); 73 } 74 catch (BAD_OPERATION x) 75 { 76 } 78 79 if (orb == null) 80 { 81 orb = getORB(environment); 82 stub.connect(orb); 83 } 84 return stub; 85 } 86 87 90 91 protected RMIServerImpl createRMIServer(JMXServiceURL url, Map environment) throws IOException 92 { 93 return new RMIIIOPServerImpl (environment); 94 } 95 96 public JMXServiceURL bindServer(Object server, JMXServiceURL url, Map environment) throws IOException 97 { 98 RMIServerImpl rmiServer = (RMIServerImpl )server; 99 Stub stub = (Stub )PortableRemoteObject.toStub(rmiServer); 100 stub.connect(getORB(environment)); 101 return super.bindServer(server, url, environment); 102 } 103 104 protected String encodeStub(RMIServerImpl rmiServer, Map environment) throws IOException 105 { 106 Stub stub = (Stub )bindClient(rmiServer.toStub(), environment); 107 String ior = getORB(environment).object_to_string(stub); 108 return IOR_CONTEXT + ior; 109 } 110 111 private ORB checkORB(Map environment) 112 { 113 if (environment == null) return null; 114 Object candidateORB = environment.get(ORB_KEY); 115 if (candidateORB != null) 116 { 117 if (!(candidateORB instanceof ORB )) throw new IllegalArgumentException ("Property " + ORB_KEY + " must specify a " + ORB .class.getName() + ", not " + candidateORB.getClass().getName()); 119 return (ORB )candidateORB; 120 } 121 return null; 122 } 123 124 128 private synchronized ORB getORB(Map environment) 129 { 130 if (orb == null) 131 { 132 orb = checkORB(environment); 133 if (orb == null) 134 { 135 Properties props = new Properties (); 136 for (Iterator i = environment.entrySet().iterator(); i.hasNext();) 138 { 139 Map.Entry entry = (Map.Entry )i.next(); 140 Object key = entry.getKey(); 141 Object value = entry.getValue(); 142 if (key instanceof String && value instanceof String ) 143 { 144 props.setProperty((String )key, (String )value); 145 } 146 } 147 orb = ORB.init((String [])null, props); 148 } 149 } 150 return orb; 151 } 152 153 protected boolean isEncodedForm(JMXServiceURL url) 154 { 155 String path = url.getURLPath(); 156 if (path != null && path.startsWith(IOR_CONTEXT)) return true; 157 return super.isEncodedForm(url); 158 } 159 160 public void destroyServer(Object server, JMXServiceURL url, Map environment) throws IOException 161 { 162 if (!isEncodedForm(url)) return; 163 if (orb != null) 164 { 165 orb.shutdown(true); 166 orb.destroy(); 167 } 168 } 169 } 170 | Popular Tags |