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 12:40:55 UTC

[maven-resolver] branch i120 created (now 3fcf63e)

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

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


      at 3fcf63e  use try with resources

This branch includes the following new commits:

     new 3fcf63e  use try with resources

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[maven-resolver] 01/01: use try with resources

Posted by el...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 3fcf63e302f0d600e40aa5ff463ac100c622c326
Author: Elliotte Rusty Harold <el...@ibiblio.org>
AuthorDate: Sun Jun 14 08:40:41 2020 -0400

    use try with resources
---
 .../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;
         }