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