1
2
3
4
5
6
7
8
9
10
11
12
13
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
60
61
62 public void configureClassLoader() throws Exception
63 {
64 if (classPathFiles != null)
65 {
66 Log.debug("Setting up classpath ...");
67
68
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
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
103 if (webXmlFile != null && webXmlFile.exists())
104 return webXmlFile.toURL();
105
106
107
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
129
130
131
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
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
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
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 }