Author: cziegeler Date: Tue Jan 11 14:09:56 2011 New Revision: 1057642 URL: http://svn.apache.org/viewvc?rev=1057642&view=rev Log: SLING-1928 : JSP API classes from the environment can't be used.
Modified: sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/JspScriptEngineFactory.java sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/jasper/compiler/JspRuntimeContext.java Modified: sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/JspScriptEngineFactory.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/JspScriptEngineFactory.java?rev=1057642&r1=1057641&r2=1057642&view=diff ============================================================================== --- sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/JspScriptEngineFactory.java (original) +++ sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/JspScriptEngineFactory.java Tue Jan 11 14:09:56 2011 @@ -115,6 +115,9 @@ public class JspScriptEngineFactory private ServiceRegistration eventHandlerRegistration; + /** The handler for the jsp factories. */ + private JspRuntimeContext.JspFactoryHandler jspFactoryHandler; + public static final String[] SCRIPT_TYPE = { "jsp", "jspf", "jspx" }; public static final String[] NAMES = { "jsp", "JSP" }; @@ -161,6 +164,7 @@ public class JspScriptEngineFactory } final SlingIOProvider io = this.ioProvider; io.setRequestResourceResolver(resolver); + jspFactoryHandler.incUsage(); try { final JspServletWrapperAdapter jsp = getJspWrapperAdapter(scriptHelper); // create a SlingBindings object @@ -168,6 +172,7 @@ public class JspScriptEngineFactory slingBindings.putAll(bindings); jsp.service(slingBindings); } finally { + jspFactoryHandler.decUsage(); io.resetRequestResourceResolver(); } } @@ -217,6 +222,8 @@ public class JspScriptEngineFactory Thread.currentThread().setContextClassLoader(jspClassLoader); try { + this.jspFactoryHandler = JspRuntimeContext.initFactoryHandler(); + this.tldLocationsCache = new SlingTldLocationsCache(componentContext.getBundleContext()); // prepare some classes @@ -283,6 +290,8 @@ public class JspScriptEngineFactory } ioProvider = null; + this.jspFactoryHandler.destroy(); + this.jspFactoryHandler = null; } /** Modified: sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/jasper/compiler/JspRuntimeContext.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/jasper/compiler/JspRuntimeContext.java?rev=1057642&r1=1057641&r2=1057642&view=diff ============================================================================== --- sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/jasper/compiler/JspRuntimeContext.java (original) +++ sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/jasper/compiler/JspRuntimeContext.java Tue Jan 11 14:09:56 2011 @@ -33,8 +33,14 @@ import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; +import javax.servlet.Servlet; import javax.servlet.ServletContext; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.jsp.JspApplicationContext; +import javax.servlet.jsp.JspEngineInfo; import javax.servlet.jsp.JspFactory; +import javax.servlet.jsp.PageContext; import org.apache.juli.logging.Log; import org.apache.juli.logging.LogFactory; @@ -71,11 +77,86 @@ public final class JspRuntimeContext { /** The {...@link IOProvider} used to get access to output */ private IOProvider ioProvider = IOProvider.DEFAULT; + /** This is a delegate forwarding either to our own factory + * or the original one. + * Depending on the USE_OWN_FACTORY flag. + */ + public static final class JspFactoryHandler extends JspFactory { + + private final JspFactory original; + + private final JspFactory own; + + public JspFactoryHandler(final JspFactory orig, final JspFactory own) { + this.original = (orig instanceof JspFactoryHandler ? ((JspFactoryHandler)orig).original : orig); + this.own = own; + } + + private JspFactory getFactory() { + final Integer useOwnFactory = USE_OWN_FACTORY.get(); + if ( useOwnFactory == null || useOwnFactory.intValue() == 0 ) { + return this.original; + } + return this.own; + } + + @Override + public PageContext getPageContext(Servlet paramServlet, + ServletRequest paramServletRequest, + ServletResponse paramServletResponse, String paramString, + boolean paramBoolean1, int paramInt, boolean paramBoolean2) { + return this.getFactory().getPageContext(paramServlet, paramServletRequest, + paramServletResponse, paramString, paramBoolean1, + paramInt, paramBoolean2); + } + + @Override + public void releasePageContext(PageContext paramPageContext) { + this.getFactory().releasePageContext(paramPageContext); + } + + @Override + public JspEngineInfo getEngineInfo() { + return this.getFactory().getEngineInfo(); + } + + @Override + public JspApplicationContext getJspApplicationContext( + ServletContext paramServletContext) { + return this.getFactory().getJspApplicationContext(paramServletContext); + } + + /** + * Reset the jsp factory. + */ + public void destroy() { + final JspFactory current = JspFactory.getDefaultFactory(); + if ( current == this ) { + JspFactory.setDefaultFactory(this.original); + } + } + + public void incUsage() { + final Integer count = JspRuntimeContext.USE_OWN_FACTORY.get(); + int newCount = 1; + if ( count != null ) { + newCount = count + 1; + } + JspRuntimeContext.USE_OWN_FACTORY.set(newCount); + } + + public void decUsage() { + final Integer count = JspRuntimeContext.USE_OWN_FACTORY.get(); + JspRuntimeContext.USE_OWN_FACTORY.set(count - 1); + } + } + /** * Preload classes required at runtime by a JSP servlet so that * we don't get a defineClassInPackage security exception. + * And set jsp factory */ - static { + public static JspFactoryHandler initFactoryHandler() { JspFactoryImpl factory = new JspFactoryImpl(); SecurityClassLoad.securityClassLoad(factory.getClass().getClassLoader()); if( System.getSecurityManager() != null ) { @@ -98,9 +179,13 @@ public final class JspRuntimeContext { } } - JspFactory.setDefaultFactory(factory); + final JspFactoryHandler key = new JspFactoryHandler(JspFactory.getDefaultFactory(), factory); + JspFactory.setDefaultFactory(key); + return key; } + private static final ThreadLocal<Integer> USE_OWN_FACTORY = new ThreadLocal<Integer>(); + // ----------------------------------------------------------- Constructors /**