View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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.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   * JSTLBundleDiscoverer
49   * 
50   * Fix various shortcomings with the way jasper parses the tld files. Plugs the
51   * JSTL tlds assuming that they are packaged with the bundle that contains the
52   * JSTL classes.
53   * <p>
54   * Pluggable tlds at the server level are handled by
55   * {@link ContainerTldBundleDiscoverer}.
56   * </p>
57   */
58  public class JSTLBundleDiscoverer implements TldBundleDiscoverer
59  {
60      private static final Logger LOG = Log.getLogger(JSTLBundleDiscoverer.class);
61      
62  
63      /**
64       * Default name of a class that belongs to the jstl bundle. From that class
65       * we locate the corresponding bundle and register it as a bundle that
66       * contains tld files.
67       */
68      private static String DEFAULT_JSTL_BUNDLE_CLASS = "org.apache.taglibs.standard.tag.el.core.WhenTag";
69  
70      // used to be "org.apache.jasper.runtime.JspFactoryImpl" but now
71      // the standard tag library implementation are stored in a separate bundle.
72  
73      // DISABLED please use the tld bundle argument for the OSGiAppProvider
74      // /**
75      // * Default name of a class that belongs to the bundle where the Java
76      // server Faces tld files are defined.
77      // * This is the sun's reference implementation.
78      // */
79      // private static String DEFAUT_JSF_IMPL_CLASS =
80      // "com.sun.faces.config.ConfigureListener";
81  
82      /**
83       * Default jsp factory implementation. Idally jasper is osgified and we can
84       * use services. In the mean time we statically set the jsp factory
85       * implementation. bug #299733
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              // sanity check:
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             // bug #299733
106             JspFactory fact = JspFactory.getDefaultFactory();
107             if (fact == null)
108             { // bug #299733
109               // JspFactory does a simple
110               // Class.getForName("org.apache.jasper.runtime.JspFactoryImpl")
111               // however its bundles does not import the jasper package
112               // so it fails. let's help things out:
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      * The jasper TldScanner expects a URLClassloader to parse a jar for the
126      * /META-INF/*.tld it may contain. We place the bundles that we know contain
127      * such tag-libraries. Please note that it will work if and only if the
128      * bundle is a jar (!) Currently we just hardcode the bundle that contains
129      * the jstl implemenation.
130      * 
131      * A workaround when the tld cannot be parsed with this method is to copy
132      * and paste it inside the WEB-INF of the webapplication where it is used.
133      * 
134      * Support only 2 types of packaging for the bundle: - the bundle is a jar
135      * (recommended for runtime.) - the bundle is a folder and contain jars in
136      * the root and/or in the lib folder (nice for PDE developement situations)
137      * Unsupported: the bundle is a jar that embeds more jars.
138      * 
139      * @return array of URLs
140      * @throws Exception
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         // Look for the jstl bundle
149         // We assume the jstl's tlds are defined there.
150         // We assume that the jstl bundle is imported by this bundle
151         // So we can look for this class using this bundle's classloader:
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                 // try to find the jar files inside this folder
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      * Jasper resolves the dtd when it parses a taglib descriptor. It uses this
198      * code to do that:
199      * ParserUtils.getClass().getResourceAsStream(resourcePath); where
200      * resourcePath is for example:
201      * /javax/servlet/jsp/resources/web-jsptaglibrary_1_2.dtd Unfortunately, the
202      * dtd file is not in the exact same classloader as ParserUtils class and
203      * the dtds are packaged in 2 separate bundles. OSGi does not look in the
204      * dependencies' classloader when a resource is searched.
205      * <p>
206      * The workaround consists of setting the entity resolver. That is a patch
207      * added to the version of glassfish-jasper-jetty. IT is also present in the
208      * latest version of glassfish jasper. Could not use introspection to set
209      * new value on a static friendly field :(
210      * </p>
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      * Instead of using the ParserUtil's classloader, we use a class that is
228      * indeed next to the resource for sure.
229      */
230     static class MyFixedupEntityResolver implements EntityResolver
231     {
232         /**
233          * Same values than in ParserUtils...
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                             // if that failed try again with the original code:
260                             // although it is likely not changed.
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 }