KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > rmi > CORBA > Util


1 /*
2  * @(#)Util.java 1.45 04/06/21
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 /*
8  * Licensed Materials - Property of IBM
9  * RMI-IIOP v1.0
10  * Copyright IBM Corp. 1998 1999 All Rights Reserved
11  *
12  * US Government Users Restricted Rights - Use, duplication or
13  * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
14  */

15
16 package javax.rmi.CORBA;
17
18 import java.rmi.RemoteException JavaDoc;
19
20 import org.omg.CORBA.ORB JavaDoc;
21 import org.omg.CORBA.INITIALIZE JavaDoc;
22 import org.omg.CORBA.SystemException JavaDoc;
23 import org.omg.CORBA.Any JavaDoc;
24 import org.omg.CORBA.portable.InputStream JavaDoc;
25 import org.omg.CORBA.portable.OutputStream JavaDoc;
26 import org.omg.CORBA.portable.ObjectImpl JavaDoc;
27
28 import javax.rmi.CORBA.Tie JavaDoc;
29 import java.rmi.Remote JavaDoc;
30 import java.io.File JavaDoc;
31 import java.io.FileInputStream JavaDoc;
32 import java.net.MalformedURLException JavaDoc ;
33 import java.security.AccessController JavaDoc;
34 import java.security.PrivilegedAction JavaDoc;
35 import java.util.Properties JavaDoc;
36 import java.rmi.server.RMIClassLoader JavaDoc;
37
38 import com.sun.corba.se.impl.orbutil.GetPropertyAction;
39
40 /**
41  * Provides utility methods that can be used by stubs and ties to
42  * perform common operations.
43  */

