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()) LOG.debug(this.getClass().getSimpleName() + ".doStart()");
103         if (_monitoredDir == null)
104         {
105             throw new IllegalStateException("No configuration dir specified");
106         }
107 
108         File scandir = _monitoredDir.getFile();
109         LOG.info("Deployment monitor " + scandir + " at interval " + _scanInterval);
110         _scanner = new Scanner();
111         _scanner.setScanDirs(Collections.singletonList(scandir));
112         _scanner.setScanInterval(_scanInterval);
113         _scanner.setRecursive(_recursive);
114         _scanner.setFilenameFilter(_filenameFilter);
115         _scanner.setReportDirs(true);
116         _scanner.addListener(_scannerListener);
117         _scanner.start();
118     }
119 
120     /* ------------------------------------------------------------ */
121     @Override
122     protected void doStop() throws Exception
123     {
124         if (_scanner!=null)
125         {
126             _scanner.stop();
127             _scanner.removeListener(_scannerListener);
128             _scanner = null;
129         }
130     }
131 
132     /* ------------------------------------------------------------ */
133     protected void fileAdded(String filename) throws Exception
134     {
135         if (LOG.isDebugEnabled()) LOG.debug("added ",filename);
136         App app = ScanningAppProvider.this.createApp(filename);
137         if (app != null)
138         {
139             _appMap.put(filename,app);
140             _deploymentManager.addApp(app);
141         }
142     }
143 
144     /* ------------------------------------------------------------ */
145     protected void fileChanged(String filename) throws Exception
146     {
147         if (LOG.isDebugEnabled()) LOG.debug("changed ",filename);
148         App app = _appMap.remove(filename);
149         if (app != null)
150         {
151             _deploymentManager.removeApp(app);
152         }
153         app = ScanningAppProvider.this.createApp(filename);
154         if (app != null)
155         {
156             _appMap.put(filename,app);
157             _deploymentManager.addApp(app);
158         }
159     }
160     
161     /* ------------------------------------------------------------ */
162     protected void fileRemoved(String filename) throws Exception
163     {
164         if (LOG.isDebugEnabled()) LOG.debug("removed ",filename);
165         App app = _appMap.remove(filename);
166         if (app != null)
167             _deploymentManager.removeApp(app);
168     }
169     
170     /* ------------------------------------------------------------ */
171     /**
172      * Get the deploymentManager.
173      * 
174      * @return the deploymentManager
175      */
176     public DeploymentManager getDeploymentManager()
177     {
178         return _deploymentManager;
179     }
180 
181 
182     /* ------------------------------------------------------------ */
183     public Resource getMonitoredDirResource()
184     {
185         return _monitoredDir;
186     }
187 
188     /* ------------------------------------------------------------ */
189     public String getMonitoredDirName()
190     {
191         return _monitoredDir.toString();
192     }
193 
194     /* ------------------------------------------------------------ */
195     public int getScanInterval()
196     {
197         return _scanInterval;
198     }
199 
200     /* ------------------------------------------------------------ */
201     public boolean isRecursive()
202     {
203         return _recursive;
204     }
205 
206     /* ------------------------------------------------------------ */
207     public void setDeploymentManager(DeploymentManager deploymentManager)
208     {
209         _deploymentManager = deploymentManager;
210     }
211     
212     /* ------------------------------------------------------------ */
213     public void setMonitoredDirResource(Resource contextsDir)
214     {
215         _monitoredDir = contextsDir;
216     }
217 
218     /* ------------------------------------------------------------ */
219     public void addScannerListener(Scanner.Listener listener)
220     {
221         _scanner.addListener(listener);
222     }
223 
224     /* ------------------------------------------------------------ */
225     /**
226      * @deprecated Use {@link #setMonitoredDirName(String)}
227      */
228     public void setMonitoredDir(String dir)
229     {
230         setMonitoredDirName(dir);
231     }
232     
233     /* ------------------------------------------------------------ */
234     /**
235      * @param dir
236      *            Directory to scan for context descriptors or war files
237      */
238     public void setMonitoredDirName(String dir)
239     {
240         try
241         {
242             setMonitoredDirResource(Resource.newResource(dir));
243         }
244         catch (Exception e)
245         {
246             throw new IllegalArgumentException(e);
247         }
248     }
249 
250     /* ------------------------------------------------------------ */
251     protected void setRecursive(boolean recursive)
252     {
253         _recursive = recursive;
254     }
255 
256     /* ------------------------------------------------------------ */
257     public void setScanInterval(int scanInterval)
258     {
259         _scanInterval = scanInterval;
260     }
261 }