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