You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by el...@apache.org on 2020/06/14 22:37:25 UTC

[maven-resolver] branch master updated: use try with resources (#56)

This is an automated email from the ASF dual-hosted git repository.

elharo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-resolver.git


The following commit(s) were added to refs/heads/master by this push:
     new 194808a  use try with resources (#56)
194808a is described below

commit 194808aaa8ce4e1fae5ebfd1f9163cdce3fed1fb
Author: Elliotte Rusty Harold <el...@users.noreply.github.com>
AuthorDate: Sun Jun 14 18:37:18 2020 -0400

    use try with resources (#56)
---
 .../aether/transport/wagon/WagonTransporter.java   | 142 ++++-----------------
 1 file changed, 22 insertions(+), 120 deletions(-)

diff --git a/maven-resolver-transport-wagon/src/main/java/org/eclipse/aether/transport/wagon/WagonTransporter.java b/maven-resolver-transport-wagon/src/main/java/org/eclipse/aether/transport/wagon/WagonTransporter.java
index 619d49f..6520d8f 100644
--- a/maven-resolver-transport-wagon/src/main/java/org/eclipse/aether/transport/wagon/WagonTransporter.java
+++ b/maven-resolver-transport-wagon/src/main/java/org/eclipse/aether/transport/wagon/WagonTransporter.java
@@ -25,6 +25,7 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.util.Locale;
 import java.util.Map;
@@ -34,9 +35,11 @@ import java.util.UUID;
 import java.util.concurrent.ConcurrentLinkedQueue;
 import java.util.concurrent.atomic.AtomicBoolean;
 
+import org.apache.maven.wagon.ConnectionException;
 import org.apache.maven.wagon.ResourceDoesNotExistException;
 import org.apache.maven.wagon.StreamingWagon;
 import org.apache.maven.wagon.Wagon;
+import org.apache.maven.wagon.WagonException;
 import org.apache.maven.wagon.authentication.AuthenticationInfo;
 import org.apache.maven.wagon.proxy.ProxyInfo;
 import org.apache.maven.wagon.proxy.ProxyInfoProvider;
@@ -263,6 +266,7 @@ final class WagonTransporter
 
             proxy = new ProxyInfoProvider()
             {
+                @Override
                 public ProxyInfo getProxyInfo( String protocol )
                 {
                     return prox;
@@ -285,7 +289,7 @@ final class WagonTransporter
     }
 
     private void connectWagon( Wagon wagon )
-        throws Exception
+        throws WagonException
     {
         if ( !headers.isEmpty() )
         {
@@ -298,7 +302,7 @@ final class WagonTransporter
             {
                 // normal for non-http wagons
             }
-            catch ( Exception e )
+            catch ( InvocationTargetException | IllegalAccessException | RuntimeException e )
             {
                 LOGGER.debug( "Could not set user agent for Wagon {}", wagon.getClass().getName(), e );
             }
@@ -342,7 +346,7 @@ final class WagonTransporter
                 wagon.disconnect();
             }
         }
-        catch ( Exception e )
+        catch ( ConnectionException e )
         {
             LOGGER.debug( "Could not disconnect Wagon {}", wagon, e );
         }
@@ -431,7 +435,7 @@ final class WagonTransporter
                 wagons.add( wagon );
             }
         }
-        catch ( Exception e )
+        catch ( RuntimeException e )
         {
             throw WagonCancelledException.unwrap( e );
         }
@@ -481,7 +485,7 @@ final class WagonTransporter
     {
 
         void run( Wagon wagon )
-            throws Exception;
+            throws IOException, WagonException;
 
     }
 
@@ -496,8 +500,9 @@ final class WagonTransporter
             this.task = task;
         }
 
+        @Override
         public void run( Wagon wagon )
-            throws Exception
+            throws WagonException
         {
             String src = task.getLocation().toString();
             if ( !wagon.resourceExists( src ) )
@@ -520,34 +525,17 @@ final class WagonTransporter
             this.task = task;
         }
 
+        @Override
         public void run( Wagon wagon )
-            throws Exception
+            throws IOException, WagonException
         {
             String src = task.getLocation().toString();
             File file = task.getDataFile();
             if ( file == null && wagon instanceof StreamingWagon )
             {
-                OutputStream dst = null;
-                try
+                try ( OutputStream dst = task.newOutputStream() )
                 {
-                    dst = task.newOutputStream();
                     ( (StreamingWagon) wagon ).getToStream( src, dst );
-                    dst.close();
-                    dst = null;
-                }
-                finally
-                {
-                    try
-                    {
-                        if ( dst != null )
-                        {
-                            dst.close();
-                        }
-                    }
-                    catch ( final IOException e )
-                    {
-                        // Suppressed due to an exception already thrown in the try block.
-                    }
                 }
             }
             else
@@ -583,45 +571,10 @@ final class WagonTransporter
         private void readTempFile( File dst )
             throws IOException
         {
-            FileInputStream in = null;
-            OutputStream out = null;
-            try
+            try ( FileInputStream in = new FileInputStream( dst );
+                    OutputStream out = task.newOutputStream() )
             {
-                in = new FileInputStream( dst );
-                out = task.newOutputStream();
                 copy( out, in );
-                out.close();
-                out = null;
-                in.close();
-                in = null;
-            }
-            finally
-            {
-                try
-                {
-                    if ( out != null )
-                    {
-                        out.close();
-                    }
-                }
-                catch ( final IOException e )
-                {
-                    // Suppressed due to an exception already thrown in the try block.
-                }
-                finally
-                {
-                    try
-                    {
-                        if ( in != null )
-                        {
-                            in.close();
-                        }
-                    }
-                    catch ( final IOException e )
-                    {
-                        // Suppressed due to an exception already thrown in the try block.
-                    }
-                }
             }
         }
 
@@ -638,35 +591,18 @@ final class WagonTransporter
             this.task = task;
         }
 
+        @Override
         public void run( Wagon wagon )
-            throws Exception
+            throws WagonException, IOException
         {
             String dst = task.getLocation().toString();
             File file = task.getDataFile();
             if ( file == null && wagon instanceof StreamingWagon )
             {
-                InputStream src = null;
-                try
+                try ( InputStream src = task.newInputStream() )
                 {
-                    src = task.newInputStream();
                     // StreamingWagon uses an internal buffer on src input stream.
                     ( (StreamingWagon) wagon ).putFromStream( src, dst, task.getDataLength(), -1 );
-                    src.close();
-                    src = null;
-                }
-                finally
-                {
-                    try
-                    {
-                        if ( src != null )
-                        {
-                            src.close();
-                        }
-                    }
-                    catch ( final IOException e )
-                    {
-                        // Suppressed due to an exception already thrown in the try block.
-                    }
                 }
             }
             else
@@ -690,51 +626,17 @@ final class WagonTransporter
             throws IOException
         {
             File tmp = newTempFile();
-            OutputStream out = null;
-            InputStream in = null;
-            try
+            
+            try ( InputStream in = task.newInputStream();
+                    OutputStream out = new FileOutputStream( tmp ) ) 
             {
-                in = task.newInputStream();
-                out = new FileOutputStream( tmp );
                 copy( out, in );
-                out.close();
-                out = null;
-                in.close();
-                in = null;
             }
             catch ( IOException e )
             {
                 delTempFile( tmp );
                 throw e;
             }
-            finally
-            {
-                try
-                {
-                    if ( out != null )
-                    {
-                        out.close();
-                    }
-                }
-                catch ( final IOException e )
-                {
-                    // Suppressed due to an exception already thrown in the try block.
-                }
-                finally
-                {
-                    try
-                    {
-                        if ( in != null )
-                        {
-                            in.close();
-                        }
-                    }
-                    catch ( final IOException e )
-                    {
-                        // Suppressed due to an exception already thrown in the try block.
-                    }
-                }
-            }
 
             return tmp;
         }