View Javadoc

1   // ========================================================================
2   // Copyright (c) 2009-2010 Intalio, Inc.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
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   * Fix various shortcomings with the way jasper parses the tld files.
40   * Plugs the JSTL tlds assuming that they are packaged with the bundle that contains the JSTL classes.
41   * 
42   */
43  public class WebappRegistrationCustomizerImpl implements WebappRegistrationCustomizer
44  {
45      
46  	/**
47  	 * Default name of a class that belongs to the jstl bundle.
48  	 * From that class we locate the corresponding bundle and register it
49  	 * as a bundle that contains tld files.
50  	 */
51  	private static String DEFAULT_JSTL_BUNDLE_CLASS = "org.apache.taglibs.standard.tag.el.core.WhenTag";
52  	//used to be "org.apache.jasper.runtime.JspFactoryImpl" but now 
53  	//the standard tag library implementation are stored in a separate bundle.
54  	
55  	//DISABLED please use the tld bundle argument for the OSGiAppProvider
56  //	/**
57  //	 * Default name of a class that belongs to the bundle where the Java server Faces tld files are defined.
58  //	 * This is the sun's reference implementation. 
59  //	 */
60  //	private static String DEFAUT_JSF_IMPL_CLASS = "com.sun.faces.config.ConfigureListener";
61  
62  	/**
63  	 * Default jsp factory implementation.
64  	 * Idally jasper is osgified and we can use services.
65  	 * In the mean time we statically set the jsp factory implementation.
66  	 * bug #299733
67  	 */
68  	private static String DEFAULT_JSP_FACTORY_IMPL_CLASS = "org.apache.jasper.runtime.JspFactoryImpl";
69  	
70      public WebappRegistrationCustomizerImpl()
71      {
72          fixupDtdResolution();
73          
74          try
75          {
76            //sanity check:
77              Class cl = getClass().getClassLoader().loadClass("org.apache.jasper.servlet.JspServlet");
78              //System.err.println("found the jsp servlet: " + cl.getName());
79          }
80          catch (Exception e)
81          {
82              System.err.println("Unable to locate the JspServlet: jsp support unavailable.");
83              e.printStackTrace();
84              return;
85          }
86          try
87          {
88              //bug #299733
89              JspFactory fact = JspFactory.getDefaultFactory();
90              if (fact == null)
91              {   //bug #299733
92                  //JspFactory does a simple Class.getForName("org.apache.jasper.runtime.JspFactoryImpl")
93                  //however its bundles does not import the jasper package
94                  //so it fails. let's help things out:
95                  fact = (JspFactory)JettyBootstrapActivator.class.getClassLoader()
96                      .loadClass(DEFAULT_JSP_FACTORY_IMPL_CLASS).newInstance();
97                  JspFactory.setDefaultFactory(fact);
98              }
99      
100         }
101         catch (Exception e)
102         {
103             System.err.println("Unable to set the JspFactory: jsp support incomplete.");
104             e.printStackTrace();
105         }
106     }
107     
108     /**
109      * TODO: right now only the jetty-jsp bundle is scanned for common taglibs. Should support a way to plug more bundles that contain taglibs.
110      * 
111      * The jasper TldScanner expects a URLClassloader to parse a jar for the /META-INF/*.tld it may contain. We place the bundles that we know contain such
112      * tag-libraries. Please note that it will work if and only if the bundle is a jar (!) Currently we just hardcode the bundle that contains the jstl
113      * implemenation.
114      * 
115      * A workaround when the tld cannot be parsed with this method is to copy and paste it inside the WEB-INF of the webapplication where it is used.
116      * 
117      * Support only 2 types of packaging for the bundle: - the bundle is a jar (recommended for runtime.) - the bundle is a folder and contain jars in the root
118      * and/or in the lib folder (nice for PDE developement situations) Unsupported: the bundle is a jar that embeds more jars.
119      * 
120      * @return
121      * @throws Exception
122      */
123     public URL[] getJarsWithTlds(OSGiAppProvider provider, BundleFileLocatorHelper locatorHelper) throws Exception
124     {
125     	
126     	HashSet<Class<?>> classesToAddToTheTldBundles = new HashSet<Class<?>>();
127 
128     	//Look for the jstl bundle
129     	//We assume the jstl's tlds are defined there.
130     	//We assume that the jstl bundle is imported by this bundle
131     	//So we can look for this class using this bundle's classloader:
132     	Class<?> jstlClass = WebappRegistrationCustomizerImpl.class.getClassLoader().loadClass(DEFAULT_JSTL_BUNDLE_CLASS);
133     	
134     	classesToAddToTheTldBundles.add(jstlClass);
135     	
136 //    	//should we also take care of the JSF?
137 //    	try
138 //    	{
139 //    		jstlClass = WebappRegistrationCustomizerImpl.class.getClassLoader().loadClass(DEFAUT_JSF_IMPL_CLASS);
140 //    		classesToAddToTheTldBundles.add(jstlClass);
141 //    	}
142 //    	catch (Throwable t)
143 //    	{
144 //    		//never mind we can live without JSF it is optional.
145 //    	}
146         ArrayList<URL> urls = new ArrayList<URL>();
147     	for (Class<?> cl : classesToAddToTheTldBundles)
148     	{
149 	        Bundle tldBundle = FrameworkUtil.getBundle(cl);
150 	        File tldBundleLocation = locatorHelper.getBundleInstallLocation(tldBundle);
151 	        if (tldBundleLocation.isDirectory())
152 	        {
153 	            // try to find the jar files inside this folder
154 	            for (File f : tldBundleLocation.listFiles())
155 	            {
156 	                if (f.getName().endsWith(".jar") && f.isFile())
157 	                {
158 	                    urls.add(f.toURI().toURL());
159 	                }
160 	                else if (f.isDirectory() && f.getName().equals("lib"))
161 	                {
162 	                    for (File f2 : tldBundleLocation.listFiles())
163 	                    {
164 	                        if (f2.getName().endsWith(".jar") && f2.isFile())
165 	                        {
166 	                            urls.add(f2.toURI().toURL());
167 	                        }
168 	                    }
169 	                }
170 	            }
171 	            
172 	        }
173 	        else
174 	        {
175 	            urls.add(tldBundleLocation.toURI().toURL());
176 	        }
177     	}
178     	return urls.toArray(new URL[urls.size()]);
179     }
180 	
181     /**
182      * Jasper resolves the dtd when it parses a taglib descriptor. 
183      * It uses this code to do that: ParserUtils.getClass().getResourceAsStream(resourcePath); where
184      * resourcePath is for example: /javax/servlet/jsp/resources/web-jsptaglibrary_1_2.dtd Unfortunately, 
185      * the dtd file is not in the exact same classloader as
186      * ParserUtils class and the dtds are packaged in 2 separate bundles.
187      * OSGi does not look in the dependencies' classloader when a resource is searched.
188      * <p>
189      * The workaround consists of setting the entity resolver. That is a patch 
190      * added to the version of glassfish-jasper-jetty. IT is also present in the latest
191      * version of glassfish jasper. Could not use introspection to set new value
192      * on a static friendly field :(
193      * </p>
194      */
195     void fixupDtdResolution()
196     {
197         try
198         {
199             ParserUtils.setEntityResolver(new MyFixedupEntityResolver());
200 
201         }
202         catch (Exception e)
203         {
204             e.printStackTrace();
205         }
206 
207     }
208 
209     /**
210      * Instead of using the ParserUtil's classloader, we use a class that is indeed next to the resource for sure.
211      */
212     static class MyFixedupEntityResolver implements EntityResolver
213     {
214         /**
215          * Same values than in ParserUtils...
216          */
217         static final String[] CACHED_DTD_PUBLIC_IDS =
218         { Constants.TAGLIB_DTD_PUBLIC_ID_11, Constants.TAGLIB_DTD_PUBLIC_ID_12,
219           Constants.WEBAPP_DTD_PUBLIC_ID_22, Constants.WEBAPP_DTD_PUBLIC_ID_23, };
220 
221         static final String[] CACHED_DTD_RESOURCE_PATHS =
222         { Constants.TAGLIB_DTD_RESOURCE_PATH_11,
223           Constants.TAGLIB_DTD_RESOURCE_PATH_12,
224           Constants.WEBAPP_DTD_RESOURCE_PATH_22,
225           Constants.WEBAPP_DTD_RESOURCE_PATH_23, };
226 
227         // static final String[] CACHED_SCHEMA_RESOURCE_PATHS = {
228         // Constants.TAGLIB_SCHEMA_RESOURCE_PATH_20,
229         // Constants.TAGLIB_SCHEMA_RESOURCE_PATH_21,
230         // Constants.WEBAPP_SCHEMA_RESOURCE_PATH_24,
231         // Constants.WEBAPP_SCHEMA_RESOURCE_PATH_25,
232         // };
233         public InputSource resolveEntity(String publicId, String systemId) throws SAXException
234         {
235             for (int i = 0; i < CACHED_DTD_PUBLIC_IDS.length; i++)
236             {
237                 String cachedDtdPublicId = CACHED_DTD_PUBLIC_IDS[i];
238                 if (cachedDtdPublicId.equals(publicId))
239                 {
240                     String resourcePath = CACHED_DTD_RESOURCE_PATHS[i];
241                     InputStream input = null;
242                     input = Servlet.class.getResourceAsStream(resourcePath);
243                     if (input == null)
244                     {
245                         input = JspContext.class.getResourceAsStream(resourcePath);
246                         if (input == null)
247                         {
248                             // if that failed try again with the original code:
249                             // although it is likely not changed.
250                             input = this.getClass().getResourceAsStream(resourcePath);
251                         }
252                     }
253                     if (input == null)
254                     {
255                         throw new SAXException(Localizer.getMessage("jsp.error.internal.filenotfound",resourcePath));
256                     }
257                     InputSource isrc = new InputSource(input);
258                     return isrc;
259                 }
260             }
261 
262             return null;
263         }
264     }
265 	
266 }