First part in moving to java.nio

This commit is contained in:
Martin Stockhammer 2017-09-02 13:14:33 +02:00
parent 6abccf3510
commit ad9fee4399
17 changed files with 208 additions and 133 deletions

View File

@ -0,0 +1,58 @@
package org.apache.archiva.common.utils;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
/**
*
* Utility class for file manipulation
*
* @author Martin Stockhammer <martin_s@apache.org>
*/
public class FileUtils
{
public static void deleteQuietly(Path dir) {
try
{
Files.walk(dir)
.sorted( Comparator.reverseOrder())
.forEach( file -> {
try
{
Files.delete( file );
}
catch ( IOException e )
{
// Ignore this
}
});
}
catch ( IOException e )
{
// Ignore this
}
}
}

View File

@ -211,7 +211,7 @@ public abstract class AbstractRepositoryPurge
log.error( "Error during metadata retrieval {}: {}", metaBaseId, e.getMessage( ) );
}
}
Path artifactFile = repository.toFile( reference ).toPath( );
Path artifactFile = repository.toFile( reference );
for ( RepositoryListener listener : listeners )
{

View File

@ -31,6 +31,9 @@ import org.apache.archiva.repository.layout.LayoutException;
import org.apache.commons.lang.time.DateUtils;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
@ -112,12 +115,12 @@ public class DaysOldRepositoryPurge
artifactFile.getAbsolutePath( ) );
newArtifactReference.setVersion( version );
File newArtifactFile = repository.toFile( newArtifactReference );
Path newArtifactFile = repository.toFile( newArtifactReference );
// Is this a generic snapshot "1.0-SNAPSHOT" ?
if ( VersionUtil.isGenericSnapshot( newArtifactReference.getVersion( ) ) )
{
if ( newArtifactFile.lastModified( ) < olderThanThisDate.getTimeInMillis( ) )
if ( Files.getLastModifiedTime( newArtifactFile ).toMillis() < olderThanThisDate.getTimeInMillis( ) )
{
artifactsToDelete.addAll( repository.getRelatedArtifacts( newArtifactReference ) );
}
@ -135,7 +138,7 @@ public class DaysOldRepositoryPurge
}
purge( artifactsToDelete );
}
catch ( ContentNotFoundException e )
catch ( ContentNotFoundException | IOException e )
{
throw new RepositoryPurgeException( e.getMessage( ), e );
}

View File

