1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.policy;
20
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Set;
26 import java.util.concurrent.CountDownLatch;
27 import java.util.concurrent.atomic.AtomicInteger;
28
29 import org.eclipse.jetty.policy.loader.DefaultPolicyLoader;
30 import org.eclipse.jetty.util.Scanner;
31 import org.eclipse.jetty.util.component.AbstractLifeCycle;
32
33
34
35
36
37
38
39 public abstract class PolicyMonitor extends AbstractLifeCycle
40 {
41
42
43
44
45 private String _policyDirectory;
46
47
48
49
50 private Scanner _scanner;
51
52
53
54
55
56 private boolean _reload = true;
57
58
59
60
61 private int _scanInterval = 1;
62
63
64
65
66 private LatchScannerListener _scanningListener;
67
68
69
70
71 private boolean _initialized = false;
72
73
74
75
76 private AtomicInteger _scanCount = new AtomicInteger(0);
77
78
79
80
81 public PolicyMonitor()
82 {
83
84 }
85
86
87
88
89
90
91 public PolicyMonitor( String directory )
92 {
93 this();
94 _policyDirectory = directory;
95 }
96
97
98
99
100
101
102 public void setPolicyDirectory( String directory )
103 {
104 if (isRunning())
105 {
106 throw new PolicyException("policy monitor is running, unable to set policy directory");
107 }
108
109 _policyDirectory = directory;
110 }
111
112
113
114
115
116
117 public int getScanInterval()
118 {
119 return _scanInterval;
120 }
121
122
123
124
125
126
127
128 public void setScanInterval( int scanInterval )
129 {
130 if (isRunning())
131 {
132 throw new PolicyException("policy monitor is running, unable to set scan interval");
133 }
134
135 _scanInterval = scanInterval;
136 }
137
138
139
140
141
142
143
144 public boolean isInitialized()
145 {
146 return _initialized;
147 }
148
149
150
151
152
153
154 public int getScanCount()
155 {
156 return _scanCount.get();
157 }
158
159
160
161
162
163
164 public synchronized void waitForScan() throws Exception
165 {
166
167 CountDownLatch latch = new CountDownLatch(2);
168
169 _scanningListener.setScanningLatch(latch);
170 _scanner.scan();
171 latch.await();
172 }
173
174
175
176
177
178
179 public boolean isReloadEnabled()
180 {
181 return _reload;
182 }
183
184
185
186
187
188
189
190
191 public void setReload(boolean reload)
192 {
193 if (isRunning())
194 {
195 throw new PolicyException("policy monitor is running, unable to set reload at this time");
196 }
197
198 _reload = reload;
199 }
200
201
202
203
204
205
206
207 private void processPolicyFile(String filename)
208 {
209 try
210 {
211 File policyFile = new File(filename);
212
213 Set<PolicyBlock> policyBlocks = DefaultPolicyLoader.load(new FileInputStream(policyFile),JettyPolicy.getContext());
214
215 for (PolicyBlock policy : policyBlocks)
216 {
217 onPolicyChange(policy);
218 }
219 }
220 catch (Exception e)
221 {
222 e.printStackTrace();
223 }
224 }
225
226
227
228
229 @Override
230 protected void doStart() throws Exception
231 {
232 super.doStart();
233
234 _scanner = new Scanner();
235
236 List<File> scanDirs = new ArrayList<File>();
237
238 scanDirs.add(new File( _policyDirectory ) );
239
240
241
242 _scanner.addListener(new Scanner.DiscreteListener()
243 {
244
245 public void fileRemoved(String filename) throws Exception
246 {
247
248 }
249
250
251 public void fileChanged(String filename) throws Exception
252 {
253 if (_reload && filename.endsWith("policy"))
254 {
255
256 processPolicyFile(filename);
257 }
258 }
259
260 public void fileAdded(String filename) throws Exception
261 {
262 if (filename.endsWith("policy"))
263 {
264
265 processPolicyFile(filename);
266 }
267 }
268 });
269
270 _scanningListener = new LatchScannerListener();
271
272 _scanner.addListener(_scanningListener);
273
274 _scanner.setScanDirs(scanDirs);
275 _scanner.setReportExistingFilesOnStartup(true);
276 _scanner.start();
277 _scanner.setScanInterval(_scanInterval);
278 }
279
280
281
282
283 @Override
284 protected void doStop() throws Exception
285 {
286 super.doStop();
287
288 _scanner.stop();
289 }
290
291
292
293
294
295
296 private class LatchScannerListener implements Scanner.ScanCycleListener
297 {
298 CountDownLatch _latch;
299
300 public void scanStarted(int cycle) throws Exception
301 {
302
303 }
304
305 public void scanEnded(int cycle) throws Exception
306 {
307 _initialized = true;
308 _scanCount.incrementAndGet();
309 if ( _latch != null )
310 {
311 _latch.countDown();
312 }
313 }
314
315 public void setScanningLatch( CountDownLatch latch )
316 {
317 _latch = latch;
318 }
319 }
320
321
322
323
324
325
326
327 public abstract void onPolicyChange(PolicyBlock grant);
328 }