Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/jndi/cosnaming/IiopUrl.java
38924 views
/*1* Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.jndi.cosnaming;2627import javax.naming.Name;28import javax.naming.NamingException;2930import java.net.MalformedURLException;31import java.util.Vector;32import java.util.StringTokenizer;33import com.sun.jndi.toolkit.url.UrlUtil;3435/**36* Extract components of an "iiop" or "iiopname" URL.37*38* The format of an iiopname URL is defined in INS 98-10-11 as follows:39*40* iiopname url = "iiopname://" [addr_list]["/" string_name]41* addr_list = [address ","]* address42* address = [version host [":" port]]43* host = DNS style host name | IP address44* version = major "." minor "@" | empty_string45* port = number46* major = number47* minor = number48* string_name = stringified name | empty_string49*50* The default port is 9999. The default version is "1.0"51* US-ASCII alphanumeric characters are not escaped. Any characters outside52* of this range are escaped except for the following:53* ; / : ? : @ & = + $ , - _ . ! ~ * ' ( )54* Escaped characters is escaped by using a % followed by its 2 hexadecimal55* numbers representing the octet.56*57* For backward compatibility, the "iiop" URL as defined in INS 97-6-658* is also supported:59*60* iiop url = "iiop://" [host [":" port]] ["/" string_name]61* The default port is 900.62*63* @author Rosanna Lee64*/6566public final class IiopUrl {67static final private int DEFAULT_IIOPNAME_PORT = 9999;68static final private int DEFAULT_IIOP_PORT = 900;69static final private String DEFAULT_HOST = "localhost";70private Vector<Address> addresses;71private String stringName;7273public static class Address {74public int port = -1;75public int major, minor;76public String host;7778public Address(String hostPortVers, boolean oldFormat)79throws MalformedURLException {80// [version host [":" port]]81int start;8283// Parse version84int at;85if (oldFormat || (at = hostPortVers.indexOf('@')) < 0) {86major = 1;87minor = 0;88start = 0; // start at the beginning89} else {90int dot = hostPortVers.indexOf('.');91if (dot < 0) {92throw new MalformedURLException(93"invalid version: " + hostPortVers);94}95try {96major = Integer.parseInt(hostPortVers.substring(0, dot));97minor = Integer.parseInt(hostPortVers.substring(dot+1, at));98} catch (NumberFormatException e) {99throw new MalformedURLException(100"Nonnumeric version: " + hostPortVers);101}102start = at + 1; // skip '@' sign103}104105// Parse host and port106int slash = hostPortVers.indexOf('/', start);107if (slash < 0) {108slash = hostPortVers.length();109}110if (hostPortVers.startsWith("[", start)) { // at IPv6 literal111int brac = hostPortVers.indexOf(']', start + 1);112if (brac < 0 || brac > slash) {113throw new IllegalArgumentException(114"IiopURL: name is an Invalid URL: " + hostPortVers);115}116117// include brackets118host = hostPortVers.substring(start, brac + 1);119start = brac + 1;120} else { // at hostname or IPv4121int colon = hostPortVers.indexOf(':', start);122int hostEnd = (colon < 0 || colon > slash)123? slash124: colon;125if (start < hostEnd) {126host = hostPortVers.substring(start, hostEnd);127}128start = hostEnd; // skip past host129}130if ((start + 1 < slash)) {131if ( hostPortVers.startsWith(":", start)) { // parse port132start++; // skip past ":"133port = Integer.parseInt(hostPortVers.134substring(start, slash));135} else {136throw new IllegalArgumentException(137"IiopURL: name is an Invalid URL: " + hostPortVers);138}139}140start = slash;141if ("".equals(host) || host == null) {142host = DEFAULT_HOST ;143}144if (port == -1) {145port = (oldFormat ? DEFAULT_IIOP_PORT :146DEFAULT_IIOPNAME_PORT);147}148}149}150151public Vector<Address> getAddresses() {152return addresses;153}154155/**156* Returns a possibly empty but non-null string that is the "string_name"157* portion of the URL.158*/159public String getStringName() {160return stringName;161}162163public Name getCosName() throws NamingException {164return CNCtx.parser.parse(stringName);165}166167public IiopUrl(String url) throws MalformedURLException {168int addrStart;169boolean oldFormat;170171if (url.startsWith("iiopname://")) {172oldFormat = false;173addrStart = 11;174} else if (url.startsWith("iiop://")) {175oldFormat = true;176addrStart = 7;177} else {178throw new MalformedURLException("Invalid iiop/iiopname URL: " + url);179}180int addrEnd = url.indexOf('/', addrStart);181if (addrEnd < 0) {182addrEnd = url.length();183stringName = "";184} else {185stringName = UrlUtil.decode(url.substring(addrEnd+1));186}187addresses = new Vector<>(3);188if (oldFormat) {189// Only one host:port part, not multiple190addresses.addElement(191new Address(url.substring(addrStart, addrEnd), oldFormat));192} else {193StringTokenizer tokens =194new StringTokenizer(url.substring(addrStart, addrEnd), ",");195while (tokens.hasMoreTokens()) {196addresses.addElement(new Address(tokens.nextToken(), oldFormat));197}198if (addresses.size() == 0) {199addresses.addElement(new Address("", oldFormat));200}201}202}203204// for testing only205/*public static void main(String[] args) {206try {207IiopUrl url = new IiopUrl(args[0]);208Vector addrs = url.getAddresses();209String name = url.getStringName();210211for (int i = 0; i < addrs.size(); i++) {212Address addr = (Address)addrs.elementAt(i);213System.out.println("host: " + addr.host);214System.out.println("port: " + addr.port);215System.out.println("version: " + addr.major + " " + addr.minor);216}217System.out.println("name: " + name);218} catch (MalformedURLException e) {219e.printStackTrace();220}221} */222}223224225