@ -672,7 +672,7 @@ public class DefaultRepositoryProxyConnectors
private File toLocalFile( ManagedRepositoryContent repository, ArtifactReference artifact )
{
return repository.toFile( artifact );
return repository.toFile( artifact ).toFile();
}
/**

View File

@ -26,7 +26,7 @@ import org.apache.archiva.model.ProjectReference;
import org.apache.archiva.model.VersionedReference;
import org.apache.archiva.repository.layout.LayoutException;
import java.io.File;
import java.nio.file.Path;
import java.util.Set;
/**
@ -197,7 +197,7 @@ public interface ManagedRepositoryContent
* @param reference the artifact reference to use.
* @return the relative path to the artifact.
*/
File toFile( ArtifactReference reference );
Path toFile( ArtifactReference reference );
/**
* Given an {@link ArchivaArtifact}, return the file reference to the artifact.
@ -205,7 +205,7 @@ public interface ManagedRepositoryContent
* @param reference the archiva artifact to use.
* @return the relative path to the artifact.
*/
File toFile( ArchivaArtifact reference );
Path toFile( ArchivaArtifact reference );
/**
* Given a {@link ProjectReference}, return the path to the metadata for

View File

@ -21,6 +21,7 @@ package org.apache.archiva.repository.metadata;
import org.apache.archiva.checksum.ChecksumAlgorithm;
import org.apache.archiva.checksum.ChecksummedFile;
import org.apache.archiva.common.utils.FileUtils;
import org.apache.archiva.common.utils.PathUtil;
import org.apache.archiva.common.utils.VersionComparator;
import org.apache.archiva.common.utils.VersionUtil;
@ -43,7 +44,6 @@ import org.apache.archiva.repository.RemoteRepositoryContent;
import org.apache.archiva.repository.layout.LayoutException;
import org.apache.archiva.xml.XMLException;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang.time.DateUtils;
@ -54,8 +54,10 @@ import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
@ -69,8 +71,10 @@ import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.stream.Stream;
/**
* MetadataTools
@ -362,9 +366,9 @@ public class MetadataTools
ProjectReference reference, String proxyId )
{
String metadataPath = getRepositorySpecificName( proxyId, toPath( reference ) );
File metadataFile = new File( managedRepository.getRepoRoot(), metadataPath );
Path metadataFile = Paths.get( managedRepository.getRepoRoot(), metadataPath );
if ( !metadataFile.exists() || !metadataFile.isFile() )
if ( !Files.exists(metadataFile) || !Files.isRegularFile( metadataFile ))
{
// Nothing to do. return null.
return null;
@ -372,13 +376,13 @@ public class MetadataTools
try
{
return MavenMetadataReader.read( metadataFile );
return MavenMetadataReader.read( metadataFile.toFile() );
}
catch ( XMLException e )
{
// TODO: [monitor] consider a monitor for this event.
// TODO: consider a read-redo on monitor return code?
log.warn( "Unable to read metadata: {}", metadataFile.getAbsolutePath(), e );
log.warn( "Unable to read metadata: {}", metadataFile.toAbsolutePath(), e );
return null;
}
}
@ -387,9 +391,9 @@ public class MetadataTools
String logicalResource, String proxyId )
{
String metadataPath = getRepositorySpecificName( proxyId, logicalResource );
File metadataFile = new File( managedRepository.getRepoRoot(), metadataPath );
Path metadataFile = Paths.get( managedRepository.getRepoRoot(), metadataPath );
if ( !metadataFile.exists() || !metadataFile.isFile() )
if ( !Files.exists(metadataFile) || !Files.isRegularFile( metadataFile))
{
// Nothing to do. return null.
return null;
@ -397,13 +401,13 @@ public class MetadataTools
try
{
return MavenMetadataReader.read( metadataFile );
return MavenMetadataReader.read( metadataFile.toFile() );
}
catch ( XMLException e )
{
// TODO: [monitor] consider a monitor for this event.
// TODO: consider a read-redo on monitor return code?
log.warn( "Unable to read metadata: {}", metadataFile.getAbsolutePath(), e );
log.warn( "Unable to read metadata: {}", metadataFile.toAbsolutePath(), e );
return null;
}
}
@ -412,9 +416,9 @@ public class MetadataTools
VersionedReference reference, String proxyId )
{
String metadataPath = getRepositorySpecificName( proxyId, toPath( reference ) );
File metadataFile = new File( managedRepository.getRepoRoot(), metadataPath );
Path metadataFile = Paths.get( managedRepository.getRepoRoot(), metadataPath );
if ( !metadataFile.exists() || !metadataFile.isFile() )
if ( !Files.exists(metadataFile) || !Files.isRegularFile(metadataFile))
{
// Nothing to do. return null.
return null;
@ -422,13 +426,13 @@ public class MetadataTools
try
{
return MavenMetadataReader.read( metadataFile );
return MavenMetadataReader.read( metadataFile.toFile() );
}
catch ( XMLException e )
{
// TODO: [monitor] consider a monitor for this event.
// TODO: consider a read-redo on monitor return code?
log.warn( "Unable to read metadata: {}", metadataFile.getAbsolutePath(), e );
log.warn( "Unable to read metadata: {}", metadataFile.toAbsolutePath(), e );
return null;
}
}
@ -436,7 +440,7 @@ public class MetadataTools
public void updateMetadata( ManagedRepositoryContent managedRepository, String logicalResource )
throws RepositoryMetadataException
{
final File metadataFile = new File( managedRepository.getRepoRoot(), logicalResource );
final Path metadataFile = Paths.get( managedRepository.getRepoRoot(), logicalResource );
ArchivaRepositoryMetadata metadata = null;
//Gather and merge all metadata available
@ -464,7 +468,7 @@ public class MetadataTools
{
availableVersions.addAll( metadataAvailableVersions );
}
availableVersions = findPossibleVersions( availableVersions, metadataFile.getParentFile() );
availableVersions = findPossibleVersions( availableVersions, metadataFile.getParent() );
if ( availableVersions.size() > 0 )
{
@ -473,7 +477,7 @@ public class MetadataTools
RepositoryMetadataWriter.write( metadata, metadataFile );
ChecksummedFile checksum = new ChecksummedFile( metadataFile );
ChecksummedFile checksum = new ChecksummedFile( metadataFile.toFile() );
checksum.fixChecksums( algorithms );
}
@ -482,23 +486,31 @@ public class MetadataTools
* subdirectories that contain poms.
*
* @param metadataParentDirectory
* @return origional set plus newley found versions
* @return origional set plus newly found versions
*/
private Set<String> findPossibleVersions( Set<String> versions, File metadataParentDirectory )
private Set<String> findPossibleVersions( Set<String> versions, Path metadataParentDirectory )
{
Set<String> result = new HashSet<String>( versions );
for ( File directory : metadataParentDirectory.listFiles() )
{
if ( directory.isDirectory() )
{
for ( File possiblePom : directory.listFiles() )
try (Stream<Path> stream = Files.list( metadataParentDirectory )) {
stream.filter( Files::isDirectory ).filter(
p ->
{
if ( possiblePom.getName().endsWith( ".pom" ) )
try(Stream<Path> substream = Files.list(p))
{
result.add( directory.getName() );
return substream.anyMatch( f -> Files.isRegularFile( f ) && f.endsWith( ".pom" ));
}
catch ( IOException e )
{
return false;
}
}
}
).forEach(
p -> result.add(p.getFileName().toString())
);
} catch (IOException e) {
//
}
return result;
}
@ -507,12 +519,12 @@ public class MetadataTools
ManagedRepositoryContent managedRepository, String logicalResource )
{
List<ArchivaRepositoryMetadata> metadatas = new ArrayList<>();
File file = new File( managedRepository.getRepoRoot(), logicalResource );
if ( file.exists() )
Path file = Paths.get( managedRepository.getRepoRoot(), logicalResource );
if ( Files.exists(file) )
{
try
{
ArchivaRepositoryMetadata existingMetadata = MavenMetadataReader.read( file );
ArchivaRepositoryMetadata existingMetadata = MavenMetadataReader.read( file.toFile() );
if ( existingMetadata != null )
{
metadatas.add( existingMetadata );
@ -520,7 +532,7 @@ public class MetadataTools
}
catch ( XMLException e )
{
log.debug( "Could not read metadata at {}. Metadata will be removed.", file.getAbsolutePath() );
log.debug( "Could not read metadata at {}. Metadata will be removed.", file.toAbsolutePath() );
FileUtils.deleteQuietly( file );
}
}
@ -563,7 +575,7 @@ public class MetadataTools
public void updateMetadata( ManagedRepositoryContent managedRepository, ProjectReference reference )
throws LayoutException, RepositoryMetadataException, IOException, ContentNotFoundException
{
File metadataFile = new File( managedRepository.getRepoRoot(), toPath( reference ) );
Path metadataFile = Paths.get( managedRepository.getRepoRoot(), toPath( reference ) );
long lastUpdated = getExistingLastUpdated( metadataFile );
@ -578,11 +590,11 @@ public class MetadataTools
// TODO: do we know this information instead?
// Set<Plugin> allPlugins = managedRepository.getPlugins( reference );
Set<Plugin> allPlugins;
if ( metadataFile.exists() )
if ( Files.exists(metadataFile))
{
try
{
allPlugins = new LinkedHashSet<Plugin>( MavenMetadataReader.read( metadataFile ).getPlugins() );
allPlugins = new LinkedHashSet<Plugin>( MavenMetadataReader.read( metadataFile.toFile() ).getPlugins() );
}
catch ( XMLException e )
{
@ -638,7 +650,7 @@ public class MetadataTools
// Save the metadata model to disk.
RepositoryMetadataWriter.write( metadata, metadataFile );
ChecksummedFile checksum = new ChecksummedFile( metadataFile );
ChecksummedFile checksum = new ChecksummedFile( metadataFile.toFile() );
checksum.fixChecksums( algorithms );
}
@ -733,9 +745,9 @@ public class MetadataTools
}
}
private long getExistingLastUpdated( File metadataFile )
private long getExistingLastUpdated( Path metadataFile )
{
if ( !metadataFile.exists() )
if ( !Files.exists(metadataFile) )
{
// Doesn't exist.
return 0;
@ -743,7 +755,7 @@ public class MetadataTools
try
{
ArchivaRepositoryMetadata metadata = MavenMetadataReader.read( metadataFile );
ArchivaRepositoryMetadata metadata = MavenMetadataReader.read( metadataFile.toFile() );
return getLastUpdated( metadata );
}
@ -773,7 +785,7 @@ public class MetadataTools
public void updateMetadata( ManagedRepositoryContent managedRepository, VersionedReference reference )
throws LayoutException, RepositoryMetadataException, IOException, ContentNotFoundException
{
File metadataFile = new File( managedRepository.getRepoRoot(), toPath( reference ) );
Path metadataFile = Paths.get( managedRepository.getRepoRoot(), toPath( reference ) );
long lastUpdated = getExistingLastUpdated( metadataFile );
@ -878,7 +890,7 @@ public class MetadataTools
// Save the metadata model to disk.
RepositoryMetadataWriter.write( metadata, metadataFile );
ChecksummedFile checksum = new ChecksummedFile( metadataFile );
ChecksummedFile checksum = new ChecksummedFile( metadataFile.toFile() );
checksum.fixChecksums( algorithms );
}
@ -936,39 +948,28 @@ public class MetadataTools
path = path.substring( 0, idx );
}
File repoDir = new File( managedRepository.getRepoRoot(), path );
Path repoDir = Paths.get( managedRepository.getRepoRoot(), path );
if ( !repoDir.exists() )
if ( !Files.exists(repoDir))
{
throw new IOException( "Unable to gather the list of snapshot versions on a non-existant directory: "
+ repoDir.getAbsolutePath() );
+ repoDir.toAbsolutePath() );
}
if ( !repoDir.isDirectory() )
if ( !Files.isDirectory( repoDir ))
{
throw new IOException(
"Unable to gather the list of snapshot versions on a non-directory: " + repoDir.getAbsolutePath() );
"Unable to gather the list of snapshot versions on a non-directory: " + repoDir.toAbsolutePath() );
}
File repoFiles[] = repoDir.listFiles();
for ( int i = 0; i < repoFiles.length; i++ )
{
if ( repoFiles[i].isDirectory() )
{
// Skip it. it's a directory.
continue;
}
String relativePath = PathUtil.getRelative( managedRepository.getRepoRoot(), repoFiles[i] );
if ( filetypes.matchesArtifactPattern( relativePath ) )
{
ArtifactReference artifact = managedRepository.toArtifactReference( relativePath );
return artifact;
try(Stream<Path> stream = Files.list(repoDir)) {
String result = stream.filter( Files::isRegularFile ).map( path1 ->
PathUtil.getRelative( managedRepository.getRepoRoot(), path1.toFile() )
).filter( filetypes::matchesArtifactPattern ).findFirst().orElse( null );
if (result!=null) {
return managedRepository.toArtifactReference( result );
}
}
// No artifact was found.
return null;
}

View File

@ -19,21 +19,21 @@ package org.apache.archiva.repository.metadata;
* under the License.
*/
import org.apache.archiva.common.utils.FileUtils;
import org.apache.archiva.model.ArchivaRepositoryMetadata;
import org.apache.archiva.model.Plugin;
import org.apache.archiva.xml.XMLException;
import org.apache.archiva.xml.XMLWriter;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
@ -44,11 +44,11 @@ import java.util.List;
*/
public class RepositoryMetadataWriter
{
public static void write( ArchivaRepositoryMetadata metadata, File outputFile )
public static void write( ArchivaRepositoryMetadata metadata, Path outputFile )
throws RepositoryMetadataException
{
boolean thrown = false;
try (FileWriter writer = new FileWriter( outputFile ))
try (FileWriter writer = new FileWriter( outputFile.toFile() ))
{
write( metadata, writer );
writer.flush();
@ -57,7 +57,7 @@ public class RepositoryMetadataWriter
{
thrown = true;
throw new RepositoryMetadataException(
"Unable to write metadata file: " + outputFile.getAbsolutePath() + " - " + e.getMessage(), e );
"Unable to write metadata file: " + outputFile.toAbsolutePath() + " - " + e.getMessage(), e );
}
finally
{

View File

@ -71,6 +71,9 @@ import javax.ws.rs.core.Response;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@ -94,6 +97,8 @@ public class DefaultBrowseService
implements BrowseService
{
private Charset ARTIFACT_CONTENT_ENCODING=Charset.forName( "UTF-8" );
@Inject
private DependencyTreeBuilder dependencyTreeBuilder;
@ -706,8 +711,8 @@ public class DefaultBrowseService
ArchivaArtifact archivaArtifact = new ArchivaArtifact( groupId, artifactId, version, classifier,
StringUtils.isEmpty( type ) ? "jar" : type,
repoId );
File file = managedRepositoryContent.toFile( archivaArtifact );
if ( file.exists() )
Path file = managedRepositoryContent.toFile( archivaArtifact );
if ( Files.exists(file) )
{
return readFileEntries( file, path, repoId );
}
@ -783,8 +788,8 @@ public class DefaultBrowseService
ArchivaArtifact archivaArtifact = new ArchivaArtifact( groupId, artifactId, version, classifier,
StringUtils.isEmpty( type ) ? "jar" : type,
repoId );
File file = managedRepositoryContent.toFile( archivaArtifact );
if ( !file.exists() )
Path file = managedRepositoryContent.toFile( archivaArtifact );
if ( !Files.exists(file) )
{
log.debug( "file: {} not exists for repository: {} try next repository", file, repoId );
continue;
@ -792,18 +797,18 @@ public class DefaultBrowseService
if ( StringUtils.isNotBlank( path ) )
{
// zip entry of the path -> path must a real file entry of the archive
JarFile jarFile = new JarFile( file );
JarFile jarFile = new JarFile( file.toFile() );
ZipEntry zipEntry = jarFile.getEntry( path );
try (InputStream inputStream = jarFile.getInputStream( zipEntry ))
{
return new ArtifactContent( IOUtils.toString( inputStream ), repoId );
return new ArtifactContent( IOUtils.toString( inputStream, ARTIFACT_CONTENT_ENCODING ), repoId );
}
finally
{
closeQuietly( jarFile );
}
}
return new ArtifactContent( FileUtils.readFileToString( file ), repoId );
return new ArtifactContent( new String(Files.readAllBytes( file ), ARTIFACT_CONTENT_ENCODING), repoId );
}
}
catch ( IOException e )
@ -857,9 +862,9 @@ public class DefaultBrowseService
StringUtils.isEmpty( classifier )
? ""
: classifier, "jar", repoId );
File file = managedRepositoryContent.toFile( archivaArtifact );
Path file = managedRepositoryContent.toFile( archivaArtifact );
if ( file != null && file.exists() )
if ( file != null && Files.exists(file) )
{
return true;
}
@ -867,13 +872,13 @@ public class DefaultBrowseService
// in case of SNAPSHOT we can have timestamped version locally !
if ( StringUtils.endsWith( version, VersionUtil.SNAPSHOT ) )
{
File metadataFile = new File( file.getParent(), MetadataTools.MAVEN_METADATA );
if ( metadataFile.exists() )
Path metadataFile = file.getParent().resolve(MetadataTools.MAVEN_METADATA );
if ( Files.exists(metadataFile) )
{
try
{
ArchivaRepositoryMetadata archivaRepositoryMetadata =
MavenMetadataReader.read( metadataFile );
MavenMetadataReader.read( metadataFile.toFile() );
int buildNumber = archivaRepositoryMetadata.getSnapshotVersion().getBuildNumber();
String timeStamp = archivaRepositoryMetadata.getSnapshotVersion().getTimestamp();
// rebuild file name with timestamped version and build number
@ -884,9 +889,9 @@ public class DefaultBrowseService
.append( ( StringUtils.isEmpty( classifier ) ? "" : "-" + classifier ) ) //
.append( ".jar" ).toString();
File timeStampFile = new File( file.getParent(), timeStampFileName );
log.debug( "try to find timestamped snapshot version file: {}", timeStampFile.getPath() );
if ( timeStampFile.exists() )
Path timeStampFile = file.getParent().resolve( timeStampFileName );
log.debug( "try to find timestamped snapshot version file: {}", timeStampFile.toAbsolutePath() );
if ( Files.exists(timeStampFile) )
{
return true;
}
@ -900,9 +905,9 @@ public class DefaultBrowseService
String path = managedRepositoryContent.toPath( archivaArtifact );
file = connectors.fetchFromProxies( managedRepositoryContent, path );
file = connectors.fetchFromProxies( managedRepositoryContent, path ).toPath();
if ( file != null && file.exists() )
if ( file != null && Files.exists(file) )
{
// download pom now
String pomPath = StringUtils.substringBeforeLast( path, ".jar" ) + ".pom";
@ -1093,7 +1098,7 @@ public class DefaultBrowseService
}
}
protected List<ArtifactContentEntry> readFileEntries(final File file, final String filterPath, final String repoId )
protected List<ArtifactContentEntry> readFileEntries(final Path file, final String filterPath, final String repoId )
throws IOException
{
String cleanedfilterPath = filterPath==null ? "" : (StringUtils.startsWith(filterPath, "/") ?
@ -1103,7 +1108,7 @@ public class DefaultBrowseService
if (!StringUtils.endsWith(cleanedfilterPath,"/") && !StringUtils.isEmpty(cleanedfilterPath)) {
filterDepth++;
}
JarFile jarFile = new JarFile( file );
JarFile jarFile = new JarFile( file.toFile() );
try
{
Enumeration<JarEntry> jarEntryEnumeration = jarFile.entries();

View File

@ -613,7 +613,7 @@ public class DefaultRepositoriesService
projectMetadata.setReleasedVersion( latestVersion );
}
RepositoryMetadataWriter.write( projectMetadata, projectMetadataFile );
RepositoryMetadataWriter.write( projectMetadata, projectMetadataFile.toPath() );
if ( fixChecksums )
{
@ -1187,7 +1187,7 @@ public class DefaultRepositoriesService
metadata.setLastUpdatedTimestamp( lastUpdatedTimestamp );
metadata.setAvailableVersions( availableVersions );
RepositoryMetadataWriter.write( metadata, metadataFile );
RepositoryMetadataWriter.write( metadata, metadataFile.toPath() );
ChecksummedFile checksum = new ChecksummedFile( metadataFile );
checksum.fixChecksums( algorithms );
}

View File

@ -26,6 +26,7 @@ import org.apache.archiva.maven2.model.Artifact;
import org.apache.commons.io.FilenameUtils;
import java.io.File;
import java.nio.file.Path;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
@ -79,7 +80,7 @@ public class ArtifactBuilder
ref.setClassifier( classifier );
ref.setType( type );
File file = managedRepositoryContent.toFile( ref );
Path file = managedRepositoryContent.toFile( ref );
String extension = getExtensionFromFile(file);
@ -124,10 +125,10 @@ public class ArtifactBuilder
/**
* Extract file extension
*/
String getExtensionFromFile( File file )
String getExtensionFromFile( Path file )
{
// we are just interested in the section after the last -
String[] parts = file.getName().split( "-" );
String[] parts = file.getFileName().toString().split( "-" );
if ( parts.length > 0 )
{
// get anything after a dot followed by a letter a-z, including other dots
@ -139,7 +140,7 @@ public class ArtifactBuilder
}
}
// just in case
return FilenameUtils.getExtension( file.getName() );
return FilenameUtils.getExtension( file.toFile().getName() );
}
}

View File

@ -28,6 +28,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
@ -56,7 +58,7 @@ public class ArtifactContentEntriesTests
throws Exception
{
File file = new File( getBasedir(),
Path file = Paths.get( getBasedir(),
"src/test/repo-with-osgi/commons-logging/commons-logging/1.1/commons-logging-1.1.jar" );
List<ArtifactContentEntry> artifactContentEntries = browseService.readFileEntries( file, null, "foo" );
@ -74,7 +76,7 @@ public class ArtifactContentEntriesTests
throws Exception
{
File file = new File( getBasedir(),
Path file = Paths.get( getBasedir(),
"src/test/repo-with-osgi/commons-logging/commons-logging/1.1/commons-logging-1.1.jar" );
List<ArtifactContentEntry> artifactContentEntries = browseService.readFileEntries( file, "", "foo" );
@ -92,7 +94,7 @@ public class ArtifactContentEntriesTests
throws Exception
{
File file = new File( getBasedir(),
Path file = Paths.get( getBasedir(),
"src/test/repo-with-osgi/commons-logging/commons-logging/1.1/commons-logging-1.1.jar" );
List<ArtifactContentEntry> artifactContentEntries = browseService.readFileEntries( file, "/", "foo" );
@ -110,7 +112,7 @@ public class ArtifactContentEntriesTests
throws Exception
{
File file = new File( getBasedir(),
Path file = Paths.get( getBasedir(),
"src/test/repo-with-osgi/commons-logging/commons-logging/1.1/commons-logging-1.1.jar" );
List<ArtifactContentEntry> artifactContentEntries = browseService.readFileEntries( file, "org", "foo" );
@ -127,7 +129,7 @@ public class ArtifactContentEntriesTests
throws Exception
{
File file = new File( getBasedir(),
Path file = Paths.get( getBasedir(),
"src/test/repo-with-osgi/commons-logging/commons-logging/1.1/commons-logging-1.1.jar" );
List<ArtifactContentEntry> artifactContentEntries =
@ -145,7 +147,7 @@ public class ArtifactContentEntriesTests
throws Exception
{
File file = new File( getBasedir(),
Path file = Paths.get( getBasedir(),
"src/test/repo-with-osgi/commons-logging/commons-logging/1.1/commons-logging-1.1.jar" );
List<ArtifactContentEntry> artifactContentEntries =

View File

@ -21,6 +21,7 @@ package org.apache.archiva.rest.services.utils;
import static org.assertj.core.api.Assertions.*;
import java.io.File;
import java.nio.file.Paths;
import org.easymock.TestSubject;
import org.junit.Test;
@ -33,36 +34,36 @@ public class ArtifactBuilderTest
@Test
public void testBuildSnapshot()
{
assertThat( builder.getExtensionFromFile( new File( "/tmp/foo-2.3-20141119.064321-40.jar" ) ) ).isEqualTo( "jar" );
assertThat( builder.getExtensionFromFile( Paths.get( "/tmp/foo-2.3-20141119.064321-40.jar" ) ) ).isEqualTo( "jar" );
}
@Test
public void testBuildPom()
{
assertThat( builder.getExtensionFromFile( new File( "/tmp/foo-1.0.pom" ) ) ).isEqualTo( "pom" );
assertThat( builder.getExtensionFromFile( Paths.get( "/tmp/foo-1.0.pom" ) ) ).isEqualTo( "pom" );
}
@Test
public void testBuildJar()
{
assertThat( builder.getExtensionFromFile( new File( "/tmp/foo-1.0-sources.jar" ) ) ).isEqualTo( "jar" );
assertThat( builder.getExtensionFromFile( Paths.get( "/tmp/foo-1.0-sources.jar" ) ) ).isEqualTo( "jar" );
}
@Test
public void testBuildTarGz()
{
assertThat( builder.getExtensionFromFile( new File( "/tmp/foo-1.0.tar.gz" ) ) ).isEqualTo( "tar.gz" );
assertThat( builder.getExtensionFromFile( Paths.get( "/tmp/foo-1.0.tar.gz" ) ) ).isEqualTo( "tar.gz" );
}
@Test
public void testBuildPomZip()
{
assertThat( builder.getExtensionFromFile( new File( "/tmp/foo-1.0.pom.zip" ) ) ).isEqualTo( "pom.zip" );
assertThat( builder.getExtensionFromFile( Paths.get( "/tmp/foo-1.0.pom.zip" ) ) ).isEqualTo( "pom.zip" );
}
@Test
public void testBuildR00()
{
assertThat( builder.getExtensionFromFile( new File( "/tmp/foo-1.0.r00" ) ) ).isEqualTo( "r00" );
assertThat( builder.getExtensionFromFile( Paths.get( "/tmp/foo-1.0.r00" ) ) ).isEqualTo( "r00" );
}
}

View File

@ -643,7 +643,7 @@ public class DefaultFileUploadService
projectMetadata.setReleasedVersion( latestVersion );
}
RepositoryMetadataWriter.write( projectMetadata, projectMetadataFile );
RepositoryMetadataWriter.write( projectMetadata, projectMetadataFile.toPath() );
if ( fixChecksums )
{
@ -677,7 +677,7 @@ public class DefaultFileUploadService
metadata.getSnapshotVersion().setTimestamp( timestamp );
metadata.setLastUpdatedTimestamp( lastUpdatedTimestamp );
RepositoryMetadataWriter.write( metadata, metadataFile );
RepositoryMetadataWriter.write( metadata, metadataFile.toPath() );
if ( fixChecksums )
{

View File

@ -1269,7 +1269,7 @@ public class ArchivaDavResourceFactory
}
outputFile.getParentFile().mkdirs();
RepositoryMetadataWriter.write( mergedMetadata, outputFile );
RepositoryMetadataWriter.write( mergedMetadata, outputFile.toPath() );
createChecksumFile( outputFilename, digestSha1 );
createChecksumFile( outputFilename, digestMd5 );

View File

@ -89,6 +89,7 @@ import java.io.IOException;
import java.io.Reader;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@ -687,9 +688,9 @@ public class Maven2RepositoryStorage
connectors.fetchFromProxies( managedRepository, pomReference );
// Open and read the POM from the managed repo
File pom = managedRepository.toFile( pomReference );
Path pom = managedRepository.toFile( pomReference );
if ( !pom.exists() )
if ( !Files.exists(pom) )
{
return;
}
@ -699,7 +700,7 @@ public class Maven2RepositoryStorage
// MavenXpp3Reader leaves the file open, so we need to close it ourselves.
Model model = null;
try (Reader reader = Files.newBufferedReader( pom.toPath(), Charset.defaultCharset() ))
try (Reader reader = Files.newBufferedReader( pom, Charset.defaultCharset() ))
{
model = MAVEN_XPP_3_READER.read( reader );
}

View File

@ -40,6 +40,9 @@ import javax.inject.Inject;
import javax.inject.Named;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
@ -167,25 +170,25 @@ public class ManagedDefaultRepositoryContent
public Set<ArtifactReference> getRelatedArtifacts( ArtifactReference reference )
throws ContentNotFoundException
{
File artifactFile = toFile( reference );
File repoDir = artifactFile.getParentFile();
Path artifactFile = toFile( reference );
Path repoDir = artifactFile.getParent();
if ( !repoDir.exists() )
if ( !Files.exists(repoDir))
{
throw new ContentNotFoundException(
"Unable to get related artifacts using a non-existant directory: " + repoDir.getAbsolutePath() );
"Unable to get related artifacts using a non-existant directory: " + repoDir.toAbsolutePath() );
}
if ( !repoDir.isDirectory() )
if ( !Files.isDirectory( repoDir ) )
{
throw new ContentNotFoundException(
"Unable to get related artifacts using a non-directory: " + repoDir.getAbsolutePath() );
"Unable to get related artifacts using a non-directory: " + repoDir.toAbsolutePath() );
}
Set<ArtifactReference> foundArtifacts = new HashSet<>();
// First gather up the versions found as artifacts in the managed repository.
File repoFiles[] = repoDir.listFiles();
File repoFiles[] = repoDir.toFile().listFiles();
for (File repoFile : repoFiles)
{
if (repoFile.isDirectory()) {
@ -350,8 +353,8 @@ public class ManagedDefaultRepositoryContent
@Override
public boolean hasContent( ArtifactReference reference )
{
File artifactFile = toFile( reference );
return artifactFile.exists() && artifactFile.isFile();
Path artifactFile = toFile( reference );
return Files.exists(artifactFile) && Files.isRegularFile( artifactFile );
}
@Override
@ -406,15 +409,15 @@ public class ManagedDefaultRepositoryContent
}
@Override
public File toFile( ArtifactReference reference )
public Path toFile( ArtifactReference reference )
{
return new File( repository.getLocation(), toPath( reference ) );
return Paths.get( repository.getLocation(), toPath( reference ) );
}
@Override
public File toFile( ArchivaArtifact reference )
public Path toFile( ArchivaArtifact reference )
{
return new File( repository.getLocation(), toPath( reference ) );
return Paths.get( repository.getLocation(), toPath( reference ) );
}
/**

View File

@ -329,7 +329,7 @@ public class Maven2RepositoryMerger
projectMetadata.setReleasedVersion( latestVersion );
}
RepositoryMetadataWriter.write( projectMetadata, projectMetaDataFileIntargetRepo );
RepositoryMetadataWriter.write( projectMetadata, projectMetaDataFileIntargetRepo.toPath() );
}
@ -346,7 +346,7 @@ public class Maven2RepositoryMerger
}
versionMetadata.setLastUpdatedTimestamp( lastUpdatedTimestamp );
RepositoryMetadataWriter.write( versionMetadata, versionMetaDataFileInTargetRepo );
RepositoryMetadataWriter.write( versionMetadata, versionMetaDataFileInTargetRepo.toPath() );
}
private ArchivaRepositoryMetadata getMetadata( File metadataFile )