1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.plus.webapp;
20
21 import java.net.URL;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.Iterator;
25 import java.util.List;
26
27 import javax.naming.Binding;
28 import javax.naming.Context;
29 import javax.naming.InitialContext;
30 import javax.naming.Name;
31 import javax.naming.NameNotFoundException;
32 import javax.naming.NamingException;
33
34 import org.eclipse.jetty.jndi.ContextFactory;
35 import org.eclipse.jetty.jndi.NamingContext;
36 import org.eclipse.jetty.jndi.NamingUtil;
37 import org.eclipse.jetty.jndi.local.localContextRoot;
38 import org.eclipse.jetty.plus.jndi.EnvEntry;
39 import org.eclipse.jetty.plus.jndi.NamingEntryUtil;
40 import org.eclipse.jetty.util.log.Log;
41 import org.eclipse.jetty.util.log.Logger;
42 import org.eclipse.jetty.webapp.AbstractConfiguration;
43 import org.eclipse.jetty.webapp.WebAppContext;
44 import org.eclipse.jetty.xml.XmlConfiguration;
45
46
47
48
49
50
51
52 public class EnvConfiguration extends AbstractConfiguration
53 {
54 private static final Logger LOG = Log.getLogger(EnvConfiguration.class);
55
56 private static final String JETTY_ENV_BINDINGS = "org.eclipse.jetty.jndi.EnvConfiguration";
57 private URL jettyEnvXmlUrl;
58
59 public void setJettyEnvXml (URL url)
60 {
61 this.jettyEnvXmlUrl = url;
62 }
63
64
65
66
67
68 @Override
69 public void preConfigure (WebAppContext context) throws Exception
70 {
71
72 createEnvContext(context);
73 }
74
75
76
77
78 @Override
79 public void configure (WebAppContext context) throws Exception
80 {
81 if (LOG.isDebugEnabled())
82 LOG.debug("Created java:comp/env for webapp "+context.getContextPath());
83
84
85
86 if (jettyEnvXmlUrl == null)
87 {
88
89
90 org.eclipse.jetty.util.resource.Resource web_inf = context.getWebInf();
91 if(web_inf!=null && web_inf.isDirectory())
92 {
93 org.eclipse.jetty.util.resource.Resource jettyEnv = web_inf.addPath("jetty-env.xml");
94 if(jettyEnv.exists())
95 {
96 jettyEnvXmlUrl = jettyEnv.getURL();
97 }
98 }
99 }
100
101 if (jettyEnvXmlUrl != null)
102 {
103 synchronized (localContextRoot.getRoot())
104 {
105
106 final List<Bound> bindings = new ArrayList<Bound>();
107 NamingContext.Listener listener = new NamingContext.Listener()
108 {
109 public void unbind(NamingContext ctx, Binding binding)
110 {
111 }
112
113 public Binding bind(NamingContext ctx, Binding binding)
114 {
115 bindings.add(new Bound(ctx,binding.getName()));
116 return binding;
117 }
118 };
119
120 try
121 {
122 localContextRoot.getRoot().addListener(listener);
123 XmlConfiguration configuration = new XmlConfiguration(jettyEnvXmlUrl);
124 configuration.configure(context);
125 }
126 finally
127 {
128 localContextRoot.getRoot().removeListener(listener);
129 context.setAttribute(JETTY_ENV_BINDINGS,bindings);
130 }
131 }
132 }
133
134
135 bindEnvEntries(context);
136 }
137
138
139
140
141
142
143
144 @Override
145 public void deconfigure (WebAppContext context) throws Exception
146 {
147
148 ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
149 Thread.currentThread().setContextClassLoader(context.getClassLoader());
150 ContextFactory.associateClassLoader(context.getClassLoader());
151 try
152 {
153 Context ic = new InitialContext();
154 Context compCtx = (Context)ic.lookup ("java:comp");
155 compCtx.destroySubcontext("env");
156
157
158 @SuppressWarnings("unchecked")
159 List<Bound> bindings = (List<Bound>)context.getAttribute(JETTY_ENV_BINDINGS);
160 context.setAttribute(JETTY_ENV_BINDINGS,null);
161 if (bindings!=null)
162 {
163 Collections.reverse(bindings);
164 for (Bound b:bindings)
165 b._context.destroySubcontext(b._name);
166 }
167 }
168 catch (NameNotFoundException e)
169 {
170 LOG.warn(e);
171 }
172 finally
173 {
174 ContextFactory.disassociateClassLoader();
175 Thread.currentThread().setContextClassLoader(oldLoader);
176 }
177 }
178
179
180
181
182
183
184
185 @Override
186 public void destroy (WebAppContext context) throws Exception
187 {
188 try
189 {
190
191 NamingContext scopeContext = (NamingContext)NamingEntryUtil.getContextForScope(context);
192 scopeContext.getParent().destroySubcontext(scopeContext.getName());
193 }
194 catch (NameNotFoundException e)
195 {
196 LOG.ignore(e);
197 LOG.debug("No jndi entries scoped to webapp {}", context);
198 }
199 catch (NamingException e)
200 {
201 LOG.debug("Error unbinding jndi entries scoped to webapp "+context, e);
202 }
203 }
204
205
206
207
208
209
210
211
212 public void bindEnvEntries (WebAppContext context)
213 throws NamingException
214 {
215 LOG.debug("Binding env entries from the jvm scope");
216 InitialContext ic = new InitialContext();
217 Context envCtx = (Context)ic.lookup("java:comp/env");
218 Object scope = null;
219 List<Object> list = NamingEntryUtil.lookupNamingEntries(scope, EnvEntry.class);
220 Iterator<Object> itor = list.iterator();
221 while (itor.hasNext())
222 {
223 EnvEntry ee = (EnvEntry)itor.next();
224 ee.bindToENC(ee.getJndiName());
225 Name namingEntryName = NamingEntryUtil.makeNamingEntryName(null, ee);
226 NamingUtil.bind(envCtx, namingEntryName.toString(), ee);
227 }
228
229 LOG.debug("Binding env entries from the server scope");
230
231 scope = context.getServer();
232 list = NamingEntryUtil.lookupNamingEntries(scope, EnvEntry.class);
233 itor = list.iterator();
234 while (itor.hasNext())
235 {
236 EnvEntry ee = (EnvEntry)itor.next();
237 ee.bindToENC(ee.getJndiName());
238 Name namingEntryName = NamingEntryUtil.makeNamingEntryName(null, ee);
239 NamingUtil.bind(envCtx, namingEntryName.toString(), ee);
240 }
241
242 LOG.debug("Binding env entries from the context scope");
243 scope = context;
244 list = NamingEntryUtil.lookupNamingEntries(scope, EnvEntry.class);
245 itor = list.iterator();
246 while (itor.hasNext())
247 {
248 EnvEntry ee = (EnvEntry)itor.next();
249 ee.bindToENC(ee.getJndiName());
250 Name namingEntryName = NamingEntryUtil.makeNamingEntryName(null, ee);
251 NamingUtil.bind(envCtx, namingEntryName.toString(), ee);
252 }
253 }
254
255 protected void createEnvContext (WebAppContext wac)
256 throws NamingException
257 {
258 ClassLoader old_loader = Thread.currentThread().getContextClassLoader();
259 Thread.currentThread().setContextClassLoader(wac.getClassLoader());
260 ContextFactory.associateClassLoader(wac.getClassLoader());
261 try
262 {
263 Context context = new InitialContext();
264 Context compCtx = (Context)context.lookup ("java:comp");
265 compCtx.createSubcontext("env");
266 }
267 finally
268 {
269 ContextFactory.disassociateClassLoader();
270 Thread.currentThread().setContextClassLoader(old_loader);
271 }
272 }
273
274 private static class Bound
275 {
276 final NamingContext _context;
277 final String _name;
278 Bound(NamingContext context, String name)
279 {
280 _context=context;
281 _name=name;
282 }
283 }
284 }