API Overview API Index Package Overview Direct link to this page
JDK 1.6
  java.rmi. Naming View Javadoc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339

/*
 * @(#)Naming.java	1.24 05/11/17
 *
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package java.rmi;

import java.rmi.registry.*;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * The <code>Naming</code> class provides methods for storing and obtaining
 * references to remote objects in a remote object registry.  Each method of
 * the <code>Naming</code> class takes as one of its arguments a name that
 * is a <code>java.lang.String</code> in URL format (without the
 * scheme component) of the form:
 *
 * <PRE>
 *    //host:port/name
 * </PRE>
 * 
 * <P>where <code>host</code> is the host (remote or local) where the registry
 * is located, <code>port</code> is the port number on which the registry
 * accepts calls, and where <code>name</code> is a simple string uninterpreted
 * by the registry. Both <code>host</code> and <code>port</code> are optional.
 * If <code>host</code> is omitted, the host defaults to the local host. If
 * <code>port</code> is omitted, then the port defaults to 1099, the
 * "well-known" port that RMI's registry, <code>rmiregistry</code>, uses.
 *
 * <P><em>Binding</em> a name for a remote object is associating or
 * registering a name for a remote object that can be used at a later time to
 * look up that remote object.  A remote object can be associated with a name
 * using the <code>Naming</code> class's <code>bind</code> or
 * <code>rebind</code> methods.
 *
 * <P>Once a remote object is registered (bound) with the RMI registry on the
 * local host, callers on a remote (or local) host can lookup the remote
 * object by name, obtain its reference, and then invoke remote methods on the
 * object.  A registry may be shared by all servers running on a host or an
 * individual server process may create and use its own registry if desired
 * (see <code>java.rmi.registry.LocateRegistry.createRegistry</code> method
 * for details).
 *
 * @version 1.13, 09/05/99
 * @author  Ann Wollrath
 * @author  Roger Riggs
 * @since   JDK1.1
 * @see     java.rmi.registry.Registry
 * @see     java.rmi.registry.LocateRegistry
 * @see     java.rmi.registry.LocateRegistry#createRegistry(int)
 */
public final class Naming {
    /**
     * Disallow anyone from creating one of these
     */
    private Naming() {}

    /**
     * Returns a reference, a stub, for the remote object associated
     * with the specified <code>name</code>.
     *
     * @param name a name in URL format (without the scheme component) 
     * @return a reference for a remote object
     * @exception NotBoundException if name is not currently bound
     * @exception RemoteException if registry could not be contacted
     * @exception AccessException if this operation is not permitted
     * @exception MalformedURLException if the name is not an appropriately
     *  formatted URL
     * @since JDK1.1
     */
    public static Remote lookup(String name)
	throws NotBoundException,
	    java.net.MalformedURLException,
	    RemoteException
    {
	ParsedNamingURL parsed = parseURL(name);
	Registry registry = getRegistry(parsed);

	if (parsed.name == null)
	    return registry;
	return registry.lookup(parsed.name);
    }

    /**
     * Binds the specified <code>name</code> to a remote object.
     *
     * @param name a name in URL format (without the scheme component) 
     * @param obj a reference for the remote object (usually a stub)
     * @exception AlreadyBoundException if name is already bound
     * @exception MalformedURLException if the name is not an appropriately
     *  formatted URL
     * @exception RemoteException if registry could not be contacted
     * @exception AccessException if this operation is not permitted (if
     * originating from a non-local host, for example)
     * @since JDK1.1
     */
    public static void bind(String name, Remote obj)
	throws AlreadyBoundException,
	    java.net.MalformedURLException,
	    RemoteException
    {
	ParsedNamingURL parsed = parseURL(name);
	Registry registry = getRegistry(parsed);

	if (obj == null)
	    throw new NullPointerException("cannot bind to null");

	registry.bind(parsed.name, obj);
    }

    /**
     * Destroys the binding for the specified name that is associated
     * with a remote object.
     *
     * @param name a name in URL format (without the scheme component) 
     * @exception NotBoundException if name is not currently bound
     * @exception MalformedURLException if the name is not an appropriately
     *  formatted URL
     * @exception RemoteException if registry could not be contacted
     * @exception AccessException if this operation is not permitted (if
     * originating from a non-local host, for example)
     * @since JDK1.1
     */
    public static void unbind(String name)
	throws RemoteException,
	    NotBoundException,
	    java.net.MalformedURLException
    {
	ParsedNamingURL parsed = parseURL(name);
	Registry registry = getRegistry(parsed);

	registry.unbind(parsed.name);
    }

    /** 
     * Rebinds the specified name to a new remote object. Any existing
     * binding for the name is replaced.
     *
     * @param name a name in URL format (without the scheme component)
     * @param obj new remote object to associate with the name
     * @exception MalformedURLException if the name is not an appropriately
     *  formatted URL
     * @exception RemoteException if registry could not be contacted
     * @exception AccessException if this operation is not permitted (if
     * originating from a non-local host, for example)
     * @since JDK1.1
     */
    public static void rebind(String name, Remote obj)
	throws RemoteException, java.net.MalformedURLException
    {
	ParsedNamingURL parsed = parseURL(name);
	Registry registry = getRegistry(parsed);

	if (obj == null)
	    throw new NullPointerException("cannot bind to null");

	registry.rebind(parsed.name, obj);
    }

