From 6b9da927e4bf110d42b90f532c612739245fffd3 Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Tue, 8 Apr 2014 12:03:38 +0000 Subject: [PATCH] start using try with resources git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1585697 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/archiva/cli/ArchivaCli.java | 24 ++-- .../artifact/LegacyToDefaultConverter.java | 111 ++++++++---------- .../archiva/checksum/ChecksummedFile.java | 30 ++--- 3 files changed, 67 insertions(+), 98 deletions(-) diff --git a/archiva-cli/src/main/java/org/apache/archiva/cli/ArchivaCli.java b/archiva-cli/src/main/java/org/apache/archiva/cli/ArchivaCli.java index 031bcac69..a65049597 100644 --- a/archiva-cli/src/main/java/org/apache/archiva/cli/ArchivaCli.java +++ b/archiva-cli/src/main/java/org/apache/archiva/cli/ArchivaCli.java @@ -75,22 +75,19 @@ public class ArchivaCli private static String getVersion() throws IOException { - InputStream pomStream = ArchivaCli.class.getResourceAsStream( POM_PROPERTIES ); - if ( pomStream == null ) - { - throw new IOException( "Failed to load " + POM_PROPERTIES ); - } - try + + + try (InputStream pomStream = ArchivaCli.class.getResourceAsStream( POM_PROPERTIES )) { + if ( pomStream == null ) + { + throw new IOException( "Failed to load " + POM_PROPERTIES ); + } Properties properties = new Properties(); properties.load( pomStream ); return properties.getProperty( "version" ); } - finally - { - IOUtils.closeQuietly( pomStream ); - } } private ClassPathXmlApplicationContext applicationContext; @@ -266,16 +263,11 @@ public class ArchivaCli Properties p = new Properties(); - FileInputStream fis = new FileInputStream( properties ); - try + try(FileInputStream fis = new FileInputStream( properties )) { p.load( fis ); } - finally - { - IOUtils.closeQuietly( fis ); - } File oldRepositoryPath = new File( p.getProperty( SOURCE_REPO_PATH ) ); diff --git a/archiva-modules/archiva-base/archiva-artifact-converter/src/main/java/org/apache/archiva/converter/artifact/LegacyToDefaultConverter.java b/archiva-modules/archiva-base/archiva-artifact-converter/src/main/java/org/apache/archiva/converter/artifact/LegacyToDefaultConverter.java index 1faac13aa..4fe0536a9 100644 --- a/archiva-modules/archiva-base/archiva-artifact-converter/src/main/java/org/apache/archiva/converter/artifact/LegacyToDefaultConverter.java +++ b/archiva-modules/archiva-base/archiva-artifact-converter/src/main/java/org/apache/archiva/converter/artifact/LegacyToDefaultConverter.java @@ -25,7 +25,6 @@ import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException; import org.apache.archiva.transaction.FileTransaction; import org.apache.archiva.transaction.TransactionException; import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.factory.ArtifactFactory; import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager; @@ -68,7 +67,7 @@ import java.util.regex.Matcher; /** * LegacyToDefaultConverter */ -@Service ("artifactConverter#legacy-to-default") +@Service("artifactConverter#legacy-to-default") public class LegacyToDefaultConverter implements ArtifactConverter { @@ -185,7 +184,7 @@ public class LegacyToDefaultConverter } } - @SuppressWarnings ("unchecked") + @SuppressWarnings("unchecked") private boolean copyPom( Artifact artifact, ArtifactRepository targetRepository, FileTransaction transaction ) throws ArtifactConversionException { @@ -243,58 +242,58 @@ public class LegacyToDefaultConverter else { // v3 POM - StringReader stringReader = new StringReader( contents ); - StringWriter writer = null; - try + try (StringReader stringReader = new StringReader( contents )) { - org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader v3Reader = - new org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader(); - org.apache.maven.model.v3_0_0.Model v3Model = v3Reader.read( stringReader ); - if ( doRelocation( artifact, v3Model, targetRepository, transaction ) ) + try (StringWriter writer = new StringWriter()) { - Artifact relocatedPom = - artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(), - artifact.getVersion() ); - targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( relocatedPom ) ); - } + org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader v3Reader = + new org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader(); + org.apache.maven.model.v3_0_0.Model v3Model = v3Reader.read( stringReader ); + + if ( doRelocation( artifact, v3Model, targetRepository, transaction ) ) + { + Artifact relocatedPom = + artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(), + artifact.getVersion() ); + targetFile = + new File( targetRepository.getBasedir(), targetRepository.pathOf( relocatedPom ) ); + } - Model v4Model = translator.translate( v3Model ); + Model v4Model = translator.translate( v3Model ); - translator.validateV4Basics( v4Model, v3Model.getGroupId(), v3Model.getArtifactId(), - v3Model.getVersion(), v3Model.getPackage() ); + translator.validateV4Basics( v4Model, v3Model.getGroupId(), v3Model.getArtifactId(), + v3Model.getVersion(), v3Model.getPackage() ); - writer = new StringWriter(); - MavenXpp3Writer xpp3Writer = new MavenXpp3Writer(); - xpp3Writer.write( writer, v4Model ); + MavenXpp3Writer xpp3Writer = new MavenXpp3Writer(); + xpp3Writer.write( writer, v4Model ); - transaction.createFile( writer.toString(), targetFile, digesters ); + transaction.createFile( writer.toString(), targetFile, digesters ); - List warnings = translator.getWarnings(); + List warnings = translator.getWarnings(); - for ( String message : warnings ) + for ( String message : warnings ) + { + addWarning( artifact, message ); + } + } + catch ( XmlPullParserException e ) { - addWarning( artifact, message ); + addWarning( artifact, + Messages.getString( "invalid.source.pom", e.getMessage() ) ); //$NON-NLS-1$ + result = false; + } + catch ( IOException e ) + { + throw new ArtifactConversionException( Messages.getString( "unable.to.write.converted.pom" ), + e ); //$NON-NLS-1$ + } + catch ( PomTranslationException e ) + { + addWarning( artifact, + Messages.getString( "invalid.source.pom", e.getMessage() ) ); //$NON-NLS-1$ + result = false; } - } - catch ( XmlPullParserException e ) - { - addWarning( artifact, Messages.getString( "invalid.source.pom", e.getMessage() ) ); //$NON-NLS-1$ - result = false; - } - catch ( IOException e ) - { - throw new ArtifactConversionException( Messages.getString( "unable.to.write.converted.pom" ), - e ); //$NON-NLS-1$ - } - catch ( PomTranslationException e ) - { - addWarning( artifact, Messages.getString( "invalid.source.pom", e.getMessage() ) ); //$NON-NLS-1$ - result = false; - } - finally - { - IOUtils.closeQuietly( writer ); } } } @@ -410,11 +409,10 @@ public class LegacyToDefaultConverter { Metadata metadata; MetadataXpp3Reader reader = new MetadataXpp3Reader(); - FileReader fileReader = null; - try + + try (FileReader fileReader = new FileReader( file )) { - fileReader = new FileReader( file ); - metadata = reader.read( fileReader ); + return reader.read( fileReader ); } catch ( FileNotFoundException e ) { @@ -431,11 +429,6 @@ public class LegacyToDefaultConverter throw new ArtifactConversionException( Messages.getString( "error.reading.target.metadata" ), e ); //$NON-NLS-1$ } - finally - { - IOUtils.closeQuietly( fileReader ); - } - return metadata; } private boolean validateMetadata( Artifact artifact ) @@ -465,7 +458,7 @@ public class LegacyToDefaultConverter return result; } - @SuppressWarnings ("unchecked") + @SuppressWarnings("unchecked") private boolean validateMetadata( Metadata metadata, RepositoryMetadata repositoryMetadata, Artifact artifact ) { String groupIdKey; @@ -590,11 +583,9 @@ public class LegacyToDefaultConverter if ( changed ) { - StringWriter writer = null; - try - { - writer = new StringWriter(); + try (StringWriter writer = new StringWriter()) + { MetadataXpp3Writer mappingWriter = new MetadataXpp3Writer(); mappingWriter.write( writer, metadata ); @@ -606,10 +597,6 @@ public class LegacyToDefaultConverter throw new ArtifactConversionException( Messages.getString( "error.writing.target.metadata" ), e ); //$NON-NLS-1$ } - finally - { - IOUtils.closeQuietly( writer ); - } } } diff --git a/archiva-modules/archiva-base/archiva-checksum/src/main/java/org/apache/archiva/checksum/ChecksummedFile.java b/archiva-modules/archiva-base/archiva-checksum/src/main/java/org/apache/archiva/checksum/ChecksummedFile.java index 0aeb76285..b505494d2 100644 --- a/archiva-modules/archiva-base/archiva-checksum/src/main/java/org/apache/archiva/checksum/ChecksummedFile.java +++ b/archiva-modules/archiva-base/archiva-checksum/src/main/java/org/apache/archiva/checksum/ChecksummedFile.java @@ -27,6 +27,7 @@ import org.slf4j.LoggerFactory; import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -75,18 +76,13 @@ public class ChecksummedFile public String calculateChecksum( ChecksumAlgorithm checksumAlgorithm ) throws IOException { - FileInputStream fis = null; - try + + try (FileInputStream fis = new FileInputStream( referenceFile )) { Checksum checksum = new Checksum( checksumAlgorithm ); - fis = new FileInputStream( referenceFile ); checksum.update( fis ); return checksum.getChecksum(); } - finally - { - IOUtils.closeQuietly( fis ); - } } /** @@ -144,8 +140,8 @@ public class ChecksummedFile */ public boolean isValidChecksums( ChecksumAlgorithm algorithms[] ) { - FileInputStream fis = null; - try + + try (FileInputStream fis = new FileInputStream( referenceFile )) { List checksums = new ArrayList( algorithms.length ); // Create checksum object for each algorithm. @@ -170,7 +166,6 @@ public class ChecksummedFile // Parse file once, for all checksums. try { - fis = new FileInputStream( referenceFile ); Checksum.update( checksums, fis ); } catch ( IOException e ) @@ -205,10 +200,10 @@ public class ChecksummedFile } return valid; - } - finally + } catch ( IOException e ) { - IOUtils.closeQuietly( fis ); + log.warn( "Unable to read / parse checksum: {}", e.getMessage() ); + return false; } } @@ -234,11 +229,10 @@ public class ChecksummedFile return true; } - FileInputStream fis = null; - try + + try (FileInputStream fis = new FileInputStream( referenceFile )) { // Parse file once, for all checksums. - fis = new FileInputStream( referenceFile ); Checksum.update( checksums, fis ); } catch ( IOException e ) @@ -246,10 +240,6 @@ public class ChecksummedFile log.warn( e.getMessage(), e ); return false; } - finally - { - IOUtils.closeQuietly( fis ); - } boolean valid = true; -- 2.39.5