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