View Javadoc

1   package org.eclipse.jetty.deploy.providers;
2   
3   import java.io.File;
4   import java.io.FilenameFilter;
5   import java.io.IOException;
6   import java.net.MalformedURLException;
7   
8   import org.eclipse.jetty.deploy.App;
9   import org.eclipse.jetty.deploy.WebAppDeployer;
10  import org.eclipse.jetty.deploy.util.FileID;
11  import org.eclipse.jetty.server.handler.ContextHandler;
12  import org.eclipse.jetty.util.URIUtil;
13  import org.eclipse.jetty.util.resource.Resource;
14  import org.eclipse.jetty.webapp.WebAppContext;
15  
16  
17  /* ------------------------------------------------------------ */
18  /** Context directory App Provider.
19   * <p>This specialization of {@link MonitoredDirAppProvider} is the
20   * replacement for {@link WebAppDeployer} and it will scan a directory
21   * only for war files or directories files.</p>
22   * @see WebAppDeployer
23   */
24  public class WebAppProvider extends ScanningAppProvider
25  {
26      private boolean _extractWars = false;
27      private boolean _parentLoaderPriority = false;
28      private String _defaultsDescriptor;
29      private Filter _filter;
30      private String[] _configurationClasses;
31  
32      private static class Filter implements FilenameFilter
33      {
34          private File _contexts;
35          
36          public boolean accept(File dir, String name)
37          {
38              if (!dir.exists())
39                  return false;
40              String lowername = name.toLowerCase();
41              
42              File file = new File(dir,name);
43              // is it not a directory and not a war ?
44              if (!file.isDirectory() && !lowername.endsWith(".war"))
45                  return false;
46              
47              // is it a directory for an existing war file?
48              if (file.isDirectory() && 
49                      (new File(dir,name+".war").exists() ||
50                       new File(dir,name+".WAR").exists()))
51              {
52                  return false;
53              }
54              
55              // is there a contexts config file
56              if (_contexts!=null)
57              {
58                  String context=name;
59                  if (!file.isDirectory())
60                      context=context.substring(0,context.length()-4);
61                  if (new File(_contexts,context+".xml").exists() ||
62                      new File(_contexts,context+".XML").exists() )
63                  {
64                      return false;
65                  }
66              }
67                 
68              return true;
69          }
70      }
71      
72      /* ------------------------------------------------------------ */
73      public WebAppProvider()
74      {
75          super(new Filter());
76          _filter=(Filter)_filenameFilter;
77          setScanInterval(0);
78      }
79  
80      /* ------------------------------------------------------------ */
81      /** Get the extractWars.
82       * @return the extractWars
83       */
84      public boolean isExtractWars()
85      {
86          return _extractWars;
87      }
88  
89      /* ------------------------------------------------------------ */
90      /** Set the extractWars.
91       * @param extractWars the extractWars to set
92       */
93      public void setExtractWars(boolean extractWars)
94      {
95          _extractWars = extractWars;
96      }
97  
98      /* ------------------------------------------------------------ */
99      /** Get the parentLoaderPriority.
100      * @return the parentLoaderPriority
101      */
102     public boolean isParentLoaderPriority()
103     {
104         return _parentLoaderPriority;
105     }
106 
107     /* ------------------------------------------------------------ */
108     /** Set the parentLoaderPriority.
109      * @param parentLoaderPriority the parentLoaderPriority to set
110      */
111     public void setParentLoaderPriority(boolean parentLoaderPriority)
112     {
113         _parentLoaderPriority = parentLoaderPriority;
114     }
115 
116     /* ------------------------------------------------------------ */
117     /** Get the defaultsDescriptor.
118      * @return the defaultsDescriptor
119      */
120     public String getDefaultsDescriptor()
121     {
122         return _defaultsDescriptor;
123     }
124 
125     /* ------------------------------------------------------------ */
126     /** Set the defaultsDescriptor.
127      * @param defaultsDescriptor the defaultsDescriptor to set
128      */
129     public void setDefaultsDescriptor(String defaultsDescriptor)
130     {
131         _defaultsDescriptor = defaultsDescriptor;
132     }
133 
134     /* ------------------------------------------------------------ */
135     public String getContextXmlDir()
136     {
137         return _filter._contexts==null?null:_filter._contexts.toString();
138     }
139 
140     /* ------------------------------------------------------------ */
141     /**
142      * Set the directory in which to look for context XML files.
143      * <p>
144      * If a webapp call "foo/" or "foo.war" is discovered in the monitored
145      * directory, then the ContextXmlDir is examined to see if a foo.xml
146      * file exists.  If it does, then this deployer will not deploy the webapp
147      * and the ContextProvider should be used to act on the foo.xml file.
148      * @see ContextProvider
149      * @param contextsDir
150      */
151     public void setContextXmlDir(String contextsDir)
152     {
153         try
154         {
155             _filter._contexts=Resource.newResource(contextsDir).getFile();
156         }
157         catch (MalformedURLException e)
158         {
159             e.printStackTrace();
160         }
161         catch (IOException e)
162         {
163             throw new RuntimeException(e);
164         }
165     }
166     
167     
168     /* ------------------------------------------------------------ */
169     /**
170      * @param configurations The configuration class names.
171      */
172     public void setConfigurationClasses(String[] configurations)
173     {
174         _configurationClasses = configurations==null?null:(String[])configurations.clone();
175     }  
176     
177     /* ------------------------------------------------------------ */
178     /**
179      * 
180      */
181     public String[] getConfigurationClasses()
182     {
183         return _configurationClasses;
184     }
185     
186     
187     /* ------------------------------------------------------------ */
188     public ContextHandler createContextHandler(final App app) throws Exception
189     {
190         Resource resource = Resource.newResource(app.getOriginId());
191         File file = resource.getFile();
192         if (!resource.exists())
193             throw new IllegalStateException("App resouce does not exist "+resource);
194 
195         String context = file.getName();
196         
197         if (file.isDirectory())
198         {
199             // must be a directory
200         }
201         else if (FileID.isWebArchiveFile(file))
202         {
203             // Context Path is the same as the archive.
204             context = context.substring(0,context.length() - 4);
205         }
206         else
207             throw new IllegalStateException("unable to create ContextHandler for "+app);
208         
209         // special case of archive (or dir) named "root" is / context
210         if (context.equalsIgnoreCase("root") || context.equalsIgnoreCase("root/"))
211             context = URIUtil.SLASH;
212 
213         // Ensure "/" is Prepended to all context paths.
214         if (context.charAt(0) != '/')
215             context = "/" + context;
216 
217         // Ensure "/" is Not Trailing in context paths.
218         if (context.endsWith("/") && context.length() > 0)
219             context = context.substring(0,context.length() - 1);
220 
221         WebAppContext wah = new WebAppContext();
222         wah.setContextPath(context);
223         wah.setWar(file.getAbsolutePath());
224         if (_defaultsDescriptor != null)
225             wah.setDefaultsDescriptor(_defaultsDescriptor);
226         wah.setExtractWAR(_extractWars);
227         wah.setParentLoaderPriority(_parentLoaderPriority);
228         if (_configurationClasses != null)
229             wah.setConfigurationClasses(_configurationClasses);
230 
231         return wah; 
232     }
233     
234 }