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