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