[MNG-6168] Fix unclosed streams

This commit is contained in:
Christian Schulte 2017-03-24 22:04:37 +01:00
parent f0535a40e2
commit 0931bb2cc7
8 changed files with 60 additions and 133 deletions

View File

@ -29,7 +29,6 @@ import org.apache.commons.lang3.Validate;
import org.apache.maven.artifact.repository.metadata.Metadata;
import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.ReaderFactory;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
@ -58,19 +57,14 @@ public class DefaultMetadataReader
{
Validate.notNull( input, "input cannot be null" );
try
try ( final Reader in = input )
{
MetadataXpp3Reader r = new MetadataXpp3Reader();
return r.read( input, isStrict( options ) );
return new MetadataXpp3Reader().read( in, isStrict( options ) );
}
catch ( XmlPullParserException e )
{
throw new MetadataParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e );
}
finally
{
IOUtil.close( input );
}
}
public Metadata read( InputStream input, Map<String, ?> options )
@ -78,19 +72,14 @@ public class DefaultMetadataReader
{
Validate.notNull( input, "input cannot be null" );
try
try ( final InputStream in = input )
{
MetadataXpp3Reader r = new MetadataXpp3Reader();
return r.read( input, isStrict( options ) );
return new MetadataXpp3Reader().read( in, isStrict( options ) );
}
catch ( XmlPullParserException e )
{
throw new MetadataParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e );
}
finally
{
IOUtil.close( input );
}
}
private boolean isStrict( Map<String, ?> options )

View File

@ -31,7 +31,6 @@ import javax.inject.Singleton;
import org.apache.commons.lang3.Validate;
import org.apache.maven.toolchain.model.PersistedToolchains;
import org.apache.maven.toolchain.model.io.xpp3.MavenToolchainsXpp3Reader;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.ReaderFactory;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
@ -62,19 +61,14 @@ public class DefaultToolchainsReader
{
Validate.notNull( input, "input cannot be null" );
try
try ( final Reader in = input )
{
MavenToolchainsXpp3Reader r = new MavenToolchainsXpp3Reader();
return r.read( input, isStrict( options ) );
return new MavenToolchainsXpp3Reader().read( in, isStrict( options ) );
}
catch ( XmlPullParserException e )
{
throw new ToolchainsParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e );
}
finally
{
IOUtil.close( input );
}
}
@Override
@ -83,19 +77,14 @@ public class DefaultToolchainsReader
{
Validate.notNull( input, "input cannot be null" );
try
try ( final InputStream in = input )
{
MavenToolchainsXpp3Reader r = new MavenToolchainsXpp3Reader();
return r.read( input, isStrict( options ) );
return new MavenToolchainsXpp3Reader().read( in, isStrict( options ) );
}
catch ( XmlPullParserException e )
{
throw new ToolchainsParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e );
}
finally
{
IOUtil.close( input );
}
}
private boolean isStrict( Map<String, ?> options )

View File

