maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From khmarba...@apache.org
Subject [08/27] maven git commit: [MNG-6168] Fix unclosed streams
Date Mon, 01 May 2017 14:43:26 GMT
[MNG-6168] Fix unclosed streams


Project: http://git-wip-us.apache.org/repos/asf/maven/repo
Commit: http://git-wip-us.apache.org/repos/asf/maven/commit/0931bb2c
Tree: http://git-wip-us.apache.org/repos/asf/maven/tree/0931bb2c
Diff: http://git-wip-us.apache.org/repos/asf/maven/diff/0931bb2c

Branch: refs/heads/MNG-6069
Commit: 0931bb2cc7630cc79adb98407db13315b4a709ee
Parents: f0535a4
Author: Christian Schulte <schulte@apache.org>
Authored: Fri Mar 24 22:04:37 2017 +0100
Committer: Christian Schulte <schulte@apache.org>
Committed: Fri Mar 24 22:04:37 2017 +0100

----------------------------------------------------------------------
 .../metadata/io/DefaultMetadataReader.java      | 19 ++-------
 .../toolchain/io/DefaultToolchainsReader.java   | 19 ++-------
 .../maven/model/io/DefaultModelReader.java      | 18 +++------
 .../maven/model/io/DefaultModelWriter.java      | 27 +++++--------
 .../internal/DefaultVersionRangeResolver.java   | 18 +++------
 .../internal/DefaultVersionResolver.java        | 42 ++++++++------------
 .../settings/io/DefaultSettingsReader.java      | 19 ++-------
 .../settings/io/DefaultSettingsWriter.java      | 27 +++++--------
 8 files changed, 58 insertions(+), 131 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/maven/blob/0931bb2c/maven-core/src/main/java/org/apache/maven/artifact/repository/metadata/io/DefaultMetadataReader.java
----------------------------------------------------------------------
diff --git a/maven-core/src/main/java/org/apache/maven/artifact/repository/metadata/io/DefaultMetadataReader.java
b/maven-core/src/main/java/org/apache/maven/artifact/repository/metadata/io/DefaultMetadataReader.java
index c769f89..51a2849 100644
--- a/maven-core/src/main/java/org/apache/maven/artifact/repository/metadata/io/DefaultMetadataReader.java
+++ b/maven-core/src/main/java/org/apache/maven/artifact/repository/metadata/io/DefaultMetadataReader.java
@@ -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 )

http://git-wip-us.apache.org/repos/asf/maven/blob/0931bb2c/maven-core/src/main/java/org/apache/maven/toolchain/io/DefaultToolchainsReader.java
----------------------------------------------------------------------
diff --git a/maven-core/src/main/java/org/apache/maven/toolchain/io/DefaultToolchainsReader.java
b/maven-core/src/main/java/org/apache/maven/toolchain/io/DefaultToolchainsReader.java
index 9b5bdd7..86a1784 100644
--- a/maven-core/src/main/java/org/apache/maven/toolchain/io/DefaultToolchainsReader.java
+++ b/maven-core/src/main/java/org/apache/maven/toolchain/io/DefaultToolchainsReader.java
@@ -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 )

http://git-wip-us.apache.org/repos/asf/maven/blob/0931bb2c/maven-model-builder/src/main/java/org/apache/maven/model/io/DefaultModelReader.java
----------------------------------------------------------------------
diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/io/DefaultModelReader.java
b/maven-model-builder/src/main/java/org/apache/maven/model/io/DefaultModelReader.java
index 0ba6673..e07ac3b 100644
--- a/maven-model-builder/src/main/java/org/apache/maven/model/io/DefaultModelReader.java
+++ b/maven-model-builder/src/main/java/org/apache/maven/model/io/DefaultModelReader.java
@@ -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
-        {
-            return read( input, isStrict( options ), getSource( options ) );
-        }
-        finally
+        try ( final Reader in = input )
         {
-            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
-        {
-            return read( ReaderFactory.newXmlReader( input ), isStrict( options ), getSource(
options ) );
-        }
-        finally
+        try ( final XmlStreamReader in = ReaderFactory.newXmlReader( input ) )
         {
-            IOUtil.close( input );
+            return read( in, isStrict( options ), getSource( options ) );
         }
     }
 

http://git-wip-us.apache.org/repos/asf/maven/blob/0931bb2c/maven-model-builder/src/main/java/org/apache/maven/model/io/DefaultModelWriter.java
----------------------------------------------------------------------
diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/io/DefaultModelWriter.java
b/maven-model-builder/src/main/java/org/apache/maven/model/io/DefaultModelWriter.java
index 0d9af13..c899efa 100644
--- a/maven-model-builder/src/main/java/org/apache/maven/model/io/DefaultModelWriter.java
+++ b/maven-model-builder/src/main/java/org/apache/maven/model/io/DefaultModelWriter.java
@@ -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 );
         }
     }
 

http://git-wip-us.apache.org/repos/asf/maven/blob/0931bb2c/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionRangeResolver.java
----------------------------------------------------------------------
diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionRangeResolver.java
b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionRangeResolver.java
index ddd3e97..c12c9e0 100644
--- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionRangeResolver.java
+++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionRangeResolver.java
@@ -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();
     }

http://git-wip-us.apache.org/repos/asf/maven/blob/0931bb2c/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionResolver.java
----------------------------------------------------------------------
diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionResolver.java
b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionResolver.java
index 2b0890b..c23794b 100644
--- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionResolver.java
+++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionResolver.java
@@ -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 = 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 )
                             {
-                                Versioning repaired = new Versioning();
+                                final Versioning repaired = new Versioning();
                                 repaired.setLastUpdated( versioning.getLastUpdated() );
-                                Snapshot snapshot = new Snapshot();
-                                snapshot.setLocalCopy( true );
-                                repaired.setSnapshot( snapshot );
+                                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();
     }

http://git-wip-us.apache.org/repos/asf/maven/blob/0931bb2c/maven-settings-builder/src/main/java/org/apache/maven/settings/io/DefaultSettingsReader.java
----------------------------------------------------------------------
diff --git a/maven-settings-builder/src/main/java/org/apache/maven/settings/io/DefaultSettingsReader.java
b/maven-settings-builder/src/main/java/org/apache/maven/settings/io/DefaultSettingsReader.java
index 9beee97..897f742 100644
--- a/maven-settings-builder/src/main/java/org/apache/maven/settings/io/DefaultSettingsReader.java
+++ b/maven-settings-builder/src/main/java/org/apache/maven/settings/io/DefaultSettingsReader.java
@@ -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 )

http://git-wip-us.apache.org/repos/asf/maven/blob/0931bb2c/maven-settings-builder/src/main/java/org/apache/maven/settings/io/DefaultSettingsWriter.java
----------------------------------------------------------------------
diff --git a/maven-settings-builder/src/main/java/org/apache/maven/settings/io/DefaultSettingsWriter.java
b/maven-settings-builder/src/main/java/org/apache/maven/settings/io/DefaultSettingsWriter.java
index 877fbde..19d8b9b 100644
--- a/maven-settings-builder/src/main/java/org/apache/maven/settings/io/DefaultSettingsWriter.java
+++ b/maven-settings-builder/src/main/java/org/apache/maven/settings/io/DefaultSettingsWriter.java
@@ -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 );
         }
     }
 


Mime
View raw message