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 getWebAppContext().setServerClasses( newServerClasses );
89 }
90
91
92
93
94 protected URL findWebXml () throws IOException, MalformedURLException
95 {
96
97 if (webXmlFile != null && webXmlFile.exists())
98 return webXmlFile.toURL();
99
100
101
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
123
124
125
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
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
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
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 }