1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.osgi.annotations;
20
21 import java.util.HashSet;
22 import java.util.Set;
23
24 import org.eclipse.jetty.annotations.AnnotationParser.Handler;
25 import org.eclipse.jetty.annotations.ClassNameResolver;
26 import org.eclipse.jetty.osgi.boot.OSGiWebInfConfiguration;
27 import org.eclipse.jetty.osgi.boot.OSGiWebappConstants;
28 import org.eclipse.jetty.util.log.Log;
29 import org.eclipse.jetty.util.log.Logger;
30 import org.eclipse.jetty.util.resource.Resource;
31 import org.eclipse.jetty.webapp.WebAppContext;
32 import org.osgi.framework.Bundle;
33 import org.osgi.framework.Constants;
34
35
36
37
38
39
40 public class AnnotationConfiguration extends org.eclipse.jetty.annotations.AnnotationConfiguration
41 {
42 private static final Logger LOG = Log.getLogger(org.eclipse.jetty.annotations.AnnotationConfiguration.class);
43
44 public class BundleParserTask extends ParserTask
45 {
46
47 public BundleParserTask (AnnotationParser parser, Set<? extends Handler>handlers, Resource resource, ClassNameResolver resolver)
48 {
49 super(parser, handlers, resource, resolver);
50 }
51
52 public Void call() throws Exception
53 {
54 if (_parser != null)
55 {
56 org.eclipse.jetty.osgi.annotations.AnnotationParser osgiAnnotationParser = (org.eclipse.jetty.osgi.annotations.AnnotationParser)_parser;
57 Bundle bundle = osgiAnnotationParser.getBundle(_resource);
58 if (_stat != null)
59 _stat.start();
60 osgiAnnotationParser.parse(_handlers, bundle, _resolver);
61 if (_stat != null)
62 _stat.end();
63 }
64 return null;
65 }
66 }
67
68
69 public AnnotationConfiguration()
70 {
71 }
72
73
74
75
76 @Override
77 protected org.eclipse.jetty.annotations.AnnotationParser createAnnotationParser()
78 {
79 return new AnnotationParser();
80 }
81
82
83
84
85
86
87
88
89
90
91
92 @Override
93 public void parseWebInfLib (WebAppContext context, org.eclipse.jetty.annotations.AnnotationParser parser)
94 throws Exception
95 {
96 AnnotationParser oparser = (AnnotationParser)parser;
97
98 Bundle webbundle = (Bundle) context.getAttribute(OSGiWebappConstants.JETTY_OSGI_BUNDLE);
99 Set<Bundle> fragAndRequiredBundles = (Set<Bundle>)context.getAttribute(OSGiWebInfConfiguration.FRAGMENT_AND_REQUIRED_BUNDLES);
100 if (fragAndRequiredBundles != null)
101 {
102
103 for (Bundle bundle : fragAndRequiredBundles)
104 {
105
106 if (bundle.getState() == Bundle.UNINSTALLED)
107 continue;
108
109 Resource bundleRes = oparser.indexBundle(bundle);
110 if (!context.getMetaData().getWebInfJars().contains(bundleRes))
111 {
112 context.getMetaData().addWebInfJar(bundleRes);
113 }
114
115 if (bundle.getHeaders().get(Constants.FRAGMENT_HOST) != null)
116 {
117
118 parseFragmentBundle(context,oparser,webbundle,bundle);
119 }
120 }
121 }
122
123 oparser.indexBundle(webbundle);
124 parseWebBundle(context,oparser,webbundle);
125
126
127 super.parseWebInfLib(context,parser);
128 if (fragAndRequiredBundles != null)
129 {
130
131 for (Bundle requiredBundle : fragAndRequiredBundles)
132 {
133
134 if (requiredBundle.getState() == Bundle.UNINSTALLED)
135 continue;
136
137 if (requiredBundle.getHeaders().get(Constants.FRAGMENT_HOST) == null)
138 {
139
140 parseRequiredBundle(context,oparser,webbundle,requiredBundle);
141 }
142 }
143 }
144 }
145
146
147
148
149
150
151
152
153
154 protected void parseFragmentBundle(WebAppContext context, AnnotationParser parser,
155 Bundle webbundle, Bundle fragmentBundle) throws Exception
156 {
157 parseBundle(context,parser,webbundle,fragmentBundle);
158 }
159
160
161
162
163
164
165
166
167 protected void parseWebBundle(WebAppContext context, AnnotationParser parser, Bundle webbundle)
168 throws Exception
169 {
170 parseBundle(context,parser,webbundle,webbundle);
171 }
172
173
174
175
176
177
178
179
180
181 protected void parseRequiredBundle(WebAppContext context, AnnotationParser parser,
182 Bundle webbundle, Bundle requiredBundle) throws Exception
183 {
184 parseBundle(context,parser,webbundle,requiredBundle);
185 }
186
187 protected void parseBundle(WebAppContext context, AnnotationParser parser,
188 Bundle webbundle, Bundle bundle) throws Exception
189 {
190
191 Resource bundleRes = parser.getResource(bundle);
192 Set<Handler> handlers = new HashSet<Handler>();
193 handlers.addAll(_discoverableAnnotationHandlers);
194 if (_classInheritanceHandler != null)
195 handlers.add(_classInheritanceHandler);
196 handlers.addAll(_containerInitializerAnnotationHandlers);
197
198 ClassNameResolver classNameResolver = createClassNameResolver(context);
199 if (_parserTasks != null)
200 {
201 BundleParserTask task = new BundleParserTask(parser, handlers, bundleRes, classNameResolver);
202 _parserTasks.add(task);
203 if (LOG.isDebugEnabled())
204 task.setStatistic(new TimeStatistic());
205 }
206 }
207
208
209
210
211
212
213 protected ClassNameResolver createClassNameResolver(final WebAppContext context)
214 {
215 return createClassNameResolver(context,true,false,false,false);
216 }
217
218 protected ClassNameResolver createClassNameResolver(final WebAppContext context,
219 final boolean excludeSysClass, final boolean excludeServerClass, final boolean excludeEverythingElse,
220 final boolean overrideIsParenLoaderIsPriority)
221 {
222 return new ClassNameResolver ()
223 {
224 public boolean isExcluded (String name)
225 {
226 if (context.isSystemClass(name)) return excludeSysClass;
227 if (context.isServerClass(name)) return excludeServerClass;
228 return excludeEverythingElse;
229 }
230
231 public boolean shouldOverride (String name)
232 {
233
234 if (context.isParentLoaderPriority())
235 return overrideIsParenLoaderIsPriority;
236 return !overrideIsParenLoaderIsPriority;
237 }
238 };
239 }
240
241 }