jdk/src/share/classes/java/rmi/Naming.java
author anthony
Tue, 05 May 2009 17:56:31 +0400
changeset 3444 18840bd1c784
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6818787: It is possible to reposition the security icon too far from the border of the window on X11 Summary: The constraints for the position of the icon are moved to the shared code Reviewed-by: art, dcherepanov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1996-2005 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package java.rmi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.rmi.registry.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.URI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.URISyntaxException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * The <code>Naming</code> class provides methods for storing and obtaining
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * references to remote objects in a remote object registry.  Each method of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * the <code>Naming</code> class takes as one of its arguments a name that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * is a <code>java.lang.String</code> in URL format (without the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * scheme component) of the form:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <PRE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *    //host:port/name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * </PRE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <P>where <code>host</code> is the host (remote or local) where the registry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * is located, <code>port</code> is the port number on which the registry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * accepts calls, and where <code>name</code> is a simple string uninterpreted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * by the registry. Both <code>host</code> and <code>port</code> are optional.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * If <code>host</code> is omitted, the host defaults to the local host. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <code>port</code> is omitted, then the port defaults to 1099, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * "well-known" port that RMI's registry, <code>rmiregistry</code>, uses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <P><em>Binding</em> a name for a remote object is associating or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * registering a name for a remote object that can be used at a later time to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * look up that remote object.  A remote object can be associated with a name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * using the <code>Naming</code> class's <code>bind</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <code>rebind</code> methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <P>Once a remote object is registered (bound) with the RMI registry on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * local host, callers on a remote (or local) host can lookup the remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * object by name, obtain its reference, and then invoke remote methods on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * object.  A registry may be shared by all servers running on a host or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * individual server process may create and use its own registry if desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * (see <code>java.rmi.registry.LocateRegistry.createRegistry</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * for details).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * @author  Ann Wollrath
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * @author  Roger Riggs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * @see     java.rmi.registry.Registry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * @see     java.rmi.registry.LocateRegistry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * @see     java.rmi.registry.LocateRegistry#createRegistry(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
public final class Naming {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Disallow anyone from creating one of these
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private Naming() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Returns a reference, a stub, for the remote object associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * with the specified <code>name</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @param name a name in URL format (without the scheme component)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @return a reference for a remote object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @exception NotBoundException if name is not currently bound
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @exception RemoteException if registry could not be contacted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @exception AccessException if this operation is not permitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @exception MalformedURLException if the name is not an appropriately
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *  formatted URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public static Remote lookup(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        throws NotBoundException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            java.net.MalformedURLException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            RemoteException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        ParsedNamingURL parsed = parseURL(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        Registry registry = getRegistry(parsed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (parsed.name == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            return registry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        return registry.lookup(parsed.name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Binds the specified <code>name</code> to a remote object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @param name a name in URL format (without the scheme component)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @param obj a reference for the remote object (usually a stub)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @exception AlreadyBoundException if name is already bound
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @exception MalformedURLException if the name is not an appropriately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *  formatted URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @exception RemoteException if registry could not be contacted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @exception AccessException if this operation is not permitted (if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * originating from a non-local host, for example)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public static void bind(String name, Remote obj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        throws AlreadyBoundException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            java.net.MalformedURLException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            RemoteException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        ParsedNamingURL parsed = parseURL(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        Registry registry = getRegistry(parsed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (obj == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            throw new NullPointerException("cannot bind to null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        registry.bind(parsed.name, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Destroys the binding for the specified name that is associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * with a remote object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param name a name in URL format (without the scheme component)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @exception NotBoundException if name is not currently bound
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @exception MalformedURLException if the name is not an appropriately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *  formatted URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @exception RemoteException if registry could not be contacted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @exception AccessException if this operation is not permitted (if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * originating from a non-local host, for example)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public static void unbind(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        throws RemoteException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            NotBoundException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            java.net.MalformedURLException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        ParsedNamingURL parsed = parseURL(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        Registry registry = getRegistry(parsed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        registry.unbind(parsed.name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Rebinds the specified name to a new remote object. Any existing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * binding for the name is replaced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @param name a name in URL format (without the scheme component)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @param obj new remote object to associate with the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @exception MalformedURLException if the name is not an appropriately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *  formatted URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @exception RemoteException if registry could not be contacted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @exception AccessException if this operation is not permitted (if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * originating from a non-local host, for example)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public static void rebind(String name, Remote obj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        throws RemoteException, java.net.MalformedURLException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        ParsedNamingURL parsed = parseURL(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        Registry registry = getRegistry(parsed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (obj == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            throw new NullPointerException("cannot bind to null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        registry.rebind(parsed.name, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * Returns an array of the names bound in the registry.  The names are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * URL-formatted (without the scheme component) strings. The array contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * a snapshot of the names present in the registry at the time of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @param   name a registry name in URL format (without the scheme
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *          component)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @return  an array of names (in the appropriate format) bound
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *          in the registry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @exception MalformedURLException if the name is not an appropriately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *  formatted URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @exception RemoteException if registry could not be contacted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    public static String[] list(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        throws RemoteException, java.net.MalformedURLException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        ParsedNamingURL parsed = parseURL(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        Registry registry = getRegistry(parsed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        String prefix = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if (parsed.port > 0 || !parsed.host.equals(""))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            prefix += "//" + parsed.host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (parsed.port > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            prefix += ":" + parsed.port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        prefix += "/";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        String[] names = registry.list();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        for (int i = 0; i < names.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            names[i] = prefix + names[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        return names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Returns a registry reference obtained from information in the URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    private static Registry getRegistry(ParsedNamingURL parsed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        throws RemoteException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        return LocateRegistry.getRegistry(parsed.host, parsed.port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Dissect Naming URL strings to obtain referenced host, port and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * object name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @return an object which contains each of the above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @exception MalformedURLException if given url string is malformed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    private static ParsedNamingURL parseURL(String str)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        throws MalformedURLException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            return intParseURL(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        } catch (URISyntaxException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            /* With RFC 3986 URI handling, 'rmi://:<port>' and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
             * '//:<port>' forms will result in a URI syntax exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
             * Convert the authority to a localhost:<port> form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            MalformedURLException mue = new MalformedURLException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                "invalid URL String: " + str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            mue.initCause(ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            int indexSchemeEnd = str.indexOf(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            int indexAuthorityBegin = str.indexOf("//:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            if (indexAuthorityBegin < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                throw mue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            if ((indexAuthorityBegin == 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    ((indexSchemeEnd > 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    (indexAuthorityBegin == indexSchemeEnd + 1))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                int indexHostBegin = indexAuthorityBegin + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                String newStr = str.substring(0, indexHostBegin) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                                "localhost" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                                str.substring(indexHostBegin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    return intParseURL(newStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                } catch (URISyntaxException inte) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    throw mue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                } catch (MalformedURLException inte) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    throw inte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            throw mue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    private static ParsedNamingURL intParseURL(String str)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        throws MalformedURLException, URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        URI uri = new URI(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        if (uri.isOpaque()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            throw new MalformedURLException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                "not a hierarchical URL: " + str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        if (uri.getFragment() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            throw new MalformedURLException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                "invalid character, '#', in URL name: " + str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        } else if (uri.getQuery() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            throw new MalformedURLException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                "invalid character, '?', in URL name: " + str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        } else if (uri.getUserInfo() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            throw new MalformedURLException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                "invalid character, '@', in URL host: " + str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        String scheme = uri.getScheme();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        if (scheme != null && !scheme.equals("rmi")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            throw new MalformedURLException("invalid URL scheme: " + str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        String name = uri.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        if (name != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            if (name.startsWith("/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                name = name.substring(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            if (name.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                name = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        String host = uri.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        if (host == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            host = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                 * With 2396 URI handling, forms such as 'rmi://host:bar'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                 * or 'rmi://:<port>' are parsed into a registry based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                 * authority. We only want to allow server based naming
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                 * authorities.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                uri.parseServerAuthority();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            } catch (URISyntaxException use) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                // Check if the authority is of form ':<port>'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                String authority = uri.getAuthority();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                if (authority != null && authority.startsWith(":")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    // Convert the authority to 'localhost:<port>' form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                    authority = "localhost" + authority;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                        uri = new URI(null, authority, null, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                        // Make sure it now parses to a valid server based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                        // naming authority
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                        uri.parseServerAuthority();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    } catch (URISyntaxException use2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                        throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                            MalformedURLException("invalid authority: " + str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                        MalformedURLException("invalid authority: " + str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        int port = uri.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        if (port == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            port = Registry.REGISTRY_PORT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        return new ParsedNamingURL(host, port, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * Simple class to enable multiple URL return values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    private static class ParsedNamingURL {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        String host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        ParsedNamingURL(String host, int port, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            this.host = host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            this.port = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
}