KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > rmi > registry > LocateRegistry


1 /*
2  * @(#)LocateRegistry.java 1.33 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.rmi.registry;
9
10 import java.rmi.RemoteException JavaDoc;
11 import java.rmi.server.ObjID JavaDoc;
12 import java.rmi.server.RMIClientSocketFactory JavaDoc;
13 import java.rmi.server.RMIServerSocketFactory JavaDoc;
14 import java.rmi.server.RemoteRef JavaDoc;
15 import java.rmi.server.UnicastRemoteObject JavaDoc;
16 import sun.rmi.registry.RegistryImpl;
17 import sun.rmi.server.UnicastRef2;
18 import sun.rmi.server.UnicastRef;
19 import sun.rmi.server.Util;
20 import sun.rmi.transport.LiveRef;
21 import sun.rmi.transport.tcp.TCPEndpoint;
22
23 /**
24  * <code>LocateRegistry</code> is used to obtain a reference to a bootstrap
25  * remote object registry on a particular host (including the local host), or
26  * to create a remote object registry that accepts calls on a specific port.
27  *
28  * <p> Note that a <code>getRegistry</code> call does not actually make a
29  * connection to the remote host. It simply creates a local reference to
30  * the remote registry and will succeed even if no registry is running on
31  * the remote host. Therefore, a subsequent method invocation to a remote
32  * registry returned as a result of this method may fail.
33  *
34  * @version 1.33, 12/19/03
35  * @author Ann Wollrath
36  * @author Peter Jones
37  * @since JDK1.1
38  * @see java.rmi.registry.Registry
39  */

