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.resource.Resource;
31  
32  /**
33   */
34  public abstract class ScanningAppProvider extends AbstractLifeCycle implements AppProvider
35  {
36      private Map<String, App> _appMap = new HashMap<String, App>();
37  
38      private DeploymentManager _deploymentManager;
39      protected final FilenameFilter _filenameFilter;
40      private Resource _monitoredDir;
41      private boolean _recursive = false;
42      private int _scanInterval = 10;
43      private Scanner _scanner;
44  
45      /* ------------------------------------------------------------ */
46      private final Scanner.DiscreteListener _scannerListener = new Scanner.DiscreteListener()
47      {
48          public void fileAdded(String filename) throws Exception
49          {
50              ScanningAppProvider.this.fileAdded(filename);
51          }
52  
53          public void fileChanged(String filename) throws Exception
54          {
55              ScanningAppProvider.this.fileChanged(filename);
56          }
57  
58          public void fileRemoved(String filename) throws Exception
59          {
60              ScanningAppProvider.this.fileRemoved(filename);
61          }
62      };
63  
64      /* ------------------------------------------------------------ */
65      protected ScanningAppProvider(FilenameFilter filter)
66      {
67          _filenameFilter = filter;
68      }
69  
70      /* ------------------------------------------------------------ */
71      /**
72       * @return The index of currently deployed applications.
73       */
74      protected Map<String, App> getDeployedApps()
75      {
76          return _appMap;
77      }
78  
79      /* ------------------------------------------------------------ */
80      /**
81       * Called by the Scanner.DiscreteListener to create a new App object.
82       * Isolated in a method so that it is possible to override the default App
83       * object for specialized implementations of the AppProvider.
84       * 
85       * @param filename
86       *            The file that is the context.xml. It is resolved by
87       *            {@link Resource#newResource(String)}
88       * @return The App object for this particular context definition file.
89       */
90      protected App createApp(String filename)
91      {
92          return new App(_deploymentManager,this,filename);
93      }
94  
95      /* ------------------------------------------------------------ */
96      @Override
97      protected void doStart() throws Exception
98      {
99          if (Log.isDebugEnabled()) Log.debug(this.getClass().getSimpleName() + ".doStart()");
100         if (_monitoredDir == null)
101         {
102             throw new IllegalStateException("No configuration dir specified");
103         }
104 
105         File scandir = _monitoredDir.getFile();
106         Log.info("Deployment monitor " + scandir + " at interval " + _scanInterval);
107         _scanner = new Scanner();
108         _scanner.setScanDirs(Collections.singletonList(scandir));
109         _scanner.setScanInterval(_scanInterval);
110         _scanner.setRecursive(_recursive);
111         _scanner.setFilenameFilter(_filenameFilter);
112         _scanner.setReportDirs(true);
113         _scanner.addListener(_scannerListener);
114         _scanner.start();
115     }
116 
117     /* ------------------------------------------------------------ */
118     @Override
119     protected void doStop() throws Exception
120     {
121         if (_scanner!=null)
122         {
123             _scanner.stop();
124             _scanner.removeListener(_scannerListener);
125             _scanner = null;
126         }
127     }
128 
129     /* ------------------------------------------------------------ */
130     protected void fileAdded(String filename) throws Exception
131     {
132         if (Log.isDebugEnabled()) Log.debug("added ",filename);
133         App app = ScanningAppProvider.this.createApp(filename);
134         if (app != null)
135         {
136             _appMap.put(filename,app);
137             _deploymentManager.addApp(app);
138         }
139     }
140 
141     /* ------------------------------------------------------------ */
142     protected void fileChanged(String filename) throws Exception
143     {
144         if (Log.isDebugEnabled()) Log.debug("changed ",filename);
145         App app = _appMap.remove(filename);
146         if (app != null)
147         {
148             _deploymentManager.removeApp(app);
149         }
150         app = ScanningAppProvider.this.createApp(filename);
151         if (app != null)
152         {
153             _appMap.put(filename,app);
154             _deploymentManager.addApp(app);
155         }
156     }
157     
158     /* ------------------------------------------------------------ */
159     protected void fileRemoved(String filename) throws Exception
160     {
161         if (Log.isDebugEnabled()) Log.debug("removed ",filename);
162         App app = _appMap.remove(filename);
163         if (app != null)
164             _deploymentManager.removeApp(app);
165     }
166     
167     /* ------------------------------------------------------------ */
168     /**
169      * Get the deploymentManager.
170      * 
171      * @return the deploymentManager
172      */
173     public DeploymentManager getDeploymentManager()
174     {
175         return _deploymentManager;
176     }
177 
178     /* ------------------------------------------------------------ */
179     @Deprecated
180     public Resource getMonitoredDir()
181     {
182         return _monitoredDir;
183     }
184 
185     /* ------------------------------------------------------------ */
186     public Resource getMonitoredDirResource()
187     {
188         return _monitoredDir;
189     }
190 
191     /* ------------------------------------------------------------ */
192     public String getMonitoredDirName()
193     {
194         return _monitoredDir.toString();
195     }
196 
197     /* ------------------------------------------------------------ */
198     public int getScanInterval()
199     {
200         return _scanInterval;
201     }
202 
203     /* ------------------------------------------------------------ */
204     public boolean isRecursive()
205     {
206         return _recursive;
207     }
208 
209     /* ------------------------------------------------------------ */
210     public void setDeploymentManager(DeploymentManager deploymentManager)
211     {
212         _deploymentManager = deploymentManager;
213     }
214 
215     /* ------------------------------------------------------------ */
216     /**
217      * @deprecated use {@link #setMonitoredDirResource(Resource)}
218      */
219     public void setMonitoredDir(Resource dir)
220     {
221         setMonitoredDirResource(dir);
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     public void setMonitoredDirResource(Resource contextsDir)
235     {
236         _monitoredDir = contextsDir;
237     }
238     
239     /* ------------------------------------------------------------ */
240     /**
241      * @param dir
242      *            Directory to scan for context descriptors or war files
243      */
244     public void setMonitoredDirName(String dir)
245     {
246         try
247         {
248             setMonitoredDirResource(Resource.newResource(dir));
249         }
250         catch (Exception e)
251         {
252             throw new IllegalArgumentException(e);
253         }
254     }
255 
256     /* ------------------------------------------------------------ */
257     protected void setRecursive(boolean recursive)
258     {
259         _recursive = recursive;
260     }
261 
262     /* ------------------------------------------------------------ */
263     public void setScanInterval(int scanInterval)
264     {
265         _scanInterval = scanInterval;
266     }
267 }