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