1/* 2 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 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 26package com.sun.jndi.url.rmi; 27 28import java.util.Hashtable; 29 30import javax.naming.*; 31import javax.naming.spi.ResolveResult; 32import com.sun.jndi.toolkit.url.GenericURLContext; 33import 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 */ 48public 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(); 80 } 81 if (url.startsWith("[", i)) { // at IPv6 literal 82 int brac = url.indexOf(']', i + 1); 83 if (brac < 0 || brac > slash) { 84 throw new IllegalArgumentException( 85 "rmiURLContext: name is an Invalid URL: " + url); 86 } 87 host = url.substring(i, brac + 1); // include brackets 88 i = brac + 1; // skip past "[...]" 89 } else { // at host name or IPv4 90 int colon = url.indexOf(':', i); 91 int hostEnd = (colon < 0 || colon > slash) 92 ? slash 93 : colon; 94 if (i < hostEnd) { 95 host = url.substring(i, hostEnd); 96 } 97 i = hostEnd; // skip past host 98 } 99 if ((i + 1 < slash)) { 100 if ( url.startsWith(":", i)) { // parse port 101 i++; // skip past ":" 102 port = Integer.parseInt(url.substring(i, slash)); 103 } else { 104 throw new IllegalArgumentException( 105 "rmiURLContext: name is an Invalid URL: " + url); 106 } 107 } 108 i = slash; 109 } 110 if ("".equals(host)) { 111 host = null; 112 } 113 if (url.startsWith("/", i)) { // skip "/" before object name 114 i++; 115 } 116 if (i < url.length()) { 117 objName = url.substring(i); 118 } 119 120 // Represent object name as empty or single-component composite name. 121 CompositeName remaining = new CompositeName(); 122 if (objName != null) { 123 remaining.add(objName); 124 } 125 126 // Debug 127 //System.out.println("host=" + host + " port=" + port + 128 // " objName=" + remaining.toString() + "\n"); 129 130 // Create a registry context. 131 Context regCtx = new RegistryContext(host, port, env); 132 133 return (new ResolveResult(regCtx, remaining)); 134 } 135} 136