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 naming entries configured in environment for webapp "+context);
198 }
199 }
200
201
202
203
204
205
206
207
208 public void bindEnvEntries (WebAppContext context)
209 throws NamingException
210 {
211 LOG.debug("Binding env entries from the jvm scope");
212 InitialContext ic = new InitialContext();
213 Context envCtx = (Context)ic.lookup("java:comp/env");
214 Object scope = null;
215 List<Object> list = NamingEntryUtil.lookupNamingEntries(scope, EnvEntry.class);
216 Iterator<Object> itor = list.iterator();
217 while (itor.hasNext())
218 {
219 EnvEntry ee = (EnvEntry)itor.next();
220 ee.bindToENC(ee.getJndiName());
221 Name namingEntryName = NamingEntryUtil.makeNamingEntryName(null, ee);
222 NamingUtil.bind(envCtx, namingEntryName.toString(), ee);
223 }
224
225 LOG.debug("Binding env entries from the server scope");
226
227 scope = context.getServer();
228 list = NamingEntryUtil.lookupNamingEntries(scope, EnvEntry.class);
229 itor = list.iterator();
230 while (itor.hasNext())
231 {
232 EnvEntry ee = (EnvEntry)itor.next();
233 ee.bindToENC(ee.getJndiName());
234 Name namingEntryName = NamingEntryUtil.makeNamingEntryName(null, ee);
235 NamingUtil.bind(envCtx, namingEntryName.toString(), ee);
236 }
237
238 LOG.debug("Binding env entries from the context scope");
239 scope = context;
240 list = NamingEntryUtil.lookupNamingEntries(scope, EnvEntry.class);
241 itor = list.iterator();
242 while (itor.hasNext())
243 {
244 EnvEntry ee = (EnvEntry)itor.next();
245 ee.bindToENC(ee.getJndiName());
246 Name namingEntryName = NamingEntryUtil.makeNamingEntryName(null, ee);
247 NamingUtil.bind(envCtx, namingEntryName.toString(), ee);
248 }
249 }
250
251 protected void createEnvContext (WebAppContext wac)
252 throws NamingException
253 {
254 ClassLoader old_loader = Thread.currentThread().getContextClassLoader();
255 Thread.currentThread().setContextClassLoader(wac.getClassLoader());
256 ContextFactory.associateClassLoader(wac.getClassLoader());
257 try
258 {
259 Context context = new InitialContext();
260 Context compCtx = (Context)context.lookup ("java:comp");
261 compCtx.createSubcontext("env");
262 }
263 finally
264 {
265 ContextFactory.disassociateClassLoader();
266 Thread.currentThread().setContextClassLoader(old_loader);
267 }
268 }
269
270 private static class Bound
271 {
272 final NamingContext _context;
273 final String _name;
274 Bound(NamingContext context, String name)
275 {
276 _context=context;
277 _name=name;
278 }
279 }
280 }