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.ArrayList;
22 import java.util.List;
23
24 import org.eclipse.jetty.annotations.AbstractDiscoverableAnnotationHandler;
25 import org.eclipse.jetty.annotations.AnnotationParser.DiscoverableAnnotationHandler;
26 import org.eclipse.jetty.annotations.ClassNameResolver;
27 import org.eclipse.jetty.osgi.boot.OSGiWebappConstants;
28 import org.eclipse.jetty.osgi.boot.utils.internal.PackageAdminServiceTracker;
29 import org.eclipse.jetty.util.resource.Resource;
30 import org.eclipse.jetty.webapp.DiscoveredAnnotation;
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
43
44
45
46 @Override
47 protected org.eclipse.jetty.annotations.AnnotationParser createAnnotationParser()
48 {
49 return new AnnotationParser();
50 }
51
52
53
54
55
56
57
58
59
60
61
62 @Override
63 public void parseWebInfLib (WebAppContext context, org.eclipse.jetty.annotations.AnnotationParser parser)
64 throws Exception
65 {
66 AnnotationParser oparser = (AnnotationParser)parser;
67
68 Bundle webbundle = (Bundle) context.getAttribute(OSGiWebappConstants.JETTY_OSGI_BUNDLE);
69 Bundle[] fragAndRequiredBundles = PackageAdminServiceTracker.INSTANCE.getFragmentsAndRequiredBundles(webbundle);
70 if (fragAndRequiredBundles != null)
71 {
72
73 for (Bundle bundle : fragAndRequiredBundles)
74 {
75 Resource bundleRes = oparser.indexBundle(bundle);
76 if (!context.getMetaData().getWebInfJars().contains(bundleRes))
77 {
78 context.getMetaData().addWebInfJar(bundleRes);
79 }
80 }
81
82
83 for (Bundle fragmentBundle : fragAndRequiredBundles)
84 {
85 if (fragmentBundle.getHeaders().get(Constants.FRAGMENT_HOST) != null)
86 {
87
88 parseFragmentBundle(context,oparser,webbundle,fragmentBundle);
89 }
90 }
91 }
92
93 parseWebBundle(context,oparser,webbundle);
94
95
96 super.parseWebInfLib(context,parser);
97 if (fragAndRequiredBundles != null)
98 {
99
100 for (Bundle requiredBundle : fragAndRequiredBundles)
101 {
102 if (requiredBundle.getHeaders().get(Constants.FRAGMENT_HOST) == null)
103 {
104
105 parseRequiredBundle(context,oparser,webbundle,requiredBundle);
106 }
107 }
108 }
109 }
110
111
112
113
114
115
116
117
118
119 protected void parseFragmentBundle(WebAppContext context, AnnotationParser parser,
120 Bundle webbundle, Bundle fragmentBundle) throws Exception
121 {
122 parseBundle(context,parser,webbundle,fragmentBundle);
123 }
124
125
126
127
128
129
130
131
132
133 protected void parseWebBundle(WebAppContext context, AnnotationParser parser, Bundle webbundle)
134 throws Exception
135 {
136 parseBundle(context,parser,webbundle,webbundle);
137 }
138
139
140
141
142
143
144
145
146
147 protected void parseRequiredBundle(WebAppContext context, AnnotationParser parser,
148 Bundle webbundle, Bundle requiredBundle) throws Exception
149 {
150 parseBundle(context,parser,webbundle,requiredBundle);
151 }
152
153 protected void parseBundle(WebAppContext context, AnnotationParser parser,
154 Bundle webbundle, Bundle bundle) throws Exception
155 {
156
157 Resource bundleRes = parser.getResource(bundle);
158
159 parser.clearHandlers();
160 for (DiscoverableAnnotationHandler h:_discoverableAnnotationHandlers)
161 {
162 if (h instanceof AbstractDiscoverableAnnotationHandler)
163 {
164 if (webbundle == bundle)
165 ((AbstractDiscoverableAnnotationHandler)h).setResource(null);
166 else
167 ((AbstractDiscoverableAnnotationHandler)h).setResource(bundleRes);
168 }
169 }
170 parser.registerHandlers(_discoverableAnnotationHandlers);
171 parser.registerHandler(_classInheritanceHandler);
172 parser.registerHandlers(_containerInitializerAnnotationHandlers);
173
174 parser.parse(bundle,createClassNameResolver(context));
175 }
176
177
178
179
180
181
182 protected ClassNameResolver createClassNameResolver(final WebAppContext context)
183 {
184 return createClassNameResolver(context,true,false,false,false);
185 }
186
187 protected ClassNameResolver createClassNameResolver(final WebAppContext context,
188 final boolean excludeSysClass, final boolean excludeServerClass, final boolean excludeEverythingElse,
189 final boolean overrideIsParenLoaderIsPriority)
190 {
191 return new ClassNameResolver ()
192 {
193 public boolean isExcluded (String name)
194 {
195 if (context.isSystemClass(name)) return excludeSysClass;
196 if (context.isServerClass(name)) return excludeServerClass;
197 return excludeEverythingElse;
198 }
199
200 public boolean shouldOverride (String name)
201 {
202
203 if (context.isParentLoaderPriority())
204 return overrideIsParenLoaderIsPriority;
205 return !overrideIsParenLoaderIsPriority;
206 }
207 };
208 }
209
210 }