1
2
3
4
5
6
7
8
9
10
11
12
13 package org.eclipse.jetty.osgi.boot.jsp;
14
15 import java.io.IOException;
16 import java.net.URL;
17 import java.util.Collection;
18 import java.util.Enumeration;
19 import java.util.LinkedHashSet;
20
21 import org.eclipse.jetty.osgi.boot.OSGiWebappConstants;
22 import org.eclipse.jetty.osgi.boot.utils.internal.DefaultFileLocatorHelper;
23 import org.eclipse.jetty.util.log.Log;
24 import org.eclipse.jetty.util.resource.Resource;
25 import org.eclipse.jetty.webapp.TagLibConfiguration;
26 import org.eclipse.jetty.webapp.WebAppContext;
27 import org.osgi.framework.Bundle;
28 import org.osgi.framework.BundleReference;
29 import org.osgi.service.packageadmin.PackageAdmin;
30 import org.osgi.util.tracker.ServiceTracker;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public class TagLibOSGiConfiguration extends TagLibConfiguration
47 {
48 private ServiceTracker packageAdminServiceTracker = null;
49
50
51
52
53
54
55
56
57
58 public void preConfigure(WebAppContext context) throws Exception
59 {
60 String requireTldBundle = (String)context.getAttribute(OSGiWebappConstants.REQUIRE_TLD_BUNDLE);
61 if (requireTldBundle != null)
62 {
63 Collection<Resource> resources = getRequireTldBundleAsJettyResources(context, requireTldBundle);
64 if (resources != null && !resources.isEmpty())
65 {
66 Collection<Resource> previouslySet = (Collection<Resource>)
67 context.getAttribute(TagLibConfiguration.TLD_RESOURCES);
68 if (previouslySet != null)
69 {
70 resources.addAll(previouslySet);
71 }
72 context.setAttribute(TagLibConfiguration.TLD_RESOURCES, resources);
73 }
74 }
75 super.preConfigure(context);
76
77 }
78
79
80
81
82
83
84 private Collection<Resource> getRequireTldBundleAsJettyResources(
85 WebAppContext context, String requireTldBundle)
86 {
87 Bundle bundle = (Bundle)
88 context.getAttribute(OSGiWebappConstants.JETTY_OSGI_BUNDLE);
89 PackageAdmin packAdmin = getBundleAdmin();
90 String[] symbNames = requireTldBundle.split(", ");
91 Collection<Resource> tlds = new LinkedHashSet<Resource>();
92 for (String symbName : symbNames)
93 {
94 Bundle[] bs = packAdmin.getBundles(symbName, null);
95 if (bs == null || bs.length == 0)
96 {
97 throw new IllegalArgumentException("Unable to locate the bundle '"
98 + symbName + "' specified in the "
99 + OSGiWebappConstants.REQUIRE_TLD_BUNDLE
100 + " of the manifest of "
101 + bundle.getSymbolicName());
102 }
103
104 Enumeration<URL> en = bs[0].findEntries("META-INF", "*.tld", false);
105 boolean atLeastOneTldFound = false;
106 while (en.hasMoreElements())
107 {
108 atLeastOneTldFound = true;
109 URL oriUrl = en.nextElement();
110 URL url = DefaultFileLocatorHelper.getLocalURL(oriUrl);
111 Resource tldResource;
112 try
113 {
114 tldResource = Resource.newResource(url);
115 }
116 catch (IOException e)
117 {
118 throw new IllegalArgumentException("Unable to locate the "
119 + "tld resource in '"
120 + url.toString()
121 + "' in the bundle '" + bs[0].getSymbolicName()
122 + "' while registering the "
123 + OSGiWebappConstants.REQUIRE_TLD_BUNDLE
124 + " of the manifest of "
125 + bundle.getSymbolicName(), e);
126 }
127 tlds.add(tldResource);
128 }
129 if (!atLeastOneTldFound)
130 {
131 Log.warn("No '/META-INF/*.tld' resources were found "
132 + " in the bundle '" + bs[0].getSymbolicName()
133 + "' while registering the "
134 + OSGiWebappConstants.REQUIRE_TLD_BUNDLE
135 + " of the manifest of "
136 + bundle.getSymbolicName());
137 }
138 }
139 return tlds;
140 }
141
142 private PackageAdmin getBundleAdmin()
143 {
144 if (packageAdminServiceTracker == null)
145 {
146 Bundle bootBundle = ((BundleReference)OSGiWebappConstants.class.getClassLoader()).getBundle();
147 packageAdminServiceTracker = new ServiceTracker(bootBundle.getBundleContext(),
148 PackageAdmin.class.getName(), null);
149 packageAdminServiceTracker.open();
150 }
151 return (PackageAdmin) packageAdminServiceTracker.getService();
152 }
153
154 }