Skip to content

Commit 8394857

Browse files
committed
437419 Allow scanning of META-INF for resources,fragments,tlds for unpacked jars
1 parent 0751256 commit 8394857

File tree

4 files changed

+84
-156
lines changed

4 files changed

+84
-156
lines changed

jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/annotations/AnnotationConfiguration.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import org.eclipse.jetty.annotations.AnnotationParser.Handler;
2525
import org.eclipse.jetty.annotations.ClassNameResolver;
26+
import org.eclipse.jetty.osgi.boot.OSGiWebInfConfiguration;
2627
import org.eclipse.jetty.osgi.boot.OSGiWebappConstants;
2728
import org.eclipse.jetty.osgi.boot.utils.internal.PackageAdminServiceTracker;
2829
import org.eclipse.jetty.util.log.Log;
@@ -92,7 +93,7 @@ public void parseWebInfLib (WebAppContext context, org.eclipse.jetty.annotations
9293
AnnotationParser oparser = (AnnotationParser)parser;
9394

9495
Bundle webbundle = (Bundle) context.getAttribute(OSGiWebappConstants.JETTY_OSGI_BUNDLE);
95-
Bundle[] fragAndRequiredBundles = PackageAdminServiceTracker.INSTANCE.getFragmentsAndRequiredBundles(webbundle);
96+
Set<Bundle> fragAndRequiredBundles = (Set<Bundle>)context.getAttribute(OSGiWebInfConfiguration.FRAGMENT_AND_REQUIRED_BUNDLES);
9697
if (fragAndRequiredBundles != null)
9798
{
9899
//index and scan fragments

jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/OSGiMetaInfConfiguration.java

Lines changed: 0 additions & 129 deletions
This file was deleted.

jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/OSGiWebInfConfiguration.java

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.ArrayList;
2424
import java.util.HashSet;
2525
import java.util.List;
26+
import java.util.Set;
2627
import java.util.StringTokenizer;
2728
import java.util.TreeMap;
2829
import java.util.regex.Pattern;
@@ -51,6 +52,9 @@ public class OSGiWebInfConfiguration extends WebInfConfiguration
5152

5253

5354
public static final String CONTAINER_BUNDLE_PATTERN = "org.eclipse.jetty.server.webapp.containerIncludeBundlePattern";
55+
public static final String FRAGMENT_AND_REQUIRED_BUNDLES = "org.eclipse.jetty.osgi.fragmentAndRequiredBundles";
56+
public static final String FRAGMENT_AND_REQUIRED_RESOURCES = "org.eclipse.jetty.osgi.fragmentAndRequiredResources";
57+
5458

5559
/* ------------------------------------------------------------ */
5660
/**
@@ -87,7 +91,6 @@ public void preConfigure(final WebAppContext context) throws Exception
8791
while (tokenizer.hasMoreTokens())
8892
names.add(tokenizer.nextToken());
8993
}
90-
9194
HashSet<Resource> matchingResources = new HashSet<Resource>();
9295
if ( !names.isEmpty() || pattern != null)
9396
{
@@ -111,14 +114,20 @@ public void preConfigure(final WebAppContext context) throws Exception
111114
matchingResources.addAll(getBundleAsResource(bundle));
112115
}
113116
}
114-
}
115-
117+
}
116118
for (Resource r:matchingResources)
117119
{
118120
context.getMetaData().addContainerResource(r);
119121
}
120122
}
121123

124+
@Override
125+
public void postConfigure(WebAppContext context) throws Exception
126+
{
127+
context.setAttribute(FRAGMENT_AND_REQUIRED_BUNDLES, null);
128+
context.setAttribute(FRAGMENT_AND_REQUIRED_RESOURCES, null);
129+
super.postConfigure(context);
130+
}
122131

123132
/* ------------------------------------------------------------ */
124133
/**
@@ -137,12 +146,34 @@ protected List<Resource> findJars (WebAppContext context)
137146
if (webInfJars != null)
138147
mergedResources.addAll(webInfJars);
139148

140-
//add fragment jars as if in WEB-INF/lib of the associated webapp
141-
Bundle[] fragments = PackageAdminServiceTracker.INSTANCE.getFragmentsAndRequiredBundles((Bundle)context.getAttribute(OSGiWebappConstants.JETTY_OSGI_BUNDLE));
142-
for (Bundle frag : fragments)
149+
//add fragment jars and any Required-Bundles as if in WEB-INF/lib of the associated webapp
150+
Bundle[] bundles = PackageAdminServiceTracker.INSTANCE.getFragmentsAndRequiredBundles((Bundle)context.getAttribute(OSGiWebappConstants.JETTY_OSGI_BUNDLE));
151+
if (bundles != null && bundles.length > 0)
143152
{
144-
File fragFile = BundleFileLocatorHelperFactory.getFactory().getHelper().getBundleInstallLocation(frag);
145-
mergedResources.add(Resource.newResource(fragFile.toURI()));
153+
Set<Bundle> fragsAndReqsBundles = (Set<Bundle>)context.getAttribute(FRAGMENT_AND_REQUIRED_BUNDLES);
154+
if (fragsAndReqsBundles == null)
155+
{
156+
fragsAndReqsBundles = new HashSet<Bundle>();
157+
context.setAttribute(FRAGMENT_AND_REQUIRED_BUNDLES, fragsAndReqsBundles);
158+
}
159+
160+
Set<Resource> fragsAndReqsResources = (Set<Resource>)context.getAttribute(FRAGMENT_AND_REQUIRED_RESOURCES);
161+
if (fragsAndReqsResources == null)
162+
{
163+
fragsAndReqsResources = new HashSet<Resource>();
164+
context.setAttribute(FRAGMENT_AND_REQUIRED_RESOURCES, fragsAndReqsResources);
165+
}
166+
167+
for (Bundle b : bundles)
168+
{
169+
//add to context attribute storing associated fragments and required bundles
170+
fragsAndReqsBundles.add(b);
171+
File f = BundleFileLocatorHelperFactory.getFactory().getHelper().getBundleInstallLocation(b);
172+
Resource r = Resource.newResource(f.toURI());
173+
//add to convenience context attribute storing fragments and required bundles as Resources
174+
fragsAndReqsResources.add(r);
175+
mergedResources.add(r);
176+
}
146177
}
147178

148179
return mergedResources;
@@ -165,9 +196,8 @@ public void configure(WebAppContext context) throws Exception
165196
Bundle bundle = (Bundle)context.getAttribute(OSGiWebappConstants.JETTY_OSGI_BUNDLE);
166197
if (bundle != null)
167198
{
168-
//TODO anything we need to do to improve PackageAdminServiceTracker?
169-
Bundle[] fragments = PackageAdminServiceTracker.INSTANCE.getFragmentsAndRequiredBundles(bundle);
170-
if (fragments != null && fragments.length != 0)
199+
Set<Bundle> fragments = (Set<Bundle>)context.getAttribute(FRAGMENT_AND_REQUIRED_BUNDLES);
200+
if (fragments != null && !fragments.isEmpty())
171201
{
172202
// sorted extra resource base found in the fragments.
173203
// the resources are either overriding the resourcebase found in the

jetty-webapp/src/main/java/org/eclipse/jetty/webapp/MetaInfConfiguration.java

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -131,41 +131,50 @@ public void scanJars (final WebAppContext context, Collection<Resource> jars, bo
131131
* Scan for META-INF/resources dir in the given jar.
132132
*
133133
* @param context
134-
* @param jar
134+
* @param target
135135
* @param cache
136136
* @throws Exception
137137
*/
138-
public void scanForResources (WebAppContext context, Resource jar, ConcurrentHashMap<Resource,Resource> cache)
138+
public void scanForResources (WebAppContext context, Resource target, ConcurrentHashMap<Resource,Resource> cache)
139139
throws Exception
140140
{
141141
Resource resourcesDir = null;
142-
if (cache != null && cache.containsKey(jar))
142+
if (cache != null && cache.containsKey(target))
143143
{
144-
resourcesDir = cache.get(jar);
144+
resourcesDir = cache.get(target);
145145
if (resourcesDir == EmptyResource.INSTANCE)
146146
{
147-
if (LOG.isDebugEnabled()) LOG.debug(jar+" cached as containing no META-INF/resources");
147+
if (LOG.isDebugEnabled()) LOG.debug(target+" cached as containing no META-INF/resources");
148148
return;
149149
}
150150
else
151-
if (LOG.isDebugEnabled()) LOG.debug(jar+" META-INF/resources found in cache ");
151+
if (LOG.isDebugEnabled()) LOG.debug(target+" META-INF/resources found in cache ");
152152
}
153153
else
154154
{
155155
//not using caches or not in the cache so check for the resources dir
156-
if (LOG.isDebugEnabled()) LOG.debug(jar+" META-INF/resources checked");
157-
URI uri = jar.getURI();
158-
resourcesDir = Resource.newResource("jar:"+uri+"!/META-INF/resources");
156+
if (LOG.isDebugEnabled()) LOG.debug(target+" META-INF/resources checked");
157+
if (target.isDirectory())
158+
{
159+
//TODO think how to handle an unpacked jar file (eg for osgi)
160+
resourcesDir = target.addPath("/META-INF/resources");
161+
}
162+
else
163+
{
164+
//Resource represents a packed jar
165+
URI uri = target.getURI();
166+
resourcesDir = Resource.newResource("jar:"+uri+"!/META-INF/resources");
167+
}
159168
if (!resourcesDir.exists() || !resourcesDir.isDirectory())
160169
resourcesDir = EmptyResource.INSTANCE;
161170

162171
if (cache != null)
163172
{
164-
Resource old = cache.putIfAbsent(jar, resourcesDir);
173+
Resource old = cache.putIfAbsent(target, resourcesDir);
165174
if (old != null)
166175
resourcesDir = old;
167176
else
168-
if (LOG.isDebugEnabled()) LOG.debug(jar+" META-INF/resources cache updated");
177+
if (LOG.isDebugEnabled()) LOG.debug(target+" META-INF/resources cache updated");
169178
}
170179

171180
if (resourcesDir == EmptyResource.INSTANCE)
@@ -210,8 +219,16 @@ public void scanForFragment (WebAppContext context, Resource jar, ConcurrentHash
210219
{
211220
//not using caches or not in the cache so check for the web-fragment.xml
212221
if (LOG.isDebugEnabled()) LOG.debug(jar+" META-INF/web-fragment.xml checked");
213-
URI uri = jar.getURI();
214-
webFrag = Resource.newResource("jar:"+uri+"!/META-INF/web-fragment.xml");
222+
if (jar.isDirectory())
223+
{
224+
//TODO ????
225+
webFrag = jar.addPath("/META-INF/web-fragment.xml");
226+
}
227+
else
228+
{
229+
URI uri = jar.getURI();
230+
webFrag = Resource.newResource("jar:"+uri+"!/META-INF/web-fragment.xml");
231+
}
215232
if (!webFrag.exists() || webFrag.isDirectory())
216233
webFrag = EmptyResource.INSTANCE;
217234

@@ -270,8 +287,17 @@ public void scanForTlds (WebAppContext context, Resource jar, ConcurrentHashMap<
270287
else
271288
{
272289
//not using caches or not in the cache so find all tlds
273-
URI uri = jar.getURI();
274-
Resource metaInfDir = Resource.newResource("jar:"+uri+"!/META-INF/");
290+
Resource metaInfDir = null;
291+
if (jar.isDirectory())
292+
{
293+
//TODO ??????
294+
metaInfDir = jar.addPath("/META-INF/");
295+
}
296+
else
297+
{
298+
URI uri = jar.getURI();
299+
metaInfDir = Resource.newResource("jar:"+uri+"!/META-INF/");
300+
}
275301

276302
//find any *.tld files inside META-INF or subdirs
277303
tlds = new HashSet<URL>();

0 commit comments

Comments
 (0)