1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.osgi.boot;
20
21 import java.net.URL;
22 import java.util.ArrayList;
23 import java.util.Enumeration;
24 import java.util.List;
25
26 import org.eclipse.jetty.osgi.boot.internal.webapp.BundleFileLocatorHelperFactory;
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.MetaInfConfiguration;
32 import org.eclipse.jetty.webapp.WebAppContext;
33 import org.osgi.framework.Bundle;
34
35 public class OSGiMetaInfConfiguration extends MetaInfConfiguration
36 {
37 private static final Logger LOG = Log.getLogger(OSGiMetaInfConfiguration.class);
38
39
40
41
42
43
44
45 @Override
46 public void preConfigure(final WebAppContext context) throws Exception
47 {
48 List<Resource> frags = (List<Resource>) context.getAttribute(METAINF_FRAGMENTS);
49 List<Resource> resfrags = (List<Resource>) context.getAttribute(METAINF_RESOURCES);
50 List<Resource> tldfrags = (List<Resource>) context.getAttribute(METAINF_TLDS);
51
52 Bundle[] fragments = PackageAdminServiceTracker.INSTANCE.getFragmentsAndRequiredBundles((Bundle)context.getAttribute(OSGiWebappConstants.JETTY_OSGI_BUNDLE));
53
54
55 for (Bundle frag : fragments)
56 {
57 URL webFrag = frag.getEntry("/META-INF/web-fragment.xml");
58 Enumeration<URL> resEnum = frag.findEntries("/META-INF/resources", "*", true);
59 Enumeration<URL> tldEnum = frag.findEntries("/META-INF", "*.tld", false);
60 if (webFrag != null || (resEnum != null && resEnum.hasMoreElements()) || (tldEnum != null && tldEnum.hasMoreElements()))
61 {
62 try
63 {
64 if (webFrag != null)
65 {
66 if (frags == null)
67 {
68 frags = new ArrayList<Resource>();
69 context.setAttribute(METAINF_FRAGMENTS, frags);
70 }
71 frags.add(Resource.newResource(BundleFileLocatorHelperFactory.getFactory().getHelper().getBundleInstallLocation(frag).toURI()));
72 }
73 if (resEnum != null && resEnum.hasMoreElements())
74 {
75 URL resourcesEntry = frag.getEntry("/META-INF/resources/");
76 if (resourcesEntry == null)
77 {
78
79
80
81
82 }
83 else
84 {
85 if (resfrags == null)
86 {
87 resfrags = new ArrayList<Resource>();
88 context.setAttribute(METAINF_RESOURCES, resfrags);
89 }
90 resfrags.add(Resource.newResource(BundleFileLocatorHelperFactory.getFactory().getHelper().getLocalURL(resourcesEntry)));
91 }
92 }
93 if (tldEnum != null && tldEnum.hasMoreElements())
94 {
95 if (tldfrags == null)
96 {
97 tldfrags = new ArrayList<Resource>();
98 context.setAttribute(METAINF_TLDS, tldfrags);
99 }
100 while (tldEnum.hasMoreElements())
101 {
102 URL tldUrl = tldEnum.nextElement();
103 tldfrags.add(Resource.newResource(BundleFileLocatorHelperFactory.getFactory().getHelper().getLocalURL(tldUrl)));
104 }
105 }
106 }
107 catch (Exception e)
108 {
109 LOG.warn("Unable to locate the bundle " + frag.getBundleId(), e);
110 }
111 }
112 }
113
114 super.preConfigure(context);
115 }
116 }