View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2012 Sabre Holdings.
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.ant;
20  
21  import java.io.File;
22  import java.util.Iterator;
23  import java.util.List;
24  
25  import org.eclipse.jetty.ant.utils.TaskLog;
26  import org.eclipse.jetty.util.log.Log;
27  import org.eclipse.jetty.util.log.Logger;
28  import org.eclipse.jetty.webapp.WebAppClassLoader;
29  import org.eclipse.jetty.webapp.WebAppContext;
30  import org.eclipse.jetty.webapp.WebXmlConfiguration;
31  
32  
33  /**
34   * This configuration object provides additional way to inject application
35   * properties into the configured web application. The list of classpath files,
36   * the application base directory and web.xml file could be specified in this
37   * way.
38   */
39  public class AntWebXmlConfiguration extends WebXmlConfiguration
40  {
41      private static final Logger LOG = Log.getLogger(WebXmlConfiguration.class);
42  
43  
44  
45      /** List of classpath files. */
46      private List classPathFiles;
47  
48      /** Web application root directory. */
49      private File webAppBaseDir;
50  
51      /** Web application web.xml file. */
52      private File webXmlFile;
53  
54      private File webDefaultXmlFile;
55  
56      public AntWebXmlConfiguration() throws ClassNotFoundException
57      {
58      }
59  
60      public File getWebDefaultXmlFile()
61      {
62          return this.webDefaultXmlFile;
63      }
64  
65      public void setWebDefaultXmlFile(File webDefaultXmlfile)
66      {
67          this.webDefaultXmlFile = webDefaultXmlfile;
68      }
69  
70      public void setClassPathFiles(List classPathFiles)
71      {
72          this.classPathFiles = classPathFiles;
73      }
74  
75      public void setWebAppBaseDir(File webAppBaseDir)
76      {
77          this.webAppBaseDir = webAppBaseDir;
78      }
79  
80      public void setWebXmlFile(File webXmlFile)
81      {
82          this.webXmlFile = webXmlFile;
83  
84          if (webXmlFile.exists())
85          {
86              TaskLog.log("web.xml file = " + webXmlFile);
87          }
88      }
89  
90      /**
91       * Adds classpath files into web application classloader, and
92       * sets web.xml and base directory for the configured web application.
93       *
94       * @see WebXmlConfiguration#configure(WebAppContext)
95       */
96      public void configure(WebAppContext context) throws Exception
97      {
98          if (context.isStarted())
99          {
100             TaskLog.log("Cannot configure webapp after it is started");
101             return;
102         }
103 
104 
105         if (webXmlFile.exists())
106         {
107             context.setDescriptor(webXmlFile.getCanonicalPath());
108         }
109 
110         super.configure(context);
111 
112         Iterator filesIterator = classPathFiles.iterator();
113 
114         while (filesIterator.hasNext())
115         {
116             File classPathFile = (File) filesIterator.next();
117             if (classPathFile.exists())
118             {
119                 ((WebAppClassLoader) context.getClassLoader())
120                         .addClassPath(classPathFile.getCanonicalPath());
121             }
122         }
123     }
124 }