1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.eclipse.jetty.plus.webapp;
15
16 import java.net.URL;
17 import java.util.Iterator;
18 import java.util.List;
19
20 import javax.naming.Context;
21 import javax.naming.InitialContext;
22 import javax.naming.Name;
23 import javax.naming.NameNotFoundException;
24 import javax.naming.NamingException;
25
26 import org.eclipse.jetty.jndi.NamingUtil;
27 import org.eclipse.jetty.plus.jndi.EnvEntry;
28 import org.eclipse.jetty.plus.jndi.NamingEntry;
29 import org.eclipse.jetty.plus.jndi.NamingEntryUtil;
30 import org.eclipse.jetty.util.log.Log;
31 import org.eclipse.jetty.webapp.Configuration;
32 import org.eclipse.jetty.webapp.WebAppContext;
33 import org.eclipse.jetty.xml.XmlConfiguration;
34
35
36
37
38
39
40
41 public class EnvConfiguration implements Configuration
42 {
43 private URL jettyEnvXmlUrl;
44
45
46
47
48 public void setJettyEnvXml (URL url)
49 {
50 this.jettyEnvXmlUrl = url;
51 }
52
53
54
55
56
57
58
59 public void preConfigure (WebAppContext context) throws Exception
60 {
61
62 createEnvContext(context);
63 }
64
65
66
67
68 public void configure (WebAppContext context) throws Exception
69 {
70 if (Log.isDebugEnabled())
71 Log.debug("Created java:comp/env for webapp "+context.getContextPath());
72
73
74
75 if (jettyEnvXmlUrl == null)
76 {
77
78
79
80 org.eclipse.jetty.util.resource.Resource web_inf = context.getWebInf();
81 if(web_inf!=null && web_inf.isDirectory())
82 {
83 org.eclipse.jetty.util.resource.Resource jettyEnv = web_inf.addPath("jetty-env.xml");
84 if(jettyEnv.exists())
85 {
86 jettyEnvXmlUrl = jettyEnv.getURL();
87 }
88 }
89 }
90
91
92 if (jettyEnvXmlUrl != null)
93 {
94 XmlConfiguration configuration = new XmlConfiguration(jettyEnvXmlUrl);
95 configuration.configure(context);
96 }
97
98
99 bindEnvEntries(context);
100 }
101
102 public void postConfigure(WebAppContext context) throws Exception
103 {
104
105
106 }
107
108
109
110
111
112
113
114 public void deconfigure (WebAppContext context) throws Exception
115 {
116
117 ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
118 Thread.currentThread().setContextClassLoader(context.getClassLoader());
119 try
120 {
121 Context ic = new InitialContext();
122 Context compCtx = (Context)ic.lookup ("java:comp");
123 compCtx.destroySubcontext("env");
124 }
125 catch (NameNotFoundException e)
126 {
127 Log.warn(e);
128 }
129
130
131 try
132 {
133 Context scopeContext = NamingEntryUtil.getContextForScope(context);
134 scopeContext.destroySubcontext(NamingEntry.__contextName);
135 }
136 catch (NameNotFoundException e)
137 {
138 Log.ignore(e);
139 Log.debug("No naming entries configured in environment for webapp "+context);
140 }
141 Thread.currentThread().setContextClassLoader(oldLoader);
142 }
143
144
145
146
147
148
149
150
151 public void bindEnvEntries (WebAppContext context)
152 throws NamingException
153 {
154 Log.debug("Binding env entries from the jvm scope");
155 InitialContext ic = new InitialContext();
156 Context envCtx = (Context)ic.lookup("java:comp/env");
157 Object scope = null;
158 List list = NamingEntryUtil.lookupNamingEntries(scope, EnvEntry.class);
159 Iterator itor = list.iterator();
160 while (itor.hasNext())
161 {
162 EnvEntry ee = (EnvEntry)itor.next();
163 ee.bindToENC(ee.getJndiName());
164 Name namingEntryName = NamingEntryUtil.makeNamingEntryName(null, ee);
165 NamingUtil.bind(envCtx, namingEntryName.toString(), ee);
166 }
167
168 Log.debug("Binding env entries from the server scope");
169
170 scope = context.getServer();
171 list = NamingEntryUtil.lookupNamingEntries(scope, EnvEntry.class);
172 itor = list.iterator();
173 while (itor.hasNext())
174 {
175 EnvEntry ee = (EnvEntry)itor.next();
176 ee.bindToENC(ee.getJndiName());
177 Name namingEntryName = NamingEntryUtil.makeNamingEntryName(null, ee);
178 NamingUtil.bind(envCtx, namingEntryName.toString(), ee);
179 }
180
181 Log.debug("Binding env entries from the context scope");
182 scope = context;
183 list = NamingEntryUtil.lookupNamingEntries(scope, EnvEntry.class);
184 itor = list.iterator();
185 while (itor.hasNext())
186 {
187 EnvEntry ee = (EnvEntry)itor.next();
188 ee.bindToENC(ee.getJndiName());
189 Name namingEntryName = NamingEntryUtil.makeNamingEntryName(null, ee);
190 NamingUtil.bind(envCtx, namingEntryName.toString(), ee);
191 }
192 }
193
194 protected void createEnvContext (WebAppContext wac)
195 throws NamingException
196 {
197 ClassLoader old_loader = Thread.currentThread().getContextClassLoader();
198 Thread.currentThread().setContextClassLoader(wac.getClassLoader());
199 try
200 {
201 Context context = new InitialContext();
202 Context compCtx = (Context)context.lookup ("java:comp");
203 compCtx.createSubcontext("env");
204 }
205 finally
206 {
207 Thread.currentThread().setContextClassLoader(old_loader);
208 }
209 }
210 }