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

orudyy pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/qpid-broker-j.git


The following commit(s) were added to refs/heads/main by this push:
     new f6f79d1  QPID-8550 - [Broker-J] Use try-resources even when 
streams/readers are closed in invoked API
f6f79d1 is described below

commit f6f79d11fe51b1323173fb20e2f13fb06ec3eeaa
Author: dakirily <daniel.kiril...@gmail.com>
AuthorDate: Tue Jul 20 12:45:14 2021 +0200

    QPID-8550 - [Broker-J] Use try-resources even when streams/readers are 
closed in invoked API
    
    This closes #103
---
 .../qpid/server/store/JsonFileConfigStore.java      | 17 ++++++++++-------
 .../java/org/apache/qpid/server/util/FileUtils.java | 12 +++++++-----
 .../virtualhostnode/AbstractVirtualHostNode.java    | 21 +++++++++++----------
 .../config/JavaScriptConfigEvaluator.java           |  9 +++++----
 .../org/apache/qpid/test/utils/TestFileUtils.java   |  6 ++----
 .../org/apache/qpid/tests/http/HttpTestHelper.java  |  7 +++++--
 .../org/apache/qpid/tools/RestStressTestClient.java |  9 +++++----
 7 files changed, 45 insertions(+), 36 deletions(-)

diff --git 
a/broker-core/src/main/java/org/apache/qpid/server/store/JsonFileConfigStore.java
 
b/broker-core/src/main/java/org/apache/qpid/server/store/JsonFileConfigStore.java
index f394b11..c68ca00 100644
--- 
a/broker-core/src/main/java/org/apache/qpid/server/store/JsonFileConfigStore.java
+++ 
b/broker-core/src/main/java/org/apache/qpid/server/store/JsonFileConfigStore.java
@@ -156,18 +156,21 @@ public class JsonFileConfigStore extends 
AbstractJsonFileStore implements Durabl
 
             boolean updated = false;
             Collection<ConfiguredObjectRecord> records = 
Collections.emptyList();
-            ConfiguredObjectRecordConverter configuredObjectRecordConverter =
+            final ConfiguredObjectRecordConverter 
configuredObjectRecordConverter =
                     new ConfiguredObjectRecordConverter(_parent.getModel());
 
-            records = configuredObjectRecordConverter.readFromJson(_rootClass, 
_parent, new FileReader(configFile));
+            try (FileReader configFileReader =  new FileReader(configFile))
+            {
+                records = 
configuredObjectRecordConverter.readFromJson(_rootClass, _parent, 
configFileReader);
+            }
 
-            if(_rootClass == null)
+            if (_rootClass == null)
             {
                 _rootClass = configuredObjectRecordConverter.getRootClass();
                 _classNameMapping = 
generateClassNameMap(configuredObjectRecordConverter.getModel(), _rootClass);
             }
 
