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 }
63
64
65
66
67 public void configure (WebAppContext context) throws Exception
68 {
69
70
71 createEnvContext();
72 if (Log.isDebugEnabled())
73 Log.debug("Created java:comp/env for webapp "+context.getContextPath());
74
75
76
77 if (jettyEnvXmlUrl == null)
78 {
79
80
81
82 org.eclipse.jetty.util.resource.Resource web_inf = context.getWebInf();
83 if(web_inf!=null && web_inf.isDirectory())
84 {
85 org.eclipse.jetty.util.resource.Resource jettyEnv = web_inf.addPath("jetty-env.xml");
86 if(jettyEnv.exists())
87 {
88 jettyEnvXmlUrl = jettyEnv.getURL();
89 }
90 }
91 }
92
93
94 if (jettyEnvXmlUrl != null)
95 {
96 XmlConfiguration configuration = new XmlConfiguration(jettyEnvXmlUrl);
97 configuration.configure(context);
98 }
99
100
101 bindEnvEntries(context);
102 }
103
104 public void postConfigure(WebAppContext context) throws Exception
105 {
106
107
108 }
109
110
111
112
113
114
115
116 public void deconfigure (WebAppContext context) throws Exception
117 {
118
119 ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
120 Thread.currentThread().setContextClassLoader(context.getClassLoader());
121 try
122 {
123 Context ic = new InitialContext();
124 Context compCtx = (Context)ic.lookup ("java:comp");
125 compCtx.destroySubcontext("env");
126 }
127 catch (NameNotFoundException e)
128 {
129 Log.warn(e);
130 }
131
132
133 try
134 {
135 Context scopeContext = NamingEntryUtil.getContextForScope(context);
136 scopeContext.destroySubcontext(NamingEntry.__contextName);
137 }
138 catch (NameNotFoundException e)
139 {
140 Log.ignore(e);
141 Log.debug("No naming entries configured in environment for webapp "+context);
142 }
143 Thread.currentThread().setContextClassLoader(oldLoader);
144 }
145
146
147
148
149
150
151
152
153 public void bindEnvEntries (WebAppContext context)
154 throws NamingException
155 {
156 Log.debug("Binding env entries from the jvm scope");
157 InitialContext ic = new InitialContext();
158 Context envCtx = (Context)ic.lookup("java:comp/env");
159 Object scope = null;
160 List list = NamingEntryUtil.lookupNamingEntries(scope, EnvEntry.class);
161 Iterator itor = list.iterator();
162 while (itor.hasNext())
163 {
164 EnvEntry ee = (EnvEntry)itor.next();
165 ee.bindToENC(ee.getJndiName());
166 Name namingEntryName = NamingEntryUtil.makeNamingEntryName(null, ee);
167 NamingUtil.bind(envCtx, namingEntryName.toString(), ee);
168 }
169
170 Log.debug("Binding env entries from the server scope");
171
172 scope = context.getServer();
173 list = NamingEntryUtil.lookupNamingEntries(scope, EnvEntry.class);
174 itor = list.iterator();
175 while (itor.hasNext())
176 {
177 EnvEntry ee = (EnvEntry)itor.next();
178 ee.bindToENC(ee.getJndiName());
179 Name namingEntryName = NamingEntryUtil.makeNamingEntryName(null, ee);
180 NamingUtil.bind(envCtx, namingEntryName.toString(), ee);
181 }
182
183 Log.debug("Binding env entries from the context scope");
184 scope = context;
185 list = NamingEntryUtil.lookupNamingEntries(scope, EnvEntry.class);
186 itor = list.iterator();
187 while (itor.hasNext())
188 {
189 EnvEntry ee = (EnvEntry)itor.next();
190 ee.bindToENC(ee.getJndiName());
191 Name namingEntryName = NamingEntryUtil.makeNamingEntryName(null, ee);
192 NamingUtil.bind(envCtx, namingEntryName.toString(), ee);
193 }
194 }
195
196 protected void createEnvContext ()
197 throws NamingException
198 {
199 Context context = new InitialContext();
200 Context compCtx = (Context)context.lookup ("java:comp");
201 compCtx.createSubcontext("env");
202 }
203 }