@ -32,8 +32,8 @@ import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.model.io.xpp3.MavenXpp3ReaderEx;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.ReaderFactory;
import org.codehaus.plexus.util.xml.XmlStreamReader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
/**
@ -65,13 +65,9 @@ public class DefaultModelReader
{
Validate.notNull( input, "input cannot be null" );
try
try ( final Reader in = input )
{
return read( input, isStrict( options ), getSource( options ) );
}
finally
{
IOUtil.close( input );
return read( in, isStrict( options ), getSource( options ) );
}
}
@ -81,13 +77,9 @@ public class DefaultModelReader
{
Validate.notNull( input, "input cannot be null" );
try
try ( final XmlStreamReader in = ReaderFactory.newXmlReader( input ) )
{
return read( ReaderFactory.newXmlReader( input ), isStrict( options ), getSource( options ) );
}
finally
{
IOUtil.close( input );
return read( in, isStrict( options ), getSource( options ) );
}
}

View File

@ -30,7 +30,6 @@ import org.apache.commons.lang3.Validate;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.WriterFactory;
/**
@ -62,14 +61,9 @@ public class DefaultModelWriter
Validate.notNull( output, "output cannot be null" );
Validate.notNull( model, "model cannot be null" );
try
try ( final Writer out = output )
{
MavenXpp3Writer w = new MavenXpp3Writer();
w.write( output, model );
}
finally
{
IOUtil.close( output );
new MavenXpp3Writer().write( out, model );
}
}
@ -80,19 +74,16 @@ public class DefaultModelWriter
Validate.notNull( output, "output cannot be null" );
Validate.notNull( model, "model cannot be null" );
try
String encoding = model.getModelEncoding();
// TODO Use StringUtils here
if ( encoding == null || encoding.length() <= 0 )
{
String encoding = model.getModelEncoding();
// TODO Use StringUtils here
if ( encoding == null || encoding.length() <= 0 )
{
encoding = "UTF-8";
}
write( new OutputStreamWriter( output, encoding ), options, model );
encoding = "UTF-8";
}
finally
try ( final Writer out = new OutputStreamWriter( output, encoding ) )
{
IOUtil.close( output );
write( out, options, model );
}
}

View File

@ -24,7 +24,6 @@ import org.apache.maven.artifact.repository.metadata.Versioning;
import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.util.IOUtil;
import org.eclipse.aether.RepositoryEvent;
import org.eclipse.aether.RepositoryEvent.EventType;
import org.eclipse.aether.RepositorySystemSession;
@ -57,7 +56,9 @@ import org.eclipse.aether.version.VersionScheme;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
@ -258,23 +259,20 @@ public class DefaultVersionRangeResolver
ArtifactRepository repository, VersionRangeResult result )
{
Versioning versioning = null;
FileInputStream fis = null;
try
{
if ( metadata != null )
{
try ( SyncContext syncContext = syncContextFactory.newInstance( session, true ) )
{
syncContext.acquire( null, Collections.singleton( metadata ) );
if ( metadata.getFile() != null && metadata.getFile().exists() )
{
fis = new FileInputStream( metadata.getFile() );
org.apache.maven.artifact.repository.metadata.Metadata m =
new MetadataXpp3Reader().read( fis, false );
versioning = m.getVersioning();
try ( final InputStream in = new FileInputStream( metadata.getFile() ) )
{
versioning = new MetadataXpp3Reader().read( in, false ).getVersioning();
}
}
}
}
@ -284,10 +282,6 @@ public class DefaultVersionRangeResolver
invalidMetadata( session, trace, metadata, repository, e );
result.addException( e );
}
finally
{
IOUtil.close( fis );
}
return ( versioning != null ) ? versioning : new Versioning();
}

View File

@ -26,7 +26,6 @@ import org.apache.maven.artifact.repository.metadata.Versioning;
import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.StringUtils;
import org.eclipse.aether.RepositoryCache;
import org.eclipse.aether.RepositoryEvent;
@ -61,9 +60,11 @@ import org.eclipse.aether.util.ConfigUtils;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
@ -324,43 +325,38 @@ public class DefaultVersionResolver
ArtifactRepository repository, VersionResult result )
{
Versioning versioning = null;
FileInputStream fis = null;
try
{
if ( metadata != null )
{
try ( SyncContext syncContext = syncContextFactory.newInstance( session, true ) )
{
syncContext.acquire( null, Collections.singleton( metadata ) );
if ( metadata.getFile() != null && metadata.getFile().exists() )
{
fis = new FileInputStream( metadata.getFile() );
org.apache.maven.artifact.repository.metadata.Metadata m =
new MetadataXpp3Reader().read( fis, false );
versioning = m.getVersioning();
/*
* NOTE: Users occasionally misuse the id "local" for remote repos which screws up the metadata
* of the local repository. This is especially troublesome during snapshot resolution so we try
* to handle that gracefully.
*/
if ( versioning != null && repository instanceof LocalRepository )
try ( final InputStream in = new FileInputStream( metadata.getFile() ) )
{
if ( versioning.getSnapshot() != null && versioning.getSnapshot().getBuildNumber() > 0 )
{
Versioning repaired = new Versioning();
repaired.setLastUpdated( versioning.getLastUpdated() );
Snapshot snapshot = new Snapshot();
snapshot.setLocalCopy( true );
repaired.setSnapshot( snapshot );
versioning = repaired;
versioning = new MetadataXpp3Reader().read( in, false ).getVersioning();
/*
NOTE: Users occasionally misuse the id "local" for remote repos which screws up the metadata
of the local repository. This is especially troublesome during snapshot resolution so we try
to handle that gracefully.
*/
if ( versioning != null && repository instanceof LocalRepository
&& versioning.getSnapshot() != null
&& versioning.getSnapshot().getBuildNumber() > 0 )
{
final Versioning repaired = new Versioning();
repaired.setLastUpdated( versioning.getLastUpdated() );
repaired.setSnapshot( new Snapshot() );
repaired.getSnapshot().setLocalCopy( true );
versioning = repaired;
throw new IOException( "Snapshot information corrupted with remote repository data"
+ ", please verify that no remote repository uses the id '"
+ repository.getId() + "'" );
}
}
}
@ -372,10 +368,6 @@ public class DefaultVersionResolver
invalidMetadata( session, trace, metadata, repository, e );
result.addException( e );
}
finally
{
IOUtil.close( fis );
}
return ( versioning != null ) ? versioning : new Versioning();
}

