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.rmi.RemoteException;
  32 import java.util.Hashtable;
  33 import java.util.Properties;
  34 import java.util.Enumeration;
  35 import java.applet.Applet;
  36 
  37 import org.omg.CORBA.ORB;
  38 
  39 import javax.naming.Context;
  40 import javax.naming.ConfigurationException;
  41 import javax.rmi.CORBA.Stub;
  42 import javax.rmi.PortableRemoteObject;
  43 
  44 /**
  45   * Contains utilities for performing CORBA-related tasks:
  46   * 1. Get the org.omg.CORBA.Object for a java.rmi.Remote object.
  47   * 2. Create an ORB to use for a given host/port, and environment properties.
  48   *
  49   * @author Simon Nash
  50   * @author Bryan Atsatt
  51   */
  52 
  53 public class CorbaUtils {
  54     /**
  55       * Returns the CORBA object reference associated with a Remote
  56       * object by using the javax.rmi.CORBA package.
  57       *<p>
  58       * Use reflection to avoid hard dependencies on javax.rmi.CORBA package.
  59       * This method effective does the following:
  60       *<blockquote><pre>
  61       * java.lang.Object stub;
  62       * try {
  63       *     stub = PortableRemoteObject.toStub(remoteObj);
  64       * } catch (Exception e) {
  65       *     throw new ConfigurationException("Object not exported or not found");
  66       * }
  67       * if (!(stub instanceof javax.rmi.CORBA.Stub)) {
  68       *     return null; // JRMP impl or JRMP stub
  69       * }
  70       * try {
  71       *     ((javax.rmi.CORBA.Stub)stub).connect(orb);  // try to connect IIOP stub
  72       * } catch (RemoteException e) {
  73       *     // ignore 'already connected' error
  74       * }
  75       * return (javax.rmi.CORBA.Stub)stub;
  76       *
  77       * @param remoteObj The non-null remote object for
  78       * @param orb       The non-null ORB to connect the remote object to
  79       * @return The CORBA Object for remoteObj; null if <tt>remoteObj</tt>
  80       *                 is a JRMP implementation or JRMP stub.
  81       * @exception ConfigurationException The CORBA Object cannot be obtained
  82       *         because of configuration problems.
  83       */
  84     public static org.omg.CORBA.Object remoteToCorba(Remote remoteObj, ORB orb)
  85         throws ConfigurationException {
  86 
  87 // First, get remoteObj's stub
  88 
  89             // javax.rmi.CORBA.Stub stub = PortableRemoteObject.toStub(remoteObj);
  90 
  91             Remote stub;
  92 
  93             try {
  94                 stub = PortableRemoteObject.toStub(remoteObj);
  95             } catch (Throwable t) {
  96                 ConfigurationException ce = new ConfigurationException(
  97     "Problem with PortableRemoteObject.toStub(); object not exported or stub not found");
  98                 ce.setRootCause(t);
  99                 throw ce;
 100             }
 101 
 102 // Next, make sure that the stub is javax.rmi.CORBA.Stub
 103 
 104             if (!(stub instanceof Stub)) {
 105                 return null;  // JRMP implementation or JRMP stub
 106             }
 107 
 108 // Next, make sure that the stub is connected
 109             try {
 110                 ((Stub) stub).connect(orb);
 111             } catch (RemoteException e) {
 112                 // ignore RemoteException because stub might have already
 113                 // been connected
 114             } catch (Throwable t) {
 115                 ConfigurationException ce = new ConfigurationException(
 116                         "Problem invoking javax.rmi.CORBA.Stub.connect()");
 117                 ce.setRootCause(t);
 118                 throw ce;
 119             }
 120 // Finally, return stub
 121             return (org.omg.CORBA.Object)stub;
 122     }
 123 
 124     /**
 125      * Get ORB using given server and port number, and properties from environment.
 126      *
 127      * @param server Possibly null server; if null means use default;
 128      *               For applet, it is the applet host; for app, it is localhost.
 129      * @param port   Port number, -1 means default port
 130      * @param env    Possibly null environment. Contains environment properties.
 131      *               Could contain ORB itself; or applet used for initializing ORB.
 132      *               Use all String properties from env for initializing ORB
 133      * @return A non-null ORB.
 134      */
 135     public static ORB getOrb(String server, int port, Hashtable<?,?> env) {
 136         // See if we can get info from environment
 137         Properties orbProp;
 138 
 139         // Extract any org.omg.CORBA properties from environment
 140         if (env != null) {
 141             if (env instanceof Properties) {
 142                 // Already a Properties, just clone
 143                 orbProp = (Properties) env.clone();
 144             } else {
 145                 // Get all String properties
 146                 Enumeration<?> envProp;
 147                 orbProp = new Properties();
 148                 for (envProp = env.keys(); envProp.hasMoreElements();) {
 149                     String key = (String)envProp.nextElement();
 150                     Object val = env.get(key);
 151                     if (val instanceof String) {
 152                         orbProp.put(key, val);
 153                     }
 154                 }
 155             }
 156         } else {
 157             orbProp = new Properties();
 158         }
 159 
 160         if (server != null) {
 161             orbProp.put("org.omg.CORBA.ORBInitialHost", server);
 162         }
 163         if (port >= 0) {
 164             orbProp.put("org.omg.CORBA.ORBInitialPort", ""+port);
 165         }
 166 
 167         // Get Applet from environment
 168         if (env != null) {
 169             Applet applet = (Applet) env.get(Context.APPLET);
 170             if (applet != null) {
 171             // Create ORBs using applet and orbProp
 172                 return ORB.init(applet, orbProp);
 173             }
 174         }
 175 

 176         return ORB.init(new String[0], orbProp);
 177     }

































 178 }
--- EOF ---