001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: *
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: package java.rmi;
020:
021: import java.net.MalformedURLException;
022: import java.net.URI;
023: import java.net.URISyntaxException;
024: import java.rmi.registry.LocateRegistry;
025: import java.rmi.registry.Registry;
026:
027: import org.apache.harmony.rmi.internal.nls.Messages;
028:
029: public final class Naming {
030:
031: // This class could not be instantiated.
032: private Naming() {
033: }
034:
035: public static String[] list(String name) throws RemoteException,
036: MalformedURLException {
037: if (name == null) {
038: // rmi.00=URL could not be null.
039: throw new NullPointerException(Messages.getString("rmi.00")); //$NON-NLS-1$
040: }
041: RegistryURL url = getRegistryURL(name, true);
042: Registry reg = LocateRegistry.getRegistry(url.host, url.port);
043: String[] names = reg.list();
044: String regName = "//" + ((url.host == null) ? "" : url.host) + ":" + url.port + "/"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
045:
046: for (int i = 0; i < names.length; ++i) {
047: names[i] = regName + names[i];
048: }
049: return names;
050: }
051:
052: public static void rebind(String name, Remote obj)
053: throws RemoteException, MalformedURLException {
054: if (name == null) {
055: // rmi.00=URL could not be null.
056: throw new NullPointerException(Messages.getString("rmi.00")); //$NON-NLS-1$
057: }
058: RegistryURL url = getRegistryURL(name, false);
059: Registry reg = LocateRegistry.getRegistry(url.host, url.port);
060: reg.rebind(url.name, obj);
061: }
062:
063: public static void unbind(String name) throws RemoteException,
064: NotBoundException, MalformedURLException {
065: if (name == null) {
066: // rmi.00=URL could not be null.
067: throw new NullPointerException(Messages.getString("rmi.00")); //$NON-NLS-1$
068: }
069: RegistryURL url = getRegistryURL(name, false);
070: Registry reg = LocateRegistry.getRegistry(url.host, url.port);
071: reg.unbind(url.name);
072: }
073:
074: public static void bind(String name, Remote obj)
075: throws AlreadyBoundException, MalformedURLException,
076: RemoteException {
077: if (obj == null) {
078: throw new NullPointerException(Messages.getString("rmi.5C")); //$NON-NLS-1$
079: }
080:
081: if (name == null) {
082: // rmi.00=URL could not be null.
083: throw new NullPointerException(Messages.getString("rmi.00")); //$NON-NLS-1$
084: }
085: RegistryURL url = getRegistryURL(name, false);
086: Registry reg = LocateRegistry.getRegistry(url.host, url.port);
087: reg.bind(url.name, obj);
088: }
089:
090: public static Remote lookup(String name) throws NotBoundException,
091: MalformedURLException, RemoteException {
092: if (name == null) {
093: // rmi.00=URL could not be null.
094: throw new NullPointerException(Messages.getString("rmi.00")); //$NON-NLS-1$
095: }
096: RegistryURL url = getRegistryURL(name, false);
097: Registry reg = LocateRegistry.getRegistry(url.host, url.port);
098: return reg.lookup(url.name);
099: }
100:
101: /*
102: * Parse the given name and returns URL containing parsed parameters.
103: */
104: private static RegistryURL getRegistryURL(String strUrl,
105: boolean ignoreEmptyNames) throws MalformedURLException {
106: URI uri;
107:
108: try {
109: uri = new URI(strUrl);
110: } catch (URISyntaxException use) {
111: // rmi.01=Invalid URL "{0}":{1}
112: throw new MalformedURLException(Messages.getString(
113: "rmi.01", strUrl, use)); //$NON-NLS-1$
114: }
115: String prot = uri.getScheme();
116:
117: if ((prot != null) && !prot.toLowerCase().equals("rmi")) { //$NON-NLS-1$
118: // rmi.02=Non-rmi protocol in URL "{0}": {1}
119: throw new MalformedURLException(Messages.getString(
120: "rmi.02", strUrl, prot)); //$NON-NLS-1$
121: }
122:
123: if (uri.getUserInfo() != null) {
124: // rmi.03=Invalid character ('@') in URL "{0}" host part.
125: throw new MalformedURLException(Messages.getString(
126: "rmi.03", strUrl)); //$NON-NLS-1$
127: } else if (uri.getQuery() != null) {
128: // rmi.04=Invalid character ('?') in URL "{0}" name part.
129: throw new MalformedURLException(Messages.getString(
130: "rmi.04", strUrl)); //$NON-NLS-1$
131: } else if (uri.getFragment() != null) {
132: // rmi.05=Invalid character ('\#') in URL "{0}" name part.
133: throw new MalformedURLException(Messages.getString(
134: "rmi.05", strUrl)); //$NON-NLS-1$
135: }
136: int port = uri.getPort();
137: String auth = uri.getAuthority();
138:
139: if (auth != null && auth.startsWith(":") && auth.length() != 1) { //$NON-NLS-1$
140: // to handle URLs like "rmi://:1099/xxx"
141: try {
142: port = Integer.parseInt(auth.substring(1));
143: } catch (NumberFormatException nfe) {
144: // rmi.06=Invalid port number in URL "{0}": {0}
145: throw new MalformedURLException(Messages.getString(
146: "rmi.06", strUrl, auth.substring(1))); //$NON-NLS-1$
147: }
148: }
149:
150: if (port == -1) {
151: port = Registry.REGISTRY_PORT;
152: }
153: String path = uri.getPath();
154:
155: if (!ignoreEmptyNames) {
156: if (path == null || path.length() == 0) {
157: // rmi.07=Name could not be empty (URL: "{0}").
158: throw new MalformedURLException(Messages.getString(
159: "rmi.07", strUrl)); //$NON-NLS-1$
160: }
161: }
162:
163: if (path != null && path.startsWith("/")) { //$NON-NLS-1$
164: path = path.substring(1);
165: }
166: String host = uri.getHost();
167:
168: if (host == null) {
169: host = "localhost"; //$NON-NLS-1$
170: }
171: return new RegistryURL(host, port, path);
172: }
173:
174: /**
175: * Auxiliary class holding information about host, port and name.
176: */
177: private static class RegistryURL {
178:
179: // Host name.
180: String host;
181:
182: // Port number.
183: int port;
184:
185: // bind name
186: String name;
187:
188: /**
189: * Constructs RegistryURL from the given host, port and bind name.
190: */
191: RegistryURL(String host, int port, String name) {
192: this.host = host;
193: this.port = port;
194: this.name = name;
195: }
196: }
197: }
|