KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (C) MX4J.
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.Map JavaDoc;
14 import java.util.Properties JavaDoc;
15
16 import javax.management.remote.JMXServiceURL JavaDoc;
17 import javax.management.remote.rmi.RMIIIOPServerImpl JavaDoc;
18 import javax.management.remote.rmi.RMIServer JavaDoc;
19 import javax.management.remote.rmi.RMIServerImpl JavaDoc;
20 import javax.rmi.CORBA.Stub JavaDoc;
21 import javax.rmi.PortableRemoteObject JavaDoc;
22
23 import mx4j.remote.resolver.rmi.RMIResolver;
24 import org.omg.CORBA.ORB JavaDoc;
25 import org.omg.CORBA.BAD_OPERATION JavaDoc;
26
27 /**
28  *
29  * @author <a HREF="mailto:biorn_steedom@users.sourceforge.net">Simone Bordet</a>
30  * @version $Revision: 1.8 $
31  */

32 public class IIOPResolver extends RMIResolver
33 {
34    private static final String JavaDoc IOR_CONTEXT = "/ior/";
35
36    private ORB JavaDoc orb;
37
38
39 //********************************************************************************************************************//
40
// CLIENT METHODS
41

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

86
87    protected RMIServerImpl JavaDoc createRMIServer(JMXServiceURL JavaDoc url, Map JavaDoc environment) throws IOException JavaDoc
88    {
89       return new RMIIIOPServerImpl JavaDoc(environment);
90    }
91
92    protected String JavaDoc encodeStub(RMIServerImpl JavaDoc rmiServer, Map JavaDoc environment) throws IOException JavaDoc
93    {
94       Stub JavaDoc stub = (Stub JavaDoc)bindClient(rmiServer.toStub(), environment);
95       String JavaDoc ior = orb.object_to_string(stub);
96       return IOR_CONTEXT + ior;
97    }
98
99    /**
100     * Creates a new ORB, if not already created.
101     * This method is accessed from both client and server.
102     */

103    private synchronized ORB JavaDoc getORB(Map JavaDoc environment)
104    {
105       if (orb == null)
106       {
107          Properties JavaDoc props = new Properties JavaDoc();
108          props.putAll(environment);
109          orb = ORB.init((String JavaDoc[])null, props);
110       }
111       return orb;
112    }
113
114    protected boolean isEncodedForm(JMXServiceURL JavaDoc url)
115    {
116       String JavaDoc 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 JavaDoc server, Map JavaDoc environment) throws IOException JavaDoc
122    {
123       if (orb != null)
124       {
125          orb.shutdown(true);
126          orb.destroy();
127       }
128    }
129 }
130
Popular Tags