1   //========================================================================
2   //$Id: Jetty6MavenConfiguration.java 2419 2008-03-07 01:06:43Z 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.WebAppClassLoader;
34  import org.mortbay.log.Log;
35  import org.mortbay.util.LazyList;
36  
37  public class Jetty6MavenConfiguration extends Configuration 
38  {
39      private List classPathFiles;
40      private File webXmlFile;
41     
42      public Jetty6MavenConfiguration() throws ClassNotFoundException
43      {
44          super();
45      }
46  
47      public void setClassPathConfiguration(List classPathFiles)
48      {
49          this.classPathFiles = classPathFiles;
50      }
51      
52      public void setWebXml (File webXmlFile)
53      {
54          this.webXmlFile = webXmlFile;
55      }
56      
57      
58      /** Set up the classloader for the webapp, using the various parts of the Maven project
59       * @see org.mortbay.jetty.webapp.Configuration#configureClassLoader()
60       */
61      public void configureClassLoader() throws Exception 
62      {
63          if (classPathFiles != null)
64          {
65              Log.debug("Setting up classpath ...");
66  
67              //put the classes dir and all dependencies into the classpath
68              Iterator itor = classPathFiles.iterator();
69              while (itor.hasNext())
70                  ((WebAppClassLoader)getWebAppContext().getClassLoader()).addClassPath(((File)itor.next()).getCanonicalPath());
71  
72              if (Log.isDebugEnabled())
73                  Log.debug("Classpath = "+LazyList.array2List(((URLClassLoader)getWebAppContext().getClassLoader()).getURLs()));
74          }
75          else
76          {
77              super.configureClassLoader();
78          }
79  
80          // knock out environmental maven and plexus classes from webAppContext
81          String[] existingServerClasses = getWebAppContext().getServerClasses();
82          String[] newServerClasses = new String[2+(existingServerClasses==null?0:existingServerClasses.length)];
83          newServerClasses[0] = "-org.apache.maven.";
84          newServerClasses[1] = "-org.codehaus.plexus.";
85          System.arraycopy( existingServerClasses, 0, newServerClasses, 2, existingServerClasses.length );
86          
87          getWebAppContext().setServerClasses( newServerClasses );
88      }
89  
90      
91  
92      
93      protected URL findWebXml () throws IOException, MalformedURLException
94      {
95          //if an explicit web.xml file has been set (eg for jetty:run) then use it
96          if (webXmlFile != null && webXmlFile.exists())
97              return webXmlFile.toURL();
98          
99          //if we haven't overridden location of web.xml file, use the
100         //standard way of finding it
101         Log.debug("Looking for web.xml file in WEB-INF");
102         return super.findWebXml();
103     }
104     
105     
106     
107     public void parseAnnotations()
108     throws Exception
109     {
110         String v = System.getProperty("java.version");
111         String[] version = v.split("\\.");
112         if (version==null)
113         {
114             Log.info("Unable to determine jvm version, annotations will not be supported");
115             return;
116         }
117         int  major = Integer.parseInt(version[0]);
118         int minor = Integer.parseInt(version[1]);
119         if ((major >= 1) && (minor >= 5))
120         {     
121             //TODO it would be nice to be able to re-use the parseAnnotations() method on 
122             //the org.mortbay.jetty.annotations.Configuration class, but it's too difficult?
123             
124             //able to use annotations on on jdk1.5 and above
125             Class annotationParserClass = Thread.currentThread().getContextClassLoader().loadClass("org.mortbay.jetty.annotations.AnnotationParser");
126             Method parseAnnotationsMethod = 
127                 annotationParserClass.getMethod("parseAnnotations", new Class[] {Class.class, RunAsCollection.class, InjectionCollection.class, LifeCycleCallbackCollection.class });
128 
129             //look thru _servlets
130             Iterator itor = LazyList.iterator(_servlets);
131             while (itor.hasNext())
132             {
133                 ServletHolder holder = (ServletHolder)itor.next();
134                 Class servlet = getWebAppContext().loadClass(holder.getClassName());
135                 parseAnnotationsMethod.invoke(null, new Object[] {servlet, _runAsCollection,  _injections, _callbacks});
136             }
137 
138             //look thru _filters
139             itor = LazyList.iterator(_filters);
140             while (itor.hasNext())
141             {
142                 FilterHolder holder = (FilterHolder)itor.next();
143                 Class filter = getWebAppContext().loadClass(holder.getClassName());
144                 parseAnnotationsMethod.invoke(null, new Object[] {filter, null, _injections, _callbacks});
145             }
146 
147             //look thru _listeners
148             itor = LazyList.iterator(_listeners);
149             while (itor.hasNext())
150             {
151                 Object listener = itor.next();
152                 parseAnnotationsMethod.invoke(null, new Object[] {listener.getClass(), null, _injections, _callbacks});
153             }
154         }
155         else
156             Log.info("Annotations are not supported on jvms prior to jdk1.5");
157     }
158 }