src/share/classes/com/sun/jndi/url/rmi/rmiURLContext.java

Print this page




   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.jndi.url.rmi;
  27 
  28 import java.util.Hashtable;
  29 import java.rmi.registry.LocateRegistry;
  30 
  31 import javax.naming.*;
  32 import javax.naming.spi.ResolveResult;
  33 import com.sun.jndi.toolkit.url.GenericURLContext;
  34 import com.sun.jndi.rmi.registry.RegistryContext;
  35 
  36 
  37 /**
  38  * An RMI URL context resolves names that are URLs of the form
  39  * <pre>
  40  *   rmi://[host][:port][/[object]]
  41  * or
  42  *   rmi:[/][object]
  43  * </pre>
  44  * If an object is specified, the URL resolves to the named object.
  45  * Otherwise, the URL resolves to the specified RMI registry.
  46  *
  47  * @author Scott Seligman
  48  */
  49 public class rmiURLContext extends GenericURLContext {
  50 
  51     public rmiURLContext(Hashtable env) {
  52         super(env);
  53     }
  54 
  55     /**
  56      * Resolves the registry portion of "url" to the corresponding
  57      * RMI registry, and returns the atomic object name as the
  58      * remaining name.
  59      */
  60     protected ResolveResult getRootURLContext(String url, Hashtable env)
  61             throws NamingException
  62     {
  63         if (!url.startsWith("rmi:")) {
  64             throw (new IllegalArgumentException(
  65                     "rmiURLContext: name is not an RMI URL: " + url));
  66         }
  67 
  68         // Parse the URL.
  69 
  70         String host = null;
  71         int port = -1;
  72         String objName = null;
  73 
  74         int i = 4;              // index into url, following the "rmi:"
  75 
  76         if (url.startsWith("//", i)) {          // parse "//host:port"
  77             i += 2;                             // skip past "//"
  78             int slash = url.indexOf('/', i);
  79             if (slash < 0) {
  80                 slash = url.length();




   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.jndi.url.rmi;
  27 
  28 import java.util.Hashtable;

  29 
  30 import javax.naming.*;
  31 import javax.naming.spi.ResolveResult;
  32 import com.sun.jndi.toolkit.url.GenericURLContext;
  33 import com.sun.jndi.rmi.registry.RegistryContext;
  34 
  35 
  36 /**
  37  * An RMI URL context resolves names that are URLs of the form
  38  * <pre>
  39  *   rmi://[host][:port][/[object]]
  40  * or
  41  *   rmi:[/][object]
  42  * </pre>
  43  * If an object is specified, the URL resolves to the named object.
  44  * Otherwise, the URL resolves to the specified RMI registry.
  45  *
  46  * @author Scott Seligman
  47  */
  48 public class rmiURLContext extends GenericURLContext {
  49 
  50     public rmiURLContext(Hashtable<?,?> env) {
  51         super(env);
  52     }
  53 
  54     /**
  55      * Resolves the registry portion of "url" to the corresponding
  56      * RMI registry, and returns the atomic object name as the
  57      * remaining name.
  58      */
  59     protected ResolveResult getRootURLContext(String url, Hashtable<?,?> env)
  60             throws NamingException
  61     {
  62         if (!url.startsWith("rmi:")) {
  63             throw (new IllegalArgumentException(
  64                     "rmiURLContext: name is not an RMI URL: " + url));
  65         }
  66 
  67         // Parse the URL.
  68 
  69         String host = null;
  70         int port = -1;
  71         String objName = null;
  72 
  73         int i = 4;              // index into url, following the "rmi:"
  74 
  75         if (url.startsWith("//", i)) {          // parse "//host:port"
  76             i += 2;                             // skip past "//"
  77             int slash = url.indexOf('/', i);
  78             if (slash < 0) {
  79                 slash = url.length();