KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jnp > interfaces > java > javaURLContextFactory


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.java;
23
24 import java.util.Hashtable JavaDoc;
25 import javax.naming.*;
26 import javax.naming.spi.*;
27
28 import org.jnp.interfaces.NamingContext;
29 import org.jnp.interfaces.Naming;
30
31 /**
32  * Implementation of "java:" namespace factory. The context is associated
33  * with the thread, so the root context must be set before this is used in a thread
34  *
35  * @see <related>
36  * @author $Author: starksm $
37  * @version $Revision: 37459 $
38  */

39 public class javaURLContextFactory
40    implements ObjectFactory
41 {
42    // Constants -----------------------------------------------------
43

44    // Attributes ----------------------------------------------------
45

46    // Static --------------------------------------------------------
47
private static ThreadLocal JavaDoc server = new ThreadLocal JavaDoc();
48    
49    public static void setRoot(Naming srv)
50    {
51       // TODO: Add security check here
52
server.set(srv);
53    }
54    
55    public static Naming getRoot()
56    {
57       // TODO: Add security check here
58
return (Naming)server.get();
59    }
60    
61    // Constructors --------------------------------------------------
62

63    // Public --------------------------------------------------------
64

65    // ObjectFactory implementation ----------------------------------
66
public Object JavaDoc getObjectInstance(Object JavaDoc obj,
67                                 Name name,
68                                 Context nameCtx,
69                                 Hashtable JavaDoc environment)
70                          throws Exception JavaDoc
71    {
72       if (obj == null)
73          return new NamingContext(environment, name, (Naming)server.get());
74       else if (obj instanceof String JavaDoc)
75       {
76          String JavaDoc url = (String JavaDoc)obj;
77          Context ctx = new NamingContext(environment, name, (Naming)server.get());
78          
79          Name n = ctx.getNameParser(name).parse(url.substring(url.indexOf(":")+1));
80          if (n.size() >= 3)
81          {
82             // Provider URL?
83
if (n.get(0).toString().equals("") &&
84                 n.get(1).toString().equals(""))
85             {
86                ctx.addToEnvironment(Context.PROVIDER_URL, n.get(2));
87             }
88          }
89          return ctx;
90       } else
91       {
92          return null;
93       }
94    }
95     
96    // Y overrides ---------------------------------------------------
97

98    // Package protected ---------------------------------------------
99

100    // Protected -----------------------------------------------------
101

102    // Private -------------------------------------------------------
103

104    // Inner classes -------------------------------------------------
105
}
Popular Tags