KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > cmi > LowerOrb


1 /*
2  * Copyright (C) 2002-2004, Simon Nieuviarts
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  */

19 package org.objectweb.carol.cmi;
20
21 import java.lang.reflect.Constructor JavaDoc;
22 import java.lang.reflect.InvocationTargetException JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24 import java.rmi.NoSuchObjectException JavaDoc;
25 import java.rmi.Remote JavaDoc;
26 import java.rmi.RemoteException JavaDoc;
27 import java.rmi.server.ObjID JavaDoc;
28 import java.rmi.server.RemoteRef JavaDoc;
29 import java.rmi.server.RemoteStub JavaDoc;
30
31 import javax.rmi.CORBA.PortableRemoteObjectDelegate JavaDoc;
32
33 import org.objectweb.carol.rmi.multi.JrmpPRODelegate;
34
35 class SunLowerOrb {
36     private static Class JavaDoc liveref;
37     private static Constructor JavaDoc liveref_cons;
38     private static Class JavaDoc usref;
39     private static Constructor JavaDoc usref_cons;
40     private static Method JavaDoc usref_export;
41     private static Class JavaDoc tcpep;
42     private static Constructor JavaDoc tcpep_cons;
43     private static Constructor JavaDoc liveref_cons2;
44     private static Class JavaDoc uref;
45     private static Constructor JavaDoc uref_cons;
46     private static boolean init = false;
47
48     static {
49         try {
50             liveref = Class.forName("sun.rmi.transport.LiveRef");
51             Class JavaDoc[] p0 = { ObjID JavaDoc.class, int.class };
52             liveref_cons = liveref.getConstructor(p0);
53             usref = Class.forName("sun.rmi.server.UnicastServerRef");
54             Class JavaDoc[] p1 = { liveref };
55             usref_cons = usref.getConstructor(p1);
56             Class JavaDoc[] p2 = { Remote JavaDoc.class, Object JavaDoc.class, boolean.class };
57             usref_export = usref.getMethod("exportObject", p2);
58             tcpep = Class.forName("sun.rmi.transport.tcp.TCPEndpoint");
59             Class JavaDoc[] p3 = { String JavaDoc.class, int.class };
60             tcpep_cons = tcpep.getConstructor(p3);
61             Class JavaDoc ep = Class.forName("sun.rmi.transport.Endpoint");
62             Class JavaDoc[] p4 = { ObjID JavaDoc.class, ep, boolean.class };
63             liveref_cons2 = liveref.getConstructor(p4);
64             uref = Class.forName("sun.rmi.server.UnicastRef");
65             Class JavaDoc[] p5 = { liveref };
66             uref_cons = uref.getConstructor(p5);
67             init = true;
68         } catch (ClassNotFoundException JavaDoc e) {
69             // Init failed
70
} catch (SecurityException JavaDoc e) {
71             // Init failed
72
} catch (NoSuchMethodException JavaDoc e) {
73             // Init failed
74
}
75     }
76
77     public static boolean isValid() {
78         return init;
79     }
80
81     public static Remote JavaDoc export(Remote JavaDoc obj, int port, ObjID JavaDoc id) throws IllegalArgumentException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
82         Object JavaDoc[] p0 = { id, new Integer JavaDoc(port) };
83         Object JavaDoc lr = liveref_cons.newInstance(p0);
84         Object JavaDoc[] p1 = { lr };
85         Object JavaDoc usr = usref_cons.newInstance(p1);
86         Object JavaDoc[] p2 = { obj, null, new Boolean JavaDoc(true) };
87         Object JavaDoc ret = usref_export.invoke(usr, p2);
88         return (Remote JavaDoc)ret;
89     }
90
91     public static RemoteRef JavaDoc getRemoteRef(String JavaDoc host, int port, ObjID JavaDoc id) throws IllegalArgumentException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
92         Object JavaDoc[] p0 = { host, new Integer JavaDoc(port) };
93         Object JavaDoc ep = tcpep_cons.newInstance(p0);
94         Object JavaDoc[] p1 = { id, ep, new Boolean JavaDoc(false) };
95         Object JavaDoc ref = liveref_cons2.newInstance(p1);
96         Object JavaDoc[] p2 = { ref };
97         Object JavaDoc rr = uref_cons.newInstance(p2);
98         return (RemoteRef JavaDoc)rr;
99     }
100 }
101
102
103 class GcjLowerOrb {
104     private static boolean init = false;
105
106     static {
107         try {
108             //init = true;
109
} catch (SecurityException JavaDoc e) {
110             // Init failed
111
}
112     }
113
114     public static boolean isValid() {
115         return init;
116     }
117
118     public static Remote JavaDoc export(Remote JavaDoc obj, int port, ObjID JavaDoc id) throws IllegalArgumentException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
119         return null;
120     }
121
122     public static RemoteRef JavaDoc getRemoteRef(String JavaDoc host, int port, ObjID JavaDoc id) throws IllegalArgumentException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
123         return null;
124     }
125 }
126
127
128 public class LowerOrb {
129     private static String JavaDoc prefix = "rmi:";
130     public static final int DEFAULT_CREG_PORT = 1099;
131     public static final int REG_ID = 0xC2C91901;
132     private static ObjID JavaDoc id = new ObjID JavaDoc(REG_ID);
133     private static PortableRemoteObjectDelegate JavaDoc rmi = new JrmpPRODelegate(true);
134
135     public static Remote JavaDoc toStub(Remote JavaDoc obj) throws NoSuchObjectException JavaDoc {
136         return rmi.toStub(obj);
137     }
138
139     public static void exportObject(Remote JavaDoc obj) throws RemoteException JavaDoc {
140         rmi.exportObject(obj);
141     }
142
143     public static void unexportObject(Remote JavaDoc obj) throws NoSuchObjectException JavaDoc {
144         rmi.unexportObject(obj);
145     }
146
147     public static PortableRemoteObjectDelegate JavaDoc getPRODelegate() {
148         return rmi;
149     }
150
151     public static Remote JavaDoc exportRegistry(Remote JavaDoc obj, int port)
152         throws RemoteException JavaDoc {
153         /*
154         LiveRef lref = new LiveRef(id, port);
155         UnicastServerRef uref = new UnicastServerRef(lref);
156         return uref.exportObject(obj, null, true);
157         */

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 JavaDoc("Don't know how to export registry : ORB specific");
165             }
166         } catch (InvocationTargetException JavaDoc e) {
167             Throwable JavaDoc t = e.getTargetException();
168             if (t instanceof RemoteException JavaDoc) {
169                 throw (RemoteException JavaDoc)t;
170             } else {
171                 throw new RemoteException JavaDoc("Unexpected exception", t);
172             }
173         } catch (RemoteException JavaDoc e) {
174             throw e;
175         } catch (Exception JavaDoc e) {
176             throw new RemoteException JavaDoc("Unexpected exception", e);
177         }
178     }
179
180     private static Class JavaDoc[] stubConsParamTypes = { RemoteRef JavaDoc.class };
181
182     public static Remote JavaDoc getRegistryStub(
183         String JavaDoc className,
184         String JavaDoc host,
185         int port)
186         throws RemoteException JavaDoc {
187         if (port <= 0)
188             throw new RemoteException JavaDoc("Invalid port no " + port);
189         RemoteRef JavaDoc rr;
190         try {
191             /*
192             Endpoint ep = new TCPEndpoint(host, port);
193             LiveRef ref = new LiveRef(id, ep, false);
194             RemoteRef rr = new UnicastRef(ref);
195             */

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 JavaDoc("Don't know how to build a stub : ORB specific");
202             }
203             Class JavaDoc stubcl = Class.forName(className + "_Stub");
204             Object JavaDoc[] p0 = { rr };
205             Constructor JavaDoc cons = stubcl.getConstructor(stubConsParamTypes);
206             return (RemoteStub JavaDoc) cons.newInstance(p0);
207         } catch (InvocationTargetException JavaDoc e) {
208             Throwable JavaDoc t = e.getTargetException();
209             if (t instanceof RemoteException JavaDoc) {
210                 throw (RemoteException JavaDoc)t;
211             } else {
212                 throw new RemoteException JavaDoc("Unexpected exception", t);
213             }
214         } catch (RemoteException JavaDoc e) {
215             throw e;
216         } catch (Exception JavaDoc e) {
217             throw new RemoteException JavaDoc("Unexpected exception", e);
218         }
219     }
220 }
221
Popular Tags