package textbender.g.rmi; // Copyright 2006, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Textbender Software"), to deal in the Textbender Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Textbender Software, and to permit persons to whom the Textbender Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Textbender Software. THE TEXTBENDER SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE TEXTBENDER SOFTWARE OR THE USE OR OTHER DEALINGS IN THE TEXTBENDER SOFTWARE. import java.rmi.*; import java.rmi.registry.*; //import java.rmi.server.ServerException; import textbender.g.util.logging.LoggerX; /** RMI boot-registry utilities and definitions. *

* Note that Sun's implementation of the registry * is supposed to restrict binding/rebinding to local (same host) callers. * Although other hosts cannot bind, they can do lookups. *

*/ public class RegistryX { private RegistryX() {} /** Ensures a boot-registry exists on the local host at the * {@linkplain textbender.g.rmi.RegistryX#STANDARD_REGISTRY_PORT standard port}, * and returns a reference to it. The same as LocateRegistry.getRegistry(), * except that it creates the registry, if necessary. */ public static Registry ensureRegistry() throws RemoteException { Registry registry = getRegistryOrNull(); if( registry == null ) { LoggerX.i(RegistryX.class).info( "creating RMI boot-registry" ); registry = LocateRegistry.createRegistry( STANDARD_REGISTRY_PORT ); // Client could later destroy it, if for some reason they wanted to keep the VM running without the registry, via UnicastRemoteObject.unexportObject(registry,true). That would only work if 'registry' is this same object, not one obtained by getRegistry() (because that is a remote reference, not the actual object). To know that, client would have to code it in-line, instead of calling this method. } return registry; } /** Returns a remote reference to the boot-registry on the local host at the * {@linkplain textbender.g.rmi.RegistryX#STANDARD_REGISTRY_PORT standard port}, * but only if one exists. Otherwise the same as LocateRegistry.getRegistry(). * * @return remote reference to the boot-registry, * or null if no registry exists */ public static Registry getRegistryOrNull() throws RemoteException { Registry registry = LocateRegistry.getRegistry(); // usually succeeds (regardless of whether an actual registry exists or not), per LocateRegistry try { registry.list(); // as a test. (Or could blindly create it, and catch the ExportException thrown if it already exists.) } catch( RemoteException x ) { if( x instanceof ServerException && x.getCause() instanceof AccessException || x instanceof AccessException ) throw x; // per Registry.list() // else probably no registry exists registry = null; } return registry; } /** Default port for the RMI boot-registry. */ public static final int STANDARD_REGISTRY_PORT = 1099; }