1 //
2 // ========================================================================
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
8 //
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
11 //
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
14 //
15 // You may elect to redistribute this code under either of these licenses.
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 * Fix various shortcomings with the way jasper parses the tld files. Plugs the
48 * JSTL tlds assuming that they are packaged with the bundle that contains the
49 * JSTL classes.
50 * <p>
51 * Pluggable tlds at the server level are handled by
52 * {@link PluggableWebAppRegistrationCustomizerImpl}.
53 * </p>
54 */
55 public class WebappRegistrationCustomizerImpl implements WebappRegistrationCustomizer
56 {
57 private static final Logger LOG = Log.getLogger(WebappRegistrationCustomizerImpl.class);
58
59
60 /**
61 * Default name of a class that belongs to the jstl bundle. From that class
62 * we locate the corresponding bundle and register it as a bundle that
63 * contains tld files.
64 */
65 private static String DEFAULT_JSTL_BUNDLE_CLASS = "org.apache.taglibs.standard.tag.el.core.WhenTag";
66
67 // used to be "org.apache.jasper.runtime.JspFactoryImpl" but now
68 // the standard tag library implementation are stored in a separate bundle.
69
70 // DISABLED please use the tld bundle argument for the OSGiAppProvider
71 // /**
72 // * Default name of a class that belongs to the bundle where the Java
73 // server Faces tld files are defined.
74 // * This is the sun's reference implementation.
75 // */
76 // private static String DEFAUT_JSF_IMPL_CLASS =
77 // "com.sun.faces.config.ConfigureListener";
78
79 /**
80 * Default jsp factory implementation. Idally jasper is osgified and we can
81 * use services. In the mean time we statically set the jsp factory
82 * implementation. bug #299733
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 // sanity check:
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 // bug #299733
103 JspFactory fact = JspFactory.getDefaultFactory();
104 if (fact == null)
105 { // bug #299733
106 // JspFactory does a simple
107 // Class.getForName("org.apache.jasper.runtime.JspFactoryImpl")
108 // however its bundles does not import the jasper package
109 // so it fails. let's help things out:
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 * The jasper TldScanner expects a URLClassloader to parse a jar for the
123 * /META-INF/*.tld it may contain. We place the bundles that we know contain
124 * such tag-libraries. Please note that it will work if and only if the
125 * bundle is a jar (!) Currently we just hardcode the bundle that contains
126 * the jstl implemenation.
127 *
128 * A workaround when the tld cannot be parsed with this method is to copy
129 * and paste it inside the WEB-INF of the webapplication where it is used.
130 *
131 * Support only 2 types of packaging for the bundle: - the bundle is a jar
132 * (recommended for runtime.) - the bundle is a folder and contain jars in
133 * the root and/or in the lib folder (nice for PDE developement situations)
134 * Unsupported: the bundle is a jar that embeds more jars.
135 *
136 * @return array of URLs
137 * @throws Exception
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 // Look for the jstl bundle
146 // We assume the jstl's tlds are defined there.
147 // We assume that the jstl bundle is imported by this bundle
148 // So we can look for this class using this bundle's classloader:
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 // try to find the jar files inside this folder
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 * Jasper resolves the dtd when it parses a taglib descriptor. It uses this
195 * code to do that:
196 * ParserUtils.getClass().getResourceAsStream(resourcePath); where
197 * resourcePath is for example:
198 * /javax/servlet/jsp/resources/web-jsptaglibrary_1_2.dtd Unfortunately, the
199 * dtd file is not in the exact same classloader as ParserUtils class and
200 * the dtds are packaged in 2 separate bundles. OSGi does not look in the
201 * dependencies' classloader when a resource is searched.
202 * <p>
203 * The workaround consists of setting the entity resolver. That is a patch
204 * added to the version of glassfish-jasper-jetty. IT is also present in the
205 * latest version of glassfish jasper. Could not use introspection to set
206 * new value on a static friendly field :(
207 * </p>
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 * Instead of using the ParserUtil's classloader, we use a class that is
225 * indeed next to the resource for sure.
226 */
227 static class MyFixedupEntityResolver implements EntityResolver
228 {
229 /**
230 * Same values than in ParserUtils...
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 // if that failed try again with the original code:
257 // although it is likely not changed.
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 }