View File

@ -29,7 +29,6 @@ import org.apache.commons.lang3.Validate;
import org.apache.maven.settings.Settings;
import org.apache.maven.settings.io.xpp3.SettingsXpp3Reader;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.ReaderFactory;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
@ -60,19 +59,14 @@ public class DefaultSettingsReader
{
Validate.notNull( input, "input cannot be null" );
try
try ( final Reader in = input )
{
SettingsXpp3Reader r = new SettingsXpp3Reader();
return r.read( input, isStrict( options ) );
return new SettingsXpp3Reader().read( in, isStrict( options ) );
}
catch ( XmlPullParserException e )
{
throw new SettingsParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e );
}
finally
{
IOUtil.close( input );
}
}
@Override
@ -81,19 +75,14 @@ public class DefaultSettingsReader
{
Validate.notNull( input, "input cannot be null" );
try
try ( final InputStream in = input )
{
SettingsXpp3Reader r = new SettingsXpp3Reader();
return r.read( input, isStrict( options ) );
return new SettingsXpp3Reader().read( in, isStrict( options ) );
}
catch ( XmlPullParserException e )
{
throw new SettingsParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e );
}
finally
{
IOUtil.close( input );
}
}
private boolean isStrict( Map<String, ?> options )

View File

@ -30,7 +30,6 @@ import org.apache.commons.lang3.Validate;
import org.apache.maven.settings.Settings;
import org.apache.maven.settings.io.xpp3.SettingsXpp3Writer;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.WriterFactory;
/**
@ -62,14 +61,9 @@ public class DefaultSettingsWriter
Validate.notNull( output, "output cannot be null" );
Validate.notNull( settings, "settings cannot be null" );
try
try ( final Writer out = output )
{
SettingsXpp3Writer w = new SettingsXpp3Writer();
w.write( output, settings );
}
finally
{
IOUtil.close( output );
new SettingsXpp3Writer().write( out, settings );
}
}
@ -80,19 +74,16 @@ public class DefaultSettingsWriter
Validate.notNull( output, "output cannot be null" );
Validate.notNull( settings, "settings cannot be null" );
try
String encoding = settings.getModelEncoding();
// TODO Use StringUtils here
if ( encoding == null || encoding.length() <= 0 )
{
String encoding = settings.getModelEncoding();
// TODO Use StringUtils here
if ( encoding == null || encoding.length() <= 0 )
{
encoding = "UTF-8";
}
write( new OutputStreamWriter( output, encoding ), options, settings );
encoding = "UTF-8";
}
finally
try ( final Writer out = new OutputStreamWriter( output, encoding ) )
{
IOUtil.close( output );
write( out, options, settings );
}
}