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