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