src/share/classes/javax/rmi/CORBA/Util.java

Mon, 11 Jul 2016 16:50:57 +0100

author
coffeys
date
Mon, 11 Jul 2016 16:50:57 +0100
changeset 1377
ec7b9aafd7e1
parent 1294
4821afbaa2a6
child 1410
9c913ea7e4a1
permissions
-rw-r--r--

8160904: Typo in code from 8079718 fix : enableCustomValueHanlde
Reviewed-by: chegar, msheppar

     1 /*
     2  * Copyright (c) 1998, 2016, 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  */
    32 package javax.rmi.CORBA;
    34 import java.rmi.RemoteException;
    36 import org.omg.CORBA.ORB;
    37 import org.omg.CORBA.INITIALIZE;
    38 import org.omg.CORBA.SystemException;
    39 import org.omg.CORBA.Any;
    40 import org.omg.CORBA.portable.InputStream;
    41 import org.omg.CORBA.portable.OutputStream;
    42 import org.omg.CORBA.portable.ObjectImpl;
    44 import javax.rmi.CORBA.Tie;
    45 import java.rmi.Remote;
    46 import java.io.File;
    47 import java.io.FileInputStream;
    48 import java.io.SerializablePermission;
    49 import java.net.MalformedURLException ;
    50 import java.security.AccessController;
    51 import java.security.PrivilegedAction;
    52 import java.util.Properties;
    53 import java.rmi.server.RMIClassLoader;
    55 import com.sun.corba.se.impl.orbutil.GetPropertyAction;
    57 /**
    58  * Provides utility methods that can be used by stubs and ties to
    59  * perform common operations.
    60  */
    61 public class Util {
    63     // This can only be set at static initialization time (no sync necessary).
    64     private static final javax.rmi.CORBA.UtilDelegate utilDelegate;
    65     private static final String UtilClassKey = "javax.rmi.CORBA.UtilClass";
    67     private static final String ALLOW_CREATEVALUEHANDLER_PROP = "jdk.rmi.CORBA.allowCustomValueHandler";
    68     private static boolean allowCustomValueHandler;
    70     static {
    71         utilDelegate = (javax.rmi.CORBA.UtilDelegate)createDelegate(UtilClassKey);
    72         allowCustomValueHandler = readAllowCustomValueHandlerProperty();
    73     }
    75     private static boolean readAllowCustomValueHandlerProperty () {
    76        return AccessController
    77         .doPrivileged(new PrivilegedAction<Boolean>() {
    78             @Override
    79             public Boolean run() {
    80                 return Boolean.getBoolean(ALLOW_CREATEVALUEHANDLER_PROP);
    81             }
    82         });
    83     }
    85     private Util(){}
    87     /**
    88      * Maps a SystemException to a RemoteException.
    89      * @param ex the SystemException to map.
    90      * @return the mapped exception.
    91      */
    92     public static RemoteException mapSystemException(SystemException ex) {
    94         if (utilDelegate != null) {
    95             return utilDelegate.mapSystemException(ex);
    96         }
    97         return null;
    98     }
   100     /**
   101      * Writes any java.lang.Object as a CORBA any.
   102      * @param out the stream in which to write the any.
   103      * @param obj the object to write as an any.
   104      */
   105     public static void writeAny(OutputStream out, Object obj) {
   107         if (utilDelegate != null) {
   108             utilDelegate.writeAny(out, obj);
   109         }
   110     }
   112     /**
   113      * Reads a java.lang.Object as a CORBA any.
   114      * @param in the stream from which to read the any.
   115      * @return the object read from the stream.
   116      */
   117     public static Object readAny(InputStream in) {
   119         if (utilDelegate != null) {
   120             return utilDelegate.readAny(in);
   121         }
   122         return null;
   123     }
   125     /**
   126      * Writes a java.lang.Object as a CORBA Object. If <code>obj</code> is
   127      * an exported RMI-IIOP server object, the tie is found
   128      * and wired to <code>obj</code>, then written to
   129      * <code>out.write_Object(org.omg.CORBA.Object)</code>.
   130      * If <code>obj</code> is a CORBA Object, it is written to
   131      * <code>out.write_Object(org.omg.CORBA.Object)</code>.
   132      * @param out the stream in which to write the object.
   133      * @param obj the object to write.
   134      */
   135     public static void writeRemoteObject(OutputStream out,
   136                                          java.lang.Object obj) {
   138         if (utilDelegate != null) {
   139             utilDelegate.writeRemoteObject(out, obj);
   140         }
   142     }
   144     /**
   145      * Writes a java.lang.Object as either a value or a CORBA Object.
   146      * If <code>obj</code> is a value object or a stub object, it is written to
   147      * <code>out.write_abstract_interface(java.lang.Object)</code>. If <code>obj</code>
   148 is
   149 an exported
   150      * RMI-IIOP server object, the tie is found and wired to <code>obj</code>,
   151      * then written to <code>out.write_abstract_interface(java.lang.Object)</code>.
   152      * @param out the stream in which to write the object.
   153      * @param obj the object to write.
   154      */
   155     public static void writeAbstractObject(OutputStream out,
   156                                            java.lang.Object obj) {
   158         if (utilDelegate != null) {
   159             utilDelegate.writeAbstractObject(out, obj);
   160         }
   161     }
   163     /**
   164      * Registers a target for a tie. Adds the tie to an internal table and calls
   165      * {@link Tie#setTarget} on the tie object.
   166      * @param tie the tie to register.
   167      * @param target the target for the tie.
   168      */
   169     public static void registerTarget(javax.rmi.CORBA.Tie tie,
   170                                       java.rmi.Remote target) {
   172         if (utilDelegate != null) {
   173             utilDelegate.registerTarget(tie, target);
   174         }
   176     }
   178     /**
   179      * Removes the associated tie from an internal table and calls {@link
   180 Tie#deactivate}
   181      * to deactivate the object.
   182      * @param target the object to unexport.
   183      */
   184     public static void unexportObject(java.rmi.Remote target)
   185         throws java.rmi.NoSuchObjectException
   186     {
   188         if (utilDelegate != null) {
   189             utilDelegate.unexportObject(target);
   190         }
   192     }
   194     /**
   195      * Returns the tie (if any) for a given target object.
   196      * @return the tie or null if no tie is registered for the given target.
   197      */
   198     public static Tie getTie (Remote target) {
   200         if (utilDelegate != null) {
   201             return utilDelegate.getTie(target);
   202         }
   203         return null;
   204     }
   207     /**
   208      * Returns a singleton instance of a class that implements the
   209      * {@link ValueHandler} interface.
   210      * @return a class which implements the ValueHandler interface.
   211      */
   212     public static ValueHandler createValueHandler() {
   214         isCustomSerializationPermitted();
   216         if (utilDelegate != null) {
   217             return utilDelegate.createValueHandler();
   218         }
   219         return null;
   220     }
   222     /**
   223      * Returns the codebase, if any, for the given class.
   224      * @param clz the class to get a codebase for.
   225      * @return a space-separated list of URLs, or null.
   226      */
   227     public static String getCodebase(java.lang.Class clz) {
   228         if (utilDelegate != null) {
   229             return utilDelegate.getCodebase(clz);
   230         }
   231         return null;
   232     }
   234     /**
   235      * Returns a class instance for the specified class.
   236      * <P>The spec for this method is the "Java to IDL language
   237      * mapping", ptc/00-01-06.
   238      * <P>In Java SE Platform, this method works as follows:
   239      * <UL><LI>Find the first non-null <tt>ClassLoader</tt> on the
   240      * call stack and attempt to load the class using this
   241      * <tt>ClassLoader</tt>.
   242      * <LI>If the first step fails, and if <tt>remoteCodebase</tt>
   243      * is non-null and
   244      * <tt>useCodebaseOnly</tt> is false, then call
   245      * <tt>java.rmi.server.RMIClassLoader.loadClass(remoteCodebase, className)</tt>.
   246      * <LI>If <tt>remoteCodebase</tt> is null or <tt>useCodebaseOnly</tt>
   247      * is true, then call <tt>java.rmi.server.RMIClassLoader.loadClass(className)</tt>.
   248      * <LI>If a class was not successfully loaded by step 1, 2, or 3,
   249      * and <tt>loader</tt> is non-null, then call <tt>loader.loadClass(className)</tt>.
   250      * <LI>If a class was successfully loaded by step 1, 2, 3, or 4, then
   251      *  return the loaded class, else throw <tt>ClassNotFoundException</tt>.
   252      * @param className the name of the class.
   253      * @param remoteCodebase a space-separated list of URLs at which
   254      * the class might be found. May be null.
   255      * @param loader a <tt>ClassLoader</tt> that may be used to
   256      * load the class if all other methods fail.
   257      * @return the <code>Class</code> object representing the loaded class.
   258      * @exception ClassNotFoundException if class cannot be loaded.
   259      */
   260     public static Class loadClass(String className,
   261                                   String remoteCodebase,
   262                                   ClassLoader loader)
   263         throws ClassNotFoundException {
   264         if (utilDelegate != null) {
   265             return utilDelegate.loadClass(className,remoteCodebase,loader);
   266         }
   267         return null ;
   268     }
   271     /**
   272      * The <tt>isLocal</tt> method has the same semantics as the
   273      * <tt>ObjectImpl._is_local</tt>
   274      * method, except that it can throw a <tt>RemoteException</tt>.
   275      *
   276      * The <tt>_is_local()</tt> method is provided so that stubs may determine if a
   277      * particular object is implemented by a local servant and hence local
   278      * invocation APIs may be used.
   279      *
   280      * @param stub the stub to test.
   281      *
   282      * @return The <tt>_is_local()</tt> method returns true if
   283      * the servant incarnating the object is located in the same process as
   284      * the stub and they both share the same ORB instance.  The <tt>_is_local()</tt>
   285      * method returns false otherwise. The default behavior of <tt>_is_local()</tt> is
   286      * to return false.
   287      *
   288      * @throws RemoteException The Java to IDL specification does not
   289      * specify the conditions that cause a <tt>RemoteException</tt> to be thrown.
   290      */
   291     public static boolean isLocal(Stub stub) throws RemoteException {
   293         if (utilDelegate != null) {
   294             return utilDelegate.isLocal(stub);
   295         }
   297         return false;
   298     }
   300     /**
   301      * Wraps an exception thrown by an implementation
   302      * method.  It returns the corresponding client-side exception.
   303      * @param orig the exception to wrap.
   304      * @return the wrapped exception.
   305      */
   306     public static RemoteException wrapException(Throwable orig) {
   308         if (utilDelegate != null) {
   309             return utilDelegate.wrapException(orig);
   310         }
   312         return null;
   313     }
   315     /**
   316      * Copies or connects an array of objects. Used by local stubs
   317      * to copy any number of actual parameters, preserving sharing
   318      * across parameters as necessary to support RMI semantics.
   319      * @param obj the objects to copy or connect.
   320      * @param orb the ORB.
   321      * @return the copied or connected objects.
   322      * @exception RemoteException if any object could not be copied or connected.
   323      */
   324     public static Object[] copyObjects (Object[] obj, ORB orb)
   325         throws RemoteException {
   327         if (utilDelegate != null) {
   328             return utilDelegate.copyObjects(obj, orb);
   329         }
   331         return null;
   332     }
   334     /**
   335      * Copies or connects an object. Used by local stubs to copy
   336      * an actual parameter, result object, or exception.
   337      * @param obj the object to copy.
   338      * @param orb the ORB.
   339      * @return the copy or connected object.
   340      * @exception RemoteException if the object could not be copied or connected.
   341      */
   342     public static Object copyObject (Object obj, ORB orb)
   343         throws RemoteException {
   345         if (utilDelegate != null) {
   346             return utilDelegate.copyObject(obj, orb);
   347         }
   348         return null;
   349     }
   351     // Same code as in PortableRemoteObject. Can not be shared because they
   352     // are in different packages and the visibility needs to be package for
   353     // security reasons. If you know a better solution how to share this code
   354     // then remove it from PortableRemoteObject. Also in Stub.java
   355     private static Object createDelegate(String classKey) {
   357         String className = (String)
   358             AccessController.doPrivileged(new GetPropertyAction(classKey));
   359         if (className == null) {
   360             Properties props = getORBPropertiesFile();
   361             if (props != null) {
   362                 className = props.getProperty(classKey);
   363             }
   364         }
   366         if (className == null) {
   367             return new com.sun.corba.se.impl.javax.rmi.CORBA.Util();
   368         }
   370         try {
   371             return loadDelegateClass(className).newInstance();
   372         } catch (ClassNotFoundException ex) {
   373             INITIALIZE exc = new INITIALIZE( "Cannot instantiate " + className);
   374             exc.initCause( ex ) ;
   375             throw exc ;
   376         } catch (Exception ex) {
   377             INITIALIZE exc = new INITIALIZE( "Error while instantiating" + className);
   378             exc.initCause( ex ) ;
   379             throw exc ;
   380         }
   381     }
   383     private static Class loadDelegateClass( String className )  throws ClassNotFoundException
   384     {
   385         try {
   386             ClassLoader loader = Thread.currentThread().getContextClassLoader();
   387             return Class.forName(className, false, loader);
   388         } catch (ClassNotFoundException e) {
   389             // ignore, then try RMIClassLoader
   390         }
   392         try {
   393             return RMIClassLoader.loadClass(className);
   394         } catch (MalformedURLException e) {
   395             String msg = "Could not load " + className + ": " + e.toString();
   396             ClassNotFoundException exc = new ClassNotFoundException( msg ) ;
   397             throw exc ;
   398         }
   399     }
   400     /**
   401      * Load the orb.properties file.
   402      */
   403     private static Properties getORBPropertiesFile ()
   404     {
   405         return (Properties) AccessController.doPrivileged(
   406             new GetORBPropertiesFileAction());
   407     }
   409     private static void isCustomSerializationPermitted() {
   410         SecurityManager sm = System.getSecurityManager();
   411         if (!allowCustomValueHandler) {
   412             if ( sm != null) {
   413                 // check that a serialization permission has been
   414                 // set to allow the loading of the Util delegate
   415                 // which provides access to custom ValueHandler
   416                 sm.checkPermission(new SerializablePermission(
   417                         "enableCustomValueHandler"));
   418             }
   419         }
   420     }
   421 }

mercurial