0N/A/*
2362N/A * Copyright (c) 1996, 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.rmi.registry;
0N/A
0N/Aimport java.rmi.RemoteException;
0N/Aimport java.rmi.server.ObjID;
0N/Aimport java.rmi.server.RMIClientSocketFactory;
0N/Aimport java.rmi.server.RMIServerSocketFactory;
0N/Aimport java.rmi.server.RemoteRef;
0N/Aimport java.rmi.server.UnicastRemoteObject;
0N/Aimport sun.rmi.registry.RegistryImpl;
0N/Aimport sun.rmi.server.UnicastRef2;
0N/Aimport sun.rmi.server.UnicastRef;
0N/Aimport sun.rmi.server.Util;
0N/Aimport sun.rmi.transport.LiveRef;
0N/Aimport sun.rmi.transport.tcp.TCPEndpoint;
0N/A
0N/A/**
0N/A * <code>LocateRegistry</code> is used to obtain a reference to a bootstrap
0N/A * remote object registry on a particular host (including the local host), or
0N/A * to create a remote object registry that accepts calls on a specific port.
0N/A *
0N/A * <p> Note that a <code>getRegistry</code> call does not actually make a
0N/A * connection to the remote host. It simply creates a local reference to
0N/A * the remote registry and will succeed even if no registry is running on
0N/A * the remote host. Therefore, a subsequent method invocation to a remote
0N/A * registry returned as a result of this method may fail.
0N/A *
0N/A * @author Ann Wollrath
0N/A * @author Peter Jones
0N/A * @since JDK1.1
0N/A * @see java.rmi.registry.Registry
0N/A */
0N/Apublic final class LocateRegistry {
0N/A
0N/A /**
0N/A * Private constructor to disable public construction.
0N/A */
0N/A private LocateRegistry() {}
0N/A
0N/A /**
0N/A * Returns a reference to the the remote object <code>Registry</code> for
0N/A * the local host on the default registry port of 1099.
0N/A *
0N/A * @return reference (a stub) to the remote object registry
0N/A * @exception RemoteException if the reference could not be created
0N/A * @since JDK1.1
0N/A */
0N/A public static Registry getRegistry()
0N/A throws RemoteException
0N/A {
0N/A return getRegistry(null, Registry.REGISTRY_PORT);
0N/A }
0N/A
0N/A /**
0N/A * Returns a reference to the the remote object <code>Registry</code> for
0N/A * the local host on the specified <code>port</code>.
0N/A *
0N/A * @param port port on which the registry accepts requests
0N/A * @return reference (a stub) to the remote object registry
0N/A * @exception RemoteException if the reference could not be created
0N/A * @since JDK1.1
0N/A */
0N/A public static Registry getRegistry(int port)
0N/A throws RemoteException
0N/A {
0N/A return getRegistry(null, port);
0N/A }
0N/A
0N/A /**
0N/A * Returns a reference to the remote object <code>Registry</code> on the
0N/A * specified <code>host</code> on the default registry port of 1099. If
0N/A * <code>host</code> is <code>null</code>, the local host is used.
0N/A *
0N/A * @param host host for the remote registry
0N/A * @return reference (a stub) to the remote object registry
0N/A * @exception RemoteException if the reference could not be created
0N/A * @since JDK1.1
0N/A */
0N/A public static Registry getRegistry(String host)
0N/A throws RemoteException
0N/A {
0N/A return getRegistry(host, Registry.REGISTRY_PORT);
0N/A }
0N/A
0N/A /**
0N/A * Returns a reference to the remote object <code>Registry</code> on the
0N/A * specified <code>host</code> and <code>port</code>. If <code>host</code>
0N/A * is <code>null</code>, the local host is used.
0N/A *
0N/A * @param host host for the remote registry
0N/A * @param port port on which the registry accepts requests
0N/A * @return reference (a stub) to the remote object registry
0N/A * @exception RemoteException if the reference could not be created
0N/A * @since JDK1.1
0N/A */
0N/A public static Registry getRegistry(String host, int port)
0N/A throws RemoteException
0N/A {
0N/A return getRegistry(host, port, null);
0N/A }
0N/A
0N/A /**
0N/A * Returns a locally created remote reference to the remote object
0N/A * <code>Registry</code> on the specified <code>host</code> and
0N/A * <code>port</code>. Communication with this remote registry will
0N/A * use the supplied <code>RMIClientSocketFactory</code> <code>csf</code>
0N/A * to create <code>Socket</code> connections to the registry on the
0N/A * remote <code>host</code> and <code>port</code>.
0N/A *
0N/A * @param host host for the remote registry
0N/A * @param port port on which the registry accepts requests
0N/A * @param csf client-side <code>Socket</code> factory used to
0N/A * make connections to the registry. If <code>csf</code>
0N/A * is null, then the default client-side <code>Socket</code>
0N/A * factory will be used in the registry stub.
0N/A * @return reference (a stub) to the remote registry
0N/A * @exception RemoteException if the reference could not be created
0N/A * @since 1.2
0N/A */
0N/A public static Registry getRegistry(String host, int port,
0N/A RMIClientSocketFactory csf)
0N/A throws RemoteException
0N/A {
0N/A Registry registry = null;
0N/A
0N/A if (port <= 0)
0N/A port = Registry.REGISTRY_PORT;
0N/A
0N/A if (host == null || host.length() == 0) {
0N/A // If host is blank (as returned by "file:" URL in 1.0.2 used in
0N/A // java.rmi.Naming), try to convert to real local host name so
0N/A // that the RegistryImpl's checkAccess will not fail.
0N/A try {
0N/A host = java.net.InetAddress.getLocalHost().getHostAddress();
0N/A } catch (Exception e) {
0N/A // If that failed, at least try "" (localhost) anyway...
0N/A host = "";
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Create a proxy for the registry with the given host, port, and
0N/A * client socket factory. If the supplied client socket factory is
0N/A * null, then the ref type is a UnicastRef, otherwise the ref type
0N/A * is a UnicastRef2. If the property
0N/A * java.rmi.server.ignoreStubClasses is true, then the proxy
0N/A * returned is an instance of a dynamic proxy class that implements
0N/A * the Registry interface; otherwise the proxy returned is an
0N/A * instance of the pregenerated stub class for RegistryImpl.
0N/A **/
0N/A LiveRef liveRef =
0N/A new LiveRef(new ObjID(ObjID.REGISTRY_ID),
0N/A new TCPEndpoint(host, port, csf, null),
0N/A false);
0N/A RemoteRef ref =
0N/A (csf == null) ? new UnicastRef(liveRef) : new UnicastRef2(liveRef);
0N/A
0N/A return (Registry) Util.createProxy(RegistryImpl.class, ref, false);
0N/A }
0N/A
0N/A /**
0N/A * Creates and exports a <code>Registry</code> instance on the local
0N/A * host that accepts requests on the specified <code>port</code>.
0N/A *
0N/A * <p>The <code>Registry</code> instance is exported as if the static
2015N/A * {@link UnicastRemoteObject#exportObject(Remote,int)
0N/A * UnicastRemoteObject.exportObject} method is invoked, passing the
0N/A * <code>Registry</code> instance and the specified <code>port</code> as
0N/A * arguments, except that the <code>Registry</code> instance is
0N/A * exported with a well-known object identifier, an {@link ObjID}
0N/A * instance constructed with the value {@link ObjID#REGISTRY_ID}.
0N/A *
0N/A * @param port the port on which the registry accepts requests
0N/A * @return the registry
0N/A * @exception RemoteException if the registry could not be exported
0N/A * @since JDK1.1
0N/A **/
0N/A public static Registry createRegistry(int port) throws RemoteException {
0N/A return new RegistryImpl(port);
0N/A }
0N/A
0N/A /**
0N/A * Creates and exports a <code>Registry</code> instance on the local
0N/A * host that uses custom socket factories for communication with that
0N/A * instance. The registry that is created listens for incoming
0N/A * requests on the given <code>port</code> using a
0N/A * <code>ServerSocket</code> created from the supplied
0N/A * <code>RMIServerSocketFactory</code>.
0N/A *
0N/A * <p>The <code>Registry</code> instance is exported as if
0N/A * the static {@link
2015N/A * UnicastRemoteObject#exportObject(Remote,int,RMIClientSocketFactory,RMIServerSocketFactory)
0N/A * UnicastRemoteObject.exportObject} method is invoked, passing the
0N/A * <code>Registry</code> instance, the specified <code>port</code>, the
0N/A * specified <code>RMIClientSocketFactory</code>, and the specified
0N/A * <code>RMIServerSocketFactory</code> as arguments, except that the
0N/A * <code>Registry</code> instance is exported with a well-known object
0N/A * identifier, an {@link ObjID} instance constructed with the value
0N/A * {@link ObjID#REGISTRY_ID}.
0N/A *
0N/A * @param port port on which the registry accepts requests
0N/A * @param csf client-side <code>Socket</code> factory used to
0N/A * make connections to the registry
0N/A * @param ssf server-side <code>ServerSocket</code> factory
0N/A * used to accept connections to the registry
0N/A * @return the registry
0N/A * @exception RemoteException if the registry could not be exported
0N/A * @since 1.2
0N/A **/
0N/A public static Registry createRegistry(int port,
0N/A RMIClientSocketFactory csf,
0N/A RMIServerSocketFactory ssf)
0N/A throws RemoteException
0N/A {
0N/A return new RegistryImpl(port, csf, ssf);
0N/A }
0N/A}