01: /*
02: * Copyright 1999-2004 Sun Microsystems, Inc. All Rights Reserved.
03: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
04: *
05: * This code is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU General Public License version 2 only, as
07: * published by the Free Software Foundation. Sun designates this
08: * particular file as subject to the "Classpath" exception as provided
09: * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22: * CA 95054 USA or visit www.sun.com if you need additional information or
23: * have any 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.ObjectFactory;
32:
33: /**
34: * An RMI URL context factory creates contexts that can resolve names
35: * that are RMI URLs as defined by rmiURLContext.
36: * In addition, if given a specific RMI URL (or an array of them), the
37: * factory will resolve all the way to the named registry or object.
38: *
39: * @author Scott Seligman
40: * @version 1.16 07/05/05
41: *
42: * @see rmiURLContext
43: */
44:
45: public class rmiURLContextFactory implements ObjectFactory {
46:
47: public Object getObjectInstance(Object urlInfo, Name name,
48: Context nameCtx, Hashtable<?, ?> env)
49: throws NamingException {
50: if (urlInfo == null) {
51: return (new rmiURLContext(env));
52: } else if (urlInfo instanceof String) {
53: return getUsingURL((String) urlInfo, env);
54: } else if (urlInfo instanceof String[]) {
55: return getUsingURLs((String[]) urlInfo, env);
56: } else {
57: throw (new ConfigurationException(
58: "rmiURLContextFactory.getObjectInstance: "
59: + "argument must be an RMI URL String or an array of them"));
60: }
61: }
62:
63: private static Object getUsingURL(String url, Hashtable env)
64: throws NamingException {
65: rmiURLContext urlCtx = new rmiURLContext(env);
66: try {
67: return urlCtx.lookup(url);
68: } finally {
69: urlCtx.close();
70: }
71: }
72:
73: /*
74: * Try each URL until lookup() succeeds for one of them.
75: * If all URLs fail, throw one of the exceptions arbitrarily.
76: * Not pretty, but potentially more informative than returning null.
77: */
78: private static Object getUsingURLs(String[] urls, Hashtable env)
79: throws NamingException {
80: if (urls.length == 0) {
81: throw (new ConfigurationException(
82: "rmiURLContextFactory: empty URL array"));
83: }
84: rmiURLContext urlCtx = new rmiURLContext(env);
85: try {
86: NamingException ne = null;
87: for (int i = 0; i < urls.length; i++) {
88: try {
89: return urlCtx.lookup(urls[i]);
90: } catch (NamingException e) {
91: ne = e;
92: }
93: }
94: throw ne;
95: } finally {
96: urlCtx.close();
97: }
98: }
99: }
|