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