Path: blob/master/src/java.rmi/share/classes/java/rmi/registry/LocateRegistry.java
41159 views
/*1* Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.rmi.registry;2627import java.rmi.RemoteException;28import java.rmi.server.ObjID;29import java.rmi.server.RMIClientSocketFactory;30import java.rmi.server.RMIServerSocketFactory;31import java.rmi.server.RemoteRef;32import java.rmi.server.UnicastRemoteObject;33import sun.rmi.registry.RegistryImpl;34import sun.rmi.server.UnicastRef2;35import sun.rmi.server.UnicastRef;36import sun.rmi.server.Util;37import sun.rmi.transport.LiveRef;38import sun.rmi.transport.tcp.TCPEndpoint;3940/**41* <code>LocateRegistry</code> is used to obtain a reference to a bootstrap42* remote object registry on a particular host (including the local host), or43* to create a remote object registry that accepts calls on a specific port.44*45* <p> Note that a <code>getRegistry</code> call does not actually make a46* connection to the remote host. It simply creates a local reference to47* the remote registry and will succeed even if no registry is running on48* the remote host. Therefore, a subsequent method invocation to a remote49* registry returned as a result of this method may fail.50*51* @author Ann Wollrath52* @author Peter Jones53* @since 1.154* @see java.rmi.registry.Registry55*/56public final class LocateRegistry {5758/**59* Private constructor to disable public construction.60*/61private LocateRegistry() {}6263/**64* Returns a reference to the remote object <code>Registry</code> for65* the local host on the default registry port of 1099.66*67* @return reference (a stub) to the remote object registry68* @throws RemoteException if the reference could not be created69* @since 1.170*/71public static Registry getRegistry()72throws RemoteException73{74return getRegistry(null, Registry.REGISTRY_PORT);75}7677/**78* Returns a reference to the remote object <code>Registry</code> for79* the local host on the specified <code>port</code>.80*81* @param port port on which the registry accepts requests82* @return reference (a stub) to the remote object registry83* @throws RemoteException if the reference could not be created84* @since 1.185*/86public static Registry getRegistry(int port)87throws RemoteException88{89return getRegistry(null, port);90}9192/**93* Returns a reference to the remote object <code>Registry</code> on the94* specified <code>host</code> on the default registry port of 1099. If95* <code>host</code> is <code>null</code>, the local host is used.96*97* @param host host for the remote registry98* @return reference (a stub) to the remote object registry99* @throws RemoteException if the reference could not be created100* @since 1.1101*/102public static Registry getRegistry(String host)103throws RemoteException104{105return getRegistry(host, Registry.REGISTRY_PORT);106}107108/**109* Returns a reference to the remote object <code>Registry</code> on the110* specified <code>host</code> and <code>port</code>. If <code>host</code>111* is <code>null</code>, the local host is used.112*113* @param host host for the remote registry114* @param port port on which the registry accepts requests115* @return reference (a stub) to the remote object registry116* @throws RemoteException if the reference could not be created117* @since 1.1118*/119public static Registry getRegistry(String host, int port)120throws RemoteException121{122return getRegistry(host, port, null);123}124125/**126* Returns a locally created remote reference to the remote object127* <code>Registry</code> on the specified <code>host</code> and128* <code>port</code>. Communication with this remote registry will129* use the supplied <code>RMIClientSocketFactory</code> <code>csf</code>130* to create <code>Socket</code> connections to the registry on the131* remote <code>host</code> and <code>port</code>.132*133* @param host host for the remote registry134* @param port port on which the registry accepts requests135* @param csf client-side <code>Socket</code> factory used to136* make connections to the registry. If <code>csf</code>137* is null, then the default client-side <code>Socket</code>138* factory will be used in the registry stub.139* @return reference (a stub) to the remote registry140* @throws RemoteException if the reference could not be created141* @since 1.2142*/143public static Registry getRegistry(String host, int port,144RMIClientSocketFactory csf)145throws RemoteException146{147Registry registry = null;148149if (port <= 0)150port = Registry.REGISTRY_PORT;151152if (host == null || host.length() == 0) {153// If host is blank (as returned by "file:" URL in 1.0.2 used in154// java.rmi.Naming), try to convert to real local host name so155// that the RegistryImpl's checkAccess will not fail.156try {157host = java.net.InetAddress.getLocalHost().getHostAddress();158} catch (Exception e) {159// If that failed, at least try "" (localhost) anyway...160host = "";161}162}163164/*165* Create a proxy for the registry with the given host, port, and166* client socket factory. If the supplied client socket factory is167* null, then the ref type is a UnicastRef, otherwise the ref type168* is a UnicastRef2. If the property169* java.rmi.server.ignoreStubClasses is true, then the proxy170* returned is an instance of a dynamic proxy class that implements171* the Registry interface; otherwise the proxy returned is an172* instance of the pregenerated stub class for RegistryImpl.173**/174LiveRef liveRef =175new LiveRef(new ObjID(ObjID.REGISTRY_ID),176new TCPEndpoint(host, port, csf, null),177false);178RemoteRef ref =179(csf == null) ? new UnicastRef(liveRef) : new UnicastRef2(liveRef);180181return (Registry) Util.createProxy(RegistryImpl.class, ref, false);182}183184/**185* Creates and exports a <code>Registry</code> instance on the local186* host that accepts requests on the specified <code>port</code>.187*188* <p>The <code>Registry</code> instance is exported as if the static189* {@link UnicastRemoteObject#exportObject(Remote,int)190* UnicastRemoteObject.exportObject} method is invoked, passing the191* <code>Registry</code> instance and the specified <code>port</code> as192* arguments, except that the <code>Registry</code> instance is193* exported with a well-known object identifier, an {@link ObjID}194* instance constructed with the value {@link ObjID#REGISTRY_ID}.195*196* @param port the port on which the registry accepts requests197* @return the registry198* @throws RemoteException if the registry could not be exported199* @since 1.1200**/201public static Registry createRegistry(int port) throws RemoteException {202return new RegistryImpl(port);203}204205/**206* Creates and exports a <code>Registry</code> instance on the local207* host that uses custom socket factories for communication with that208* instance. The registry that is created listens for incoming209* requests on the given <code>port</code> using a210* <code>ServerSocket</code> created from the supplied211* <code>RMIServerSocketFactory</code>.212*213* <p>The <code>Registry</code> instance is exported as if214* the static {@link215* UnicastRemoteObject#exportObject(Remote,int,RMIClientSocketFactory,RMIServerSocketFactory)216* UnicastRemoteObject.exportObject} method is invoked, passing the217* <code>Registry</code> instance, the specified <code>port</code>, the218* specified <code>RMIClientSocketFactory</code>, and the specified219* <code>RMIServerSocketFactory</code> as arguments, except that the220* <code>Registry</code> instance is exported with a well-known object221* identifier, an {@link ObjID} instance constructed with the value222* {@link ObjID#REGISTRY_ID}.223*224* @param port port on which the registry accepts requests225* @param csf client-side <code>Socket</code> factory used to226* make connections to the registry227* @param ssf server-side <code>ServerSocket</code> factory228* used to accept connections to the registry229* @return the registry230* @throws RemoteException if the registry could not be exported231* @since 1.2232**/233public static Registry createRegistry(int port,234RMIClientSocketFactory csf,235RMIServerSocketFactory ssf)236throws RemoteException237{238return new RegistryImpl(port, csf, ssf);239}240}241242243