rev 10321 : imported patch cosnamingApplet

   1 /*
   2  * Copyright (c) 1999, 2014, 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 package com.sun.jndi.toolkit.corba;
  27 
  28 // Needed for RMI/IIOP
  29 import java.rmi.Remote;
  30 
  31 import java.lang.reflect.Method;
  32 import java.lang.reflect.InvocationTargetException;
  33 import java.rmi.RemoteException;
  34 import java.util.Hashtable;
  35 import java.util.Properties;
  36 import java.util.Enumeration;

  37 
  38 import org.omg.CORBA.ORB;
  39 
  40 import javax.naming.Context;
  41 import javax.naming.ConfigurationException;
  42 import javax.rmi.CORBA.Stub;
  43 import javax.rmi.PortableRemoteObject;
  44 
  45 /**
  46   * Contains utilities for performing CORBA-related tasks:
  47   * 1. Get the org.omg.CORBA.Object for a java.rmi.Remote object.
  48   * 2. Create an ORB to use for a given host/port, and environment properties.
  49   *
  50   * @author Simon Nash
  51   * @author Bryan Atsatt
  52   */
  53 
  54 public class CorbaUtils {
  55     /**
  56       * Returns the CORBA object reference associated with a Remote
  57       * object by using the javax.rmi.CORBA package.
  58       *<p>
  59       * Use reflection to avoid hard dependencies on javax.rmi.CORBA package.
  60       * This method effective does the following:
  61       *<blockquote><pre>
  62       * java.lang.Object stub;
  63       * try {
  64       *     stub = PortableRemoteObject.toStub(remoteObj);
  65       * } catch (Exception e) {
  66       *     throw new ConfigurationException("Object not exported or not found");
  67       * }
  68       * if (!(stub instanceof javax.rmi.CORBA.Stub)) {
  69       *     return null; // JRMP impl or JRMP stub
  70       * }
  71       * try {
  72       *     ((javax.rmi.CORBA.Stub)stub).connect(orb);  // try to connect IIOP stub
  73       * } catch (RemoteException e) {
  74       *     // ignore 'already connected' error
  75       * }
  76       * return (javax.rmi.CORBA.Stub)stub;
  77       *
  78       * @param remoteObj The non-null remote object for
  79       * @param orb       The non-null ORB to connect the remote object to
  80       * @return The CORBA Object for remoteObj; null if <tt>remoteObj</tt>
  81       *                 is a JRMP implementation or JRMP stub.
  82       * @exception ConfigurationException The CORBA Object cannot be obtained
  83       *         because of configuration problems.
  84       */
  85     public static org.omg.CORBA.Object remoteToCorba(Remote remoteObj, ORB orb)
  86         throws ConfigurationException {
  87 
  88 // First, get remoteObj's stub
  89 
  90             // javax.rmi.CORBA.Stub stub = PortableRemoteObject.toStub(remoteObj);
  91 
  92             Remote stub;
  93 
  94             try {
  95                 stub = PortableRemoteObject.toStub(remoteObj);
  96             } catch (Throwable t) {
  97                 ConfigurationException ce = new ConfigurationException(
  98     "Problem with PortableRemoteObject.toStub(); object not exported or stub not found");
  99                 ce.setRootCause(t);
 100                 throw ce;
 101             }
 102 
 103 // Next, make sure that the stub is javax.rmi.CORBA.Stub
 104 
 105             if (!(stub instanceof Stub)) {
 106                 return null;  // JRMP implementation or JRMP stub
 107             }
 108 
 109 // Next, make sure that the stub is connected
 110             try {
 111                 ((Stub) stub).connect(orb);
 112             } catch (RemoteException e) {
 113                 // ignore RemoteException because stub might have already
 114                 // been connected
 115             } catch (Throwable t) {
 116                 ConfigurationException ce = new ConfigurationException(
 117                         "Problem invoking javax.rmi.CORBA.Stub.connect()");
 118                 ce.setRootCause(t);
 119                 throw ce;
 120             }
 121 // Finally, return stub
 122             return (org.omg.CORBA.Object)stub;
 123     }
 124 
 125     /**
 126      * Get ORB using given server and port number, and properties from environment.
 127      *
 128      * @param server Possibly null server; if null means use default;
 129      *               For applet, it is the applet host; for app, it is localhost.
 130      * @param port   Port number, -1 means default port
 131      * @param env    Possibly null environment. Contains environment properties.
 132      *               Could contain ORB itself; or applet used for initializing ORB.
 133      *               Use all String properties from env for initializing ORB
 134      * @return A non-null ORB.
 135      */
 136     public static ORB getOrb(String server, int port, Hashtable<?,?> env) {
 137         // See if we can get info from environment
 138         Properties orbProp;
 139 
 140         // Extract any org.omg.CORBA properties from environment
 141         if (env != null) {
 142             if (env instanceof Properties) {
 143                 // Already a Properties, just clone
 144                 orbProp = (Properties) env.clone();
 145             } else {
 146                 // Get all String properties
 147                 Enumeration<?> envProp;
 148                 orbProp = new Properties();
 149                 for (envProp = env.keys(); envProp.hasMoreElements();) {
 150                     String key = (String)envProp.nextElement();
 151                     Object val = env.get(key);
 152                     if (val instanceof String) {
 153                         orbProp.put(key, val);
 154                     }
 155                 }
 156             }
 157         } else {
 158             orbProp = new Properties();
 159         }
 160 
 161         if (server != null) {
 162             orbProp.put("org.omg.CORBA.ORBInitialHost", server);
 163         }
 164         if (port >= 0) {
 165             orbProp.put("org.omg.CORBA.ORBInitialPort", ""+port);
 166         }
 167 
 168         // Get Applet from environment
 169         if (env != null) {
 170             Object applet = env.get(Context.APPLET);
 171             if (applet != null) {
 172                 // Create ORBs for an applet
 173                 return initAppletORB(applet, orbProp);
 174             }
 175         }
 176 
 177         // Create ORBs using orbProp for a standalone application
 178         return ORB.init(new String[0], orbProp);
 179     }
 180 
 181     /**
 182      * This method returns a new ORB instance for the given applet
 183      * without creating a static dependency on java.applet.
 184      */
 185     private static ORB initAppletORB(Object applet, Properties orbProp) {
 186         try {
 187             Class<?> appletClass  = Class.forName("java.applet.Applet", true, null);
 188             if (!appletClass.isInstance(applet)) {
 189                 throw new ClassCastException(applet.getClass().getName());
 190             }
 191 
 192             // invoke the static method ORB.init(applet, orbProp);
 193             Method method = ORB.class.getMethod("init", appletClass, Properties.class);
 194             return (ORB) method.invoke(null, applet, orbProp);
 195         } catch (ClassNotFoundException e) {
 196             // java.applet.Applet doesn't exist and the applet parameter is
 197             // non-null; so throw CCE
 198             throw new ClassCastException(applet.getClass().getName());
 199         } catch (NoSuchMethodException e) {
 200             throw new AssertionError(e);
 201         } catch (InvocationTargetException e) {
 202             Throwable cause = e.getCause();
 203             if (cause instanceof RuntimeException) {
 204                 throw (RuntimeException) cause;
 205             } else if (cause instanceof Error) {
 206                 throw (Error) cause;
 207             }
 208             throw new AssertionError(e);
 209         } catch (IllegalAccessException iae) {
 210             throw new AssertionError(iae);
 211         }
 212     }
 213 }
--- EOF ---