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