1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.eclipse.jetty.deploy;
15
16 import java.io.File;
17 import java.io.FilenameFilter;
18 import java.util.HashMap;
19 import java.util.Map;
20
21 import org.eclipse.jetty.deploy.providers.ContextProvider;
22 import org.eclipse.jetty.deploy.providers.ScanningAppProvider;
23 import org.eclipse.jetty.server.Server;
24 import org.eclipse.jetty.server.handler.ContextHandler;
25 import org.eclipse.jetty.server.handler.ContextHandlerCollection;
26 import org.eclipse.jetty.util.AttributesMap;
27 import org.eclipse.jetty.util.Scanner;
28 import org.eclipse.jetty.util.component.AbstractLifeCycle;
29 import org.eclipse.jetty.util.log.Log;
30 import org.eclipse.jetty.util.resource.Resource;
31 import org.eclipse.jetty.xml.XmlConfiguration;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 @SuppressWarnings("unchecked")
75 @Deprecated
76 public class ContextDeployer extends AbstractLifeCycle
77 {
78 private int _scanInterval=10;
79 private Scanner _scanner;
80 private ScannerListener _scannerListener;
81 private Resource _contextsDir;
82 private Map _currentDeployments = new HashMap();
83 private ContextHandlerCollection _contexts;
84 private ConfigurationManager _configMgr;
85 private boolean _recursive = false;
86 private AttributesMap _contextAttributes = new AttributesMap();
87
88
89
90 protected class ScannerListener implements Scanner.DiscreteListener
91 {
92
93
94
95
96
97 public void fileAdded(String filename) throws Exception
98 {
99 deploy(filename);
100 }
101
102
103
104
105
106
107 public void fileChanged(String filename) throws Exception
108 {
109 redeploy(filename);
110 }
111
112
113
114
115
116
117 public void fileRemoved(String filename) throws Exception
118 {
119 undeploy(filename);
120 }
121 @Override
122 public String toString()
123 {
124 return "ContextDeployer$Scanner";
125 }
126 }
127
128
129
130
131 public ContextDeployer()
132 {
133 Log.warn("ContextDeployer is deprecated. Use ContextProvider");
134 _scanner=new Scanner();
135 }
136
137
138
139
140
141 public ContextHandlerCollection getContexts()
142 {
143 return _contexts;
144 }
145
146
147
148
149
150
151
152
153 public void setContexts(ContextHandlerCollection contexts)
154 {
155 if (isStarted()||isStarting())
156 throw new IllegalStateException("Cannot set Contexts after deployer start");
157 _contexts=contexts;
158 }
159
160
161
162
163
164
165
166 public void setScanInterval(int seconds)
167 {
168 if (isStarted()||isStarting())
169 throw new IllegalStateException("Cannot change scan interval after deployer start");
170 _scanInterval=seconds;
171 }
172
173
174 public int getScanInterval()
175 {
176 return _scanInterval;
177 }
178
179
180
181
182
183 public void setContextsDir(String dir)
184 {
185 try
186 {
187 _contextsDir=Resource.newResource(dir);
188 }
189 catch(Exception e)
190 {
191 throw new IllegalArgumentException(e);
192 }
193 }
194
195
196 public String getContextsDir()
197 {
198 return _contextsDir==null?null:_contextsDir.toString();
199 }
200
201
202
203
204
205
206
207 @Deprecated
208 public void setConfigurationDir(String dir) throws Exception
209 {
210 setConfigurationDir(Resource.newResource(dir));
211 }
212
213
214
215
216
217
218
219 @Deprecated
220 public void setConfigurationDir(File file) throws Exception
221 {
222 setConfigurationDir(Resource.newResource(file.toURL()));
223 }
224
225
226
227
228
229
230 @Deprecated
231 public void setConfigurationDir(Resource resource)
232 {
233 if (isStarted()||isStarting())
234 throw new IllegalStateException("Cannot change hot deploy dir after deployer start");
235 _contextsDir=resource;
236 }
237
238
239
240
241
242
243 @Deprecated
244 public void setDirectory(String directory) throws Exception
245 {
246 setConfigurationDir(directory);
247 }
248
249
250
251
252
253
254 @Deprecated
255 public String getDirectory()
256 {
257 return getConfigurationDir().getName();
258 }
259
260
261
262
263
264
265 @Deprecated
266 public Resource getConfigurationDir()
267 {
268 return _contextsDir;
269 }
270
271
272
273
274
275 public void setConfigurationManager(ConfigurationManager configMgr)
276 {
277 _configMgr=configMgr;
278 }
279
280
281
282
283
284 public ConfigurationManager getConfigurationManager()
285 {
286 return _configMgr;
287 }
288
289
290
291 public void setRecursive (boolean recursive)
292 {
293 _recursive=recursive;
294 }
295
296
297 public boolean getRecursive ()
298 {
299 return _recursive;
300 }
301
302
303 public boolean isRecursive()
304 {
305 return _recursive;
306 }
307
308
309
310
311
312
313
314
315 public void setAttribute (String name, Object value)
316 {
317 _contextAttributes.setAttribute(name,value);
318 }
319
320
321
322
323
324
325
326
327 public Object getAttribute (String name)
328 {
329 return _contextAttributes.getAttribute(name);
330 }
331
332
333
334
335
336
337
338 public void removeAttribute(String name)
339 {
340 _contextAttributes.removeAttribute(name);
341 }
342
343
344 private void deploy(String filename) throws Exception
345 {
346 ContextHandler context=createContext(filename);
347 Log.info("Deploy "+filename+" -> "+ context);
348 _contexts.addHandler(context);
349 _currentDeployments.put(filename,context);
350 if (_contexts.isStarted())
351 context.start();
352 }
353
354
355 private void undeploy(String filename) throws Exception
356 {
357 ContextHandler context=(ContextHandler)_currentDeployments.get(filename);
358 Log.info("Undeploy "+filename+" -> "+context);
359 if (context==null)
360 return;
361 context.stop();
362 _contexts.removeHandler(context);
363 _currentDeployments.remove(filename);
364 }
365
366
367 private void redeploy(String filename) throws Exception
368 {
369 undeploy(filename);
370 deploy(filename);
371 }
372
373
374
375
376
377
378
379 @SuppressWarnings("deprecation")
380 @Override
381 protected void doStart() throws Exception
382 {
383 if (_contextsDir==null)
384 throw new IllegalStateException("No configuration dir specified");
385
386 if (_contexts==null)
387 throw new IllegalStateException("No context handler collection specified for deployer");
388
389 _scanner.setScanDir(_contextsDir.getFile());
390 _scanner.setScanInterval(getScanInterval());
391 _scanner.setRecursive(_recursive);
392
393 _scanner.setFilenameFilter(new FilenameFilter()
394 {
395 public boolean accept(File dir, String name)
396 {
397 try
398 {
399 if (name.endsWith(".xml"))
400 return true;
401 return false;
402 }
403 catch (Exception e)
404 {
405 Log.warn(e);
406 return false;
407 }
408 }
409 });
410 _scannerListener=new ScannerListener();
411 _scanner.addListener(_scannerListener);
412 _scanner.scan();
413 _scanner.start();
414 _contexts.getServer().getContainer().addBean(_scanner);
415 }
416
417
418
419
420
421
422
423 @Override
424 protected void doStop() throws Exception
425 {
426 _scanner.removeListener(_scannerListener);
427 _scanner.stop();
428 }
429
430
431
432
433
434
435
436
437
438
439
440 private ContextHandler createContext(String filename) throws Exception
441 {
442
443
444 Resource resource = Resource.newResource(filename);
445 if (!resource.exists())
446 return null;
447
448 XmlConfiguration xmlConfiguration=new XmlConfiguration(resource.getURL());
449 xmlConfiguration.getIdMap().put("Server", _contexts.getServer());
450 if (_configMgr!=null)
451 xmlConfiguration.getProperties().putAll(_configMgr.getProperties());
452
453 ContextHandler context=(ContextHandler)xmlConfiguration.configure();
454
455
456 if (_contextAttributes!=null && _contextAttributes.size()>0)
457 {
458 AttributesMap attributes = new AttributesMap(_contextAttributes);
459 attributes.addAll(context.getAttributes());
460 context.setAttributes(attributes);
461 }
462 return context;
463 }
464
465 }