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 2022/09/15 18:42:06 UTC
[tomcat] branch 10.0.x updated: Refactor to replace Stack with ArrayDeque / ConcurrentLinkedQueue
This is an automated email from the ASF dual-hosted git repository.
markt pushed a commit to branch 10.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/10.0.x by this push:
new 090ea15ad9 Refactor to replace Stack with ArrayDeque / ConcurrentLinkedQueue
090ea15ad9 is described below
commit 090ea15ad96f686894f09812ffe5137b7c486374
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Thu Sep 15 19:41:45 2022 +0100
Refactor to replace Stack with ArrayDeque / ConcurrentLinkedQueue
---
java/org/apache/catalina/core/StandardContext.java | 11 +++++-----
.../apache/catalina/servlets/WebdavServlet.java | 15 +++++++-------
.../apache/jasper/compiler/ParserController.java | 15 +++++++-------
.../apache/tomcat/util/log/SystemLogHandler.java | 24 ++++++++++++----------
4 files changed, 34 insertions(+), 31 deletions(-)
diff --git a/java/org/apache/catalina/core/StandardContext.java b/java/org/apache/catalina/core/StandardContext.java
index fa429303c0..15a1135427 100644
--- a/java/org/apache/catalina/core/StandardContext.java
+++ b/java/org/apache/catalina/core/StandardContext.java
@@ -24,6 +24,7 @@ import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.security.AccessController;
import java.security.PrivilegedAction;
+import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -36,8 +37,8 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
+import java.util.Queue;
import java.util.Set;
-import java.util.Stack;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
@@ -5872,14 +5873,14 @@ public class StandardContext extends ContainerBase
if (parent == null) {
namingContextName = getName();
} else {
- Stack<String> stk = new Stack<>();
+ Queue<String> stk = new ArrayDeque<>();
StringBuilder buff = new StringBuilder();
while (parent != null) {
- stk.push(parent.getName());
+ stk.add(parent.getName());
parent = parent.getParent();
}
- while (!stk.empty()) {
- buff.append("/" + stk.pop());
+ while (!stk.isEmpty()) {
+ buff.append("/" + stk.remove());
}
buff.append(getName());
namingContextName = buff.toString();
diff --git a/java/org/apache/catalina/servlets/WebdavServlet.java b/java/org/apache/catalina/servlets/WebdavServlet.java
index 0a5f401399..7b9e7f84fd 100644
--- a/java/org/apache/catalina/servlets/WebdavServlet.java
+++ b/java/org/apache/catalina/servlets/WebdavServlet.java
@@ -25,6 +25,7 @@ import java.io.Writer;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
+import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
@@ -33,7 +34,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
-import java.util.Stack;
+import java.util.Queue;
import java.util.TimeZone;
import java.util.concurrent.ConcurrentHashMap;
@@ -617,15 +618,15 @@ public class WebdavServlet extends DefaultServlet {
parseProperties(req, generatedXML, path, type, properties);
} else {
// The stack always contains the object of the current level
- Stack<String> stack = new Stack<>();
- stack.push(path);
+ Queue<String> stack = new ArrayDeque<>();
+ stack.add(path);
// Stack of the objects one level below
- Stack<String> stackBelow = new Stack<>();
+ Queue<String> stackBelow = new ArrayDeque<>();
while ((!stack.isEmpty()) && (depth >= 0)) {
- String currentPath = stack.pop();
+ String currentPath = stack.remove();
parseProperties(req, generatedXML, currentPath, type, properties);
resource = resources.getResource(currentPath);
@@ -639,7 +640,7 @@ public class WebdavServlet extends DefaultServlet {
newPath += "/";
}
newPath += entry;
- stackBelow.push(newPath);
+ stackBelow.add(newPath);
}
// Displaying the lock-null resources present in that
@@ -659,7 +660,7 @@ public class WebdavServlet extends DefaultServlet {
if (stack.isEmpty()) {
depth--;
stack = stackBelow;
- stackBelow = new Stack<>();
+ stackBelow = new ArrayDeque<>();
}
generatedXML.sendData();
diff --git a/java/org/apache/jasper/compiler/ParserController.java b/java/org/apache/jasper/compiler/ParserController.java
index 376df38707..362e5ae2f4 100644
--- a/java/org/apache/jasper/compiler/ParserController.java
+++ b/java/org/apache/jasper/compiler/ParserController.java
@@ -20,7 +20,8 @@ import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
-import java.util.Stack;
+import java.util.ArrayDeque;
+import java.util.Queue;
import org.apache.jasper.JasperException;
import org.apache.jasper.JspCompilationContext;
@@ -55,7 +56,7 @@ class ParserController implements TagConstants {
* A stack to keep track of the 'current base directory'
* for include directives that refer to relative paths.
*/
- private final Stack<String> baseDirStack = new Stack<>();
+ private final Queue<String> baseDirStack = new ArrayDeque<>();
private boolean isEncodingSpecifiedInProlog;
private boolean isBomPresent;
@@ -247,7 +248,7 @@ class ParserController implements TagConstants {
}
}
- baseDirStack.pop();
+ baseDirStack.remove();
return parsedPage;
}
@@ -518,11 +519,9 @@ class ParserController implements TagConstants {
private String resolveFileName(String inFileName) {
String fileName = inFileName.replace('\\', '/');
boolean isAbsolute = fileName.startsWith("/");
- fileName = isAbsolute ? fileName
- : baseDirStack.peek() + fileName;
- String baseDir =
- fileName.substring(0, fileName.lastIndexOf('/') + 1);
- baseDirStack.push(baseDir);
+ fileName = isAbsolute ? fileName : baseDirStack.peek() + fileName;
+ String baseDir = fileName.substring(0, fileName.lastIndexOf('/') + 1);
+ baseDirStack.add(baseDir);
return fileName;
}
diff --git a/java/org/apache/tomcat/util/log/SystemLogHandler.java b/java/org/apache/tomcat/util/log/SystemLogHandler.java
index b1af0b0468..746ae4ffb0 100644
--- a/java/org/apache/tomcat/util/log/SystemLogHandler.java
+++ b/java/org/apache/tomcat/util/log/SystemLogHandler.java
@@ -18,8 +18,10 @@ package org.apache.tomcat.util.log;
import java.io.IOException;
import java.io.PrintStream;
+import java.util.ArrayDeque;
import java.util.EmptyStackException;
-import java.util.Stack;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentLinkedQueue;
/**
* This helper class may be used to do sophisticated redirection of
@@ -60,13 +62,13 @@ public class SystemLogHandler extends PrintStream {
/**
* Thread <-> CaptureLog associations.
*/
- private static final ThreadLocal<Stack<CaptureLog>> logs = new ThreadLocal<>();
+ private static final ThreadLocal<Queue<CaptureLog>> logs = new ThreadLocal<>();
/**
* Spare CaptureLog ready for reuse.
*/
- private static final Stack<CaptureLog> reuse = new Stack<>();
+ private static final Queue<CaptureLog> reuse = new ConcurrentLinkedQueue<>();
// --------------------------------------------------------- Public Methods
@@ -79,19 +81,19 @@ public class SystemLogHandler extends PrintStream {
CaptureLog log = null;
if (!reuse.isEmpty()) {
try {
- log = reuse.pop();
+ log = reuse.remove();
} catch (EmptyStackException e) {
log = new CaptureLog();
}
} else {
log = new CaptureLog();
}
- Stack<CaptureLog> stack = logs.get();
+ Queue<CaptureLog> stack = logs.get();
if (stack == null) {
- stack = new Stack<>();
+ stack = new ArrayDeque<>();
logs.set(stack);
}
- stack.push(log);
+ stack.add(log);
}
@@ -101,17 +103,17 @@ public class SystemLogHandler extends PrintStream {
* @return The captured data
*/
public static String stopCapture() {
- Stack<CaptureLog> stack = logs.get();
+ Queue<CaptureLog> stack = logs.get();
if (stack == null || stack.isEmpty()) {
return null;
}
- CaptureLog log = stack.pop();
+ CaptureLog log = stack.remove();
if (log == null) {
return null;
}
String capture = log.getCapture();
log.reset();
- reuse.push(log);
+ reuse.add(log);
return capture;
}
@@ -124,7 +126,7 @@ public class SystemLogHandler extends PrintStream {
* @return the print stream
*/
protected PrintStream findStream() {
- Stack<CaptureLog> stack = logs.get();
+ Queue<CaptureLog> stack = logs.get();
if (stack != null && !stack.isEmpty()) {
CaptureLog log = stack.peek();
if (log != null) {
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org