1 /* 2 * Copyright (c) 1999, 2004, 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 26 package com.sun.jndi.url.rmi; 27 28 29 import java.util.Hashtable; 30 31 import javax.naming.*; 32 import javax.naming.spi.ObjectFactory; 33 34 35 /** 36 * An RMI URL context factory creates contexts that can resolve names 37 * that are RMI URLs as defined by rmiURLContext. 38 * In addition, if given a specific RMI URL (or an array of them), the 39 * factory will resolve all the way to the named registry or object. 40 * 41 * @author Scott Seligman 42 * 43 * @see rmiURLContext 44 */ 45 46 47 public class rmiURLContextFactory implements ObjectFactory { 48 49 public Object getObjectInstance(Object urlInfo, Name name, 50 Context nameCtx, Hashtable<?,?> env) 51 throws NamingException 52 { 53 if (urlInfo == null) { 54 return (new rmiURLContext(env)); 55 } else if (urlInfo instanceof String) { 56 return getUsingURL((String)urlInfo, env); 57 } else if (urlInfo instanceof String[]) { 58 return getUsingURLs((String[])urlInfo, env); 59 } else { 60 throw (new ConfigurationException( 61 "rmiURLContextFactory.getObjectInstance: " + 62 "argument must be an RMI URL String or an array of them")); 63 } 64 } 65 66 private static Object getUsingURL(String url, Hashtable<?,?> env) 67 throws NamingException 68 { 69 rmiURLContext urlCtx = new rmiURLContext(env); 70 try { 71 return urlCtx.lookup(url); 72 } finally { 73 urlCtx.close(); 74 } 75 } 76 77 /* 78 * Try each URL until lookup() succeeds for one of them. 79 * If all URLs fail, throw one of the exceptions arbitrarily. 80 * Not pretty, but potentially more informative than returning null. 81 */ 82 private static Object getUsingURLs(String[] urls, Hashtable<?,?> env) 83 throws NamingException 84 { 85 if (urls.length == 0) { 86 throw (new ConfigurationException( 87 "rmiURLContextFactory: empty URL array")); 88 } 89 rmiURLContext urlCtx = new rmiURLContext(env); 90 try { 91 NamingException ne = null; 92 for (int i = 0; i < urls.length; i++) { 93 try { 94 return urlCtx.lookup(urls[i]); 95 } catch (NamingException e) { 96 ne = e; 97 } 98 } 99 throw ne; 100 } finally { 101 urlCtx.close(); 102 } 103 } 104 }