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