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