View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.deploy.providers;
20  
21  import java.io.File;
22  import java.io.FilenameFilter;
23  import java.util.Locale;
24  
25  import org.eclipse.jetty.deploy.App;
26  import org.eclipse.jetty.deploy.ConfigurationManager;
27  import org.eclipse.jetty.deploy.util.FileID;
28  import org.eclipse.jetty.server.Server;
29  import org.eclipse.jetty.server.handler.ContextHandler;
30  import org.eclipse.jetty.util.URIUtil;
31  import org.eclipse.jetty.util.annotation.ManagedAttribute;
32  import org.eclipse.jetty.util.annotation.ManagedObject;
33  import org.eclipse.jetty.util.resource.Resource;
34  import org.eclipse.jetty.webapp.WebAppContext;
35  import org.eclipse.jetty.xml.XmlConfiguration;
36  
37  /** 
38   * The webapps directory scanning provider.
39   * <p>
40   * This provider scans one or more directories (typically "webapps") for contexts to
41   * deploy, which may be:<ul>
42   * <li>A standard WAR file (must end in ".war")</li>
43   * <li>A directory containing an expanded WAR file</li>
44   * <li>A directory containing static content</li>
45   * <li>An XML descriptor in {@link XmlConfiguration} format that configures a {@link ContextHandler} instance</li>
46   * </ul>
47   * <p>
48   * To avoid double deployments and allow flexibility of the content of the scanned directories, the provider
49   * implements some heuristics to ignore some files found in the scans: <ul>
50   * <li>Hidden files (starting with ".") are ignored</li>
51   * <li>Directories with names ending in ".d" are ignored</li>
52   * <li>If a directory and a WAR file exist ( eg foo/ and foo.war) then the directory is assumed to be
53   * the unpacked WAR and only the WAR is deployed (which may reused the unpacked directory)</li>
54   * <li>If a directory and a matching XML file exist ( eg foo/ and foo.xml) then the directory is assumed to be
55   * an unpacked WAR and only the XML is deployed (which may used the directory in it's configuration)</li>
56   * <li>If a WAR file and a matching XML exist (eg foo.war and foo.xml) then the WAR is assumed to
57   * be configured by the XML and only the XML is deployed.
58   * </ul>
59   * <p>For XML configured contexts, the ID map will contain a reference to the {@link Server} instance called "Server" and
60   * properties for the webapp file as "jetty.webapp" and directory as "jetty.webapps".
61   */
62  @ManagedObject("Provider for start-up deployement of webapps based on presence in directory")
63  public class WebAppProvider extends ScanningAppProvider
64  {
65      private boolean _extractWars = false;
66      private boolean _parentLoaderPriority = false;
67      private ConfigurationManager _configurationManager;
68      private String _defaultsDescriptor;
69      private File _tempDirectory;
70      private String[] _configurationClasses;
71  
72      public class Filter implements FilenameFilter
73      {
74          @Override
75          public boolean accept(File dir, String name)
76          {
77              if (!dir.exists())
78              {
79                  return false;
80              }
81              String lowername = name.toLowerCase(Locale.ENGLISH);
82  
83              File file = new File(dir,name);
84  
85              // ignore hidden files
86              if (lowername.startsWith("."))
87                  return false;
88  
89              // Ignore some directories
90              if (file.isDirectory())
91              {
92                  // is it a nominated config directory
93                  if (lowername.endsWith(".d"))
94                      return false;
95  
96                  // is it an unpacked directory for an existing war file?
97                  if (exists(name+".war")||exists(name+".WAR"))
98                      return false;
99  
100                 // is it a directory for an existing xml file?
101                 if (exists(name+".xml")||exists(name+".XML"))
102                     return false;
103 
104                 //is it a sccs dir?
105                 if ("cvs".equals(lowername) || "cvsroot".equals(lowername))
106                     return false;
107 
108                 // OK to deploy it then
109                 return true;
110             }
111 
112             // else is it a war file
113             if (lowername.endsWith(".war"))
114             {
115                 //defer deployment decision to fileChanged()
116                 return true;
117             }
118 
119             // else is it a context XML file 
120             if (lowername.endsWith(".xml"))
121                 return true;
122 
123             return false;
124         }
125     }
126 
127     /* ------------------------------------------------------------ */
128     public WebAppProvider()
129     {
130         super();
131         setFilenameFilter(new Filter());
132         setScanInterval(0);
133     }
134 
135     /* ------------------------------------------------------------ */
136     /** Get the extractWars.
137      * @return the extractWars
138      */
139     @ManagedAttribute("extract war files")
140     public boolean isExtractWars()
141     {
142         return _extractWars;
143     }
144 
145     /* ------------------------------------------------------------ */
146     /** Set the extractWars.
147      * @param extractWars the extractWars to set
148      */
149     public void setExtractWars(boolean extractWars)
150     {
151         _extractWars = extractWars;
152     }
153 
154     /* ------------------------------------------------------------ */
155     /** Get the parentLoaderPriority.
156      * @return the parentLoaderPriority
157      */
158     @ManagedAttribute("parent classloader has priority")
159     public boolean isParentLoaderPriority()
160     {
161         return _parentLoaderPriority;
162     }
163 
164     /* ------------------------------------------------------------ */
165     /** Set the parentLoaderPriority.
166      * @param parentLoaderPriority the parentLoaderPriority to set
167      */
168     public void setParentLoaderPriority(boolean parentLoaderPriority)
169     {
170         _parentLoaderPriority = parentLoaderPriority;
171     }
172     
173     /* ------------------------------------------------------------ */
174     /** Get the defaultsDescriptor.
175      * @return the defaultsDescriptor
176      */
177     @ManagedAttribute("default descriptor for webapps")
178     public String getDefaultsDescriptor()
179     {
180         return _defaultsDescriptor;
181     }
182 
183     /* ------------------------------------------------------------ */
184     /** Set the defaultsDescriptor.
185      * @param defaultsDescriptor the defaultsDescriptor to set
186      */
187     public void setDefaultsDescriptor(String defaultsDescriptor)
188     {
189         _defaultsDescriptor = defaultsDescriptor;
190     }
191 
192     /* ------------------------------------------------------------ */
193     public ConfigurationManager getConfigurationManager()
194     {
195         return _configurationManager;
196     }
197     
198     /* ------------------------------------------------------------ */
199     /** Set the configurationManager.
200      * @param configurationManager the configurationManager to set
201      */
202     public void setConfigurationManager(ConfigurationManager configurationManager)
203     {
204         _configurationManager = configurationManager;
205     }
206     
207     /* ------------------------------------------------------------ */
208     /**
209      * @param configurations The configuration class names.
210      */
211     public void setConfigurationClasses(String[] configurations)
212     {
213         _configurationClasses = configurations==null?null:(String[])configurations.clone();
214     }  
215     
216     /* ------------------------------------------------------------ */
217     @ManagedAttribute("configuration classes for webapps to be processed through")
218     public String[] getConfigurationClasses()
219     {
220         return _configurationClasses;
221     }
222 
223     /**
224      * Set the Work directory where unpacked WAR files are managed from.
225      * <p>
226      * Default is the same as the <code>java.io.tmpdir</code> System Property.
227      *
228      * @param directory the new work directory
229      */
230     public void setTempDir(File directory)
231     {
232         _tempDirectory = directory;
233     }
234     
235     /* ------------------------------------------------------------ */
236     /**
237      * Get the user supplied Work Directory.
238      *
239      * @return the user supplied work directory (null if user has not set Temp Directory yet)
240      */
241     @ManagedAttribute("temp directory for use, null if no user set temp directory")
242     public File getTempDir()
243     {
244         return _tempDirectory;
245     }
246 
247     /* ------------------------------------------------------------ */
248     protected void initializeWebAppContextDefaults(WebAppContext webapp)
249     {
250         if (_defaultsDescriptor != null)
251             webapp.setDefaultsDescriptor(_defaultsDescriptor);
252         webapp.setExtractWAR(_extractWars);
253         webapp.setParentLoaderPriority(_parentLoaderPriority);
254         if (_configurationClasses != null)
255             webapp.setConfigurationClasses(_configurationClasses);
256 
257         if (_tempDirectory != null)
258         {
259             /* Since the Temp Dir is really a context base temp directory,
260              * Lets set the Temp Directory in a way similar to how WebInfConfiguration does it,
261              * instead of setting the WebAppContext.setTempDirectory(File).  
262              * If we used .setTempDirectory(File) all webapps will wind up in the
263              * same temp / work directory, overwriting each others work.
264              */
265             webapp.setAttribute(WebAppContext.BASETEMPDIR, _tempDirectory);
266         }
267     }
268     
269     /* ------------------------------------------------------------ */
270     @Override
271     public ContextHandler createContextHandler(final App app) throws Exception
272     {
273         Resource resource = Resource.newResource(app.getOriginId());
274         File file = resource.getFile();
275         if (!resource.exists())
276             throw new IllegalStateException("App resouce does not exist "+resource);
277 
278         String context = file.getName();
279 
280         if (resource.exists() && FileID.isXmlFile(file))
281         {
282             XmlConfiguration xmlc = new XmlConfiguration(resource.getURI().toURL())
283             {
284                 @Override
285                 public void initializeDefaults(Object context)
286                 {
287                     super.initializeDefaults(context);
288 
289                     if (context instanceof WebAppContext)
290                     {
291                         WebAppContext webapp = (WebAppContext)context;
292                         initializeWebAppContextDefaults(webapp);
293                     }
294                 }
295             };
296             
297             xmlc.getIdMap().put("Server", getDeploymentManager().getServer());
298             xmlc.getProperties().put("jetty.home",System.getProperty("jetty.home","."));
299             xmlc.getProperties().put("jetty.base",System.getProperty("jetty.base","."));
300             xmlc.getProperties().put("jetty.webapp",file.getCanonicalPath());
301             xmlc.getProperties().put("jetty.webapps",file.getParentFile().getCanonicalPath());
302 
303             if (getConfigurationManager() != null)
304                 xmlc.getProperties().putAll(getConfigurationManager().getProperties());
305             return (ContextHandler)xmlc.configure();
306         }
307         else if (file.isDirectory())
308         {
309             // must be a directory
310         }
311         else if (FileID.isWebArchiveFile(file))
312         {
313             // Context Path is the same as the archive.
314             context = context.substring(0,context.length() - 4);
315         }
316         else
317         {
318             throw new IllegalStateException("unable to create ContextHandler for "+app);
319         }
320 
321         // Ensure "/" is Not Trailing in context paths.
322         if (context.endsWith("/") && context.length() > 0)
323         {
324             context = context.substring(0,context.length() - 1);
325         }
326 
327         // Start building the webapplication
328         WebAppContext webAppContext = new WebAppContext();
329         webAppContext.setDisplayName(context);
330 
331         // special case of archive (or dir) named "root" is / context
332         if (context.equalsIgnoreCase("root"))
333         {
334             context = URIUtil.SLASH;
335         }
336         else if (context.toLowerCase(Locale.ENGLISH).startsWith("root-"))
337         {
338             int dash=context.toLowerCase(Locale.ENGLISH).indexOf('-');
339             String virtual = context.substring(dash+1);
340             webAppContext.setVirtualHosts(new String[]{virtual});
341             context = URIUtil.SLASH;
342         }
343 
344         // Ensure "/" is Prepended to all context paths.
345         if (context.charAt(0) != '/')
346         {
347             context = "/" + context;
348         }
349 
350         webAppContext.setContextPath(context);
351         webAppContext.setWar(file.getAbsolutePath());
352         initializeWebAppContextDefaults(webAppContext);
353 
354         return webAppContext;
355     }
356     
357     
358     /* ------------------------------------------------------------ */
359     @Override
360     protected void fileChanged(String filename) throws Exception
361     {        
362         File file = new File(filename);
363         if (!file.exists())
364             return;
365         
366         File parent = file.getParentFile();
367         
368         //is the file that changed a directory? 
369         if (file.isDirectory())
370         {
371             //is there a .xml file of the same name?
372             if (exists(file.getName()+".xml")||exists(file.getName()+".XML"))
373                 return; //ignore it
374 
375             //is there .war file of the same name?
376             if (exists(file.getName()+".war")||exists(file.getName()+".WAR"))
377                 return; //ignore it
378 
379              super.fileChanged(filename);
380              return;
381         }
382         
383       
384         String lowname = file.getName().toLowerCase(Locale.ENGLISH);
385         //is the file that changed a .war file?
386         if (lowname.endsWith(".war"))
387         {
388             String name = file.getName();
389             String base=name.substring(0,name.length()-4);
390             String xmlname = base+".xml";
391             if (exists(xmlname))
392             {
393                 //if a .xml file exists for it, then redeploy that instead
394                 File xml = new File (parent, xmlname);
395                 super.fileChanged(xml.getCanonicalPath());
396                 return;
397             }
398             
399             xmlname = base+".XML";
400             if (exists(xmlname))
401             {
402                 //if a .XML file exists for it, then redeploy that instead
403                 File xml = new File(parent, xmlname);
404                 super.fileChanged(xml.getCanonicalPath());
405                 return;
406             }
407             
408             //redeploy the changed war
409             super.fileChanged(filename);
410             return;
411         }
412 
413         //is the file that changed a .xml file?
414         if (lowname.endsWith(".xml"))
415             super.fileChanged(filename);
416     }
417 
418     /* ------------------------------------------------------------ */
419     @Override
420     protected void fileAdded(String filename) throws Exception
421     {
422         File file = new File(filename);
423         if (!file.exists())
424             return;
425 
426         //is the file that was added a directory? 
427         if (file.isDirectory())
428         {
429             //is there a .xml file of the same name?
430             if (exists(file.getName()+".xml")||exists(file.getName()+".XML"))
431                 return; //assume we will get added events for the xml file
432 
433             //is there .war file of the same name?
434             if (exists(file.getName()+".war")||exists(file.getName()+".WAR"))
435                 return; //assume we will get added events for the war file
436 
437             super.fileAdded(filename);
438             return;
439         }
440 
441 
442         //is the file that was added a .war file?
443         String lowname = file.getName().toLowerCase(Locale.ENGLISH);
444         if (lowname.endsWith(".war"))
445         {
446             String name = file.getName();
447             String base=name.substring(0,name.length()-4);
448             //is there a .xml file of the same name?
449             if (exists(base+".xml")||exists(base+".XML")) 
450                 return; //ignore it as we should get addition of the xml file
451 
452             super.fileAdded(filename);
453             return;
454         }
455 
456         //is the file that was added an .xml file?
457         if (lowname.endsWith(".xml"))
458             super.fileAdded(filename);
459     }
460 
461     
462     /* ------------------------------------------------------------ */
463     @Override
464     protected void fileRemoved(String filename) throws Exception
465     { 
466         File file = new File(filename);
467 
468         //is the file that was removed a directory? 
469         if (file.isDirectory())
470         {
471             //is there a .xml file of the same name?
472             if (exists(file.getName()+".xml")||exists(file.getName()+".XML"))
473                 return; //assume we will get removed events for the xml file
474 
475             //is there .war file of the same name?
476             if (exists(file.getName()+".war")||exists(file.getName()+".WAR"))
477                 return; //assume we will get removed events for the war file
478 
479             super.fileRemoved(filename);
480             return;
481         }
482   
483         //is the file that was removed a .war file?
484         String lowname = file.getName().toLowerCase(Locale.ENGLISH);
485         if (lowname.endsWith(".war"))
486         {
487             //is there a .xml file of the same name?
488             String name = file.getName();
489             String base=name.substring(0,name.length()-4);
490             if (exists(base+".xml")||exists(base+".XML"))
491                 return; //ignore it as we should get removal of the xml file
492 
493             super.fileRemoved(filename);
494             return;
495         }
496 
497         //is the file that was removed an .xml file?
498         if (lowname.endsWith(".xml"))
499             super.fileRemoved(filename);
500     }
501 
502 }