1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.eclipse.jetty.deploy.providers;
17
18 import java.io.File;
19 import java.io.FilenameFilter;
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.Map;
23
24 import org.eclipse.jetty.deploy.App;
25 import org.eclipse.jetty.deploy.AppProvider;
26 import org.eclipse.jetty.deploy.DeploymentManager;
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
33
34
35 public abstract class ScanningAppProvider extends AbstractLifeCycle implements AppProvider
36 {
37 private static final Logger LOG = Log.getLogger(ScanningAppProvider.class);
38
39 private Map<String, App> _appMap = new HashMap<String, App>();
40
41 private DeploymentManager _deploymentManager;
42 protected final FilenameFilter _filenameFilter;
43 private Resource _monitoredDir;
44 private boolean _recursive = false;
45 private int _scanInterval = 10;
46 private Scanner _scanner;
47
48
49 private final Scanner.DiscreteListener _scannerListener = new Scanner.DiscreteListener()
50 {
51 public void fileAdded(String filename) throws Exception
52 {
53 ScanningAppProvider.this.fileAdded(filename);
54 }
55
56 public void fileChanged(String filename) throws Exception
57 {
58 ScanningAppProvider.this.fileChanged(filename);
59 }
60
61 public void fileRemoved(String filename) throws Exception
62 {
63 ScanningAppProvider.this.fileRemoved(filename);
64 }
65 };
66
67
68 protected ScanningAppProvider(FilenameFilter filter)
69 {
70 _filenameFilter = filter;
71 }
72
73
74
75
76
77 protected Map<String, App> getDeployedApps()
78 {
79 return _appMap;
80 }
81
82
83
84
85
86
87
88
89
90
91
92
93 protected App createApp(String filename)
94 {
95 return new App(_deploymentManager,this,filename);
96 }
97
98
99 @Override
100 protected void doStart() throws Exception
101 {
102 if (LOG.isDebugEnabled())
103 LOG.debug(this.getClass().getSimpleName() + ".doStart()");
104 if (_monitoredDir == null)
105 {
106 throw new IllegalStateException("No configuration dir specified");
107 }
108
109 File scandir = _monitoredDir.getFile();
110 LOG.info("Deployment monitor " + scandir + " at interval " + _scanInterval);
111 _scanner = new Scanner();
112 _scanner.setScanDirs(Collections.singletonList(scandir));
113 _scanner.setScanInterval(_scanInterval);
114 _scanner.setRecursive(_recursive);
115 _scanner.setFilenameFilter(_filenameFilter);
116 _scanner.setReportDirs(true);
117 _scanner.addListener(_scannerListener);
118 _scanner.start();
119 }
120
121
122 @Override
123 protected void doStop() throws Exception
124 {
125 if (_scanner!=null)
126 {
127 _scanner.stop();
128 _scanner.removeListener(_scannerListener);
129 _scanner = null;
130 }
131 }
132
133
134 protected void fileAdded(String filename) throws Exception
135 {
136 if (LOG.isDebugEnabled())
137 LOG.debug("added {}",filename);
138 App app = ScanningAppProvider.this.createApp(filename);
139 if (app != null)
140 {
141 _appMap.put(filename,app);
142 _deploymentManager.addApp(app);
143 }
144 }
145
146
147 protected void fileChanged(String filename) throws Exception
148 {
149 if (LOG.isDebugEnabled())
150 LOG.debug("changed {}",filename);
151 App app = _appMap.remove(filename);
152 if (app != null)
153 {
154 _deploymentManager.removeApp(app);
155 }
156 app = ScanningAppProvider.this.createApp(filename);
157 if (app != null)
158 {
159 _appMap.put(filename,app);
160 _deploymentManager.addApp(app);
161 }
162 }
163
164
165 protected void fileRemoved(String filename) throws Exception
166 {
167 if (LOG.isDebugEnabled())
168 LOG.debug("removed {}",filename);
169 App app = _appMap.remove(filename);
170 if (app != null)
171 _deploymentManager.removeApp(app);
172 }
173
174
175
176
177
178
179
180 public DeploymentManager getDeploymentManager()
181 {
182 return _deploymentManager;
183 }
184
185
186
187 public Resource getMonitoredDirResource()
188 {
189 return _monitoredDir;
190 }
191
192
193 public String getMonitoredDirName()
194 {
195 return _monitoredDir.toString();
196 }
197
198
199 public int getScanInterval()
200 {
201 return _scanInterval;
202 }
203
204
205 public boolean isRecursive()
206 {
207 return _recursive;
208 }
209
210
211 public void setDeploymentManager(DeploymentManager deploymentManager)
212 {
213 _deploymentManager = deploymentManager;
214 }
215
216
217 public void setMonitoredDirResource(Resource contextsDir)
218 {
219 _monitoredDir = contextsDir;
220 }
221
222
223 public void addScannerListener(Scanner.Listener listener)
224 {
225 _scanner.addListener(listener);
226 }
227
228
229
230
231
232 public void setMonitoredDir(String dir)
233 {
234 setMonitoredDirName(dir);
235 }
236
237
238
239
240
241
242 public void setMonitoredDirName(String dir)
243 {
244 try
245 {
246 setMonitoredDirResource(Resource.newResource(dir));
247 }
248 catch (Exception e)
249 {
250 throw new IllegalArgumentException(e);
251 }
252 }
253
254
255 protected void setRecursive(boolean recursive)
256 {
257 _recursive = recursive;
258 }
259
260
261 public void setScanInterval(int scanInterval)
262 {
263 _scanInterval = scanInterval;
264 }
265 }