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

Save This Page
Home » openjdk-7 » javax » rmi » CORBA » [javadoc | source]