44 public class Util {
45
46     // This can only be set at static initialization time (no sync necessary).
47
private static javax.rmi.CORBA.UtilDelegate JavaDoc utilDelegate = null;
48     private static final String JavaDoc UtilClassKey = "javax.rmi.CORBA.UtilClass";
49     private static final String JavaDoc defaultUtilImplName =
50 "com.sun.corba.se.impl.javax.rmi.CORBA.Util";
51
52     static {
53     utilDelegate = (javax.rmi.CORBA.UtilDelegate JavaDoc)
54         createDelegateIfSpecified(UtilClassKey, defaultUtilImplName);
55     }
56
57     private Util(){}
58
59     /**
60      * Maps a SystemException to a RemoteException.
61      * @param ex the SystemException to map.
62      * @return the mapped exception.
63      */

64     public static RemoteException JavaDoc mapSystemException(SystemException JavaDoc ex) {
65     
66     if (utilDelegate != null) {
67         return utilDelegate.mapSystemException(ex);
68     }
69     return null;
70     }
71
72     /**
73      * Writes any java.lang.Object as a CORBA any.
74      * @param out the stream in which to write the any.
75      * @param obj the object to write as an any.
76      */

77     public static void writeAny(OutputStream JavaDoc out, Object JavaDoc obj) {
78
79     if (utilDelegate != null) {
80         utilDelegate.writeAny(out, obj);
81     }
82     }
83
84     /**
85      * Reads a java.lang.Object as a CORBA any.
86      * @param in the stream from which to read the any.
87      * @return the object read from the stream.
88      */

89     public static Object JavaDoc readAny(InputStream JavaDoc in) {
90
91     if (utilDelegate != null) {
92         return utilDelegate.readAny(in);
93     }
94     return null;
95     }
96
97     /**
98      * Writes a java.lang.Object as a CORBA Object. If <code>obj</code> is
99      * an exported RMI-IIOP server object, the tie is found
100      * and wired to <code>obj</code>, then written to
101 <code>out.write_Object(org.omg.CORBA.Object)</code>.
102      * If <code>obj</code> is a CORBA Object, it is written to
103      * <code>out.write_Object(org.omg.CORBA.Object)</code>.
104      * @param out the stream in which to write the object.
105      * @param obj the object to write.
106      */

107     public static void writeRemoteObject(OutputStream JavaDoc out,
108                                          java.lang.Object JavaDoc obj) {
109                                                 
110     if (utilDelegate != null) {
111         utilDelegate.writeRemoteObject(out, obj);
112     }
113
114     }
115     
116     /**
117      * Writes a java.lang.Object as either a value or a CORBA Object.
118      * If <code>obj</code> is a value object or a stub object, it is written to
119      * <code>out.write_abstract_interface(java.lang.Object)</code>. If <code>obj</code>
120 is
121 an exported
122      * RMI-IIOP server object, the tie is found and wired to <code>obj</code>,
123      * then written to <code>out.write_abstract_interface(java.lang.Object)</code>.
124      * @param out the stream in which to write the object.
125      * @param obj the object to write.
126      */

127     public static void writeAbstractObject(OutputStream JavaDoc out,
128                                            java.lang.Object JavaDoc obj) {
129                                                 
130     if (utilDelegate != null) {
131         utilDelegate.writeAbstractObject(out, obj);
132     }
133     }
134     
135     /**
136      * Registers a target for a tie. Adds the tie to an internal table and calls
137      * {@link Tie#setTarget} on the tie object.
138      * @param tie the tie to register.
139      * @param target the target for the tie.
140      */

141     public static void registerTarget(javax.rmi.CORBA.Tie JavaDoc tie,
142                                       java.rmi.Remote JavaDoc target) {
143         
144     if (utilDelegate != null) {
145         utilDelegate.registerTarget(tie, target);
146     }
147
148     }
149     
150     /**
151      * Removes the associated tie from an internal table and calls {@link
152 Tie#deactivate}
153      * to deactivate the object.
154      * @param target the object to unexport.
155      */

156     public static void unexportObject(java.rmi.Remote JavaDoc target)
157         throws java.rmi.NoSuchObjectException JavaDoc
158     {
159
160     if (utilDelegate != null) {
161         utilDelegate.unexportObject(target);
162     }
163
164     }
165     
166     /**
167      * Returns the tie (if any) for a given target object.
168      * @return the tie or null if no tie is registered for the given target.
169      */

170     public static Tie JavaDoc getTie (Remote JavaDoc target) {
171
172     if (utilDelegate != null) {
173         return utilDelegate.getTie(target);
174     }
175     return null;
176     }
177
178
179     /**
180      * Returns a singleton instance of a class that implements the
181      * {@link ValueHandler} interface.
182      * @return a class which implements the ValueHandler interface.
183      */

184     public static ValueHandler JavaDoc createValueHandler() {
185
186     if (utilDelegate != null) {
187         return utilDelegate.createValueHandler();
188     }
189     return null;
190     }
191
192     /**
193      * Returns the codebase, if any, for the given class.
194      * @param clz the class to get a codebase for.
195      * @return a space-separated list of URLs, or null.
196      */

197     public static String JavaDoc getCodebase(java.lang.Class JavaDoc clz) {
198     if (utilDelegate != null) {
199         return utilDelegate.getCodebase(clz);
200     }
201     return null;
202     }
203
204     /**
205      * Returns a class instance for the specified class.
206      * <P>The spec for this method is the "Java to IDL language
207      * mapping", ptc/00-01-06.
208      * <P>In Java 2 Platform, this method works as follows:
209      * <UL><LI>Find the first non-null <tt>ClassLoader</tt> on the
210      * call stack and attempt to load the class using this
211      * <tt>ClassLoader</tt>.
212      * <LI>If the first step fails, and if <tt>remoteCodebase</tt>
213      * is non-null and
214      * <tt>useCodebaseOnly</tt> is false, then call
215      * <tt>java.rmi.server.RMIClassLoader.loadClass(remoteCodebase, className)</tt>.
216      * <LI>If <tt>remoteCodebase</tt> is null or <tt>useCodebaseOnly</tt>
217      * is true, then call <tt>java.rmi.server.RMIClassLoader.loadClass(className)</tt>.
218      * <LI>If a class was not successfully loaded by step 1, 2, or 3,
219      * and <tt>loader</tt> is non-null, then call <tt>loader.loadClass(className)</tt>.
220      * <LI>If a class was successfully loaded by step 1, 2, 3, or 4, then
221      * return the loaded class, else throw <tt>ClassNotFoundException</tt>.
222      * @param className the name of the class.
223      * @param remoteCodebase a space-separated list of URLs at which
224      * the class might be found. May be null.
225      * @param loader a <tt>ClassLoader</tt> that may be used to
226      * load the class if all other methods fail.
227      * @return the <code>Class</code> object representing the loaded class.
228      * @exception ClassNotFoundException if class cannot be loaded.
229      */

230     public static Class JavaDoc loadClass(String JavaDoc className,
231                                   String JavaDoc remoteCodebase,
232                                   ClassLoader JavaDoc loader)
233     throws ClassNotFoundException JavaDoc {
234     if (utilDelegate != null) {
235         return utilDelegate.loadClass(className,remoteCodebase,loader);
236     }
237     return null ;
238     }
239
240
241     /**
242      * The <tt>isLocal</tt> method has the same semantics as the
243      * <tt>ObjectImpl._is_local</tt>
244      * method, except that it can throw a <tt>RemoteException</tt>.
245      *
246      * The <tt>_is_local()</tt> method is provided so that stubs may determine if a
247      * particular object is implemented by a local servant and hence local
248      * invocation APIs may be used.
249      *
250      * @param stub the stub to test.
251      *
252      * @return The <tt>_is_local()</tt> method returns true if
253      * the servant incarnating the object is located in the same process as
254      * the stub and they both share the same ORB instance. The <tt>_is_local()</tt>
255      * method returns false otherwise. The default behavior of <tt>_is_local()</tt> is
256      * to return false.
257      *
258      * @throws RemoteException The Java to IDL specification does not
259      * specify the conditions that cause a <tt>RemoteException</tt> to be thrown.
260      */

261     public static boolean isLocal(Stub JavaDoc stub) throws RemoteException JavaDoc {
262
263     if (utilDelegate != null) {
264         return utilDelegate.isLocal(stub);
265     }
266
267     return false;
268     }
269     
270     /**
271      * Wraps an exception thrown by an implementation
272      * method. It returns the corresponding client-side exception.
273      * @param orig the exception to wrap.
274      * @return the wrapped exception.
275      */

276     public static RemoteException JavaDoc wrapException(Throwable JavaDoc orig) {
277
278     if (utilDelegate != null) {
279         return utilDelegate.wrapException(orig);
280     }
281
282         return null;
283     }
284
285     /**
286      * Copies or connects an array of objects. Used by local stubs
287      * to copy any number of actual parameters, preserving sharing
288      * across parameters as necessary to support RMI semantics.
289      * @param obj the objects to copy or connect.
290      * @param orb the ORB.
291      * @return the copied or connected objects.
292      * @exception RemoteException if any object could not be copied or connected.
293      */

294     public static Object JavaDoc[] copyObjects (Object JavaDoc[] obj, ORB JavaDoc orb)
295     throws RemoteException JavaDoc {
296     
297     if (utilDelegate != null) {
298         return utilDelegate.copyObjects(obj, orb);
299     }
300     
301         return null;
302     }
303
304     /**
305      * Copies or connects an object. Used by local stubs to copy
306      * an actual parameter, result object, or exception.
307      * @param obj the object to copy.
308      * @param orb the ORB.
309      * @return the copy or connected object.
310      * @exception RemoteException if the object could not be copied or connected.
311      */

312     public static Object JavaDoc copyObject (Object JavaDoc obj, ORB JavaDoc orb)
313     throws RemoteException JavaDoc {
314
315     if (utilDelegate != null) {
316         return utilDelegate.copyObject(obj, orb);
317     }
318     return null;
319     }
320
321     // Same code as in PortableRemoteObject. Can not be shared because they
322
// are in different packages and the visibility needs to be package for
323
// security reasons. If you know a better solution how to share this code
324
// then remove it from PortableRemoteObject. Also in Stub.java
325
private static Object JavaDoc createDelegateIfSpecified(String JavaDoc classKey,
326     String JavaDoc defaultClassName)
327     {
328         String JavaDoc className = (String JavaDoc)
329             AccessController.doPrivileged(new GetPropertyAction(classKey));
330         if (className == null) {
331             Properties JavaDoc props = getORBPropertiesFile();
332             if (props != null) {
333                 className = props.getProperty(classKey);
334             }
335         }
336
337     if (className == null) {
338         className = defaultClassName;
339     }
340
341         try {
342             return loadDelegateClass(className).newInstance();
343         } catch (ClassNotFoundException JavaDoc ex) {
344         INITIALIZE JavaDoc exc = new INITIALIZE JavaDoc( "Cannot instantiate " + className);
345         exc.initCause( ex ) ;
346         throw exc ;
347         } catch (Exception JavaDoc ex) {
348         INITIALIZE JavaDoc exc = new INITIALIZE JavaDoc( "Error while instantiating" + className);
349         exc.initCause( ex ) ;
350         throw exc ;
351         }
352     }
353
354     private static Class JavaDoc loadDelegateClass( String JavaDoc className ) throws ClassNotFoundException JavaDoc
355     {
356     try {
357         ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
358         return Class.forName(className, false, loader);
359     } catch (ClassNotFoundException JavaDoc e) {
360         // ignore, then try RMIClassLoader
361
}
362
363     try {
364         return RMIClassLoader.loadClass(className);
365     } catch (MalformedURLException JavaDoc e) {
366         String JavaDoc msg = "Could not load " + className + ": " + e.toString();
367         ClassNotFoundException JavaDoc exc = new ClassNotFoundException JavaDoc( msg ) ;
368         throw exc ;
369     }
370     }
371     /**
372      * Load the orb.properties file.
373      */

374     private static Properties JavaDoc getORBPropertiesFile ()
375     {
376         return (Properties JavaDoc) AccessController.doPrivileged(
377         new GetORBPropertiesFileAction JavaDoc());
378     }
379
380 }
381
Popular Tags