1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.eclipse.jetty.jndi;
15
16
17 import java.io.IOException;
18 import java.util.Hashtable;
19 import java.util.Map;
20 import java.util.WeakHashMap;
21
22 import javax.naming.Context;
23 import javax.naming.Name;
24 import javax.naming.NameParser;
25 import javax.naming.Reference;
26 import javax.naming.StringRefAddr;
27 import javax.naming.spi.ObjectFactory;
28
29 import org.eclipse.jetty.server.handler.ContextHandler;
30
31 import org.eclipse.jetty.util.log.Logger;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 public class ContextFactory implements ObjectFactory
60 {
61 private static Logger __log = NamingUtil.__log;
62
63
64
65
66 private static final WeakHashMap __contextMap = new WeakHashMap();
67
68
69
70
71
72 private static final ThreadLocal __threadContext = new ThreadLocal();
73
74
75
76
77
78
79
80
81
82
83 public Object getObjectInstance (Object obj,
84 Name name,
85 Context nameCtx,
86 Hashtable env)
87 throws Exception
88 {
89
90 Context ctx = (Context)__threadContext.get();
91 if (ctx != null)
92 {
93 if(__log.isDebugEnabled()) __log.debug("Using the Context that is bound on the thread");
94 return ctx;
95 }
96
97
98
99 ClassLoader loader = null;
100 if (ContextHandler.getCurrentContext() != null)
101 {
102 loader = ContextHandler.getCurrentContext().getContextHandler().getClassLoader();
103 }
104
105
106 if (loader != null)
107 {
108 if (__log.isDebugEnabled()) __log.debug("Using classloader of current org.eclipse.jetty.server.handler.ContextHandler");
109 }
110 else
111 {
112
113
114 loader = Thread.currentThread().getContextClassLoader();
115 if (__log.isDebugEnabled()) __log.debug("Using thread context classloader");
116 }
117
118
119 ctx = (Context)__contextMap.get(loader);
120
121
122 if (ctx == null)
123 {
124
125 ctx = getParentClassLoaderContext(loader);
126
127
128
129 if (ctx == null)
130 {
131 Reference ref = (Reference)obj;
132 StringRefAddr parserAddr = (StringRefAddr)ref.get("parser");
133 String parserClassName = (parserAddr==null?null:(String)parserAddr.getContent());
134 NameParser parser = (NameParser)(parserClassName==null?null:loader.loadClass(parserClassName).newInstance());
135
136 ctx = new NamingContext (env,
137 name.get(0),
138 (NamingContext)nameCtx,
139 parser);
140 if(__log.isDebugEnabled())__log.debug("No entry for classloader: "+loader);
141 __contextMap.put (loader, ctx);
142 }
143 }
144
145 return ctx;
146 }
147
148
149
150
151
152
153
154 public Context getParentClassLoaderContext (ClassLoader loader)
155 {
156 Context ctx = null;
157 ClassLoader cl = loader;
158 for (cl = cl.getParent(); (cl != null) && (ctx == null); cl = cl.getParent())
159 {
160 ctx = (Context)__contextMap.get(cl);
161 }
162
163 return ctx;
164 }
165
166
167
168
169
170
171
172
173 public static Context setComponentContext(final Context ctx)
174 {
175 Context previous = (Context)__threadContext.get();
176 __threadContext.set(ctx);
177 return previous;
178 }
179
180
181
182
183
184
185 public static void resetComponentContext(final Context ctx)
186 {
187 __threadContext.set(ctx);
188 }
189
190 public static void dump(Appendable out, String indent) throws IOException
191 {
192 out.append("o.e.j.jndi.ContextFactory@").append(Long.toHexString(__contextMap.hashCode())).append("\n");
193 int size=__contextMap.size();
194 int i=0;
195 for (Map.Entry<ClassLoader,NamingContext> entry : ((Map<ClassLoader,NamingContext>)__contextMap).entrySet())
196 {
197 boolean last=++i==size;
198 ClassLoader loader=entry.getKey();
199 out.append(indent).append(" +- ").append(loader.getClass().getSimpleName()).append("@").append(Long.toHexString(loader.hashCode())).append(": ");
200
201 NamingContext context = entry.getValue();
202 context.dump(out,indent+(last?" ":" | "));
203 }
204 }
205
206 }