Skip to content

Commit

Permalink
Fix potential resource leak warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
mizdebsk committed Sep 26, 2024
1 parent e30f2f8 commit f5b2725
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -146,9 +147,9 @@ public void testReadError()

private boolean runningAsRoot()
{
try
try ( Stream<String> 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 );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down

0 comments on commit f5b2725

Please sign in to comment.