40 public final class LocateRegistry {
41
42     /**
43      * Private constructor to disable public construction.
44      */

45     private LocateRegistry() {}
46
47     /**
48      * Returns a reference to the the remote object <code>Registry</code> for
49      * the local host on the default registry port of 1099.
50      *
51      * @return reference (a stub) to the remote object registry
52      * @exception RemoteException if the reference could not be created
53      * @since JDK1.1
54      */

55     public static Registry JavaDoc getRegistry()
56     throws RemoteException JavaDoc
57     {
58     return getRegistry(null, Registry.REGISTRY_PORT);
59     }
60
61     /**
62      * Returns a reference to the the remote object <code>Registry</code> for
63      * the local host on the specified <code>port</code>.
64      *
65      * @param port port on which the registry accepts requests
66      * @return reference (a stub) to the remote object registry
67      * @exception RemoteException if the reference could not be created
68      * @since JDK1.1
69      */

70     public static Registry JavaDoc getRegistry(int port)
71     throws RemoteException JavaDoc
72     {
73     return getRegistry(null, port);
74     }
75
76     /**
77      * Returns a reference to the remote object <code>Registry</code> on the
78      * specified <code>host</code> on the default registry port of 1099. If
79      * <code>host</code> is <code>null</code>, the local host is used.
80      *
81      * @param host host for the remote registry
82      * @return reference (a stub) to the remote object registry
83      * @exception RemoteException if the reference could not be created
84      * @since JDK1.1
85      */

86     public static Registry JavaDoc getRegistry(String JavaDoc host)
87     throws RemoteException JavaDoc
88     {
89     return getRegistry(host, Registry.REGISTRY_PORT);
90     }
91
92     /**
93      * Returns a reference to the remote object <code>Registry</code> on the
94      * specified <code>host</code> and <code>port</code>. If <code>host</code>
95      * is <code>null</code>, the local host is used.
96      *
97      * @param host host for the remote registry
98      * @param port port on which the registry accepts requests
99      * @return reference (a stub) to the remote object registry
100      * @exception RemoteException if the reference could not be created
101      * @since JDK1.1
102      */

103     public static Registry JavaDoc getRegistry(String JavaDoc host, int port)
104     throws RemoteException JavaDoc
105     {
106     return getRegistry(host, port, null);
107     }
108
109     /**
110      * Returns a locally created remote reference to the remote object
111      * <code>Registry</code> on the specified <code>host</code> and
112      * <code>port</code>. Communication with this remote registry will
113      * use the supplied <code>RMIClientSocketFactory</code> <code>csf</code>
114      * to create <code>Socket</code> connections to the registry on the
115      * remote <code>host</code> and <code>port</code>.
116      *
117      * @param host host for the remote registry
118      * @param port port on which the registry accepts requests
119      * @param csf client-side <code>Socket</code> factory used to
120      * make connections to the registry. If <code>csf</code>
121      * is null, then the default client-side <code>Socket</code>
122      * factory will be used in the registry stub.
123      * @return reference (a stub) to the remote registry
124      * @exception RemoteException if the reference could not be created
125      * @since 1.2
126      */

127     public static Registry JavaDoc getRegistry(String JavaDoc host, int port,
128                        RMIClientSocketFactory JavaDoc csf)
129     throws RemoteException JavaDoc
130     {
131     Registry JavaDoc registry = null;
132
133     if (port <= 0)
134         port = Registry.REGISTRY_PORT;
135
136     if (host == null || host.length() == 0) {
137         // If host is blank (as returned by "file:" URL in 1.0.2 used in
138
// java.rmi.Naming), try to convert to real local host name so
139
// that the RegistryImpl's checkAccess will not fail.
140
try {
141         host = java.net.InetAddress.getLocalHost().getHostAddress();
142         } catch (Exception JavaDoc e) {
143         // If that failed, at least try "" (localhost) anyway...
144
host = "";
145         }
146     }
147
148     /*
149      * Create a proxy for the registry with the given host, port, and
150      * client socket factory. If the supplied client socket factory is
151      * null, then the ref type is a UnicastRef, otherwise the ref type
152      * is a UnicastRef2. If the property
153      * java.rmi.server.ignoreStubClasses is true, then the proxy
154      * returned is an instance of a dynamic proxy class that implements
155      * the Registry interface; otherwise the proxy returned is an
156      * instance of the pregenerated stub class for RegistryImpl.
157      **/

158     LiveRef liveRef =
159         new LiveRef(new ObjID JavaDoc(ObjID.REGISTRY_ID),
160             new TCPEndpoint(host, port, csf, null),
161             false);
162     RemoteRef JavaDoc ref =
163         (csf == null) ? new UnicastRef(liveRef) : new UnicastRef2(liveRef);
164
165     return (Registry JavaDoc) Util.createProxy(RegistryImpl.class, ref, false);
166     }
167
168     /**
169      * Creates and exports a <code>Registry</code> instance on the local
170      * host that accepts requests on the specified <code>port</code>.
171      *
172      * <p>The <code>Registry</code> instance is exported as if the static
173      * {@link UnicastRemoteObject.exportObject(Remote,int)
174      * UnicastRemoteObject.exportObject} method is invoked, passing the
175      * <code>Registry</code> instance and the specified <code>port</code> as
176      * arguments, except that the <code>Registry</code> instance is
177      * exported with a well-known object identifier, an {@link ObjID}
178      * instance constructed with the value {@link ObjID#REGISTRY_ID}.
179      *
180      * @param port the port on which the registry accepts requests
181      * @return the registry
182      * @exception RemoteException if the registry could not be exported
183      * @since JDK1.1
184      **/

185     public static Registry JavaDoc createRegistry(int port) throws RemoteException JavaDoc {
186     return new RegistryImpl(port);
187     }
188
189     /**
190      * Creates and exports a <code>Registry</code> instance on the local
191      * host that uses custom socket factories for communication with that
192      * instance. The registry that is created listens for incoming
193      * requests on the given <code>port</code> using a
194      * <code>ServerSocket</code> created from the supplied
195      * <code>RMIServerSocketFactory</code>.
196      *
197      * <p>The <code>Registry</code> instance is exported as if
198      * the static {@link
199      * UnicastRemoteObject.exportObject(Remote,int,RMIClientSocketFactory,RMIServerSocketFactory)
200      * UnicastRemoteObject.exportObject} method is invoked, passing the
201      * <code>Registry</code> instance, the specified <code>port</code>, the
202      * specified <code>RMIClientSocketFactory</code>, and the specified
203      * <code>RMIServerSocketFactory</code> as arguments, except that the
204      * <code>Registry</code> instance is exported with a well-known object
205      * identifier, an {@link ObjID} instance constructed with the value
206      * {@link ObjID#REGISTRY_ID}.
207      *
208      * @param port port on which the registry accepts requests
209      * @param csf client-side <code>Socket</code> factory used to
210      * make connections to the registry
211      * @param ssf server-side <code>ServerSocket</code> factory
212      * used to accept connections to the registry
213      * @return the registry
214      * @exception RemoteException if the registry could not be exported
215      * @since 1.2
216      **/

217     public static Registry JavaDoc createRegistry(int port,
218                       RMIClientSocketFactory JavaDoc csf,
219                       RMIServerSocketFactory JavaDoc ssf)
220     throws RemoteException JavaDoc
221     {
222     return new RegistryImpl(port, csf, ssf);
223     }
224 }
225
Popular Tags