iiopURLContextFactory.java revision 820:9205e980062a
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.iiop;
27
28import javax.naming.*;
29import javax.naming.spi.*;
30
31import java.util.Hashtable;
32
33import com.sun.jndi.cosnaming.CNCtx;
34
35/**
36 * An IIOP URL context factory.
37 *
38 * @author Rosanna Lee
39 */
40
41public class iiopURLContextFactory implements ObjectFactory {
42
43    public Object getObjectInstance(Object urlInfo, Name name, Context nameCtx,
44                                    Hashtable<?,?> env) throws Exception {
45
46//System.out.println("iiopURLContextFactory " + urlInfo);
47        if (urlInfo == null) {
48            return new iiopURLContext(env);
49        }
50        if (urlInfo instanceof String) {
51            return getUsingURL((String)urlInfo, env);
52        } else if (urlInfo instanceof String[]) {
53            return getUsingURLs((String[])urlInfo, env);
54        } else {
55            throw (new IllegalArgumentException(
56                    "iiopURLContextFactory.getObjectInstance: " +
57                    "argument must be a URL String or array of URLs"));
58        }
59    }
60
61    /**
62      * Resolves 'name' into a target context with remaining name.
63      * It only resolves the hostname/port number. The remaining name
64      * contains the rest of the name found in the URL.
65      *
66      * For example, with a iiop URL "iiop://localhost:900/rest/of/name",
67      * this method resolves "iiop://localhost:900/" to the "NameService"
68      * context on for the ORB at 'localhost' on port 900,
69      * and returns as the remaining name "rest/of/name".
70      */
71    static ResolveResult getUsingURLIgnoreRest(String url, Hashtable<?,?> env)
72        throws NamingException {
73        return CNCtx.createUsingURL(url, env);
74    }
75
76    private static Object getUsingURL(String url, Hashtable<?,?> env)
77        throws NamingException {
78        ResolveResult res = getUsingURLIgnoreRest(url, env);
79
80        Context ctx = (Context)res.getResolvedObj();
81        try {
82            return ctx.lookup(res.getRemainingName());
83        } finally {
84            ctx.close();
85        }
86    }
87
88    private static Object getUsingURLs(String[] urls, Hashtable<?,?> env) {
89        for (int i = 0; i < urls.length; i++) {
90            String url = urls[i];
91            try {
92                Object obj = getUsingURL(url, env);
93                if (obj != null) {
94                    return obj;
95                }
96            } catch (NamingException e) {
97            }
98        }
99        return null;    // %%% exception??
100    }
101}
102