From 8b4c8f56c5589546e3ebf30268d0239721ff8e16 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Wed, 20 Sep 2023 13:45:23 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20CSV=E9=9A=94=E7=A6=BB=E5=8A=A0=E8=BD=BD=E7=9A=84=E5=90=8C?= =?UTF-8?q?=E6=97=B6=E5=85=BC=E5=AE=B9CSVRead=E5=87=BD=E6=95=B0=E5=8F=96?= =?UTF-8?q?=E5=80=BC=20#26821?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: fit2-zhao --- .../apache/jmeter/services/FileServer.java | 55 ++++++++++++------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/framework/sdk-parent/jmeter/src/main/java/org/apache/jmeter/services/FileServer.java b/framework/sdk-parent/jmeter/src/main/java/org/apache/jmeter/services/FileServer.java index 53c6359423..e321c3c69e 100644 --- a/framework/sdk-parent/jmeter/src/main/java/org/apache/jmeter/services/FileServer.java +++ b/framework/sdk-parent/jmeter/src/main/java/org/apache/jmeter/services/FileServer.java @@ -236,6 +236,10 @@ public class FileServer { reserveFile(filename, charsetName, alias, false); } + private boolean hasThreadContext() { + return JMeterContextService.getContext() != null && JMeterContextService.getContext().getThread() != null; + } + /** * Creates an association between a filename and a File inputOutputObject, * and stores it for later use - unless it is already stored. @@ -259,10 +263,11 @@ public class FileServer { log.error("file does not exist [ " + filename + " ]"); return ""; } - - String threadName = JMeterContextService.getContext().getThread().getThreadName(); - if (!StringUtils.contains(alias, threadName)) { - alias = StringUtils.join(threadName, alias); + if (hasThreadContext()) { + String threadName = JMeterContextService.getContext().getThread().getThreadName(); + if (!StringUtils.contains(alias, threadName)) { + alias = StringUtils.join(threadName, alias); + } } FileEntry fileEntry = files.get(alias); if (fileEntry == null) { @@ -342,11 +347,12 @@ public class FileServer { */ public synchronized String readLine(String filename, boolean recycle, boolean ignoreFirstLine) throws IOException { - String threadName = JMeterContextService.getContext().getThread().getThreadName(); - if (!StringUtils.contains(filename, threadName)) { - filename = StringUtils.join(threadName, filename); + if (hasThreadContext()) { + String threadName = JMeterContextService.getContext().getThread().getThreadName(); + if (!StringUtils.contains(filename, threadName)) { + filename = StringUtils.join(threadName, filename); + } } - FileEntry fileEntry = files.get(filename); if (fileEntry != null) { if (fileEntry.inputOutputObject == null) { @@ -398,9 +404,11 @@ public class FileServer { * @return {@link BufferedReader} */ private BufferedReader getReader(String alias, boolean recycle, boolean ignoreFirstLine) throws IOException { - String threadName = JMeterContextService.getContext().getThread().getThreadName(); - if (!StringUtils.contains(alias, threadName)) { - alias = StringUtils.join(threadName, alias); + if (hasThreadContext()) { + String threadName = JMeterContextService.getContext().getThread().getThreadName(); + if (!StringUtils.contains(alias, threadName)) { + alias = StringUtils.join(threadName, alias); + } } FileEntry fileEntry = files.get(alias); @@ -459,9 +467,11 @@ public class FileServer { } public synchronized void write(String filename, String value) throws IOException { - String threadName = JMeterContextService.getContext().getThread().getThreadName(); - if (!StringUtils.contains(filename, threadName)) { - filename = StringUtils.join(threadName, filename); + if (hasThreadContext()) { + String threadName = JMeterContextService.getContext().getThread().getThreadName(); + if (!StringUtils.contains(filename, threadName)) { + filename = StringUtils.join(threadName, filename); + } } FileEntry fileEntry = files.get(filename); if (fileEntry != null) { @@ -519,11 +529,12 @@ public class FileServer { * @throws IOException when closing of the aliased file fails */ public synchronized void closeFile(String name) throws IOException { - String threadName = JMeterContextService.getContext().getThread().getThreadName(); - if (!StringUtils.contains(name, threadName)) { - name = StringUtils.join(threadName, name); + if (hasThreadContext()) { + String threadName = JMeterContextService.getContext().getThread().getThreadName(); + if (!StringUtils.contains(name, threadName)) { + name = StringUtils.join(threadName, name); + } } - FileEntry fileEntry = files.get(name); closeFile(name, fileEntry); } @@ -580,9 +591,11 @@ public class FileServer { // If path is absolute, then File constructor will simply return it String alias = path; - String threadName = JMeterContextService.getContext().getThread().getThreadName(); - if (!StringUtils.contains(alias, threadName)) { - alias = StringUtils.join(threadName, path); + if (hasThreadContext()) { + String threadName = JMeterContextService.getContext().getThread().getThreadName(); + if (!StringUtils.contains(alias, threadName)) { + alias = StringUtils.join(threadName, path); + } } FileEntry fileEntry = files.containsKey(alias) ? files.get(alias) : files.get(path); return fileEntry != null ? fileEntry.file : null;