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: /**
020: * @author Vasily Zakharov
021: * @version $Revision: 1.1.2.3 $
022: */package org.apache.harmony.jndi.provider.rmi;
023:
024: import java.util.Hashtable;
025:
026: import javax.naming.CompositeName;
027: import javax.naming.NamingException;
028:
029: import javax.naming.spi.ResolveResult;
030:
031: import org.apache.harmony.jndi.internal.nls.Messages;
032: import org.apache.harmony.jndi.provider.GenericURLContext;
033:
034: import org.apache.harmony.jndi.provider.rmi.registry.RegistryContext;
035:
036: /**
037: * RMI URL context implementation.
038: */
039: public class rmiURLContext extends GenericURLContext {
040:
041: /**
042: * Creates instance of this context with empty environment.
043: */
044: public rmiURLContext() {
045: super (null);
046: }
047:
048: /**
049: * Creates instance of this context with specified environment.
050: *
051: * @param environment
052: * Environment to copy.
053: */
054: public rmiURLContext(Hashtable<?, ?> environment) {
055: super (environment);
056: }
057:
058: /**
059: * Determines the proper {@link RegistryContext} from the specified URL and
060: * returns the {@link ResolveResult} object with that context as resolved
061: * object and the rest of the URL as remaining name.
062: *
063: * @param url
064: * URL.
065: *
066: * @param environment
067: * Environment.
068: *
069: * @return {@link ResolveResult} object with resolved context as resolved
070: * object the rest of the URL as remaining name.
071: *
072: * @throws NamingException
073: * If some naming error occurs.
074: */
075: @Override
076: protected ResolveResult getRootURLContext(String url,
077: Hashtable<?, ?> environment) throws NamingException {
078: if (!url.startsWith(RegistryContext.RMI_URL_PREFIX)) {
079: // jndi.74=Not an RMI URL, incorrect prefix: {0}
080: throw new IllegalArgumentException(Messages.getString(
081: "jndi.74", url)); //$NON-NLS-1$
082: }
083: int length = url.length();
084: int start = RegistryContext.RMI_URL_PREFIX.length();
085: String hostName = null;
086: int port = 0;
087:
088: if ((start < length) && (url.charAt(start) == '/')) {
089: start++;
090:
091: if ((start < length) && (url.charAt(start) == '/')) {
092: start++;
093:
094: // end marks either first slash or end of URL.
095: int end = url.indexOf('/', start);
096: if (end < 0) {
097: end = length;
098: }
099:
100: // hostEnd marks either end of hostname or end of URL.
101: int hostEnd = url.indexOf(':', start);
102: if ((hostEnd < 0) || (hostEnd > end)) {
103: hostEnd = end;
104: }
105:
106: // Extracting host name.
107: if (start < hostEnd) {
108: hostName = url.substring(start, hostEnd);
109: }
110:
111: // Extracting port number.
112: int portStart = hostEnd + 1;
113: if (portStart < end) {
114: try {
115: port = Integer.parseInt(url.substring(
116: portStart, end));
117: } catch (NumberFormatException e) {
118: // jndi.75=Invalid port number in URL: {0}
119: throw (IllegalArgumentException) new IllegalArgumentException(
120: Messages.getString("jndi.75", //$NON-NLS-1$
121: url)).initCause(e);
122: }
123: }
124:
125: // Point start to suffix string.
126: start = ((end < length) ? (end + 1) : length);
127: }
128: }
129:
130: // Create remaining name.
131: CompositeName name = new CompositeName();
132: if (start < length) {
133: name.add(url.substring(start));
134: }
135:
136: return new ResolveResult(new RegistryContext(hostName, port,
137: environment), name);
138: }
139:
140: }
|