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.io.InputStream;
23 import java.net.URL;
24 import java.util.ArrayList;
25 import java.util.HashSet;
26
27 import javax.servlet.Servlet;
28 import javax.servlet.jsp.JspContext;
29 import javax.servlet.jsp.JspFactory;
30
31 import org.apache.jasper.Constants;
32 import org.apache.jasper.compiler.Localizer;
33 import org.apache.jasper.xmlparser.ParserUtils;
34 import org.eclipse.jetty.deploy.DeploymentManager;
35 import org.eclipse.jetty.osgi.boot.JettyBootstrapActivator;
36 import org.eclipse.jetty.osgi.boot.utils.BundleFileLocatorHelper;
37 import org.eclipse.jetty.osgi.boot.utils.TldBundleDiscoverer;
38 import org.eclipse.jetty.util.log.Log;
39 import org.eclipse.jetty.util.log.Logger;
40 import org.osgi.framework.Bundle;
41 import org.osgi.framework.FrameworkUtil;
42 import org.xml.sax.EntityResolver;
43 import org.xml.sax.InputSource;
44 import org.xml.sax.SAXException;
45
46
47
48
49
50
51
52
53
54
55
56
57
58 public class JSTLBundleDiscoverer implements TldBundleDiscoverer
59 {
60 private static final Logger LOG = Log.getLogger(JSTLBundleDiscoverer.class);
61
62
63
64
65
66
67
68 private static String DEFAULT_JSTL_BUNDLE_CLASS = "org.apache.taglibs.standard.tag.el.core.WhenTag";
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 private static String DEFAULT_JSP_FACTORY_IMPL_CLASS = "org.apache.jasper.runtime.JspFactoryImpl";
88
89 public JSTLBundleDiscoverer()
90 {
91 fixupDtdResolution();
92
93 try
94 {
95
96 Class cl = getClass().getClassLoader().loadClass("org.apache.jasper.servlet.JspServlet");
97 }
98 catch (Exception e)
99 {
100 LOG.warn("Unable to locate the JspServlet: jsp support unavailable.", e);
101 return;
102 }
103 try
104 {
105
106 JspFactory fact = JspFactory.getDefaultFactory();
107 if (fact == null)
108 {
109
110
111
112
113 fact = (JspFactory) JettyBootstrapActivator.class.getClassLoader().loadClass(DEFAULT_JSP_FACTORY_IMPL_CLASS).newInstance();
114 JspFactory.setDefaultFactory(fact);
115 }
116
117 }
118 catch (Exception e)
119 {
120 LOG.warn("Unable to set the JspFactory: jsp support incomplete.", e);
121 }
122 }
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142 public URL[] getUrlsForBundlesWithTlds(DeploymentManager deployer, BundleFileLocatorHelper locatorHelper) throws Exception
143 {
144
145 ArrayList<URL> urls = new ArrayList<URL>();
146 HashSet<Class<?>> classesToAddToTheTldBundles = new HashSet<Class<?>>();
147
148
149
150
151
152 try
153 {
154 Class<?> jstlClass = JSTLBundleDiscoverer.class.getClassLoader().loadClass(DEFAULT_JSTL_BUNDLE_CLASS);
155
156 classesToAddToTheTldBundles.add(jstlClass);
157 }
158 catch (ClassNotFoundException e)
159 {
160 LOG.info("jstl not on classpath", e);
161 }
162 for (Class<?> cl : classesToAddToTheTldBundles)
163 {
164 Bundle tldBundle = FrameworkUtil.getBundle(cl);
165 File tldBundleLocation = locatorHelper.getBundleInstallLocation(tldBundle);
166 if (tldBundleLocation != null && tldBundleLocation.isDirectory())
167 {
168
169 for (File f : tldBundleLocation.listFiles())
170 {
171 if (f.getName().endsWith(".jar") && f.isFile())
172 {
173 urls.add(f.toURI().toURL());
174 }
175 else if (f.isDirectory() && f.getName().equals("lib"))
176 {
177 for (File f2 : tldBundleLocation.listFiles())
178 {
179 if (f2.getName().endsWith(".jar") && f2.isFile())
180 {
181 urls.add(f2.toURI().toURL());
182 }
183 }
184 }
185 }
186
187 }
188 else if (tldBundleLocation != null)
189 {
190 urls.add(tldBundleLocation.toURI().toURL());
191 }
192 }
193 return urls.toArray(new URL[urls.size()]);
194 }
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212 void fixupDtdResolution()
213 {
214 try
215 {
216 ParserUtils.setEntityResolver(new MyFixedupEntityResolver());
217
218 }
219 catch (Exception e)
220 {
221 e.printStackTrace();
222 }
223
224 }
225
226
227
228
229
230 static class MyFixedupEntityResolver implements EntityResolver
231 {
232
233
234
235 static final String[] CACHED_DTD_PUBLIC_IDS = { Constants.TAGLIB_DTD_PUBLIC_ID_11, Constants.TAGLIB_DTD_PUBLIC_ID_12,
236 Constants.WEBAPP_DTD_PUBLIC_ID_22, Constants.WEBAPP_DTD_PUBLIC_ID_23, };
237
238 static final String[] CACHED_DTD_RESOURCE_PATHS = { Constants.TAGLIB_DTD_RESOURCE_PATH_11, Constants.TAGLIB_DTD_RESOURCE_PATH_12,
239 Constants.WEBAPP_DTD_RESOURCE_PATH_22, Constants.WEBAPP_DTD_RESOURCE_PATH_23, };
240
241 static final String[] CACHED_SCHEMA_RESOURCE_PATHS = { Constants.TAGLIB_SCHEMA_RESOURCE_PATH_20, Constants.TAGLIB_SCHEMA_RESOURCE_PATH_21,
242 Constants.WEBAPP_SCHEMA_RESOURCE_PATH_24, Constants.WEBAPP_SCHEMA_RESOURCE_PATH_25, };
243
244 public InputSource resolveEntity(String publicId, String systemId) throws SAXException
245 {
246 for (int i = 0; i < CACHED_DTD_PUBLIC_IDS.length; i++)
247 {
248 String cachedDtdPublicId = CACHED_DTD_PUBLIC_IDS[i];
249 if (cachedDtdPublicId.equals(publicId))
250 {
251 String resourcePath = CACHED_DTD_RESOURCE_PATHS[i];
252 InputStream input = null;
253 input = Servlet.class.getResourceAsStream(resourcePath);
254 if (input == null)
255 {
256 input = JspContext.class.getResourceAsStream(resourcePath);
257 if (input == null)
258 {
259
260
261 input = this.getClass().getResourceAsStream(resourcePath);
262 }
263 }
264 if (input == null) { throw new SAXException(Localizer.getMessage("jsp.error.internal.filenotfound", resourcePath)); }
265 InputSource isrc = new InputSource(input);
266 return isrc;
267 }
268 }
269
270 return null;
271 }
272 }
273
274 }