You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2021/09/22 13:21:31 UTC

[tomcat] 03/05: Remove references to STM and other deleted parts of the Servlet API

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

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

commit c5ffc0638cda8fce02e61dd19c222d0136d694da
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Tue Sep 21 12:39:18 2021 +0100

    Remove references to STM and other deleted parts of the Servlet API
---
 java/jakarta/el/ImportHandler.java                 |   2 -
 java/org/apache/catalina/core/StandardWrapper.java | 248 +++------------------
 .../apache/catalina/core/mbeans-descriptors.xml    |   6 -
 java/org/apache/catalina/startup/Tomcat.java       |  19 +-
 4 files changed, 41 insertions(+), 234 deletions(-)

diff --git a/java/jakarta/el/ImportHandler.java b/java/jakarta/el/ImportHandler.java
index 7e21719..138a6da 100644
--- a/java/jakarta/el/ImportHandler.java
+++ b/java/jakarta/el/ImportHandler.java
@@ -65,7 +65,6 @@ public class ImportHandler {
         servletClassNames.add("ServletRequestListener");
         servletClassNames.add("ServletResponse");
         servletClassNames.add("SessionCookieConfig");
-        servletClassNames.add("SingleThreadModel");
         servletClassNames.add("WriteListener");
         // Classes
         servletClassNames.add("AsyncEvent");
@@ -101,7 +100,6 @@ public class ImportHandler {
         servletHttpClassNames.add("HttpSessionActivationListener");
         servletHttpClassNames.add("HttpSessionAttributeListener");
         servletHttpClassNames.add("HttpSessionBindingListener");
-        servletHttpClassNames.add("HttpSessionContext");
         servletHttpClassNames.add("HttpSessionIdListener");
         servletHttpClassNames.add("HttpSessionListener");
         servletHttpClassNames.add("HttpUpgradeHandler");
diff --git a/java/org/apache/catalina/core/StandardWrapper.java b/java/org/apache/catalina/core/StandardWrapper.java
index b003b3d..83599ae 100644
--- a/java/org/apache/catalina/core/StandardWrapper.java
+++ b/java/org/apache/catalina/core/StandardWrapper.java
@@ -24,7 +24,6 @@ import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Set;
-import java.util.Stack;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 
@@ -180,36 +179,12 @@ public class StandardWrapper extends ContainerBase
 
 
     /**
-     * Does this servlet implement the SingleThreadModel interface?
-     */
-    protected volatile boolean singleThreadModel = false;
-
-
-    /**
      * Are we unloading our servlet instance at the moment?
      */
     protected volatile boolean unloading = false;
 
 
     /**
-     * Maximum number of STM instances.
-     */
-    protected int maxInstances = 20;
-
-
-    /**
-     * Number of instances currently loaded for a STM servlet.
-     */
-    protected int nInstances = 0;
-
-
-    /**
-     * Stack containing the STM instances.
-     */
-    protected Stack<Servlet> instancePool = null;
-
-
-    /**
      * Wait time for servlet unload in ms.
      */
     protected long unloadDelay = 2000;
@@ -319,9 +294,7 @@ public class StandardWrapper extends ContainerBase
 
 
     /**
-     * @return the number of active allocations of this servlet, even if they
-     * are all for the same instance (as will be true for servlets that do
-     * not implement <code>SingleThreadModel</code>.
+     * @return the number of active allocations of this servlet.
      */
     public int getCountAllocated() {
         return this.countAllocated.get();
@@ -393,31 +366,6 @@ public class StandardWrapper extends ContainerBase
 
 
     /**
-     * @return maximum number of instances that will be allocated when a single
-     * thread model servlet is used.
-     */
-    public int getMaxInstances() {
-        return this.maxInstances;
-    }
-
-
-    /**
-     * Set the maximum number of instances that will be allocated when a single
-     * thread model servlet is used.
-     *
-     * @param maxInstances New value of maxInstances
-     */
-    public void setMaxInstances(int maxInstances) {
-
-        int oldMaxInstances = this.maxInstances;
-        this.maxInstances = maxInstances;
-        support.firePropertyChange("maxInstances", oldMaxInstances,
-                                   this.maxInstances);
-
-    }
-
-
-    /**
      * Set the parent Container of this Wrapper, but only if it is a Context.
      *
      * @param container Proposed parent Container
@@ -507,26 +455,6 @@ public class StandardWrapper extends ContainerBase
 
 
     /**
-     * Does the servlet class represented by this component implement the
-     * <code>SingleThreadModel</code> interface? This can only be determined
-     * once the class is loaded. Calling this method will not trigger loading
-     * the class since that may cause the application to behave unexpectedly.
-     *
-     * @return {@code null} if the class has not been loaded, otherwise {@code
-     *         true} if the servlet does implement {@code SingleThreadModel} and
-     *         {@code false} if it does not.
-     */
-    public Boolean isSingleThreadModel() {
-        // If the servlet has been loaded either singleThreadModel will be true
-        // or instance will be non-null
-        if (singleThreadModel || instance != null) {
-            return Boolean.valueOf(singleThreadModel);
-        }
-        return null;
-    }
-
-
-    /**
      * @return <code>true</code> if the Servlet has been marked unavailable.
      */
     @Override
@@ -729,12 +657,7 @@ public class StandardWrapper extends ContainerBase
 
     /**
      * Allocate an initialized instance of this Servlet that is ready to have
-     * its <code>service()</code> method called.  If the servlet class does
-     * not implement <code>SingleThreadModel</code>, the (only) initialized
-     * instance may be returned immediately.  If the servlet class implements
-     * <code>SingleThreadModel</code>, the Wrapper implementation must ensure
-     * that this instance is not allocated again until it is deallocated by a
-     * call to <code>deallocate()</code>.
+     * its <code>service()</code> method called.
      *
      * @exception ServletException if the servlet init() method threw
      *  an exception
@@ -750,96 +673,49 @@ public class StandardWrapper extends ContainerBase
 
         boolean newInstance = false;
 
-        // If not SingleThreadedModel, return the same instance every time
-        if (!singleThreadModel) {
-            // Load and initialize our instance if necessary
-            if (instance == null || !instanceInitialized) {
-                synchronized (this) {
-                    if (instance == null) {
-                        try {
-                            if (log.isDebugEnabled()) {
-                                log.debug("Allocating non-STM instance");
-                            }
-
-                            // Note: We don't know if the Servlet implements
-                            // SingleThreadModel until we have loaded it.
-                            instance = loadServlet();
-                            newInstance = true;
-                            if (!singleThreadModel) {
-                                // For non-STM, increment here to prevent a race
-                                // condition with unload. Bug 43683, test case
-                                // #3
-                                countAllocated.incrementAndGet();
-                            }
-                        } catch (ServletException e) {
-                            throw e;
-                        } catch (Throwable e) {
-                            ExceptionUtils.handleThrowable(e);
-                            throw new ServletException(sm.getString("standardWrapper.allocate"), e);
+        // Load and initialize our instance if necessary
+        if (instance == null || !instanceInitialized) {
+            synchronized (this) {
+                if (instance == null) {
+                    try {
+                        if (log.isDebugEnabled()) {
+                            log.debug("Allocating non-STM instance");
                         }
-                    }
-                    if (!instanceInitialized) {
-                        initServlet(instance);
-                    }
-                }
-            }
-
-            if (singleThreadModel) {
-                if (newInstance) {
-                    // Have to do this outside of the sync above to prevent a
-                    // possible deadlock
-                    synchronized (instancePool) {
-                        instancePool.push(instance);
-                        nInstances++;
-                    }
-                }
-            } else {
-                if (log.isTraceEnabled()) {
-                    log.trace("  Returning non-STM instance");
-                }
-                // For new instances, count will have been incremented at the
-                // time of creation
-                if (!newInstance) {
-                    countAllocated.incrementAndGet();
-                }
-                return instance;
-            }
-        }
 
-        synchronized (instancePool) {
-            while (countAllocated.get() >= nInstances) {
-                // Allocate a new instance if possible, or else wait
-                if (nInstances < maxInstances) {
-                    try {
-                        instancePool.push(loadServlet());
-                        nInstances++;
+                        // Note: We don't know if the Servlet implements
+                        // SingleThreadModel until we have loaded it.
+                        instance = loadServlet();
+                        newInstance = true;
+                        // Increment here to prevent a race condition
+                        // with unload. Bug 43683, test case #3
+                        countAllocated.incrementAndGet();
                     } catch (ServletException e) {
                         throw e;
                     } catch (Throwable e) {
                         ExceptionUtils.handleThrowable(e);
                         throw new ServletException(sm.getString("standardWrapper.allocate"), e);
                     }
-                } else {
-                    try {
-                        instancePool.wait();
-                    } catch (InterruptedException e) {
-                        // Ignore
-                    }
+                }
+                if (!instanceInitialized) {
+                    initServlet(instance);
                 }
             }
-            if (log.isTraceEnabled()) {
-                log.trace("  Returning allocated STM instance");
-            }
+        }
+
+        if (log.isTraceEnabled()) {
+            log.trace("  Returning non-STM instance");
+        }
+        // For new instances, count will have been incremented at the
+        // time of creation
+        if (!newInstance) {
             countAllocated.incrementAndGet();
-            return instancePool.pop();
         }
+        return instance;
     }
 
 
     /**
-     * Return this previously allocated servlet to the pool of available
-     * instances.  If this servlet class does not implement SingleThreadModel,
-     * no action is actually required.
+     * Decrement the allocation count for this servlet.
      *
      * @param servlet The servlet to be returned
      *
@@ -847,20 +723,7 @@ public class StandardWrapper extends ContainerBase
      */
     @Override
     public void deallocate(Servlet servlet) throws ServletException {
-
-        // If not SingleThreadModel, no action is required
-        if (!singleThreadModel) {
-            countAllocated.decrementAndGet();
-            return;
-        }
-
-        // Unlock and free this instance
-        synchronized (instancePool) {
-            countAllocated.decrementAndGet();
-            instancePool.push(servlet);
-            instancePool.notify();
-        }
-
+        countAllocated.decrementAndGet();
     }
 
 
@@ -1015,16 +878,17 @@ public class StandardWrapper extends ContainerBase
 
     /**
      * Load and initialize an instance of this servlet, if there is not already
-     * at least one initialized instance.  This can be used, for example, to
-     * load servlets that are marked in the deployment descriptor to be loaded
-     * at server startup time.
+     * an initialized instance.  This can be used, for example, to load servlets
+     * that are marked in the deployment descriptor to be loaded at server
+     * startup time.
+     *
      * @return the loaded Servlet instance
      * @throws ServletException for a Servlet load error
      */
     public synchronized Servlet loadServlet() throws ServletException {
 
         // Nothing to do if we already have an instance or an instance pool
-        if (!singleThreadModel && (instance != null)) {
+        if (instance != null) {
             return instance;
         }
 
@@ -1110,7 +974,7 @@ public class StandardWrapper extends ContainerBase
     private synchronized void initServlet(Servlet servlet)
             throws ServletException {
 
-        if (instanceInitialized && !singleThreadModel) {
+        if (instanceInitialized) {
             return;
         }
 
@@ -1253,7 +1117,7 @@ public class StandardWrapper extends ContainerBase
     public synchronized void unload() throws ServletException {
 
         // Nothing to do if we have never loaded the instance
-        if (!singleThreadModel && (instance == null)) {
+        if (instance == null) {
             return;
         }
         unloading = true;
@@ -1300,8 +1164,6 @@ public class StandardWrapper extends ContainerBase
                 t = ExceptionUtils.unwrapInvocationTargetException(t);
                 ExceptionUtils.handleThrowable(t);
                 instance = null;
-                instancePool = null;
-                nInstances = 0;
                 fireContainerEvent("unload", this);
                 unloading = false;
                 throw new ServletException
@@ -1339,44 +1201,8 @@ public class StandardWrapper extends ContainerBase
             Registry.getRegistry(null, null).unregisterComponent(jspMonitorON);
         }
 
-        if (singleThreadModel && (instancePool != null)) {
-            try {
-                while (!instancePool.isEmpty()) {
-                    Servlet s = instancePool.pop();
-                    if (Globals.IS_SECURITY_ENABLED) {
-                        try {
-                            SecurityUtil.doAsPrivilege("destroy", s);
-                        } finally {
-                            SecurityUtil.remove(s);
-                        }
-                    } else {
-                        s.destroy();
-                    }
-                    // Annotation processing
-                    if (!((Context) getParent()).getIgnoreAnnotations()) {
-                       ((StandardContext)getParent()).getInstanceManager().destroyInstance(s);
-                    }
-                }
-            } catch (Throwable t) {
-                t = ExceptionUtils.unwrapInvocationTargetException(t);
-                ExceptionUtils.handleThrowable(t);
-                instancePool = null;
-                nInstances = 0;
-                unloading = false;
-                fireContainerEvent("unload", this);
-                throw new ServletException
-                    (sm.getString("standardWrapper.destroyException",
-                                  getName()), t);
-            }
-            instancePool = null;
-            nInstances = 0;
-        }
-
-        singleThreadModel = false;
-
         unloading = false;
         fireContainerEvent("unload", this);
-
     }
 
 
diff --git a/java/org/apache/catalina/core/mbeans-descriptors.xml b/java/org/apache/catalina/core/mbeans-descriptors.xml
index 5a706a6..45af1f9 100644
--- a/java/org/apache/catalina/core/mbeans-descriptors.xml
+++ b/java/org/apache/catalina/core/mbeans-descriptors.xml
@@ -1630,12 +1630,6 @@
                type="java.lang.String"
                writeable="false" />
 
-    <attribute name="singleThreadModel"
-               description="Does this servlet implement the SingleThreadModel interface?"
-               type="java.lang.Boolean"
-               is="true"
-               writeable="false" />
-
     <attribute name="stateName"
                description="The name of the LifecycleState that this component is currently in"
                type="java.lang.String"
diff --git a/java/org/apache/catalina/startup/Tomcat.java b/java/org/apache/catalina/startup/Tomcat.java
index cdae77d..ac18e65 100644
--- a/java/org/apache/catalina/startup/Tomcat.java
+++ b/java/org/apache/catalina/startup/Tomcat.java
@@ -1215,22 +1215,11 @@ public class Tomcat {
 
         @Override
         public synchronized Servlet loadServlet() throws ServletException {
-            if (singleThreadModel) {
-                Servlet instance;
-                try {
-                    instance = existing.getClass().getConstructor().newInstance();
-                } catch (ReflectiveOperationException e) {
-                    throw new ServletException(e);
-                }
-                instance.init(facade);
-                return instance;
-            } else {
-                if (!instanceInitialized) {
-                    existing.init(facade);
-                    instanceInitialized = true;
-                }
-                return existing;
+            if (!instanceInitialized) {
+                existing.init(facade);
+                instanceInitialized = true;
             }
+            return existing;
         }
         @Override
         public long getAvailable() {

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