Author: markt Date: Tue Feb 28 21:18:04 2017 New Revision: 1784818 URL: http://svn.apache.org/viewvc?rev=1784818&view=rev Log: Servlet 4.0 Implement dynamic registration of JSP files as servlets
Modified: tomcat/trunk/java/javax/servlet/ServletContext.java tomcat/trunk/java/org/apache/catalina/core/ApplicationContext.java tomcat/trunk/java/org/apache/catalina/core/ApplicationContextFacade.java tomcat/trunk/java/org/apache/catalina/core/LocalStrings.properties tomcat/trunk/java/org/apache/catalina/core/StandardContext.java tomcat/trunk/java/org/apache/jasper/servlet/JspCServletContext.java tomcat/trunk/test/org/apache/tomcat/unittest/TesterServletContext.java Modified: tomcat/trunk/java/javax/servlet/ServletContext.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/javax/servlet/ServletContext.java?rev=1784818&r1=1784817&r2=1784818&view=diff ============================================================================== --- tomcat/trunk/java/javax/servlet/ServletContext.java (original) +++ tomcat/trunk/java/javax/servlet/ServletContext.java Tue Feb 28 21:18:04 2017 @@ -613,6 +613,20 @@ public interface ServletContext { Class<? extends Servlet> servletClass); /** + * + * @param jspName The servlet name under which this JSP file should be + * registered + * @param jspFile The path, relative to the web application root, for the + * JSP file to be used for this servlet + * + * @return a {@link javax.servlet.ServletRegistration.Dynamic} object + * that can be used to further configure the servlet + * + * @since Servlet 4.0 + */ + public ServletRegistration.Dynamic addJspFile(String jspName, String jspFile); + + /** * TODO SERVLET3 - Add comments * @param <T> TODO * @param c TODO Modified: tomcat/trunk/java/org/apache/catalina/core/ApplicationContext.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/ApplicationContext.java?rev=1784818&r1=1784817&r2=1784818&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/core/ApplicationContext.java (original) +++ tomcat/trunk/java/org/apache/catalina/core/ApplicationContext.java Tue Feb 28 21:18:04 2017 @@ -46,6 +46,7 @@ import javax.servlet.ServletContextAttri import javax.servlet.ServletContextListener; import javax.servlet.ServletException; import javax.servlet.ServletRegistration; +import javax.servlet.ServletRegistration.Dynamic; import javax.servlet.ServletRequestAttributeListener; import javax.servlet.ServletRequestListener; import javax.servlet.SessionCookieConfig; @@ -829,25 +830,61 @@ public class ApplicationContext implemen @Override public ServletRegistration.Dynamic addServlet(String servletName, String className) { - return addServlet(servletName, className, null); + return addServlet(servletName, className, null, null); } @Override public ServletRegistration.Dynamic addServlet(String servletName, Servlet servlet) { - return addServlet(servletName, null, servlet); + return addServlet(servletName, null, servlet, null); } @Override public ServletRegistration.Dynamic addServlet(String servletName, Class<? extends Servlet> servletClass) { - return addServlet(servletName, servletClass.getName(), null); + return addServlet(servletName, servletClass.getName(), null, null); } - private ServletRegistration.Dynamic addServlet(String servletName, - String servletClass, Servlet servlet) throws IllegalStateException { + @Override + public Dynamic addJspFile(String jspName, String jspFile) { + + // jspName is validated in addServlet() + if (jspFile == null || !jspFile.startsWith("/")) { + throw new IllegalArgumentException( + sm.getString("applicationContext.addJspFile.iae", jspFile)); + } + + String jspServletClassName = null; + Map<String,String> jspFileInitParams = new HashMap<>(); + + Wrapper jspServlet = (Wrapper) context.findChild("jsp"); + + if (jspServlet == null) { + // No JSP servlet currently defined. + // Use default JSP Servlet class name + jspServletClassName = Constants.JSP_SERVLET_CLASS; + } else { + // JSP Servlet defined. + // Use same JSP Servlet class name + jspServletClassName = jspServlet.getServletClass(); + // Use same init parameters + String[] params = jspServlet.findInitParameters(); + for (String param : params) { + jspFileInitParams.put(param, jspServlet.findInitParameter(param)); + } + } + + // Add init parameter to specify JSP file + jspFileInitParams.put("jspFile", jspFile); + + return addServlet(jspName, jspServletClassName, null, jspFileInitParams); + } + + + private ServletRegistration.Dynamic addServlet(String servletName, String servletClass, + Servlet servlet, Map<String,String> initParams) throws IllegalStateException { if (servletName == null || servletName.equals("")) { throw new IllegalArgumentException(sm.getString( @@ -887,6 +924,12 @@ public class ApplicationContext implemen wrapper.setServlet(servlet); } + if (initParams != null) { + for (Map.Entry<String, String> initParam: initParams.entrySet()) { + wrapper.addInitParameter(initParam.getKey(), initParam.getValue()); + } + } + return context.dynamicServletAdded(wrapper); } Modified: tomcat/trunk/java/org/apache/catalina/core/ApplicationContextFacade.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/ApplicationContextFacade.java?rev=1784818&r1=1784817&r2=1784818&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/core/ApplicationContextFacade.java (original) +++ tomcat/trunk/java/org/apache/catalina/core/ApplicationContextFacade.java Tue Feb 28 21:18:04 2017 @@ -41,6 +41,7 @@ import javax.servlet.Servlet; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRegistration; +import javax.servlet.ServletRegistration.Dynamic; import javax.servlet.SessionCookieConfig; import javax.servlet.SessionTrackingMode; import javax.servlet.descriptor.JspConfigDescriptor; @@ -536,6 +537,17 @@ public class ApplicationContextFacade im } } + + @Override + public Dynamic addJspFile(String jspName, String jspFile) { + if (SecurityUtil.isPackageProtectionEnabled()) { + return (ServletRegistration.Dynamic) doPrivileged("addJspFile", + new Object[]{jspName, jspFile}); + } else { + return context.addJspFile(jspName, jspFile); + } + } + @Override @SuppressWarnings("unchecked") // doPrivileged() returns the correct type Modified: tomcat/trunk/java/org/apache/catalina/core/LocalStrings.properties URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/LocalStrings.properties?rev=1784818&r1=1784817&r2=1784818&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/core/LocalStrings.properties (original) +++ tomcat/trunk/java/org/apache/catalina/core/LocalStrings.properties Tue Feb 28 21:18:04 2017 @@ -14,6 +14,7 @@ # limitations under the License. applicationContext.addFilter.ise=Filters cannot be added to context {0} as the context has been initialised +applicationContext.addJspFile.iae=The JSP file [{0}] is not valid applicationContext.addListener.iae.cnfe=Unable to create an instance of type [{0}] applicationContext.addListener.iae.init=Unable to add an instance of type [{0}] as a listener applicationContext.addListener.iae.wrongType=The type specified [{0}] is not one of the expected listener types Modified: tomcat/trunk/java/org/apache/catalina/core/StandardContext.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/StandardContext.java?rev=1784818&r1=1784817&r2=1784818&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/core/StandardContext.java (original) +++ tomcat/trunk/java/org/apache/catalina/core/StandardContext.java Tue Feb 28 21:18:04 2017 @@ -6567,6 +6567,12 @@ public class StandardContext extends Con } @Override + public Dynamic addJspFile(String jspName, String jspFile) { + throw new UnsupportedOperationException( + sm.getString("noPluggabilityServletContext.notAllowed")); + } + + @Override public <T extends Servlet> T createServlet(Class<T> c) throws ServletException { throw new UnsupportedOperationException( Modified: tomcat/trunk/java/org/apache/jasper/servlet/JspCServletContext.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/jasper/servlet/JspCServletContext.java?rev=1784818&r1=1784817&r2=1784818&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/jasper/servlet/JspCServletContext.java (original) +++ tomcat/trunk/java/org/apache/jasper/servlet/JspCServletContext.java Tue Feb 28 21:18:04 2017 @@ -588,6 +588,12 @@ public class JspCServletContext implemen @Override + public javax.servlet.ServletRegistration.Dynamic addJspFile(String jspName, String jspFile) { + return null; + } + + + @Override public <T extends Filter> T createFilter(Class<T> c) throws ServletException { return null; Modified: tomcat/trunk/test/org/apache/tomcat/unittest/TesterServletContext.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/tomcat/unittest/TesterServletContext.java?rev=1784818&r1=1784817&r2=1784818&view=diff ============================================================================== --- tomcat/trunk/test/org/apache/tomcat/unittest/TesterServletContext.java (original) +++ tomcat/trunk/test/org/apache/tomcat/unittest/TesterServletContext.java Tue Feb 28 21:18:04 2017 @@ -227,6 +227,11 @@ public class TesterServletContext implem } @Override + public Dynamic addJspFile(String jspName, String jspFile) { + throw new RuntimeException("Not implemented"); + } + + @Override public <T extends Servlet> T createServlet(Class<T> c) throws ServletException { throw new RuntimeException("Not implemented"); --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org