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.utils.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
36
37
38
39
40
41
42
43 public class OSGiMetaInfConfiguration extends MetaInfConfiguration
44 {
45 private static final Logger LOG = Log.getLogger(OSGiMetaInfConfiguration.class);
46
47
48
49
50
51
52
53 @Override
54 public void preConfigure(final WebAppContext context) throws Exception
55 {
56 List<Resource> frags = (List<Resource>) context.getAttribute(METAINF_FRAGMENTS);
57 List<Resource> resfrags = (List<Resource>) context.getAttribute(METAINF_RESOURCES);
58 List<Resource> tldfrags = (List<Resource>) context.getAttribute(METAINF_TLDS);
59
60 Bundle[] fragments = PackageAdminServiceTracker.INSTANCE.getFragmentsAndRequiredBundles((Bundle)context.getAttribute(OSGiWebappConstants.JETTY_OSGI_BUNDLE));
61
62
63 for (Bundle frag : fragments)
64 {
65 URL webFrag = frag.getEntry("/META-INF/web-fragment.xml");
66 Enumeration<URL> resEnum = frag.findEntries("/META-INF/resources", "*", true);
67 Enumeration<URL> tldEnum = frag.findEntries("/META-INF", "*.tld", false);
68 if (webFrag != null || (resEnum != null && resEnum.hasMoreElements()) || (tldEnum != null && tldEnum.hasMoreElements()))
69 {
70 try
71 {
72 if (webFrag != null)
73 {
74 if (frags == null)
75 {
76 frags = new ArrayList<Resource>();
77 context.setAttribute(METAINF_FRAGMENTS, frags);
78 }
79 frags.add(Resource.newResource(BundleFileLocatorHelperFactory.getFactory().getHelper().getBundleInstallLocation(frag).toURI()));
80 }
81 if (resEnum != null && resEnum.hasMoreElements())
82 {
83 URL resourcesEntry = frag.getEntry("/META-INF/resources/");
84 if (resourcesEntry == null)
85 {
86
87
88
89
90 }
91 else
92 {
93 if (resfrags == null)
94 {
95 resfrags = new ArrayList<Resource>();
96 context.setAttribute(METAINF_RESOURCES, resfrags);
97 }
98 resfrags.add(Resource.newResource(BundleFileLocatorHelperFactory.getFactory().getHelper().getLocalURL(resourcesEntry)));
99 }
100 }
101 if (tldEnum != null && tldEnum.hasMoreElements())
102 {
103 if (tldfrags == null)
104 {
105 tldfrags = new ArrayList<Resource>();
106 context.setAttribute(METAINF_TLDS, tldfrags);
107 }
108 while (tldEnum.hasMoreElements())
109 {
110 URL tldUrl = tldEnum.nextElement();
111 tldfrags.add(Resource.newResource(BundleFileLocatorHelperFactory.getFactory().getHelper().getLocalURL(tldUrl)));
112 }
113 }
114 }
115 catch (Exception e)
116 {
117 LOG.warn("Unable to locate the bundle " + frag.getBundleId(), e);
118 }
119 }
120 }
121
122 super.preConfigure(context);
123 }
124 }