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.util.Random;
19
20 import javax.naming.Context;
21 import javax.naming.InitialContext;
22 import javax.naming.NameNotFoundException;
23
24 import org.mortbay.jetty.plus.naming.EnvEntry;
25 import org.mortbay.jetty.plus.naming.Link;
26 import org.mortbay.jetty.plus.naming.NamingEntry;
27 import org.mortbay.jetty.plus.naming.NamingEntryUtil;
28 import org.mortbay.jetty.plus.naming.Transaction;
29 import org.mortbay.log.Log;
30 import org.mortbay.naming.NamingUtil;
31
32
33
34
35
36
37
38 public class Configuration extends AbstractConfiguration
39 {
40
41 private Integer _key;
42
43
44 public Configuration ()
45 {
46
47 }
48
49
50
51
52
53
54
55 public void bindEnvEntry(String name, Object value) throws Exception
56 {
57 InitialContext ic = null;
58 boolean bound = false;
59
60
61
62 ic = new InitialContext();
63 try
64 {
65 NamingEntry ne = (NamingEntry)ic.lookup("java:comp/env/"+NamingEntryUtil.makeNamingEntryName(ic.getNameParser(""), name));
66 if (ne!=null && ne instanceof EnvEntry)
67 {
68 EnvEntry ee = (EnvEntry)ne;
69 bound = ee.isOverrideWebXml();
70 }
71 }
72 catch (NameNotFoundException e)
73 {
74 bound = false;
75 }
76
77 if (!bound)
78 {
79
80 Context envCtx = (Context)ic.lookup("java:comp/env");
81 NamingUtil.bind(envCtx, name, value);
82 }
83 }
84
85
86
87
88
89
90
91
92
93
94
95 public void bindResourceRef(String name, Class typeClass)
96 throws Exception
97 {
98 bindEntry(name, typeClass);
99 }
100
101
102
103
104
105
106 public void bindResourceEnvRef(String name, Class typeClass)
107 throws Exception
108 {
109 bindEntry(name, typeClass);
110 }
111
112
113 public void bindMessageDestinationRef(String name, Class typeClass)
114 throws Exception
115 {
116 bindEntry(name, typeClass);
117 }
118
119 public void bindUserTransaction ()
120 throws Exception
121 {
122 try
123 {
124 Transaction.bindToENC();
125 }
126 catch (NameNotFoundException e)
127 {
128 Log.info("No Transaction manager found - if your webapp requires one, please configure one.");
129 }
130 }
131
132 public void configureClassLoader ()
133 throws Exception
134 {
135 super.configureClassLoader();
136 }
137
138
139 public void configureDefaults ()
140 throws Exception
141 {
142 super.configureDefaults();
143 }
144
145
146 public void configureWebApp ()
147 throws Exception
148 {
149 super.configureWebApp();
150
151 ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
152 Thread.currentThread().setContextClassLoader(getWebAppContext().getClassLoader());
153 lockCompEnv();
154 Thread.currentThread().setContextClassLoader(oldLoader);
155 }
156
157 public void deconfigureWebApp() throws Exception
158 {
159 ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
160 Thread.currentThread().setContextClassLoader(getWebAppContext().getClassLoader());
161 unlockCompEnv();
162 Thread.currentThread().setContextClassLoader(oldLoader);
163 super.deconfigureWebApp();
164 }
165
166 protected void lockCompEnv ()
167 throws Exception
168 {
169 Random random = new Random ();
170 _key = new Integer(random.nextInt());
171 Context context = new InitialContext();
172 Context compCtx = (Context)context.lookup("java:comp");
173 compCtx.addToEnvironment("org.mortbay.jndi.lock", _key);
174 }
175
176 protected void unlockCompEnv ()
177 throws Exception
178 {
179 if (_key!=null)
180 {
181 Context context = new InitialContext();
182 Context compCtx = (Context)context.lookup("java:comp");
183 compCtx.addToEnvironment("org.mortbay.jndi.unlock", _key);
184 }
185 }
186
187
188
189
190 public void parseAnnotations() throws Exception
191 {
192 Log.info(getClass().getName()+" does not support annotations on source. Use org.mortbay.jetty.annotations.Configuration instead");
193 }
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210 private void bindEntry (String name, Class typeClass)
211 throws Exception
212 {
213 String nameInEnvironment = name;
214 boolean bound = false;
215
216
217
218 Object scope = getWebAppContext();
219 NamingEntry ne = NamingEntryUtil.lookupNamingEntry(scope, name);
220
221 if (ne!=null && (ne instanceof Link))
222 {
223
224 nameInEnvironment = (String)((Link)ne).getObjectToBind();
225 Link l = (Link)ne;
226 }
227
228
229 scope = getWebAppContext();
230 bound = NamingEntryUtil.bindToENC(scope, name, nameInEnvironment);
231
232 if (bound)
233 return;
234
235
236 scope = getWebAppContext().getServer();
237 bound = NamingEntryUtil.bindToENC(scope, name, nameInEnvironment);
238 if (bound)
239 return;
240
241
242 bound = NamingEntryUtil.bindToENC(null, name, nameInEnvironment);
243 if (bound)
244 return;
245
246
247
248
249
250 nameInEnvironment = typeClass.getName()+"/default";
251
252 NamingEntry defaultNE = NamingEntryUtil.lookupNamingEntry(getWebAppContext().getServer(), nameInEnvironment);
253 if (defaultNE==null)
254 defaultNE = NamingEntryUtil.lookupNamingEntry(null, nameInEnvironment);
255
256 if (defaultNE!=null)
257 defaultNE.bindToENC(name);
258 else
259 throw new IllegalStateException("Nothing to bind for name "+nameInEnvironment);
260 }
261 }