KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > naming > ENCFactory


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.jboss.naming;
23
24 import java.util.Hashtable JavaDoc;
25 import java.util.WeakHashMap JavaDoc;
26 import java.security.PrivilegedAction JavaDoc;
27 import java.security.AccessController JavaDoc;
28 import javax.naming.Context JavaDoc;
29 import javax.naming.Name JavaDoc;
30 import javax.naming.spi.ObjectFactory JavaDoc;
31
32 import org.jnp.server.NamingServer;
33 import org.jnp.interfaces.NamingContext;
34
35 /**
36  * Implementation of "java:comp" namespace factory. The context is associated
37  * with the thread class loader.
38  *
39  * @author <a HREF="mailto:rickard.oberg@telkel.com">Rickard Oberg</a>
40  * @author <a HREF="mailto:scott.stark@jboss.org">Scott Stark</a>
41  * @version $Revision: 37459 $
42  */

43 public class ENCFactory
44    implements ObjectFactory JavaDoc
45 {
46    // Constants -----------------------------------------------------
47

48    // Attributes ----------------------------------------------------
49
private static WeakHashMap JavaDoc encs = new WeakHashMap JavaDoc();
50    private static ClassLoader JavaDoc topLoader;
51
52    // Static --------------------------------------------------------
53
public static void setTopClassLoader(ClassLoader JavaDoc topLoader)
54    {
55       ENCFactory.topLoader = topLoader;
56    }
57    public static ClassLoader JavaDoc getTopClassLoader()
58    {
59       return ENCFactory.topLoader;
60    }
61
62
63    // Constructors --------------------------------------------------
64

65    // Public --------------------------------------------------------
66

67    // ObjectFactory implementation ----------------------------------
68
public Object JavaDoc getObjectInstance(Object JavaDoc obj, Name JavaDoc name, Context JavaDoc nameCtx,
69       Hashtable JavaDoc environment)
70       throws Exception JavaDoc
71    {
72       // Get naming for this component
73
ClassLoader JavaDoc ctxClassLoader = GetTCLAction.getContextClassLoader();
74       synchronized (encs)
75       {
76          Context JavaDoc compCtx = (Context JavaDoc) encs.get(ctxClassLoader);
77
78          /* If this is the first time we see ctxClassLoader first check to see
79           if a parent ClassLoader has created an ENC namespace.
80          */

81          if (compCtx == null)
82          {
83             ClassLoader JavaDoc loader = ctxClassLoader;
84             GetParentAction action = new GetParentAction(ctxClassLoader);
85             while( loader != null && loader != topLoader && compCtx == null )
86             {
87                compCtx = (Context JavaDoc) encs.get(loader);
88                loader = action.getParent();
89             }
90             // If we did not find an ENC create it
91
if( compCtx == null )
92             {
93                NamingServer srv = new NamingServer();
94                compCtx = new NamingContext(environment, null, srv);
95                encs.put(ctxClassLoader, compCtx);
96             }
97          }
98          return compCtx;
99       }
100    }
101
102    private static class GetTCLAction implements PrivilegedAction JavaDoc
103    {
104       static PrivilegedAction JavaDoc ACTION = new GetTCLAction();
105       public Object JavaDoc run()
106       {
107          ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
108          return loader;
109       }
110       static ClassLoader JavaDoc getContextClassLoader()
111       {
112          ClassLoader JavaDoc loader = (ClassLoader JavaDoc) AccessController.doPrivileged(ACTION);
113          return loader;
114       }
115    }
116
117    private static class GetParentAction implements PrivilegedAction JavaDoc
118    {
119       ClassLoader JavaDoc loader;
120       GetParentAction(ClassLoader JavaDoc loader)
121       {
122          this.loader = loader;
123       }
124       public Object JavaDoc run()
125       {
126          ClassLoader JavaDoc parent = null;
127          if( loader != null )
128          {
129             parent = loader.getParent();
130             loader = parent;
131          }
132          return parent;
133       }
134       ClassLoader JavaDoc getParent()
135       {
136          ClassLoader JavaDoc parent = (ClassLoader JavaDoc) AccessController.doPrivileged(this);
137          return parent;
138       }
139    }
140 }
141
Popular Tags