View Javadoc

    1   //========================================================================
    2   //$Id: Jetty6MavenConfiguration.java 3766 2008-10-08 07:59:53Z janb $
    3   //Copyright 2000-2005 Mort Bay Consulting Pty. Ltd.
    4   //------------------------------------------------------------------------
    5   //Licensed under the Apache License, Version 2.0 (the "License");
    6   //you may not use this file except in compliance with the License.
    7   //You may obtain a copy of the License at
    8   //http://www.apache.org/licenses/LICENSE-2.0
    9   //Unless required by applicable law or agreed to in writing, software
    10  //distributed under the License is distributed on an "AS IS" BASIS,
    11  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  //See the License for the specific language governing permissions and
    13  //limitations under the License.
    14  //========================================================================
    15
    16  package org.mortbay.jetty.plugin;
    17
    18  import java.io.File;
    19  import java.io.IOException;
    20  import java.lang.reflect.Method;
    21  import java.net.MalformedURLException;
    22  import java.net.URL;
    23  import java.net.URLClassLoader;
    24  import java.util.Iterator;
    25  import java.util.List;
    26
    27  import org.mortbay.jetty.plus.annotation.InjectionCollection;
    28  import org.mortbay.jetty.plus.annotation.LifeCycleCallbackCollection;
    29  import org.mortbay.jetty.plus.annotation.RunAsCollection;
    30  import org.mortbay.jetty.plus.webapp.Configuration;
    31  import org.mortbay.jetty.servlet.FilterHolder;
    32  import org.mortbay.jetty.servlet.ServletHolder;
    33  import org.mortbay.jetty.webapp.WebAppContext;
    34  import org.mortbay.jetty.webapp.WebAppClassLoader;
    35  import org.mortbay.log.Log;
    36  import org.mortbay.util.LazyList;
    37
    38  public class Jetty6MavenConfiguration extends Configuration
    39  {
    40      private List classPathFiles;
    41      private File webXmlFile;
    42
    43      public Jetty6MavenConfiguration()
    44      {
    45          super();
    46      }
    47
    48      public void setClassPathConfiguration(List classPathFiles)
    49      {
    50          this.classPathFiles = classPathFiles;
    51      }
    52
    53      public void setWebXml (File webXmlFile)
    54      {
    55          this.webXmlFile = webXmlFile;
    56      }
    57
    58
    59      /** Set up the classloader for the webapp, using the various parts of the Maven project
    60       * @see org.mortbay.jetty.webapp.Configuration#configureClassLoader()
    61       */
    62      public void configureClassLoader() throws Exception
    63      {
    64          if (classPathFiles != null)
    65          {
    66              Log.debug("Setting up classpath ...");
    67
    68              //put the classes dir and all dependencies into the classpath
    69              Iterator itor = classPathFiles.iterator();
    70              while (itor.hasNext())
    71                  ((WebAppClassLoader)getWebAppContext().getClassLoader()).addClassPath(((File)itor.next()).getCanonicalPath());
    72
    73              if (Log.isDebugEnabled())
    74                  Log.debug("Classpath = "+LazyList.array2List(((URLClassLoader)getWebAppContext().getClassLoader()).getURLs()));
    75          }
    76          else
    77          {
    78              super.configureClassLoader();
    79          }
    80
    81          // knock out environmental maven and plexus classes from webAppContext
    82          String[] existingServerClasses = getWebAppContext().getServerClasses();
    83          String[] newServerClasses = new String[2+(existingServerClasses==null?0:existingServerClasses.length)];
    84          newServerClasses[0] = "-org.apache.maven.";
    85          newServerClasses[1] = "-org.codehaus.plexus.";
    86          System.arraycopy( existingServerClasses, 0, newServerClasses, 2, existingServerClasses.length );
    87
    88          getWebAppContext().setServerClasses( newServerClasses );
    89      }
    90
    91
    92
    93
    94      protected URL findWebXml () throws IOException, MalformedURLException
    95      {
    96          //if an explicit web.xml file has been set (eg for jetty:run) then use it
    97          if (webXmlFile != null && webXmlFile.exists())
    98              return webXmlFile.toURL();
    99
    100         //if we haven't overridden location of web.xml file, use the
    101         //standard way of finding it
    102         Log.debug("Looking for web.xml file in WEB-INF");
    103         return super.findWebXml();
    104     }
    105
    106
    107
    108     public void parseAnnotations()
    109     throws Exception
    110     {
    111         String v = System.getProperty("java.version");
    112         String[] version = v.split("\\.");
    113         if (version==null)
    114         {
    115             Log.info("Unable to determine jvm version, annotations will not be supported");
    116             return;
    117         }
    118         int  major = Integer.parseInt(version[0]);
    119         int minor = Integer.parseInt(version[1]);
    120         if ((major >= 1) && (minor >= 5))
    121         {
    122             //TODO it would be nice to be able to re-use the parseAnnotations() method on
    123             //the org.mortbay.jetty.annotations.Configuration class, but it's too difficult?
    124
    125             //able to use annotations on on jdk1.5 and above
    126             Class annotationParserClass = Thread.currentThread().getContextClassLoader().loadClass("org.mortbay.jetty.annotations.AnnotationParser");
    127             Method parseAnnotationsMethod =
    128                 annotationParserClass.getMethod("parseAnnotations", new Class[] {WebAppContext.class, Class.class, RunAsCollection.class, InjectionCollection.class, LifeCycleCallbackCollection.class });
    129
    130             //look thru _servlets
    131             Iterator itor = LazyList.iterator(_servlets);
    132             while (itor.hasNext())
    133             {
    134                 ServletHolder holder = (ServletHolder)itor.next();
    135                 Class servlet = getWebAppContext().loadClass(holder.getClassName());
    136                 parseAnnotationsMethod.invoke(null, new Object[] {getWebAppContext(), servlet, _runAsCollection,  _injections, _callbacks});
    137             }
    138
    139             //look thru _filters
    140             itor = LazyList.iterator(_filters);
    141             while (itor.hasNext())
    142             {
    143                 FilterHolder holder = (FilterHolder)itor.next();
    144                 Class filter = getWebAppContext().loadClass(holder.getClassName());
    145                 parseAnnotationsMethod.invoke(null, new Object[] {getWebAppContext(), filter, null, _injections, _callbacks});
    146             }
    147
    148             //look thru _listeners
    149             itor = LazyList.iterator(_listeners);
    150             while (itor.hasNext())
    151             {
    152                 Object listener = itor.next();
    153                 parseAnnotationsMethod.invoke(null, new Object[] {getWebAppContext(), listener.getClass(), null, _injections, _callbacks});
    154             }
    155         }
    156         else
    157             Log.info("Annotations are not supported on jvms prior to jdk1.5");
    158     }
    159 }