View Javadoc

1   // ========================================================================
2   // Copyright (c) Webtide LLC
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   //
8   // The Eclipse Public License is available at 
9   // http://www.eclipse.org/legal/epl-v10.html
10  //
11  // The Apache License v2.0 is available at
12  // http://www.apache.org/licenses/LICENSE-2.0.txt
13  //
14  // You may elect to redistribute this code under either of these licenses. 
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       * @return The index of currently deployed applications.
76       */
77      protected Map<String, App> getDeployedApps()
78      {
79          return _appMap;
80      }
81  
82      /* ------------------------------------------------------------ */
83      /**
84       * Called by the Scanner.DiscreteListener to create a new App object.
85       * Isolated in a method so that it is possible to override the default App
86       * object for specialized implementations of the AppProvider.
87       * 
88       * @param filename
89       *            The file that is the context.xml. It is resolved by
90       *            {@link Resource#newResource(String)}
91       * @return The App object for this particular context definition file.
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      * Get the deploymentManager.
177      * 
178      * @return the deploymentManager
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      * @deprecated Use {@link #setMonitoredDirName(String)}
231      */
232     public void setMonitoredDir(String dir)
233     {
234         setMonitoredDirName(dir);
235     }
236     
237     /* ------------------------------------------------------------ */
238     /**
239      * @param dir
240      *            Directory to scan for context descriptors or war files
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 }