0N/A/*
2362N/A * Copyright (c) 1999, 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.jndi.cosnaming;
0N/A
0N/Aimport javax.naming.Name;
0N/Aimport javax.naming.NamingException;
0N/A
0N/Aimport java.net.MalformedURLException;
0N/Aimport java.util.Vector;
0N/Aimport java.util.StringTokenizer;
0N/Aimport com.sun.jndi.toolkit.url.UrlUtil;
0N/A
0N/A/**
0N/A * Extract components of an "iiop" or "iiopname" URL.
0N/A *
0N/A * The format of an iiopname URL is defined in INS 98-10-11 as follows:
0N/A *
0N/A * iiopname url = "iiopname://" [addr_list]["/" string_name]
0N/A * addr_list = [address ","]* address
0N/A * address = [version host [":" port]]
0N/A * host = DNS style host name | IP address
0N/A * version = major "." minor "@" | empty_string
0N/A * port = number
0N/A * major = number
0N/A * minor = number
0N/A * string_name = stringified name | empty_string
0N/A *
0N/A * The default port is 9999. The default version is "1.0"
0N/A * US-ASCII alphanumeric characters are not escaped. Any characters outside
0N/A * of this range are escaped except for the following:
0N/A * ; / : ? : @ & = + $ , - _ . ! ~ * ' ( )
0N/A * Escaped characters is escaped by using a % followed by its 2 hexadecimal
0N/A * numbers representing the octet.
0N/A *
0N/A * For backward compatibility, the "iiop" URL as defined in INS 97-6-6
0N/A * is also supported:
0N/A *
0N/A * iiop url = "iiop://" [host [":" port]] ["/" string_name]
0N/A * The default port is 900.
0N/A *
0N/A * @author Rosanna Lee
0N/A */
0N/A
0N/Apublic final class IiopUrl {
0N/A static final private int DEFAULT_IIOPNAME_PORT = 9999;
0N/A static final private int DEFAULT_IIOP_PORT = 900;
0N/A static final private String DEFAULT_HOST = "localhost";
0N/A private Vector addresses;
0N/A private String stringName;
0N/A
0N/A public static class Address {
0N/A public int port = -1;
0N/A public int major, minor;
0N/A public String host;
0N/A
0N/A public Address(String hostPortVers, boolean oldFormat)
0N/A throws MalformedURLException {
0N/A // [version host [":" port]]
0N/A int start;
0N/A
0N/A // Parse version
0N/A int at;
0N/A if (oldFormat || (at = hostPortVers.indexOf('@')) < 0) {
0N/A major = 1;
0N/A minor = 0;
0N/A start = 0; // start at the beginning
0N/A } else {
0N/A int dot = hostPortVers.indexOf('.');
0N/A if (dot < 0) {
0N/A throw new MalformedURLException(
0N/A "invalid version: " + hostPortVers);
0N/A }
0N/A try {
0N/A major = Integer.parseInt(hostPortVers.substring(0, dot));
0N/A minor = Integer.parseInt(hostPortVers.substring(dot+1, at));
0N/A } catch (NumberFormatException e) {
0N/A throw new MalformedURLException(
0N/A "Nonnumeric version: " + hostPortVers);
0N/A }
0N/A start = at + 1; // skip '@' sign
0N/A }
0N/A
0N/A // Parse host and port
0N/A int slash = hostPortVers.indexOf('/', start);
0N/A if (slash < 0) {
0N/A slash = hostPortVers.length();
0N/A }
0N/A if (hostPortVers.startsWith("[", start)) { // at IPv6 literal
0N/A int brac = hostPortVers.indexOf(']', start + 1);
0N/A if (brac < 0 || brac > slash) {
0N/A throw new IllegalArgumentException(
0N/A "IiopURL: name is an Invalid URL: " + hostPortVers);
0N/A }
0N/A
0N/A // include brackets
0N/A host = hostPortVers.substring(start, brac + 1);
0N/A start = brac + 1;
0N/A } else { // at hostname or IPv4
0N/A int colon = hostPortVers.indexOf(':', start);
0N/A int hostEnd = (colon < 0 || colon > slash)
0N/A ? slash
0N/A : colon;
0N/A if (start < hostEnd) {
0N/A host = hostPortVers.substring(start, hostEnd);
0N/A }
0N/A start = hostEnd; // skip past host
0N/A }
0N/A if ((start + 1 < slash)) {
0N/A if ( hostPortVers.startsWith(":", start)) { // parse port
0N/A start++; // skip past ":"
0N/A port = Integer.parseInt(hostPortVers.
0N/A substring(start, slash));
0N/A } else {
0N/A throw new IllegalArgumentException(
0N/A "IiopURL: name is an Invalid URL: " + hostPortVers);
0N/A }
0N/A }
0N/A start = slash;
0N/A if ("".equals(host) || host == null) {
0N/A host = DEFAULT_HOST ;
0N/A }
0N/A if (port == -1) {
0N/A port = (oldFormat ? DEFAULT_IIOP_PORT :
0N/A DEFAULT_IIOPNAME_PORT);
0N/A }
0N/A }
0N/A }
0N/A
0N/A public Vector getAddresses() {
0N/A return addresses;
0N/A }
0N/A
0N/A /**
0N/A * Returns a possibly empty but non-null string that is the "string_name"
0N/A * portion of the URL.
0N/A */
0N/A public String getStringName() {
0N/A return stringName;
0N/A }
0N/A
0N/A public Name getCosName() throws NamingException {
0N/A return CNCtx.parser.parse(stringName);
0N/A }
0N/A
0N/A public IiopUrl(String url) throws MalformedURLException {
0N/A int addrStart;
0N/A boolean oldFormat;
0N/A
0N/A if (url.startsWith("iiopname://")) {
0N/A oldFormat = false;
0N/A addrStart = 11;
0N/A } else if (url.startsWith("iiop://")) {
0N/A oldFormat = true;
0N/A addrStart = 7;
0N/A } else {
0N/A throw new MalformedURLException("Invalid iiop/iiopname URL: " + url);
0N/A }
0N/A int addrEnd = url.indexOf('/', addrStart);
0N/A if (addrEnd < 0) {
0N/A addrEnd = url.length();
0N/A stringName = "";
0N/A } else {
0N/A stringName = UrlUtil.decode(url.substring(addrEnd+1));
0N/A }
0N/A addresses = new Vector(3);
0N/A if (oldFormat) {
0N/A // Only one host:port part, not multiple
0N/A addresses.addElement(
0N/A new Address(url.substring(addrStart, addrEnd), oldFormat));
0N/A } else {
0N/A StringTokenizer tokens =
0N/A new StringTokenizer(url.substring(addrStart, addrEnd), ",");
0N/A while (tokens.hasMoreTokens()) {
0N/A addresses.addElement(new Address(tokens.nextToken(), oldFormat));
0N/A }
0N/A if (addresses.size() == 0) {
0N/A addresses.addElement(new Address("", oldFormat));
0N/A }
0N/A }
0N/A }
0N/A
0N/A // for testing only
0N/A /*public static void main(String[] args) {
0N/A try {
0N/A IiopUrl url = new IiopUrl(args[0]);
0N/A Vector addrs = url.getAddresses();
0N/A String name = url.getStringName();
0N/A
0N/A for (int i = 0; i < addrs.size(); i++) {
0N/A Address addr = (Address)addrs.elementAt(i);
0N/A System.out.println("host: " + addr.host);
0N/A System.out.println("port: " + addr.port);
0N/A System.out.println("version: " + addr.major + " " + addr.minor);
0N/A }
0N/A System.out.println("name: " + name);
0N/A } catch (MalformedURLException e) {
0N/A e.printStackTrace();
0N/A }
0N/A } */
0N/A}