KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > remote > resolver > iiop > Resolver


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.remote.resolver.iiop;
10
11 import java.io.IOException JavaDoc;
12 import java.net.MalformedURLException JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.Properties JavaDoc;
16
17 import javax.management.remote.JMXServiceURL JavaDoc;
18 import javax.management.remote.rmi.RMIIIOPServerImpl JavaDoc;
19 import javax.management.remote.rmi.RMIServer JavaDoc;
20 import javax.management.remote.rmi.RMIServerImpl JavaDoc;
21 import javax.rmi.CORBA.Stub JavaDoc;
22 import javax.rmi.PortableRemoteObject JavaDoc;
23
24 import org.omg.CORBA.BAD_OPERATION JavaDoc;
25 import org.omg.CORBA.ORB JavaDoc;
26
27 /**
28  * @version $Revision: 1.3 $
29  */

30 public class Resolver extends mx4j.remote.resolver.rmi.Resolver
31 {
32    private static final String JavaDoc IOR_CONTEXT = "/ior/";
33
34    private ORB JavaDoc orb;
35    private static final String JavaDoc ORB_KEY = "java.naming.corba.orb";
36
37
38 //********************************************************************************************************************//
39
// CLIENT METHODS
40

41    protected RMIServer JavaDoc lookupStubInJNDI(JMXServiceURL JavaDoc url, Map JavaDoc environment) throws IOException JavaDoc
42    {
43       checkORB(environment);
44       return super.lookupStubInJNDI(url, environment);
45    }
46
47    protected RMIServer JavaDoc decodeStub(JMXServiceURL JavaDoc url, Map JavaDoc environment) throws IOException JavaDoc
48    {
49       String JavaDoc path = url.getURLPath();
50       String JavaDoc ior = IOR_CONTEXT;
51       if (path.startsWith(ior))
52       {
53          String JavaDoc encoded = path.substring(ior.length());
54          ORB JavaDoc orb = getORB(environment);
55          Object JavaDoc object = orb.string_to_object(encoded);
56          return narrowRMIServerStub(object);
57       }
58       throw new MalformedURLException JavaDoc("Unsupported binding: " + url);
59    }
60
61    protected RMIServer JavaDoc narrowRMIServerStub(Object JavaDoc stub)
62    {
63       return (RMIServer JavaDoc)PortableRemoteObject.narrow(stub, RMIServer JavaDoc.class);
64    }
65
66    public Object JavaDoc bindClient(Object JavaDoc client, Map JavaDoc environment) throws IOException JavaDoc
67    {
68       Stub JavaDoc stub = (Stub JavaDoc)client;
69       ORB JavaDoc orb = null;
70       try
71       {
72          orb = stub._orb();
73       }
74       catch (BAD_OPERATION JavaDoc x)
75       {
76          // The stub is not connected to an ORB, go on
77
}
78
79       if (orb == null)
80       {
81          orb = getORB(environment);
82          stub.connect(orb);
83       }
84       return stub;
85    }
86
87 //********************************************************************************************************************//
88
// SERVER METHODS
89

90
91    protected RMIServerImpl JavaDoc createRMIServer(JMXServiceURL JavaDoc url, Map JavaDoc environment) throws IOException JavaDoc
92    {
93       return new RMIIIOPServerImpl JavaDoc(environment);
94    }
95
96    public JMXServiceURL JavaDoc bindServer(Object JavaDoc server, JMXServiceURL JavaDoc url, Map JavaDoc environment) throws IOException JavaDoc
97    {
98       RMIServerImpl JavaDoc rmiServer = (RMIServerImpl JavaDoc)server;
99       Stub JavaDoc stub = (Stub JavaDoc)PortableRemoteObject.toStub(rmiServer);
100       stub.connect(getORB(environment));
101       return super.bindServer(server, url, environment);
102    }
103
104    protected String JavaDoc encodeStub(RMIServerImpl JavaDoc rmiServer, Map JavaDoc environment) throws IOException JavaDoc
105    {
106       Stub JavaDoc stub = (Stub JavaDoc)bindClient(rmiServer.toStub(), environment);
107       String JavaDoc ior = getORB(environment).object_to_string(stub);
108       return IOR_CONTEXT + ior;
109    }
110
111    private ORB JavaDoc checkORB(Map JavaDoc environment)
112    {
113       if (environment == null) return null;
114       Object JavaDoc candidateORB = environment.get(ORB_KEY);
115       if (candidateORB != null)
116       {
117          // Throw as required by the spec
118
if (!(candidateORB instanceof ORB JavaDoc)) throw new IllegalArgumentException JavaDoc("Property " + ORB_KEY + " must specify a " + ORB JavaDoc.class.getName() + ", not " + candidateORB.getClass().getName());
119          return (ORB JavaDoc)candidateORB;
120       }
121       return null;
122    }
123
124    /**
125     * Creates a new ORB, if not already created.
126     * This method is accessed from both client and server.
127     */

128    private synchronized ORB JavaDoc getORB(Map JavaDoc environment)
129    {
130       if (orb == null)
131       {
132          orb = checkORB(environment);
133          if (orb == null)
134          {
135             Properties JavaDoc props = new Properties JavaDoc();
136             // Using putAll() on a Properties is discouraged, since it expects only Strings
137
for (Iterator JavaDoc i = environment.entrySet().iterator(); i.hasNext();)
138             {
139                Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
140                Object JavaDoc key = entry.getKey();
141                Object JavaDoc value = entry.getValue();
142                if (key instanceof String JavaDoc && value instanceof String JavaDoc)
143                {
144                   props.setProperty((String JavaDoc)key, (String JavaDoc)value);
145                }
146             }
147             orb = ORB.init((String JavaDoc[])null, props);
148          }
149       }
150       return orb;
151    }
152
153    protected boolean isEncodedForm(JMXServiceURL JavaDoc url)
154    {
155       String JavaDoc 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 JavaDoc server, JMXServiceURL JavaDoc url, Map JavaDoc environment) throws IOException JavaDoc
161    {
162       if (!isEncodedForm(url)) return;
163       if (orb != null)
164       {
165          orb.shutdown(true);
166          orb.destroy();
167       }
168    }
169 }
170
Popular Tags