blob: da4fc119f3b145f5ad28cda67d1b05ea840ed565 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1999-2004 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package com.sun.jndi.url.rmi;
27
28import java.util.Hashtable;
29import java.rmi.registry.LocateRegistry;
30
31import javax.naming.*;
32import javax.naming.spi.ResolveResult;
33import com.sun.jndi.toolkit.url.GenericURLContext;
34import com.sun.jndi.rmi.registry.RegistryContext;
35
36
37/**
38 * An RMI URL context resolves names that are URLs of the form
39 * <pre>
40 * rmi://[host][:port][/[object]]
41 * or
42 * rmi:[/][object]
43 * </pre>
44 * If an object is specified, the URL resolves to the named object.
45 * Otherwise, the URL resolves to the specified RMI registry.
46 *
47 * @author Scott Seligman
48 */
49public class rmiURLContext extends GenericURLContext {
50
51 public rmiURLContext(Hashtable env) {
52 super(env);
53 }
54
55 /**
56 * Resolves the registry portion of "url" to the corresponding
57 * RMI registry, and returns the atomic object name as the
58 * remaining name.
59 */
60 protected ResolveResult getRootURLContext(String url, Hashtable env)
61 throws NamingException
62 {
63 if (!url.startsWith("rmi:")) {
64 throw (new IllegalArgumentException(
65 "rmiURLContext: name is not an RMI URL: " + url));
66 }
67
68 // Parse the URL.
69
70 String host = null;
71 int port = -1;
72 String objName = null;
73
74 int i = 4; // index into url, following the "rmi:"
75
76 if (url.startsWith("//", i)) { // parse "//host:port"
77 i += 2; // skip past "//"
78 int slash = url.indexOf('/', i);
79 if (slash < 0) {
80 slash = url.length();
81 }
82 if (url.startsWith("[", i)) { // at IPv6 literal
83 int brac = url.indexOf(']', i + 1);
84 if (brac < 0 || brac > slash) {
85 throw new IllegalArgumentException(
86 "rmiURLContext: name is an Invalid URL: " + url);
87 }
88 host = url.substring(i, brac + 1); // include brackets
89 i = brac + 1; // skip past "[...]"
90 } else { // at host name or IPv4
91 int colon = url.indexOf(':', i);
92 int hostEnd = (colon < 0 || colon > slash)
93 ? slash
94 : colon;
95 if (i < hostEnd) {
96 host = url.substring(i, hostEnd);
97 }
98 i = hostEnd; // skip past host
99 }
100 if ((i + 1 < slash)) {
101 if ( url.startsWith(":", i)) { // parse port
102 i++; // skip past ":"
103 port = Integer.parseInt(url.substring(i, slash));
104 } else {
105 throw new IllegalArgumentException(
106 "rmiURLContext: name is an Invalid URL: " + url);
107 }
108 }
109 i = slash;
110 }
111 if ("".equals(host)) {
112 host = null;
113 }
114 if (url.startsWith("/", i)) { // skip "/" before object name
115 i++;
116 }
117 if (i < url.length()) {
118 objName = url.substring(i);
119 }
120
121 // Represent object name as empty or single-component composite name.
122 CompositeName remaining = new CompositeName();
123 if (objName != null) {
124 remaining.add(objName);
125 }
126
127 // Debug
128 //System.out.println("host=" + host + " port=" + port +
129 // " objName=" + remaining.toString() + "\n");
130
131 // Create a registry context.
132 Context regCtx = new RegistryContext(host, port, env);
133
134 return (new ResolveResult(regCtx, remaining));
135 }
136}