From f5b27254cadcd904c5a119979c6afab2f78aeb5d Mon Sep 17 00:00:00 2001 From: Mikolaj Izdebski Date: Thu, 26 Sep 2024 22:08:50 +0200 Subject: [PATCH] Fix potential resource leak warnings --- .../xmvn/deployer/BasicDeployerTest.java | 5 +- .../impl/ArtifactInstallerFactory.java | 55 ++++++++++--------- 2 files changed, 32 insertions(+), 28 deletions(-) diff --git a/xmvn-core/src/test/java/org/fedoraproject/xmvn/deployer/BasicDeployerTest.java b/xmvn-core/src/test/java/org/fedoraproject/xmvn/deployer/BasicDeployerTest.java index b0ec7575..5a4ddbe0 100644 --- a/xmvn-core/src/test/java/org/fedoraproject/xmvn/deployer/BasicDeployerTest.java +++ b/xmvn-core/src/test/java/org/fedoraproject/xmvn/deployer/BasicDeployerTest.java @@ -30,6 +30,7 @@ import java.util.Collections; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Stream; import org.junit.jupiter.api.Test; import org.xmlunit.assertj3.XmlAssert; @@ -146,9 +147,9 @@ public void testReadError() private boolean runningAsRoot() { - try + try ( Stream lines = Files.lines( Paths.get( "/proc/self/status" ) ) ) { - return Files.lines( Paths.get( "/proc/self/status" ) ).map( s -> + return lines.map( s -> { Matcher matcher = PROCESS_UID_PATTERN.matcher( s ); diff --git a/xmvn-tools/xmvn-install/src/main/java/org/fedoraproject/xmvn/tools/install/impl/ArtifactInstallerFactory.java b/xmvn-tools/xmvn-install/src/main/java/org/fedoraproject/xmvn/tools/install/impl/ArtifactInstallerFactory.java index 95bb9e26..bda1e335 100644 --- a/xmvn-tools/xmvn-install/src/main/java/org/fedoraproject/xmvn/tools/install/impl/ArtifactInstallerFactory.java +++ b/xmvn-tools/xmvn-install/src/main/java/org/fedoraproject/xmvn/tools/install/impl/ArtifactInstallerFactory.java @@ -57,38 +57,41 @@ private ArtifactInstaller tryLoadPlugin( String type ) return cachedPluginsByType.get( type ); } - String resourceName = ArtifactInstaller.class.getCanonicalName() + "/" + type; - try ( InputStream resourceStream = - pluginRealm != null ? pluginRealm.getResourceAsStream( resourceName ) : null ) + if ( pluginRealm != null ) { - if ( resourceStream == null ) + String resourceName = ArtifactInstaller.class.getCanonicalName() + "/" + type; + try ( InputStream resourceStream = pluginRealm.getResourceAsStream( resourceName ) ) { - logger.debug( "No XMvn Installer plugin found for packaging type {}", type ); - cachedPluginsByType.put( type, null ); - return null; + if ( resourceStream != null ) + { + String pluginImplClass; + try ( BufferedReader resourceReader = + new BufferedReader( new InputStreamReader( resourceStream ) ) ) + { + pluginImplClass = resourceReader.readLine(); + } + + ArtifactInstaller pluggedInInstaller = cachedPluginsByImplClass.get( pluginImplClass ); + if ( pluggedInInstaller == null ) + { + pluggedInInstaller = + (ArtifactInstaller) pluginRealm.loadClass( pluginImplClass ).getConstructor().newInstance(); + cachedPluginsByImplClass.put( pluginImplClass, pluggedInInstaller ); + } + + cachedPluginsByType.put( type, pluggedInInstaller ); + return pluggedInInstaller; + } } - - String pluginImplClass; - try ( BufferedReader resourceReader = new BufferedReader( new InputStreamReader( resourceStream ) ) ) - { - pluginImplClass = resourceReader.readLine(); - } - - ArtifactInstaller pluggedInInstaller = cachedPluginsByImplClass.get( pluginImplClass ); - if ( pluggedInInstaller == null ) + catch ( IOException | ReflectiveOperationException e ) { - pluggedInInstaller = - (ArtifactInstaller) pluginRealm.loadClass( pluginImplClass ).getConstructor().newInstance(); - cachedPluginsByImplClass.put( pluginImplClass, pluggedInInstaller ); + throw new RuntimeException( "Unable to load XMvn Installer plugin for packaging type " + type, e ); } - - cachedPluginsByType.put( type, pluggedInInstaller ); - return pluggedInInstaller; - } - catch ( IOException | ReflectiveOperationException e ) - { - throw new RuntimeException( "Unable to load XMvn Installer plugin for packaging type " + type, e ); } + + logger.debug( "No XMvn Installer plugin found for packaging type {}", type ); + cachedPluginsByType.put( type, null ); + return null; } public ArtifactInstallerFactory( Configurator configurator )