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