From 61a97fc6985050a1c9140010625561b61ea13d5f Mon Sep 17 00:00:00 2001 From: AmilaSamith Date: Mon, 11 Dec 2023 12:18:17 +0530 Subject: [PATCH] Fix to close input streams explicitly This fix introduces a try-with-resource block which automatically closes the resources by eliminating the need to call the close() method in the finally block explicitly. resolve : wso2/micro-integrator#3041 --- .../registry/MicroIntegratorRegistry.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/components/mediation/registry/org.wso2.micro.integrator.registry/src/main/java/org/wso2/micro/integrator/registry/MicroIntegratorRegistry.java b/components/mediation/registry/org.wso2.micro.integrator.registry/src/main/java/org/wso2/micro/integrator/registry/MicroIntegratorRegistry.java index 316c34a74b..adf2f3de92 100644 --- a/components/mediation/registry/org.wso2.micro.integrator.registry/src/main/java/org/wso2/micro/integrator/registry/MicroIntegratorRegistry.java +++ b/components/mediation/registry/org.wso2.micro.integrator.registry/src/main/java/org/wso2/micro/integrator/registry/MicroIntegratorRegistry.java @@ -299,8 +299,8 @@ private boolean lookupUtil(String key, URL url) { if ("file".equals(url.getProtocol())) { try { if (new File(url.toURI()).exists()) { - try { - url.openStream(); + try (InputStream inputStream = url.openStream()) { + // This try-with-resources block is used to check whether the resource is accessible } catch (IOException e) { log.error("Error occurred while accessing registry resource: " + key, e); return true; @@ -415,8 +415,8 @@ public RegistryEntry getRegistryEntry(String key) { try { URL url = new URL(resolveRegistryURI(key)); if ("file".equals(url.getProtocol())) { - try { - url.openStream(); + try (InputStream inputStream = url.openStream()) { + // This try-with-resources block is used to check whether the resource is accessible } catch (IOException e) { log.error("Error occurred while accessing registry resource: " + key, e); return null; @@ -1133,8 +1133,8 @@ private void addConfigProperty(String name, String value) { registryType = MicroIntegratorRegistryConstants.LOCAL_HOST_REGISTRY; //Check existence of the target location - try { - rootPathUrl.openStream(); + try (InputStream inputStream = rootPathUrl.openStream()) { + // This try-with-resources block is used to check whether the resource is accessible } catch (IOException e) { // If the registry is filesystem based, user may have provided the URI relative to the CARBON_HOME if (log.isDebugEnabled()) { @@ -1147,8 +1147,7 @@ private void addConfigProperty(String name, String value) { } pathFromCarbonHome = rootPathUrl.getProtocol() + ":" + pathFromCarbonHome + value; rootPathUrl = new URL(pathFromCarbonHome); - try { - rootPathUrl.openStream(); + try (InputStream inputStream = rootPathUrl.openStream()) { value = pathFromCarbonHome; } catch (IOException e1) { //Unable to open input stream to target location