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.OSGiWebappConstants;
27 import org.eclipse.jetty.osgi.boot.utils.internal.PackageAdminServiceTracker;
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
70
71
72 @Override
73 protected org.eclipse.jetty.annotations.AnnotationParser createAnnotationParser()
74 {
75 return new AnnotationParser();
76 }
77
78
79
80
81
82
83
84
85
86
87
88 @Override
89 public void parseWebInfLib (WebAppContext context, org.eclipse.jetty.annotations.AnnotationParser parser)
90 throws Exception
91 {
92 AnnotationParser oparser = (AnnotationParser)parser;
93
94 Bundle webbundle = (Bundle) context.getAttribute(OSGiWebappConstants.JETTY_OSGI_BUNDLE);
95 Bundle[] fragAndRequiredBundles = PackageAdminServiceTracker.INSTANCE.getFragmentsAndRequiredBundles(webbundle);
96 if (fragAndRequiredBundles != null)
97 {
98
99 for (Bundle bundle : fragAndRequiredBundles)
100 {
101 Resource bundleRes = oparser.indexBundle(bundle);
102 if (!context.getMetaData().getWebInfJars().contains(bundleRes))
103 {
104 context.getMetaData().addWebInfJar(bundleRes);
105 }
106
107 if (bundle.getHeaders().get(Constants.FRAGMENT_HOST) != null)
108 {
109
110 parseFragmentBundle(context,oparser,webbundle,bundle);
111 }
112 }
113 }
114
115 oparser.indexBundle(webbundle);
116 parseWebBundle(context,oparser,webbundle);
117
118
119 super.parseWebInfLib(context,parser);
120 if (fragAndRequiredBundles != null)
121 {
122
123 for (Bundle requiredBundle : fragAndRequiredBundles)
124 {
125 if (requiredBundle.getHeaders().get(Constants.FRAGMENT_HOST) == null)
126 {
127
128 parseRequiredBundle(context,oparser,webbundle,requiredBundle);
129 }
130 }
131 }
132 }
133
134
135
136
137
138
139
140
141
142 protected void parseFragmentBundle(WebAppContext context, AnnotationParser parser,
143 Bundle webbundle, Bundle fragmentBundle) throws Exception
144 {
145 parseBundle(context,parser,webbundle,fragmentBundle);
146 }
147
148
149
150
151
152
153
154
155
156 protected void parseWebBundle(WebAppContext context, AnnotationParser parser, Bundle webbundle)
157 throws Exception
158 {
159 parseBundle(context,parser,webbundle,webbundle);
160 }
161
162
163
164
165
166
167
168
169
170 protected void parseRequiredBundle(WebAppContext context, AnnotationParser parser,
171 Bundle webbundle, Bundle requiredBundle) throws Exception
172 {
173 parseBundle(context,parser,webbundle,requiredBundle);
174 }
175
176 protected void parseBundle(WebAppContext context, AnnotationParser parser,
177 Bundle webbundle, Bundle bundle) throws Exception
178 {
179
180 Resource bundleRes = parser.getResource(bundle);
181 Set<Handler> handlers = new HashSet<Handler>();
182 handlers.addAll(_discoverableAnnotationHandlers);
183 if (_classInheritanceHandler != null)
184 handlers.add(_classInheritanceHandler);
185 handlers.addAll(_containerInitializerAnnotationHandlers);
186
187 ClassNameResolver classNameResolver = createClassNameResolver(context);
188 if (_parserTasks != null)
189 {
190 BundleParserTask task = new BundleParserTask(parser, handlers, bundleRes, classNameResolver);
191 _parserTasks.add(task);
192 if (LOG.isDebugEnabled())
193 task.setStatistic(new TimeStatistic());
194 }
195 }
196
197
198
199
200
201
202 protected ClassNameResolver createClassNameResolver(final WebAppContext context)
203 {
204 return createClassNameResolver(context,true,false,false,false);
205 }
206
207 protected ClassNameResolver createClassNameResolver(final WebAppContext context,
208 final boolean excludeSysClass, final boolean excludeServerClass, final boolean excludeEverythingElse,
209 final boolean overrideIsParenLoaderIsPriority)
210 {
211 return new ClassNameResolver ()
212 {
213 public boolean isExcluded (String name)
214 {
215 if (context.isSystemClass(name)) return excludeSysClass;
216 if (context.isServerClass(name)) return excludeServerClass;
217 return excludeEverythingElse;
218 }
219
220 public boolean shouldOverride (String name)
221 {
222
223 if (context.isParentLoaderPriority())
224 return overrideIsParenLoaderIsPriority;
225 return !overrideIsParenLoaderIsPriority;
226 }
227 };
228 }
229
230 }