CorbaUtils.java revision 820:9205e980062a
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 26package com.sun.jndi.toolkit.corba; 27 28// Needed for RMI/IIOP 29import java.rmi.Remote; 30 31import java.rmi.RemoteException; 32import java.util.Hashtable; 33import java.util.Properties; 34import java.util.Enumeration; 35import java.applet.Applet; 36 37import org.omg.CORBA.ORB; 38 39import javax.naming.Context; 40import javax.naming.ConfigurationException; 41import javax.rmi.CORBA.Stub; 42import javax.rmi.PortableRemoteObject; 43 44import java.io.UnsupportedEncodingException; 45import java.net.MalformedURLException; 46import java.net.URLDecoder; 47 48/** 49 * Contains utilities for performing CORBA-related tasks: 50 * 1. Get the org.omg.CORBA.Object for a java.rmi.Remote object. 51 * 2. Create an ORB to use for a given host/port, and environment properties. 52 * ... 53 * 54 * @author Simon Nash 55 * @author Bryan Atsatt 56 */ 57 58public class CorbaUtils { 59 /** 60 * Returns the CORBA object reference associated with a Remote 61 * object by using the javax.rmi.CORBA package. 62 *<p> 63 * This method effective does the following: 64 * <blockquote><pre> 65 * java.lang.Object stub; 66 * try { 67 * stub = PortableRemoteObject.toStub(remoteObj); 68 * } catch (Exception e) { 69 * throw new ConfigurationException("Object not exported or not found"); 70 * } 71 * if (!(stub instanceof javax.rmi.CORBA.Stub)) { 72 * return null; // JRMP impl or JRMP stub 73 * } 74 * try { 75 * ((javax.rmi.CORBA.Stub)stub).connect(orb); // try to connect IIOP stub 76 * } catch (RemoteException e) { 77 * // ignore 'already connected' error 78 * } 79 * return (javax.rmi.CORBA.Stub)stub; 80 * </pre></blockquote> 81 * 82 * @param remoteObj The non-null remote object for 83 * @param orb The non-null ORB to connect the remote object to 84 * @return The CORBA Object for remoteObj; null if {@code remoteObj} 85 * is a JRMP implementation or JRMP stub. 86 * @exception ConfigurationException The CORBA Object cannot be obtained 87 * because of configuration problems. 88 */ 89 public static org.omg.CORBA.Object remoteToCorba(Remote remoteObj, ORB orb) 90 throws ConfigurationException { 91 92// First, get remoteObj's stub 93 94 // javax.rmi.CORBA.Stub stub = PortableRemoteObject.toStub(remoteObj); 95 96 Remote stub; 97 98 try { 99 stub = PortableRemoteObject.toStub(remoteObj); 100 } catch (Throwable t) { 101 ConfigurationException ce = new ConfigurationException( 102 "Problem with PortableRemoteObject.toStub(); object not exported or stub not found"); 103 ce.setRootCause(t); 104 throw ce; 105 } 106 107// Next, make sure that the stub is javax.rmi.CORBA.Stub 108 109 if (!(stub instanceof Stub)) { 110 return null; // JRMP implementation or JRMP stub 111 } 112 113// Next, make sure that the stub is connected 114 try { 115 ((Stub) stub).connect(orb); 116 } catch (RemoteException e) { 117 // ignore RemoteException because stub might have already 118 // been connected 119 } catch (Throwable t) { 120 ConfigurationException ce = new ConfigurationException( 121 "Problem invoking javax.rmi.CORBA.Stub.connect()"); 122 ce.setRootCause(t); 123 throw ce; 124 } 125// Finally, return stub 126 return (org.omg.CORBA.Object)stub; 127 } 128 129 /** 130 * Get ORB using given server and port number, and properties from environment. 131 * 132 * @param server Possibly null server; if null means use default; 133 * For applet, it is the applet host; for app, it is localhost. 134 * @param port Port number, -1 means default port 135 * @param env Possibly null environment. Contains environment properties. 136 * Could contain ORB itself; or applet used for initializing ORB. 137 * Use all String properties from env for initializing ORB 138 * @return A non-null ORB. 139 */ 140 public static ORB getOrb(String server, int port, Hashtable<?,?> env) { 141 // See if we can get info from environment 142 Properties orbProp; 143 144 // Extract any org.omg.CORBA properties from environment 145 if (env != null) { 146 if (env instanceof Properties) { 147 // Already a Properties, just clone 148 orbProp = (Properties) env.clone(); 149 } else { 150 // Get all String properties 151 Enumeration<?> envProp; 152 orbProp = new Properties(); 153 for (envProp = env.keys(); envProp.hasMoreElements();) { 154 String key = (String)envProp.nextElement(); 155 Object val = env.get(key); 156 if (val instanceof String) { 157 orbProp.put(key, val); 158 } 159 } 160 } 161 } else { 162 orbProp = new Properties(); 163 } 164 165 if (server != null) { 166 orbProp.put("org.omg.CORBA.ORBInitialHost", server); 167 } 168 if (port >= 0) { 169 orbProp.put("org.omg.CORBA.ORBInitialPort", ""+port); 170 } 171 172 // Get Applet from environment 173 if (env != null) { 174 @SuppressWarnings("deprecation") 175 Applet applet = (Applet) env.get(Context.APPLET); 176 if (applet != null) { 177 // Create ORBs using applet and orbProp 178 return ORB.init(applet, orbProp); 179 } 180 } 181 182 return ORB.init(new String[0], orbProp); 183 } 184 185 /** 186 * Decode a URI string (according to RFC 2396). 187 */ 188 public static final String decode(String s) throws MalformedURLException { 189 try { 190 return decode(s, "8859_1"); 191 } catch (UnsupportedEncodingException e) { 192 // ISO-Latin-1 should always be available? 193 throw new MalformedURLException("ISO-Latin-1 decoder unavailable"); 194 } 195 } 196 197 /** 198 * Decode a URI string (according to RFC 2396). 199 * 200 * Three-character sequences '%xy', where 'xy' is the two-digit 201 * hexadecimal representation of the lower 8-bits of a character, 202 * are decoded into the character itself. 203 * 204 * The string is subsequently converted using the specified encoding 205 */ 206 public static final String decode(String s, String enc) 207 throws MalformedURLException, UnsupportedEncodingException { 208 try { 209 return URLDecoder.decode(s, enc); 210 } catch (IllegalArgumentException iae) { 211 MalformedURLException mue = new MalformedURLException("Invalid URI encoding: " + s); 212 mue.initCause(iae); 213 throw mue; 214 } 215 } 216 217} 218