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.jasper;
20
21 import java.io.File;
22 import java.net.URL;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Set;
29 import java.util.StringTokenizer;
30 import java.util.regex.Pattern;
31
32 import org.eclipse.jetty.deploy.DeploymentManager;
33 import org.eclipse.jetty.osgi.boot.OSGiWebInfConfiguration;
34 import org.eclipse.jetty.osgi.boot.utils.BundleFileLocatorHelper;
35 import org.eclipse.jetty.osgi.boot.utils.WebappRegistrationCustomizer;
36 import org.osgi.framework.Bundle;
37 import org.osgi.framework.FrameworkUtil;
38
39
40
41
42
43
44
45
46
47
48
49
50
51 public class PluggableWebAppRegistrationCustomizerImpl implements WebappRegistrationCustomizer
52 {
53
54
55
56
57
58
59 public static final String SYS_PROP_TLD_BUNDLES = "org.eclipse.jetty.osgi.tldbundles";
60
61
62
63
64
65
66
67
68 private static Collection<String> getTldBundles(DeploymentManager deploymentManager)
69 {
70 String sysprop = System.getProperty(SYS_PROP_TLD_BUNDLES);
71 String att = (String) deploymentManager.getContextAttribute(OSGiWebInfConfiguration.CONTAINER_BUNDLE_PATTERN);
72 if (sysprop == null && att == null) { return Collections.emptySet(); }
73 if (att == null)
74 {
75 att = sysprop;
76 }
77 else if (sysprop != null)
78 {
79 att = att + "," + sysprop;
80 }
81
82 Collection<String> tldbundles = new HashSet<String>();
83 StringTokenizer tokenizer = new StringTokenizer(att, ", \n\r\t", false);
84 while (tokenizer.hasMoreTokens())
85 {
86 tldbundles.add(tokenizer.nextToken());
87 }
88 return tldbundles;
89 }
90
91
92
93
94
95 public URL[] getJarsWithTlds(DeploymentManager deploymentManager, BundleFileLocatorHelper locatorHelper) throws Exception
96 {
97
98
99
100
101
102
103
104
105
106 Bundle[] bundles = FrameworkUtil.getBundle(PluggableWebAppRegistrationCustomizerImpl.class).getBundleContext().getBundles();
107 HashSet<URL> urls = new HashSet<URL>();
108 String tmp = System.getProperty(SYS_PROP_TLD_BUNDLES);
109 List<String> sysNames = new ArrayList<String>();
110 if (tmp != null)
111 {
112 StringTokenizer tokenizer = new StringTokenizer(tmp, ", \n\r\t", false);
113 while (tokenizer.hasMoreTokens())
114 sysNames.add(tokenizer.nextToken());
115 }
116 tmp = (String) deploymentManager.getContextAttribute(OSGiWebInfConfiguration.CONTAINER_BUNDLE_PATTERN);
117 Pattern pattern = (tmp==null? null : Pattern.compile(tmp));
118 for (Bundle bundle : bundles)
119 {
120 if (sysNames.contains(bundle.getSymbolicName()))
121 registerTldBundle(locatorHelper, bundle, urls);
122
123 if (pattern != null && pattern.matcher(bundle.getSymbolicName()).matches())
124 registerTldBundle(locatorHelper, bundle, urls);
125 }
126
127 return urls.toArray(new URL[urls.size()]);
128
129 }
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156 private void registerTldBundle(BundleFileLocatorHelper locatorHelper, Bundle bundle, Set<URL> urls) throws Exception
157 {
158 File jasperLocation = locatorHelper.getBundleInstallLocation(bundle);
159 if (jasperLocation.isDirectory())
160 {
161 for (File f : jasperLocation.listFiles())
162 {
163 if (f.getName().endsWith(".jar") && f.isFile())
164 {
165 urls.add(f.toURI().toURL());
166 }
167 else if (f.isDirectory() && f.getName().equals("lib"))
168 {
169 for (File f2 : jasperLocation.listFiles())
170 {
171 if (f2.getName().endsWith(".jar") && f2.isFile())
172 {
173 urls.add(f2.toURI().toURL());
174 }
175 }
176 }
177 }
178 urls.add(jasperLocation.toURI().toURL());
179 }
180 else
181 {
182 urls.add(jasperLocation.toURI().toURL());
183 }
184
185 }
186
187 }