You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by re...@apache.org on 2022/04/25 16:02:11 UTC

[tomcat] branch main updated: 66031: Fix NPE when using a custom JspFactory

This is an automated email from the ASF dual-hosted git repository.

remm pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/main by this push:
     new 6c30766133 66031: Fix NPE when using a custom JspFactory
6c30766133 is described below

commit 6c3076613322857da9d6f000966841049be90588
Author: remm <re...@apache.org>
AuthorDate: Mon Apr 25 18:01:36 2022 +0200

    66031: Fix NPE when using a custom JspFactory
    
    Patch by Jean-Louis Monteiro.
---
 .../apache/jasper/servlet/JasperInitializer.java   |  7 ++-
 .../jasper/servlet/TestJasperInitializer.java      | 71 ++++++++++++++++++++++
 webapps/docs/changelog.xml                         |  4 ++
 3 files changed, 79 insertions(+), 3 deletions(-)

diff --git a/java/org/apache/jasper/servlet/JasperInitializer.java b/java/org/apache/jasper/servlet/JasperInitializer.java
index f2f3700e67..c23d74e5ee 100644
--- a/java/org/apache/jasper/servlet/JasperInitializer.java
+++ b/java/org/apache/jasper/servlet/JasperInitializer.java
@@ -43,7 +43,6 @@ public class JasperInitializer implements ServletContainerInitializer {
     private static final String MSG = "org.apache.jasper.servlet.JasperInitializer";
     private final Log log = LogFactory.getLog(JasperInitializer.class); // must not be static
 
-    private static JspFactoryImpl defaultFactory;
     /**
      * Preload classes required at runtime by a JSP servlet so that
      * we don't get a defineClassInPackage security exception.
@@ -53,7 +52,6 @@ public class JasperInitializer implements ServletContainerInitializer {
         SecurityClassLoad.securityClassLoad(factory.getClass().getClassLoader());
         if (JspFactory.getDefaultFactory() == null) {
             JspFactory.setDefaultFactory(factory);
-            defaultFactory = factory;
         }
     }
 
@@ -105,7 +103,10 @@ public class JasperInitializer implements ServletContainerInitializer {
                 throw new ServletException(e);
             }
         }
-        defaultFactory.setPoolSize(poolSize);
+        JspFactory factory = JspFactory.getDefaultFactory();
+        if (factory instanceof JspFactoryImpl) {
+            ((JspFactoryImpl) factory).setPoolSize(poolSize);
+        }
 
     }
 
diff --git a/test/org/apache/jasper/servlet/TestJasperInitializer.java b/test/org/apache/jasper/servlet/TestJasperInitializer.java
new file mode 100644
index 0000000000..4b30aaaef8
--- /dev/null
+++ b/test/org/apache/jasper/servlet/TestJasperInitializer.java
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jasper.servlet;
+
+import java.util.Collections;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import jakarta.servlet.descriptor.JspConfigDescriptor;
+import jakarta.servlet.jsp.JspFactory;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import org.apache.jasper.Constants;
+import org.apache.jasper.runtime.JspFactoryImpl;
+import org.apache.tomcat.unittest.TesterServletContext;
+
+public class TestJasperInitializer {
+
+    @Test
+    public void testPoolSize() throws Exception {
+
+        final AtomicInteger actualPoolSize = new AtomicInteger(-1);
+        final JspFactoryImpl defaultFactory = new JspFactoryImpl() {
+            @Override
+            public void setPoolSize(int poolSize) {
+                actualPoolSize.set(poolSize);
+                super.setPoolSize(poolSize);
+            }
+        };
+
+        JspFactory.setDefaultFactory(defaultFactory);
+        new JasperInitializer().onStartup(Collections.emptySet(), new TesterServletContext(){
+            @Override
+            public void setAttribute(String name, Object object) {
+                // ignore
+            }
+
+            @Override
+            public JspConfigDescriptor getJspConfigDescriptor() {
+                return null;
+            }
+
+            @Override
+            public String getInitParameter(String name) {
+                if (Constants.JSP_FACTORY_POOL_SIZE_INIT_PARAM.equals(name)) {
+                    return "3";
+                }
+                return super.getInitParameter(name);
+            }
+        });
+
+        // Default value is 8 in JasperInitializer but we have overridden it
+        Assert.assertEquals(3, actualPoolSize.get());
+    }
+
+}
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index be3054eb5e..85ea9b3301 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -148,6 +148,10 @@
         is be because the associated HTML elements are no longer supported by
         any major browser. (markt)
       </update>
+      <fix>
+        <bug>66031</bug>: Fix NPE when using a custom JspFactory. Patch by
+        Jean-Louis Monteiro. (remm)
+      </fix>
     </changelog>
   </subsection>
 </section>


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org