Util.java revision 608:7e06bf1dcb09
1/*
2 * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25/*
26 * Licensed Materials - Property of IBM
27 * RMI-IIOP v1.0
28 * Copyright IBM Corp. 1998 1999  All Rights Reserved
29 *
30 */
31
32package javax.rmi.CORBA;
33
34import java.rmi.RemoteException;
35
36import org.omg.CORBA.ORB;
37import org.omg.CORBA.INITIALIZE;
38import org.omg.CORBA.SystemException;
39import org.omg.CORBA.Any;
40import org.omg.CORBA.portable.InputStream;
41import org.omg.CORBA.portable.OutputStream;
42import org.omg.CORBA.portable.ObjectImpl;
43
44import javax.rmi.CORBA.Tie;
45import java.rmi.Remote;
46import java.io.File;
47import java.io.FileInputStream;
48import java.net.MalformedURLException ;
49import java.security.AccessController;
50import java.security.PrivilegedAction;
51import java.util.Properties;
52import java.rmi.server.RMIClassLoader;
53
54import com.sun.corba.se.impl.orbutil.GetPropertyAction;
55
56/**
57 * Provides utility methods that can be used by stubs and ties to
58 * perform common operations.
59 */
60public class Util {
61
62    // This can only be set at static initialization time (no sync necessary).
63    private static final javax.rmi.CORBA.UtilDelegate utilDelegate;
64    private static final String UtilClassKey = "javax.rmi.CORBA.UtilClass";
65
66    static {
67        utilDelegate = (javax.rmi.CORBA.UtilDelegate)createDelegate(UtilClassKey);
68    }
69
70    private Util(){}
71
72    /**
73     * Maps a SystemException to a RemoteException.
74     * @param ex the SystemException to map.
75     * @return the mapped exception.
76     */
77    public static RemoteException mapSystemException(SystemException ex) {
78
79        if (utilDelegate != null) {
80            return utilDelegate.mapSystemException(ex);
81        }
82        return null;
83    }
84
85    /**
86     * Writes any java.lang.Object as a CORBA any.
87     * @param out the stream in which to write the any.
88     * @param obj the object to write as an any.
89     */
90    public static void writeAny(OutputStream out, Object obj) {
91
92        if (utilDelegate != null) {
93            utilDelegate.writeAny(out, obj);
94        }
95    }
96
97    /**
98     * Reads a java.lang.Object as a CORBA any.
99     * @param in the stream from which to read the any.
100     * @return the object read from the stream.
101     */
102    public static Object readAny(InputStream in) {
103
104        if (utilDelegate != null) {
105            return utilDelegate.readAny(in);
106        }
107        return null;
108    }
109
110    /**
111     * Writes a java.lang.Object as a CORBA Object. If <code>obj</code> is
112     * an exported RMI-IIOP server object, the tie is found
113     * and wired to <code>obj</code>, then written to
114<code>out.write_Object(org.omg.CORBA.Object)</code>.
115     * If <code>obj</code> is a CORBA Object, it is written to
116     * <code>out.write_Object(org.omg.CORBA.Object)</code>.
117     * @param out the stream in which to write the object.
118     * @param obj the object to write.
119     */
120    public static void writeRemoteObject(OutputStream out,
121                                         java.lang.Object obj) {
122
123        if (utilDelegate != null) {
124            utilDelegate.writeRemoteObject(out, obj);
125        }
126
127    }
128
129    /**
130     * Writes a java.lang.Object as either a value or a CORBA Object.
131     * If <code>obj</code> is a value object or a stub object, it is written to
132     * <code>out.write_abstract_interface(java.lang.Object)</code>. If <code>obj</code>
133is
134an exported
135     * RMI-IIOP server object, the tie is found and wired to <code>obj</code>,
136     * then written to <code>out.write_abstract_interface(java.lang.Object)</code>.
137     * @param out the stream in which to write the object.
138     * @param obj the object to write.
139     */
140    public static void writeAbstractObject(OutputStream out,
141                                           java.lang.Object obj) {
142
143        if (utilDelegate != null) {
144            utilDelegate.writeAbstractObject(out, obj);
145        }
146    }
147
148    /**
149     * Registers a target for a tie. Adds the tie to an internal table and calls
150     * {@link Tie#setTarget} on the tie object.
151     * @param tie the tie to register.
152     * @param target the target for the tie.
153     */
154    public static void registerTarget(javax.rmi.CORBA.Tie tie,
155                                      java.rmi.Remote target) {
156
157        if (utilDelegate != null) {
158            utilDelegate.registerTarget(tie, target);
159        }
160
161    }
162
163    /**
164     * Removes the associated tie from an internal table and calls {@link
165Tie#deactivate}
166     * to deactivate the object.
167     * @param target the object to unexport.
168     */
169    public static void unexportObject(java.rmi.Remote target)
170        throws java.rmi.NoSuchObjectException
171    {
172
173        if (utilDelegate != null) {
174            utilDelegate.unexportObject(target);
175        }
176
177    }
178
179    /**
180     * Returns the tie (if any) for a given target object.
181     * @return the tie or null if no tie is registered for the given target.
182     */
183    public static Tie getTie (Remote target) {
184
185        if (utilDelegate != null) {
186            return utilDelegate.getTie(target);
187        }
188        return null;
189    }
190
191
192    /**
193     * Returns a singleton instance of a class that implements the
194     * {@link ValueHandler} interface.
195     * @return a class which implements the ValueHandler interface.
196     */
197    public static ValueHandler createValueHandler() {
198
199        if (utilDelegate != null) {
200            return utilDelegate.createValueHandler();
201        }
202        return null;
203    }
204
205    /**
206     * Returns the codebase, if any, for the given class.
207     * @param clz the class to get a codebase for.
208     * @return a space-separated list of URLs, or null.
209     */
210    public static String getCodebase(java.lang.Class clz) {
211        if (utilDelegate != null) {
212            return utilDelegate.getCodebase(clz);
213        }
214        return null;
215    }
216
217    /**
218     * Returns a class instance for the specified class.
219     * <P>The spec for this method is the "Java to IDL language
220     * mapping", ptc/00-01-06.
221     * <P>In Java SE Platform, this method works as follows:
222     * <UL><LI>Find the first non-null <tt>ClassLoader</tt> on the
223     * call stack and attempt to load the class using this
224     * <tt>ClassLoader</tt>.
225     * <LI>If the first step fails, and if <tt>remoteCodebase</tt>
226     * is non-null and
227     * <tt>useCodebaseOnly</tt> is false, then call
228     * <tt>java.rmi.server.RMIClassLoader.loadClass(remoteCodebase, className)</tt>.
229     * <LI>If <tt>remoteCodebase</tt> is null or <tt>useCodebaseOnly</tt>
230     * is true, then call <tt>java.rmi.server.RMIClassLoader.loadClass(className)</tt>.
231     * <LI>If a class was not successfully loaded by step 1, 2, or 3,
232     * and <tt>loader</tt> is non-null, then call <tt>loader.loadClass(className)</tt>.
233     * <LI>If a class was successfully loaded by step 1, 2, 3, or 4, then
234     *  return the loaded class, else throw <tt>ClassNotFoundException</tt>.
235     * @param className the name of the class.
236     * @param remoteCodebase a space-separated list of URLs at which
237     * the class might be found. May be null.
238     * @param loader a <tt>ClassLoader</tt> that may be used to
239     * load the class if all other methods fail.
240     * @return the <code>Class</code> object representing the loaded class.
241     * @exception ClassNotFoundException if class cannot be loaded.
242     */
243    public static Class loadClass(String className,
244                                  String remoteCodebase,
245                                  ClassLoader loader)
246        throws ClassNotFoundException {
247        if (utilDelegate != null) {
248            return utilDelegate.loadClass(className,remoteCodebase,loader);
249        }
250        return null ;
251    }
252
253
254    /**
255     * The <tt>isLocal</tt> method has the same semantics as the
256     * <tt>ObjectImpl._is_local</tt>
257     * method, except that it can throw a <tt>RemoteException</tt>.
258     *
259     * The <tt>_is_local()</tt> method is provided so that stubs may determine if a
260     * particular object is implemented by a local servant and hence local
261     * invocation APIs may be used.
262     *
263     * @param stub the stub to test.
264     *
265     * @return The <tt>_is_local()</tt> method returns true if
266     * the servant incarnating the object is located in the same process as
267     * the stub and they both share the same ORB instance.  The <tt>_is_local()</tt>
268     * method returns false otherwise. The default behavior of <tt>_is_local()</tt> is
269     * to return false.
270     *
271     * @throws RemoteException The Java to IDL specification does not
272     * specify the conditions that cause a <tt>RemoteException</tt> to be thrown.
273     */
274    public static boolean isLocal(Stub stub) throws RemoteException {
275
276        if (utilDelegate != null) {
277            return utilDelegate.isLocal(stub);
278        }
279
280        return false;
281    }
282
283    /**
284     * Wraps an exception thrown by an implementation
285     * method.  It returns the corresponding client-side exception.
286     * @param orig the exception to wrap.
287     * @return the wrapped exception.
288     */
289    public static RemoteException wrapException(Throwable orig) {
290
291        if (utilDelegate != null) {
292            return utilDelegate.wrapException(orig);
293        }
294
295        return null;
296    }
297
298    /**
299     * Copies or connects an array of objects. Used by local stubs
300     * to copy any number of actual parameters, preserving sharing
301     * across parameters as necessary to support RMI semantics.
302     * @param obj the objects to copy or connect.
303     * @param orb the ORB.
304     * @return the copied or connected objects.
305     * @exception RemoteException if any object could not be copied or connected.
306     */
307    public static Object[] copyObjects (Object[] obj, ORB orb)
308        throws RemoteException {
309
310        if (utilDelegate != null) {
311            return utilDelegate.copyObjects(obj, orb);
312        }
313
314        return null;
315    }
316
317    /**
318     * Copies or connects an object. Used by local stubs to copy
319     * an actual parameter, result object, or exception.
320     * @param obj the object to copy.
321     * @param orb the ORB.
322     * @return the copy or connected object.
323     * @exception RemoteException if the object could not be copied or connected.
324     */
325    public static Object copyObject (Object obj, ORB orb)
326        throws RemoteException {
327
328        if (utilDelegate != null) {
329            return utilDelegate.copyObject(obj, orb);
330        }
331        return null;
332    }
333
334    // Same code as in PortableRemoteObject. Can not be shared because they
335    // are in different packages and the visibility needs to be package for
336    // security reasons. If you know a better solution how to share this code
337    // then remove it from PortableRemoteObject. Also in Stub.java
338    private static Object createDelegate(String classKey) {
339        String className = (String)
340            AccessController.doPrivileged(new GetPropertyAction(classKey));
341        if (className == null) {
342            Properties props = getORBPropertiesFile();
343            if (props != null) {
344                className = props.getProperty(classKey);
345            }
346        }
347
348        if (className == null) {
349            return new com.sun.corba.se.impl.javax.rmi.CORBA.Util();
350        }
351
352        try {
353            return loadDelegateClass(className).newInstance();
354        } catch (ClassNotFoundException ex) {
355            INITIALIZE exc = new INITIALIZE( "Cannot instantiate " + className);
356            exc.initCause( ex ) ;
357            throw exc ;
358        } catch (Exception ex) {
359            INITIALIZE exc = new INITIALIZE( "Error while instantiating" + className);
360            exc.initCause( ex ) ;
361            throw exc ;
362        }
363    }
364
365    private static Class loadDelegateClass( String className )  throws ClassNotFoundException
366    {
367        try {
368            ClassLoader loader = Thread.currentThread().getContextClassLoader();
369            return Class.forName(className, false, loader);
370        } catch (ClassNotFoundException e) {
371            // ignore, then try RMIClassLoader
372        }
373
374        try {
375            return RMIClassLoader.loadClass(className);
376        } catch (MalformedURLException e) {
377            String msg = "Could not load " + className + ": " + e.toString();
378            ClassNotFoundException exc = new ClassNotFoundException( msg ) ;
379            throw exc ;
380        }
381    }
382    /**
383     * Load the orb.properties file.
384     */
385    private static Properties getORBPropertiesFile ()
386    {
387        return (Properties) AccessController.doPrivileged(
388            new GetORBPropertiesFileAction());
389    }
390
391}
392