J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1996-2006 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package sun.rmi.registry; |
| 27 | |
| 28 | import java.util.Enumeration; |
| 29 | import java.util.Hashtable; |
| 30 | import java.util.MissingResourceException; |
| 31 | import java.util.ResourceBundle; |
| 32 | import java.io.IOException; |
| 33 | import java.net.*; |
| 34 | import java.rmi.*; |
| 35 | import java.rmi.server.ObjID; |
| 36 | import java.rmi.server.RemoteServer; |
| 37 | import java.rmi.server.ServerNotActiveException; |
| 38 | import java.rmi.registry.Registry; |
| 39 | import java.rmi.server.RMIClientSocketFactory; |
| 40 | import java.rmi.server.RMIServerSocketFactory; |
| 41 | import java.security.PrivilegedActionException; |
| 42 | import java.text.MessageFormat; |
| 43 | import sun.rmi.server.UnicastServerRef; |
| 44 | import sun.rmi.server.UnicastServerRef2; |
| 45 | import sun.rmi.transport.LiveRef; |
| 46 | import sun.rmi.transport.ObjectTable; |
| 47 | import sun.rmi.transport.Target; |
| 48 | |
| 49 | /** |
| 50 | * A "registry" exists on every node that allows RMI connections to |
| 51 | * servers on that node. The registry on a particular node contains a |
| 52 | * transient database that maps names to remote objects. When the |
| 53 | * node boots, the registry database is empty. The names stored in the |
| 54 | * registry are pure and are not parsed. A service storing itself in |
| 55 | * the registry may want to prefix its name of the service by a package |
| 56 | * name (although not required), to reduce name collisions in the |
| 57 | * registry. |
| 58 | * |
| 59 | * The LocateRegistry class is used to obtain registry for different hosts. |
| 60 | * |
| 61 | * @see java.rmi.registry.LocateRegistry |
| 62 | */ |
| 63 | public class RegistryImpl extends java.rmi.server.RemoteServer |
| 64 | implements Registry |
| 65 | { |
| 66 | |
| 67 | /* indicate compatibility with JDK 1.1.x version of class */ |
| 68 | private static final long serialVersionUID = 4666870661827494597L; |
| 69 | private Hashtable bindings = new Hashtable(101); |
| 70 | private static Hashtable allowedAccessCache = new Hashtable(3); |
| 71 | private static RegistryImpl registry; |
| 72 | private static ObjID id = new ObjID(ObjID.REGISTRY_ID); |
| 73 | |
| 74 | private static ResourceBundle resources = null; |
| 75 | |
| 76 | /** |
| 77 | * Construct a new RegistryImpl on the specified port with the |
| 78 | * given custom socket factory pair. |
| 79 | */ |
| 80 | public RegistryImpl(int port, |
| 81 | RMIClientSocketFactory csf, |
| 82 | RMIServerSocketFactory ssf) |
| 83 | throws RemoteException |
| 84 | { |
| 85 | LiveRef lref = new LiveRef(id, port, csf, ssf); |
| 86 | setup(new UnicastServerRef2(lref)); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Construct a new RegistryImpl on the specified port. |
| 91 | */ |
| 92 | public RegistryImpl(int port) |
| 93 | throws RemoteException |
| 94 | { |
| 95 | LiveRef lref = new LiveRef(id, port); |
| 96 | setup(new UnicastServerRef(lref)); |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * Create the export the object using the parameter |
| 101 | * <code>uref</code> |
| 102 | */ |
| 103 | private void setup(UnicastServerRef uref) |
| 104 | throws RemoteException |
| 105 | { |
| 106 | /* Server ref must be created and assigned before remote |
| 107 | * object 'this' can be exported. |
| 108 | */ |
| 109 | ref = uref; |
| 110 | uref.exportObject(this, null, true); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Returns the remote object for specified name in the registry. |
| 115 | * @exception RemoteException If remote operation failed. |
| 116 | * @exception NotBound If name is not currently bound. |
| 117 | */ |
| 118 | public Remote lookup(String name) |
| 119 | throws RemoteException, NotBoundException |
| 120 | { |
| 121 | synchronized (bindings) { |
| 122 | Remote obj = (Remote)bindings.get(name); |
| 123 | if (obj == null) |
| 124 | throw new NotBoundException(name); |
| 125 | return obj; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Binds the name to the specified remote object. |
| 131 | * @exception RemoteException If remote operation failed. |
| 132 | * @exception AlreadyBoundException If name is already bound. |
| 133 | */ |
| 134 | public void bind(String name, Remote obj) |
| 135 | throws RemoteException, AlreadyBoundException, AccessException |
| 136 | { |
| 137 | checkAccess("Registry.bind"); |
| 138 | synchronized (bindings) { |
| 139 | Remote curr = (Remote)bindings.get(name); |
| 140 | if (curr != null) |
| 141 | throw new AlreadyBoundException(name); |
| 142 | bindings.put(name, obj); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Unbind the name. |
| 148 | * @exception RemoteException If remote operation failed. |
| 149 | * @exception NotBound If name is not currently bound. |
| 150 | */ |
| 151 | public void unbind(String name) |
| 152 | throws RemoteException, NotBoundException, AccessException |
| 153 | { |
| 154 | checkAccess("Registry.unbind"); |
| 155 | synchronized (bindings) { |
| 156 | Remote obj = (Remote)bindings.get(name); |
| 157 | if (obj == null) |
| 158 | throw new NotBoundException(name); |
| 159 | bindings.remove(name); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Rebind the name to a new object, replaces any existing binding. |
| 165 | * @exception RemoteException If remote operation failed. |
| 166 | */ |
| 167 | public void rebind(String name, Remote obj) |
| 168 | throws RemoteException, AccessException |
| 169 | { |
| 170 | checkAccess("Registry.rebind"); |
| 171 | bindings.put(name, obj); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Returns an enumeration of the names in the registry. |
| 176 | * @exception RemoteException If remote operation failed. |
| 177 | */ |
| 178 | public String[] list() |
| 179 | throws RemoteException |
| 180 | { |
| 181 | String[] names; |
| 182 | synchronized (bindings) { |
| 183 | int i = bindings.size(); |
| 184 | names = new String[i]; |
| 185 | Enumeration enum_ = bindings.keys(); |
| 186 | while ((--i) >= 0) |
| 187 | names[i] = (String)enum_.nextElement(); |
| 188 | } |
| 189 | return names; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Check that the caller has access to perform indicated operation. |
| 194 | * The client must be on same the same host as this server. |
| 195 | */ |
| 196 | public static void checkAccess(String op) throws AccessException { |
| 197 | |
| 198 | try { |
| 199 | /* |
| 200 | * Get client host that this registry operation was made from. |
| 201 | */ |
| 202 | final String clientHostName = getClientHost(); |
| 203 | InetAddress clientHost; |
| 204 | |
| 205 | try { |
| 206 | clientHost = (InetAddress) |
| 207 | java.security.AccessController.doPrivileged( |
| 208 | new java.security.PrivilegedExceptionAction() { |
| 209 | public Object run() |
| 210 | throws java.net.UnknownHostException |
| 211 | { |
| 212 | return InetAddress.getByName(clientHostName); |
| 213 | } |
| 214 | }); |
| 215 | } catch (PrivilegedActionException pae) { |
| 216 | throw (java.net.UnknownHostException) pae.getException(); |
| 217 | } |
| 218 | |
| 219 | // if client not yet seen, make sure client allowed access |
| 220 | if (allowedAccessCache.get(clientHost) == null) { |
| 221 | |
| 222 | if (clientHost.isAnyLocalAddress()) { |
| 223 | throw new AccessException( |
| 224 | "Registry." + op + " disallowed; origin unknown"); |
| 225 | } |
| 226 | |
| 227 | try { |
| 228 | final InetAddress finalClientHost = clientHost; |
| 229 | |
| 230 | java.security.AccessController.doPrivileged( |
| 231 | new java.security.PrivilegedExceptionAction() { |
| 232 | public Object run() throws java.io.IOException { |
| 233 | /* |
| 234 | * if a ServerSocket can be bound to the client's |
| 235 | * address then that address must be local |
| 236 | */ |
| 237 | (new ServerSocket(0, 10, finalClientHost)).close(); |
| 238 | allowedAccessCache.put(finalClientHost, |
| 239 | finalClientHost); |
| 240 | return null; |
| 241 | } |
| 242 | }); |
| 243 | } catch (PrivilegedActionException pae) { |
| 244 | // must have been an IOException |
| 245 | |
| 246 | throw new AccessException( |
| 247 | "Registry." + op + " disallowed; origin " + |
| 248 | clientHost + " is non-local host"); |
| 249 | } |
| 250 | } |
| 251 | } catch (ServerNotActiveException ex) { |
| 252 | /* |
| 253 | * Local call from this VM: allow access. |
| 254 | */ |
| 255 | } catch (java.net.UnknownHostException ex) { |
| 256 | throw new AccessException("Registry." + op + |
| 257 | " disallowed; origin is unknown host"); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | public static ObjID getID() { |
| 262 | return id; |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Retrieves text resources from the locale-specific properties file. |
| 267 | */ |
| 268 | private static String getTextResource(String key) { |
| 269 | if (resources == null) { |
| 270 | try { |
| 271 | resources = ResourceBundle.getBundle( |
| 272 | "sun.rmi.registry.resources.rmiregistry"); |
| 273 | } catch (MissingResourceException mre) { |
| 274 | } |
| 275 | if (resources == null) { |
| 276 | // throwing an Error is a bit extreme, methinks |
| 277 | return ("[missing resource file: " + key + "]"); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | String val = null; |
| 282 | try { |
| 283 | val = resources.getString(key); |
| 284 | } catch (MissingResourceException mre) { |
| 285 | } |
| 286 | |
| 287 | if (val == null) { |
| 288 | return ("[missing resource: " + key + "]"); |
| 289 | } else { |
| 290 | return (val); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Main program to start a registry. <br> |
| 296 | * The port number can be specified on the command line. |
| 297 | */ |
| 298 | public static void main(String args[]) |
| 299 | { |
| 300 | // Create and install the security manager if one is not installed |
| 301 | // already. |
| 302 | if (System.getSecurityManager() == null) { |
| 303 | System.setSecurityManager(new RMISecurityManager()); |
| 304 | } |
| 305 | |
| 306 | try { |
| 307 | /* |
| 308 | * Fix bugid 4147561: When JDK tools are executed, the value of |
| 309 | * the CLASSPATH environment variable for the shell in which they |
| 310 | * were invoked is no longer incorporated into the application |
| 311 | * class path; CLASSPATH's only effect is to be the value of the |
| 312 | * system property "env.class.path". To preserve the previous |
| 313 | * (JDK1.1 and JDK1.2beta3) behavior of this tool, however, its |
| 314 | * CLASSPATH should still be considered when resolving classes |
| 315 | * being unmarshalled. To effect this old behavior, a class |
| 316 | * loader that loads from the file path specified in the |
| 317 | * "env.class.path" property is created and set to be the context |
| 318 | * class loader before the remote object is exported. |
| 319 | */ |
| 320 | String envcp = System.getProperty("env.class.path"); |
| 321 | if (envcp == null) { |
| 322 | envcp = "."; // preserve old default behavior |
| 323 | } |
| 324 | URL[] urls = sun.misc.URLClassPath.pathToURLs(envcp); |
| 325 | ClassLoader cl = new URLClassLoader(urls); |
| 326 | |
| 327 | /* |
| 328 | * Fix bugid 4242317: Classes defined by this class loader should |
| 329 | * be annotated with the value of the "java.rmi.server.codebase" |
| 330 | * property, not the "file:" URLs for the CLASSPATH elements. |
| 331 | */ |
| 332 | sun.rmi.server.LoaderHandler.registerCodebaseLoader(cl); |
| 333 | |
| 334 | Thread.currentThread().setContextClassLoader(cl); |
| 335 | |
| 336 | int regPort = Registry.REGISTRY_PORT; |
| 337 | if (args.length >= 1) { |
| 338 | regPort = Integer.parseInt(args[0]); |
| 339 | } |
| 340 | registry = new RegistryImpl(regPort); |
| 341 | // prevent registry from exiting |
| 342 | while (true) { |
| 343 | try { |
| 344 | Thread.sleep(Long.MAX_VALUE); |
| 345 | } catch (InterruptedException e) { |
| 346 | } |
| 347 | } |
| 348 | } catch (NumberFormatException e) { |
| 349 | System.err.println(MessageFormat.format( |
| 350 | getTextResource("rmiregistry.port.badnumber"), |
| 351 | args[0] )); |
| 352 | System.err.println(MessageFormat.format( |
| 353 | getTextResource("rmiregistry.usage"), |
| 354 | "rmiregistry" )); |
| 355 | } catch (Exception e) { |
| 356 | e.printStackTrace(); |
| 357 | } |
| 358 | System.exit(1); |
| 359 | } |
| 360 | } |