View Javadoc

1   // ========================================================================
2   // Copyright 2006-2007 Sabre Holdings.
3   // ------------------------------------------------------------------------
4   // Licensed under the Apache License, Version 2.0 (the "License");
5   // you may not use this file except in compliance with the License.
6   // You may obtain a copy of the License at
7   // http://www.apache.org/licenses/LICENSE-2.0
8   // Unless required by applicable law or agreed to in writing, software
9   // distributed under the License is distributed on an "AS IS" BASIS,
10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  // See the License for the specific language governing permissions and
12  // limitations under the License.
13  // ========================================================================
14  
15  package org.mortbay.jetty.ant.types;
16  
17  import java.io.File;
18  import java.util.ArrayList;
19  import java.util.Arrays;
20  import java.util.Enumeration;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import org.apache.tools.ant.BuildException;
25  import org.apache.tools.ant.DirectoryScanner;
26  import org.apache.tools.ant.Project;
27  import org.apache.tools.ant.types.FileSet;
28  
29  /**
30   * Ant's WebApp object definition.
31   *
32   * @author Jakub Pawlowicz
33   */
34  public class WebApp
35  {
36  
37      /** List of web application libraries. */
38      private List libraries = new ArrayList();
39  
40      /** List of web application class directories. */
41      private List classes = new ArrayList();
42  
43      /** Reference to Ant project variable. */
44      private Project project;
45  
46      /** Application context path. */
47      private String contextPath;
48  
49      /** Application name. */
50      private String name;
51  
52      /** Application war file (either exploded or not). */
53      private File warFile;
54  
55      /** Location of application web.xml file. */
56      private File webXmlFile;
57  
58      /** Location of jetty-env.xml file. */
59      private File jettyEnvXml;
60  
61      /** List of extra scan targets for this web application. */
62      private FileSet scanTargets;
63  
64      /** List of optional context handlers. */
65      private ContextHandlers contextHandlers;
66  
67      private File webDefaultXmlFile;
68  
69      /**
70       * Length of interval in which application will be scanned for changes (0
71       * means scanner wouldn't be created).
72       */
73      private int scanIntervalSeconds = 0;
74  
75      public WebApp(Project project)
76      {
77          this.project = project;
78      }
79  
80      public File getWebDefaultXmlFile()
81      {
82          return this.webDefaultXmlFile;
83      }
84  
85      public void setWebDefaultXmlFile(File webDefaultXmlfile)
86      {
87          this.webDefaultXmlFile = webDefaultXmlfile;
88      }
89  
90      public void addLib(FileSet lib)
91      {
92          libraries.add(lib);
93      }
94  
95      public List getLibraries()
96      {
97          return libraries;
98      }
99  
100     public void addClasses(FileSet classes)
101     {
102         this.classes.add(classes);
103     }
104 
105     public List getClasses()
106     {
107         return classes;
108     }
109 
110     public File getWarFile()
111     {
112         return warFile;
113     }
114 
115     public void setWarFile(File warFile)
116     {
117         this.warFile = warFile;
118     }
119 
120     public String getContextPath()
121     {
122         return contextPath;
123     }
124 
125     public void setContextPath(String contextPath)
126     {
127         this.contextPath = contextPath;
128     }
129 
130     public String getName()
131     {
132         return name;
133     }
134 
135     public void setName(String name)
136     {
137         this.name = name;
138     }
139 
140     /**
141      * @return a list of classpath files (libraries and class directories).
142      */
143     public List getClassPathFiles()
144     {
145         List classPathFiles = new ArrayList();
146         Iterator classesIterator = classes.iterator();
147         while (classesIterator.hasNext())
148         {
149             FileSet clazz = (FileSet) classesIterator.next();
150             classPathFiles.add(clazz.getDirectoryScanner(project).getBasedir());
151         }
152 
153         Iterator iterator = libraries.iterator();
154         while (iterator.hasNext())
155         {
156             FileSet library = (FileSet) iterator.next();
157             String[] includedFiles = library.getDirectoryScanner(project).getIncludedFiles();
158             File baseDir = library.getDirectoryScanner(project).getBasedir();
159 
160             for (int i = 0; i < includedFiles.length; i++)
161             {
162                 classPathFiles.add(new File(baseDir, includedFiles[i]));
163             }
164         }
165 
166 
167         return classPathFiles;
168     }
169 
170     /**
171      * @return a <code>FileMatchingConfiguration</code> object describing the
172      *         configuration of all libraries added to this particular web app
173      *         (both classes and libraries).
174      */
175     public FileMatchingConfiguration getLibrariesConfiguration()
176     {
177         FileMatchingConfiguration config = new FileMatchingConfiguration();
178 
179         Iterator classesIterator = classes.iterator();
180         while (classesIterator.hasNext())
181         {
182             FileSet clazz = (FileSet) classesIterator.next();
183             config.addDirectoryScanner(clazz.getDirectoryScanner(project));
184         }
185 
186         Iterator librariesIterator = libraries.iterator();
187         while (librariesIterator.hasNext())
188         {
189             FileSet library = (FileSet) librariesIterator.next();
190             config.addDirectoryScanner(library.getDirectoryScanner(project));
191         }
192 
193         return config;
194     }
195 
196     public FileMatchingConfiguration getScanTargetsConfiguration()
197     {
198         FileMatchingConfiguration configuration = new FileMatchingConfiguration();
199 
200         if (scanTargets != null)
201         {
202             configuration.addDirectoryScanner(scanTargets.getDirectoryScanner(project));
203         }
204 
205         return configuration;
206     }
207 
208     /**
209      * @return location of web.xml file (either inside WAR or on the external
210      *         location).
211      */
212     public File getWebXmlFile()
213     {
214         if (webXmlFile == null)
215         {
216             File webInf = new File(warFile, "WEB-INF");
217             return new File(webInf, "web.xml");
218         }
219 
220         return webXmlFile;
221     }
222 
223     public void setWebXmlFile(File webXmlFile)
224     {
225         this.webXmlFile = webXmlFile;
226     }
227 
228     public void addScanTargets(FileSet scanTargets)
229     {
230         if (this.scanTargets != null)
231         {
232             throw new BuildException("Only one <scanTargets> tag is allowed!");
233         }
234 
235         this.scanTargets = scanTargets;
236     }
237 
238     public void addContextHandlers(ContextHandlers contextHandlers)
239     {
240         if (this.contextHandlers != null)
241         {
242             throw new BuildException("Only one <contextHandlers> tag is allowed!");
243         }
244 
245         this.contextHandlers = contextHandlers;
246     }
247 
248     public int getScanIntervalSeconds()
249     {
250         return scanIntervalSeconds;
251     }
252 
253     public void setScanIntervalSeconds(int scanIntervalSeconds)
254     {
255         this.scanIntervalSeconds = scanIntervalSeconds;
256     }
257 
258     public File getJettyEnvXml()
259     {
260         return jettyEnvXml;
261     }
262 
263     public void setJettyEnvXml(File jettyEnvXml)
264     {
265         this.jettyEnvXml = jettyEnvXml;
266     }
267 
268     public List getContextHandlers()
269     {
270         return (contextHandlers != null ? contextHandlers.getContextHandlers() : new ArrayList());
271     }
272 }