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.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
59
60
61 public void configureClassLoader() throws Exception
62 {
63 if (classPathFiles != null)
64 {
65 Log.debug("Setting up classpath ...");
66
67
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
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
96 if (webXmlFile != null && webXmlFile.exists())
97 return webXmlFile.toURL();
98
99
100
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
122
123
124
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
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
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
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 }