-            if(records.isEmpty())
+            if (records.isEmpty())
             {
                 LOGGER.debug("File contains no records - using initial 
configuration");
                 records = Arrays.asList(initialRecords);
@@ -176,9 +179,9 @@ public class JsonFileConfigStore extends 
AbstractJsonFileStore implements Durabl
                 {
                     String containerTypeName = ((DynamicModel) 
_parent).getDefaultContainerType();
                     ConfiguredObjectRecord rootRecord = null;
-                    for(ConfiguredObjectRecord record : records)
+                    for (ConfiguredObjectRecord record : records)
                     {
-                        if(record.getParents() == null || 
record.getParents().isEmpty())
+                        if (record.getParents() == null || 
record.getParents().isEmpty())
                         {
                             rootRecord = record;
                             break;
@@ -189,7 +192,7 @@ public class JsonFileConfigStore extends 
AbstractJsonFileStore implements Durabl
                         containerTypeName = 
rootRecord.getAttributes().get(ConfiguredObject.TYPE).toString();
                     }
 
-                    QpidServiceLoader loader = new QpidServiceLoader();
+                    final QpidServiceLoader loader = new QpidServiceLoader();
                     final ContainerType<?> containerType =
                             
loader.getInstancesByType(ContainerType.class).get(containerTypeName);
 
diff --git 
a/broker-core/src/main/java/org/apache/qpid/server/util/FileUtils.java 
b/broker-core/src/main/java/org/apache/qpid/server/util/FileUtils.java
index f182339..f8124e7 100644
--- a/broker-core/src/main/java/org/apache/qpid/server/util/FileUtils.java
+++ b/broker-core/src/main/java/org/apache/qpid/server/util/FileUtils.java
@@ -60,7 +60,7 @@ public class FileUtils
     public static byte[] readFileAsBytes(String filename)
     {
 
-        try(BufferedInputStream is = new BufferedInputStream(new 
FileInputStream(filename)))
+        try (BufferedInputStream is = new BufferedInputStream(new 
FileInputStream(filename)))
         {
             return readStreamAsString(is);
         }
@@ -92,7 +92,7 @@ public class FileUtils
      */
     public static String readFileAsString(File file)
     {
-        try(BufferedInputStream is = new BufferedInputStream(new 
FileInputStream(file)))
+        try (BufferedInputStream is = new BufferedInputStream(new 
FileInputStream(file)))
         {
 
             return new String(readStreamAsString(is));
@@ -113,7 +113,7 @@ public class FileUtils
      */
     private static byte[] readStreamAsString(BufferedInputStream is)
     {
-        try(ByteArrayOutputStream inBuffer = new ByteArrayOutputStream())
+        try (ByteArrayOutputStream inBuffer = new ByteArrayOutputStream())
         {
             byte[] data = new byte[4096];
 
@@ -205,8 +205,10 @@ public class FileUtils
      */
     public static void copyCheckedEx(File src, File dst) throws IOException
     {
-        InputStream in = new FileInputStream(src);
-        copy(in, dst);
+        try (InputStream in = new FileInputStream(src))
+        {
+            copy(in, dst);
+        }
     }
 
     /**
diff --git 
a/broker-core/src/main/java/org/apache/qpid/server/virtualhostnode/AbstractVirtualHostNode.java
 
b/broker-core/src/main/java/org/apache/qpid/server/virtualhostnode/AbstractVirtualHostNode.java
index b9a4b15..382f67e 100644
--- 
a/broker-core/src/main/java/org/apache/qpid/server/virtualhostnode/AbstractVirtualHostNode.java
+++ 
b/broker-core/src/main/java/org/apache/qpid/server/virtualhostnode/AbstractVirtualHostNode.java
@@ -430,23 +430,25 @@ public abstract class AbstractVirtualHostNode<X extends 
AbstractVirtualHostNode<
 
     protected final ConfiguredObjectRecord[] getInitialRecords() throws 
IOException
     {
-        ConfiguredObjectRecordConverter converter = new 
ConfiguredObjectRecordConverter(getModel());
+        final ConfiguredObjectRecordConverter converter = new 
ConfiguredObjectRecordConverter(getModel());
 
-        Collection<ConfiguredObjectRecord> records =
-                new 
ArrayList<>(converter.readFromJson(VirtualHost.class,this,getInitialConfigReader()));
-
-        if(!records.isEmpty())
+        final Collection<ConfiguredObjectRecord> records;
+        try (final Reader initialConfigReader = getInitialConfigReader())
+        {
+            records = new 
ArrayList<>(converter.readFromJson(VirtualHost.class, this, 
initialConfigReader));
+        }
+        if (!records.isEmpty())
         {
             ConfiguredObjectRecord vhostRecord = null;
-            for(ConfiguredObjectRecord record : records)
+            for (ConfiguredObjectRecord record : records)
             {
-                if(record.getType().equals(VirtualHost.class.getSimpleName()))
+                if (record.getType().equals(VirtualHost.class.getSimpleName()))
                 {
                     vhostRecord = record;
                     break;
                 }
             }
-            if(vhostRecord != null)
+            if (vhostRecord != null)
             {
                 records.remove(vhostRecord);
                 vhostRecord = enrichInitialVirtualHostRootRecord(vhostRecord);
@@ -456,13 +458,12 @@ public abstract class AbstractVirtualHostNode<X extends 
AbstractVirtualHostNode<
             {
                 // this should be impossible as the converter should always 
generate a parent record
                 throw new IllegalConfigurationException("Somehow the initial 
configuration has records but "
-                                                        + "not a VirtualHost. 
This must be a coding error in Qpid");
+                    + "not a VirtualHost. This must be a coding error in 
Qpid");
             }
             addStandardExchangesIfNecessary(records, vhostRecord);
             enrichWithAuditInformation(records);
         }
 
-
         return records.toArray(new ConfiguredObjectRecord[records.size()]);
     }
 
diff --git 
a/perftests/src/main/java/org/apache/qpid/disttest/controller/config/JavaScriptConfigEvaluator.java
 
b/perftests/src/main/java/org/apache/qpid/disttest/controller/config/JavaScriptConfigEvaluator.java
index d760ffe..1cfb43f 100644
--- 
a/perftests/src/main/java/org/apache/qpid/disttest/controller/config/JavaScriptConfigEvaluator.java
+++ 
b/perftests/src/main/java/org/apache/qpid/disttest/controller/config/JavaScriptConfigEvaluator.java
@@ -65,14 +65,15 @@ public class JavaScriptConfigEvaluator
     {
         ScriptEngineManager mgr = new ScriptEngineManager();
         ScriptEngine engine = mgr.getEngineByName("JavaScript");
-        try
+        try (Reader json2Reader = new 
InputStreamReader(getClass().getClassLoader().getResourceAsStream("json2.js"));
+             Reader testUtilsReader = new 
InputStreamReader(getClass().getClassLoader().getResourceAsStream("test-utils.js")))
         {
-            engine.eval(new 
InputStreamReader(getClass().getClassLoader().getResourceAsStream("json2.js")));
-            engine.eval(new 
InputStreamReader(getClass().getClassLoader().getResourceAsStream("test-utils.js")));
+            engine.eval(json2Reader);
+            engine.eval(testUtilsReader);
             engine.eval(fileReader);
             engine.eval("jsonString = JSON.stringify(" + 
TEST_CONFIG_VARIABLE_NAME + ")");
         }
-        catch (ScriptException e)
+        catch (IOException | ScriptException e)
         {
             throw new DistributedTestException("Exception while evaluating 
test config", e);
         }
diff --git 
a/qpid-test-utils/src/main/java/org/apache/qpid/test/utils/TestFileUtils.java 
b/qpid-test-utils/src/main/java/org/apache/qpid/test/utils/TestFileUtils.java
index 9371850..5cdfa5f 100644
--- 
a/qpid-test-utils/src/main/java/org/apache/qpid/test/utils/TestFileUtils.java
+++ 
b/qpid-test-utils/src/main/java/org/apache/qpid/test/utils/TestFileUtils.java
@@ -129,8 +129,7 @@ public class TestFileUtils
     public static File createTempFileFromResource(TestCase testCase, String 
resourceName)
     {
         File dst = createTempFile(testCase, resourceName);
-        InputStream in = testCase.getClass().getResourceAsStream(resourceName);
-        try
+        try (InputStream in = 
testCase.getClass().getResourceAsStream(resourceName))
         {
             copy(in, dst);
         }
@@ -146,8 +145,7 @@ public class TestFileUtils
     public static File createTempFileFromResource(UnitTestBase testCase, 
String resourceName)
     {
         File dst = createTempFile(testCase, resourceName);
-        InputStream in = testCase.getClass().getResourceAsStream(resourceName);
-        try
+        try (InputStream in = 
testCase.getClass().getResourceAsStream(resourceName))
         {
             copy(in, dst);
         }
diff --git 
a/systests/qpid-systests-http-management/src/main/java/org/apache/qpid/tests/http/HttpTestHelper.java
 
b/systests/qpid-systests-http-management/src/main/java/org/apache/qpid/tests/http/HttpTestHelper.java
index 0d46830..f438f64 100644
--- 
a/systests/qpid-systests-http-management/src/main/java/org/apache/qpid/tests/http/HttpTestHelper.java
+++ 
b/systests/qpid-systests-http-management/src/main/java/org/apache/qpid/tests/http/HttpTestHelper.java
@@ -25,6 +25,7 @@ import static java.nio.charset.StandardCharsets.UTF_8;
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
 import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
 import java.net.URL;
@@ -208,8 +209,10 @@ public class HttpTestHelper
 
     private void writeJsonRequest(HttpURLConnection connection, Object data) 
throws IOException
     {
-        ObjectMapper mapper = new ObjectMapper();
-        mapper.writeValue(connection.getOutputStream(), data);
+        try (OutputStream outputStream = connection.getOutputStream())
+        {
+            new ObjectMapper().writeValue(outputStream, data);
+        }
     }
 
     public Map<String, Object> getJsonAsSingletonList(String path) throws 
IOException
diff --git 
a/tools/src/main/java/org/apache/qpid/tools/RestStressTestClient.java 
b/tools/src/main/java/org/apache/qpid/tools/RestStressTestClient.java
index 7242a65..639cfc0 100644
--- a/tools/src/main/java/org/apache/qpid/tools/RestStressTestClient.java
+++ b/tools/src/main/java/org/apache/qpid/tools/RestStressTestClient.java
@@ -309,13 +309,13 @@ public class RestStressTestClient
         public int put(String restServiceUrl, Map<String, Object> attributes) 
throws IOException
         {
             HttpURLConnection connection = createConnection("PUT", 
restServiceUrl, _cookies);
-            try
+            try (OutputStream outputStream = connection.getOutputStream())
             {
                 connection.connect();
                 if (attributes != null)
                 {
                     ObjectMapper mapper = new ObjectMapper();
-                    mapper.writeValue(connection.getOutputStream(), 
attributes);
+                    mapper.writeValue(outputStream, attributes);
                 }
                 checkResponseCode(connection);
                 return connection.getResponseCode();
@@ -570,8 +570,9 @@ public class RestStressTestClient
             {
                 _cookies = null;
             }
-            InputStream is = connection.getInputStream();
-            try(ByteArrayOutputStream baos = new ByteArrayOutputStream())
+
+            try (InputStream is = connection.getInputStream();
+                 ByteArrayOutputStream baos = new ByteArrayOutputStream())
             {
                 byte[] buffer = new byte[1024];
                 int len;

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@qpid.apache.org
For additional commands, e-mail: commits-h...@qpid.apache.org

Reply via email to