View Javadoc

1   //========================================================================
2   //$Id: Jetty6MavenConfiguration.java 6184 2010-09-27 01:23:08Z 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          if (Log.isDebugEnabled()) {
89              Log.debug("Server classes:");
90              for (int i=0;i<newServerClasses.length;i++)
91                  Log.debug(newServerClasses[i]);
92          }
93  
94          getWebAppContext().setServerClasses( newServerClasses );
95      }
96  
97      
98  
99      
100     protected URL findWebXml () throws IOException, MalformedURLException
101     {
102         //if an explicit web.xml file has been set (eg for jetty:run) then use it
103         if (webXmlFile != null && webXmlFile.exists())
104             return webXmlFile.toURL();
105         
106         //if we haven't overridden location of web.xml file, use the
107         //standard way of finding it
108         Log.debug("Looking for web.xml file in WEB-INF");
109         return super.findWebXml();
110     }
111     
112     
113     
114     public void parseAnnotations()
115     throws Exception
116     {
117         String v = System.getProperty("java.version");
118         String[] version = v.split("\\.");
119         if (version==null)
120         {
121             Log.info("Unable to determine jvm version, annotations will not be supported");
122             return;
123         }
124         int  major = Integer.parseInt(version[0]);
125         int minor = Integer.parseInt(version[1]);
126         if ((major >= 1) && (minor >= 5))
127         {     
128             //TODO it would be nice to be able to re-use the parseAnnotations() method on 
129             //the org.mortbay.jetty.annotations.Configuration class, but it's too difficult?
130             
131             //able to use annotations on on jdk1.5 and above
132             Class annotationParserClass = Thread.currentThread().getContextClassLoader().loadClass("org.mortbay.jetty.annotations.AnnotationParser");
133             Method parseAnnotationsMethod = 
134                 annotationParserClass.getMethod("parseAnnotations", new Class[] {WebAppContext.class, Class.class, RunAsCollection.class, InjectionCollection.class, LifeCycleCallbackCollection.class });
135 
136             //look thru _servlets
137             Iterator itor = LazyList.iterator(_servlets);
138             while (itor.hasNext())
139             {
140                 ServletHolder holder = (ServletHolder)itor.next();
141                 Class servlet = getWebAppContext().loadClass(holder.getClassName());
142                 parseAnnotationsMethod.invoke(null, new Object[] {getWebAppContext(), servlet, _runAsCollection,  _injections, _callbacks});
143             }
144 
145             //look thru _filters
146             itor = LazyList.iterator(_filters);
147             while (itor.hasNext())
148             {
149                 FilterHolder holder = (FilterHolder)itor.next();
150                 Class filter = getWebAppContext().loadClass(holder.getClassName());
151                 parseAnnotationsMethod.invoke(null, new Object[] {getWebAppContext(), filter, null, _injections, _callbacks});
152             }
153 
154             //look thru _listeners
155             itor = LazyList.iterator(_listeners);
156             while (itor.hasNext())
157             {
158                 Object listener = itor.next();
159                 parseAnnotationsMethod.invoke(null, new Object[] {getWebAppContext(), listener.getClass(), null, _injections, _callbacks});
160             }
161         }
162         else
163             Log.info("Annotations are not supported on jvms prior to jdk1.5");
164     }
165 }