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