KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jnp > interfaces > NamingContextFactory


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * 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 JavaDoc;
25 import javax.naming.CompoundName JavaDoc;
26 import javax.naming.Context JavaDoc;
27 import javax.naming.Name JavaDoc;
28 import javax.naming.NamingException JavaDoc;
29 import javax.naming.Reference JavaDoc;
30 import javax.naming.spi.*;
31
32 /** The jnp naming provider InitialContextFactory implementation.
33
34 @see javax.naming.spi.InitialContextFactory
35
36 @author Scott.Stark@jboss.org
37 @version $Revision: 37459 $
38  */

39 public class NamingContextFactory
40    implements InitialContextFactory, ObjectFactory
41 {
42     // InitialContextFactory implementation --------------------------
43
public Context JavaDoc getInitialContext(Hashtable JavaDoc env)
44       throws NamingException JavaDoc
45     {
46         String JavaDoc providerURL = (String JavaDoc) env.get(Context.PROVIDER_URL);
47         Name JavaDoc prefix = null;
48         /** This may be a comma separated list of provider urls in which
49           case we do not parse the urls for the requested context prefix name
50         */

51         int comma = providerURL != null ? providerURL.indexOf(',') : -1;
52         if( providerURL != null && comma < 0 )
53         {
54             Name JavaDoc name = new CompoundName JavaDoc(providerURL, NamingParser.syntax);
55             String JavaDoc serverInfo = NamingContext.parseNameForScheme(name, env);
56             if( serverInfo != null )
57             {
58                env = (Hashtable JavaDoc) env.clone();
59                // Set hostname:port value for the naming server
60
env.put(Context.PROVIDER_URL, serverInfo);
61                // Set the context prefix to name
62
Name JavaDoc parsedName = (Name JavaDoc) env.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 JavaDoc getObjectInstance(Object JavaDoc obj,
74                                 Name JavaDoc name,
75                                 Context JavaDoc nameCtx,
76                                 Hashtable JavaDoc environment)
77                          throws Exception JavaDoc
78    {
79 // System.out.println(obj+" "+name+" "+nameCtx+" "+environment);
80
Context JavaDoc ctx = getInitialContext(environment);
81       Reference JavaDoc ref = (Reference JavaDoc)obj;
82       return ctx.lookup((String JavaDoc)ref.get("URL").getContent());
83    }
84
85 }
86
Popular Tags