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