    /**
     * Returns an array of the names bound in the registry.  The names are
     * URL-formatted (without the scheme component) strings. The array contains
     * a snapshot of the names present in the registry at the time of the
     * call.
     *
     * @param 	name a registry name in URL format (without the scheme
     *		component)
     * @return 	an array of names (in the appropriate format) bound
     * 		in the registry
     * @exception MalformedURLException if the name is not an appropriately
     *  formatted URL
     * @exception RemoteException if registry could not be contacted.
     * @since JDK1.1
     */
    public static String[] list(String name)
	throws RemoteException, java.net.MalformedURLException
    {
	ParsedNamingURL parsed = parseURL(name);
	Registry registry = getRegistry(parsed);

	String prefix = "";
 	if (parsed.port > 0 || !parsed.host.equals(""))
	    prefix += "//" + parsed.host;
	if (parsed.port > 0)
	    prefix += ":" + parsed.port;
	prefix += "/";

	String[] names = registry.list();
	for (int i = 0; i < names.length; i++) {
	    names[i] = prefix + names[i];
	}
	return names;
    }

    /**
     * Returns a registry reference obtained from information in the URL.
     */
    private static Registry getRegistry(ParsedNamingURL parsed)
	throws RemoteException
    {
	return LocateRegistry.getRegistry(parsed.host, parsed.port);
    }

    /**
     * Dissect Naming URL strings to obtain referenced host, port and
     * object name.
     *
     * @return an object which contains each of the above
     * components.
     *
     * @exception MalformedURLException if given url string is malformed
     */
    private static ParsedNamingURL parseURL(String str) 
	throws MalformedURLException 
    {
	try {
	    return intParseURL(str);
	} catch (URISyntaxException ex) {
	    /* With RFC 3986 URI handling, 'rmi://:<port>' and
	     * '//:<port>' forms will result in a URI syntax exception
	     * Convert the authority to a localhost:<port> form
	     */
	    MalformedURLException mue = new MalformedURLException(
		"invalid URL String: " + str);
	    mue.initCause(ex);
	    int indexSchemeEnd = str.indexOf(':');
	    int indexAuthorityBegin = str.indexOf("__JOT_PIECE_14__
	    if (indexAuthorityBegin <__JOT_PIECE_47__) {
		throw mue;
	    }
	    if ((indexAuthorityBegin == 0) ||
		    ((indexSchemeEnd > 0) &&
		    (indexAuthorityBegin == indexSchemeEnd + 1))) {
		int indexHostBegin = indexAuthorityBegin + 2;
		String newStr = str.substring(0, indexHostBegin) +
				"localhost" +
				str.substring(indexHostBegin);
		try {
		    return intParseURL(newStr);
		} catch (URISyntaxException inte) {
		    throw mue;
		} catch (MalformedURLException inte) {
		    throw inte;
		}
	    }
	    throw mue;
	}
    }
    
    private static ParsedNamingURL intParseURL(String str)
	throws MalformedURLException, URISyntaxException
    {
	URI uri = new URI(str);
	if (uri.isOpaque()) {
	    throw new MalformedURLException(
		"not a hierarchical URL: " + str);
	}
	if (uri.getFragment() != null) {
	    throw new MalformedURLException(
		"invalid character, '#', in URL name: " + str);
	} else if (uri.getQuery() != null) {
	    throw new MalformedURLException(
		"invalid character, '?', in URL name: " + str);
	} else if (uri.getUserInfo() != null) {
	    throw new MalformedURLException(
		"invalid character, '@', in URL host: " + str);
	}
	String scheme = uri.getScheme();
	if (scheme != null && !scheme.equals("rmi")) {
	    throw new MalformedURLException("invalid URL scheme: " + str);
	}

	String name = uri.getPath();
	if (name != null) {
	    if (name.startsWith("/")) {
		name = name.substring(1);
	    }
	    if (name.length() == 0) {
		name = null;
	    }
	}

	String host = uri.getHost();
	if (host == null) {
	    host = "";
	    try {
		/*
		 * With 2396 URI handling, forms such as 'rmi://host:bar'
		 * or 'rmi://:<port>' are parsed into a registry based
		 * authority. We only want to allow server based naming
		 * authorities.
		 */
		uri.parseServerAuthority();
	    } catch (URISyntaxException use) {
		// Check if the authority is of form ':<port>'
		String authority = uri.getAuthority();
		if (authority != null && authority.startsWith(":")) {
		    // Convert the authority to 'localhost:<port>' form
		    authority = "localhost" + authority;
		    try {
			uri = new URI(null, authority, null, null, null);
			// Make sure it now parses to a valid server based
			// naming authority
			uri.parseServerAuthority();
		    } catch (URISyntaxException use2) {
			throw new
			    MalformedURLException("invalid authority: " + str);
		    }
		} else {
		    throw new
			MalformedURLException("invalid authority: " + str);
		}
	    }
	}
	int port = uri.getPort();
	if (port == -1) {
	    port = Registry.REGISTRY_PORT;
	}
	return new ParsedNamingURL(host, port, name);
    }
    
    /**
     * Simple class to enable multiple URL return values.
     */
    private static class ParsedNamingURL {
	String host;
	int port;
	String name;
	
	ParsedNamingURL(String host, int port, String name) {
	    this.host = host;
	    this.port = port;
	    this.name = name;
	}
    }
}

Generated By: JavaOnTracks Doclet 0.1.4     ©Thibaut Colar