01: /*
02: * JBoss, Home of Professional Open Source.
03: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
04: * as indicated by the @author tags. See the copyright.txt file in the
05: * distribution for a full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * published by the Free Software Foundation; either version 2.1 of
10: * the License, or (at your option) any later version.
11: *
12: * This software is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this software; if not, write to the Free
19: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21: */
22: package org.jnp.interfaces;
23:
24: import java.util.Hashtable;
25: import javax.naming.CompoundName;
26: import javax.naming.Context;
27: import javax.naming.Name;
28: import javax.naming.NamingException;
29: import javax.naming.Reference;
30: import javax.naming.spi.*;
31:
32: /** The jnp naming provider InitialContextFactory implementation.
33:
34: @see javax.naming.spi.InitialContextFactory
35:
36: @author Rickard Oberg
37: @author Scott.Stark@jboss.org
38: @version $Revision: 57199 $
39: */
40: public class NamingContextFactory implements InitialContextFactory,
41: ObjectFactory {
42: // InitialContextFactory implementation --------------------------
43: public Context getInitialContext(Hashtable env)
44: throws NamingException {
45: String providerURL = (String) env.get(Context.PROVIDER_URL);
46: Name prefix = null;
47: /** This may be a comma separated list of provider urls in which
48: case we do not parse the urls for the requested context prefix name
49: */
50: int comma = providerURL != null ? providerURL.indexOf(',') : -1;
51: if (providerURL != null && comma < 0) {
52: Name name = new CompoundName(providerURL,
53: NamingParser.syntax);
54: String serverInfo = NamingContext.parseNameForScheme(name,
55: env);
56: if (serverInfo != null) {
57: env = (Hashtable) env.clone();
58: // Set hostname:port value for the naming server
59: env.put(Context.PROVIDER_URL, serverInfo);
60: // Set the context prefix to name
61: Name parsedName = (Name) env
62: .get(NamingContext.JNP_PARSED_NAME);
63: if (parsedName != null)
64: prefix = parsedName;
65: else
66: prefix = name;
67: }
68: }
69: return new NamingContext(env, prefix, null);
70: }
71:
72: // ObjectFactory implementation ----------------------------------
73: public Object getObjectInstance(Object obj, Name name,
74: Context nameCtx, Hashtable environment) throws Exception {
75: // System.out.println(obj+" "+name+" "+nameCtx+" "+environment);
76: Context ctx = getInitialContext(environment);
77: Reference ref = (Reference) obj;
78: return ctx.lookup((String) ref.get("URL").getContent());
79: }
80:
81: }
|