1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.eclipse.jetty.annotations;
15
16 import java.net.URI;
17 import java.net.URL;
18 import java.net.URLClassLoader;
19 import java.util.ArrayList;
20 import java.util.EventListener;
21 import java.util.List;
22
23 import org.eclipse.jetty.plus.servlet.ServletHandler;
24 import org.eclipse.jetty.servlet.FilterHolder;
25 import org.eclipse.jetty.servlet.FilterMapping;
26 import org.eclipse.jetty.servlet.ServletHolder;
27 import org.eclipse.jetty.servlet.ServletMapping;
28 import org.eclipse.jetty.util.LazyList;
29 import org.eclipse.jetty.util.log.Log;
30 import org.eclipse.jetty.util.resource.Resource;
31 import org.eclipse.jetty.webapp.WebAppContext;
32 import org.eclipse.jetty.webapp.WebInfConfiguration;
33
34
35
36
37
38
39 public class AnnotationConfiguration extends org.eclipse.jetty.plus.webapp.Configuration
40 {
41 public static final String JAR_RESOURCES = WebInfConfiguration.JAR_RESOURCES;
42
43
44
45
46
47
48 public void parseAnnotations(final WebAppContext context) throws Exception
49 {
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 AnnotationFinder finder = new AnnotationFinder();
68
69
70 parseContainerPath (context, finder);
71 parseWebInfLib (context, finder);
72 parseWebInfClasses (context, finder);
73
74 AnnotationProcessor processor = new AnnotationProcessor(context, finder);
75 processor.process();
76
77 List servlets = processor.getServlets();
78 List filters = processor.getFilters();
79 List servletMappings = processor.getServletMappings();
80 List filterMappings = processor.getFilterMappings();
81 List listeners = processor.getListeners();
82
83 ServletHandler servletHandler = (ServletHandler)context.getServletHandler();
84 servletHandler.setFilters((FilterHolder[])LazyList.toArray(filters,FilterHolder.class));
85 servletHandler.setFilterMappings((FilterMapping[])LazyList.toArray(filterMappings,FilterMapping.class));
86 servletHandler.setServlets((ServletHolder[])LazyList.toArray(servlets,ServletHolder.class));
87 servletHandler.setServletMappings((ServletMapping[])LazyList.toArray(servletMappings,ServletMapping.class));
88 context.setEventListeners((EventListener[])LazyList.toArray(listeners,EventListener.class));
89 }
90
91 public void parseContainerPath (final WebAppContext context, final AnnotationFinder finder)
92 throws Exception
93 {
94
95 Log.debug("Scanning container jars");
96
97
98
99 ArrayList<URI> containerCandidateUris = findJars (context.getClassLoader().getParent(), true);
100
101
102 ArrayList<URI> containerUris = new ArrayList<URI>();
103 List<Resource> jarResources = (List<Resource>)context.getAttribute(JAR_RESOURCES);
104 for (Resource r : jarResources)
105 {
106 URI uri = r.getURI();
107 if (containerCandidateUris.contains(uri))
108 {
109 containerUris.add(uri);
110 }
111
112 }
113
114 finder.find (containerUris.toArray(new URI[containerUris.size()]),
115 new ClassNameResolver ()
116 {
117 public boolean isExcluded (String name)
118 {
119 if (context.isSystemClass(name)) return false;
120 if (context.isServerClass(name)) return true;
121 return false;
122 }
123
124 public boolean shouldOverride (String name)
125 {
126
127 if (context.isParentLoaderPriority())
128 return true;
129 return false;
130 }
131 });
132 }
133
134
135 public void parseWebInfLib (final WebAppContext context, final AnnotationFinder finder)
136 throws Exception
137 {
138 Log.debug("Scanning WEB-INF/lib jars");
139
140 ArrayList<URI> candidateUris = findJars(context.getClassLoader(), false);
141
142
143 ArrayList<URI> webInfUris = new ArrayList<URI>();
144 List<Resource> jarResources = (List<Resource>)context.getAttribute(JAR_RESOURCES);
145 for (Resource r : jarResources)
146 {
147 URI uri = r.getURI();
148 if (candidateUris.contains(uri))
149 {
150 webInfUris.add(uri);
151 }
152 }
153
154
155 finder.find(webInfUris.toArray(new URI[webInfUris.size()]),
156 new ClassNameResolver()
157 {
158 public boolean isExcluded (String name)
159 {
160 if (context.isSystemClass(name)) return true;
161 if (context.isServerClass(name)) return false;
162 return false;
163 }
164
165 public boolean shouldOverride (String name)
166 {
167
168 if (context.isParentLoaderPriority())
169 return false;
170 return true;
171 }
172 });
173
174 }
175
176 public void parseWebInfClasses (final WebAppContext context, final AnnotationFinder finder)
177 throws Exception
178 {
179 Log.debug("Scanning classes in WEB-INF/classes");
180 finder.find(context.getWebInf().addPath("classes/"),
181 new ClassNameResolver()
182 {
183 public boolean isExcluded (String name)
184 {
185 if (context.isSystemClass(name)) return true;
186 if (context.isServerClass(name)) return false;
187 return false;
188 }
189
190 public boolean shouldOverride (String name)
191 {
192
193 if (context.isParentLoaderPriority())
194 return false;
195 return true;
196 }
197 });
198 }
199
200
201
202 public ArrayList<URI> findJars (ClassLoader loader, boolean visitParent)
203 {
204 ArrayList<URI> uris = new ArrayList<URI>();
205
206 while (loader != null && (loader instanceof URLClassLoader))
207 {
208 URL[] urls = ((URLClassLoader)loader).getURLs();
209 if (urls != null)
210 {
211 for (URL u : urls)
212 {
213 try
214 {
215 uris.add(u.toURI());
216 }
217 catch (Exception e)
218 {
219 Log.warn(e);
220 }
221 }
222 }
223 if (visitParent)
224 loader = loader.getParent();
225 else
226 loader = null;
227 }
228 return uris;
229 }
230 }