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