mirror of
https://github.com/apache/archiva.git
synced 2025-02-21 01:15:08 +00:00
start using try with resources
git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1585697 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
751bffaef1
commit
6b9da927e4
@ -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 @@ private void doConversion( String properties )
|
||||
|
||||
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 ) );
|
||||
|
||||
|
@ -25,7 +25,6 @@
|
||||
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 @@
|
||||
/**
|
||||
* LegacyToDefaultConverter
|
||||
*/
|
||||
@Service ("artifactConverter#legacy-to-default")
|
||||
@Service("artifactConverter#legacy-to-default")
|
||||
public class LegacyToDefaultConverter
|
||||
implements ArtifactConverter
|
||||
{
|
||||
@ -185,7 +184,7 @@ public void convert( Artifact artifact, ArtifactRepository targetRepository )
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings ("unchecked")
|
||||
@SuppressWarnings("unchecked")
|
||||
private boolean copyPom( Artifact artifact, ArtifactRepository targetRepository, FileTransaction transaction )
|
||||
throws ArtifactConversionException
|
||||
{
|
||||
@ -243,58 +242,58 @@ private boolean copyPom( Artifact artifact, ArtifactRepository targetRepository,
|
||||
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 );
|
||||
|
||||
translator.validateV4Basics( v4Model, v3Model.getGroupId(), v3Model.getArtifactId(),
|
||||
v3Model.getVersion(), v3Model.getPackage() );
|
||||
|
||||
MavenXpp3Writer xpp3Writer = new MavenXpp3Writer();
|
||||
xpp3Writer.write( writer, v4Model );
|
||||
|
||||
transaction.createFile( writer.toString(), targetFile, digesters );
|
||||
|
||||
List<String> warnings = translator.getWarnings();
|
||||
|
||||
for ( String message : warnings )
|
||||
{
|
||||
addWarning( artifact, message );
|
||||
}
|
||||
}
|
||||
|
||||
Model v4Model = translator.translate( v3Model );
|
||||
|
||||
translator.validateV4Basics( v4Model, v3Model.getGroupId(), v3Model.getArtifactId(),
|
||||
v3Model.getVersion(), v3Model.getPackage() );
|
||||
|
||||
writer = new StringWriter();
|
||||
MavenXpp3Writer xpp3Writer = new MavenXpp3Writer();
|
||||
xpp3Writer.write( writer, v4Model );
|
||||
|
||||
transaction.createFile( writer.toString(), targetFile, digesters );
|
||||
|
||||
List<String> warnings = translator.getWarnings();
|
||||
|
||||
for ( String message : warnings )
|
||||
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 @@ private Metadata readMetadata( File file )
|
||||
{
|
||||
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 @@ private Metadata readMetadata( File file )
|
||||
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 @@ private boolean validateMetadata( Artifact artifact )
|
||||
return result;
|
||||
}
|
||||
|
||||
@SuppressWarnings ("unchecked")
|
||||
@SuppressWarnings("unchecked")
|
||||
private boolean validateMetadata( Metadata metadata, RepositoryMetadata repositoryMetadata, Artifact artifact )
|
||||
{
|
||||
String groupIdKey;
|
||||
@ -590,11 +583,9 @@ private void updateMetadata( RepositoryMetadata artifactMetadata, ArtifactReposi
|
||||
|
||||
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 @@ private void updateMetadata( RepositoryMetadata artifactMetadata, ArtifactReposi
|
||||
throw new ArtifactConversionException( Messages.getString( "error.writing.target.metadata" ),
|
||||
e ); //$NON-NLS-1$
|
||||
}
|
||||
finally
|
||||
{
|
||||
IOUtils.closeQuietly( writer );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -27,6 +27,7 @@
|
||||
|
||||
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 ChecksummedFile( final File referenceFile )
|
||||
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 boolean isValidChecksum( ChecksumAlgorithm algorithm )
|
||||
*/
|
||||
public boolean isValidChecksums( ChecksumAlgorithm algorithms[] )
|
||||
{
|
||||
FileInputStream fis = null;
|
||||
try
|
||||
|
||||
try (FileInputStream fis = new FileInputStream( referenceFile ))
|
||||
{
|
||||
List<Checksum> checksums = new ArrayList<Checksum>( algorithms.length );
|
||||
// Create checksum object for each algorithm.
|
||||
@ -170,7 +166,6 @@ public boolean isValidChecksums( ChecksumAlgorithm algorithms[] )
|
||||
// Parse file once, for all checksums.
|
||||
try
|
||||
{
|
||||
fis = new FileInputStream( referenceFile );
|
||||
Checksum.update( checksums, fis );
|
||||
}
|
||||
catch ( IOException e )
|
||||
@ -205,10 +200,10 @@ public boolean isValidChecksums( ChecksumAlgorithm algorithms[] )
|
||||
}
|
||||
|
||||
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 boolean fixChecksums( ChecksumAlgorithm[] algorithms )
|
||||
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 boolean fixChecksums( ChecksumAlgorithm[] algorithms )
|
||||
log.warn( e.getMessage(), e );
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
IOUtils.closeQuietly( fis );
|
||||
}
|
||||
|
||||
boolean valid = true;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user