From 5806dc2980d390405dd9c2785fb8de3c81cd0bcb Mon Sep 17 00:00:00 2001 From: Martin Stockhammer Date: Wed, 20 Sep 2017 21:39:27 +0200 Subject: [PATCH] Switching some more files to java.nio --- .../org/apache/archiva/cli/ArchivaCli.java | 3 +- .../apache/archiva/common/utils/BaseFile.java | 5 +- .../apache/archiva/common/utils/PathUtil.java | 50 ++++++++----------- .../archiva/common/utils/PathUtilTest.java | 14 +++--- .../archiva/common/utils/ResourceUtils.java | 11 ++-- .../DefaultLegacyRepositoryConverter.java | 2 +- .../repository/metadata/MetadataTools.java | 23 ++------- .../webdav/ArchivaDavResourceFactory.java | 2 +- .../DuplicateArtifactsConsumerTest.java | 4 +- 9 files changed, 45 insertions(+), 69 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 f853db81d..dad00f651 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 @@ -36,7 +36,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; -import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; @@ -168,7 +167,7 @@ public class ArchivaCli throws ConsumerException, MalformedURLException { ManagedRepository repo = new ManagedRepository(); - repo.setId( new File( path ).getName() ); + repo.setId( Paths.get( path ).getFileName().toString()); repo.setName( "Archiva CLI Provided Repo" ); repo.setLocation( path ); diff --git a/archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/archiva/common/utils/BaseFile.java b/archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/archiva/common/utils/BaseFile.java index 622c284d5..bc2db2a9f 100644 --- a/archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/archiva/common/utils/BaseFile.java +++ b/archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/archiva/common/utils/BaseFile.java @@ -21,6 +21,7 @@ package org.apache.archiva.common.utils; import java.io.File; import java.net.URI; +import java.nio.file.Paths; /** * BaseFile - convenient File object that tracks the Base Directory and can provide relative path values @@ -40,7 +41,7 @@ public class BaseFile public BaseFile( File repoDir, File pathFile ) { - this( repoDir, PathUtil.getRelative( repoDir.getAbsolutePath(), pathFile ) ); + this( repoDir, PathUtil.getRelative(repoDir.getAbsolutePath(), pathFile.toPath() ) ); } public BaseFile( File parent, String child ) @@ -89,7 +90,7 @@ public class BaseFile public String getRelativePath() { - return PathUtil.getRelative( this.baseDir.getAbsolutePath(), this ); + return PathUtil.getRelative( this.baseDir.getAbsolutePath(), this.toPath() ); } public void setBaseDir( File baseDir ) diff --git a/archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/archiva/common/utils/PathUtil.java b/archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/archiva/common/utils/PathUtil.java index ddafc3e0f..e029e0d28 100644 --- a/archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/archiva/common/utils/PathUtil.java +++ b/archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/archiva/common/utils/PathUtil.java @@ -21,8 +21,9 @@ package org.apache.archiva.common.utils; import org.apache.commons.lang.StringUtils; -import java.io.File; import java.net.MalformedURLException; +import java.nio.file.Path; +import java.nio.file.Paths; /** * PathUtil - simple utility methods for path manipulation. @@ -39,18 +40,18 @@ public class PathUtil return path; } - return toUrl( new File( path ) ); + return toUrl( Paths.get( path ) ); } - public static String toUrl( File file ) + public static String toUrl( Path file ) { try { - return file.toURI().toURL().toExternalForm(); + return file.toUri().toURL().toExternalForm(); } catch ( MalformedURLException e ) { - String pathCorrected = StringUtils.replaceChars( file.getAbsolutePath(), '\\', '/' ); + String pathCorrected = StringUtils.replaceChars( file.toAbsolutePath().toString(), '\\', '/' ); if ( pathCorrected.startsWith( "file:/" ) ) { return pathCorrected; @@ -65,11 +66,21 @@ public class PathUtil * * @param basedir the basedir. * @param file the file to get the relative path for. - * @return the relative path to the child. (NOTE: this path will NOT start with a {@link File#separator} character) + * @return the relative path to the child. (NOTE: this path will NOT start with a file separator character) */ - public static String getRelative( String basedir, File file ) + public static String getRelative( Path basedir, Path file ) { - return getRelative( basedir, file.getAbsolutePath() ); + if (basedir.isAbsolute() && !file.isAbsolute()) { + return basedir.normalize().relativize(file.toAbsolutePath()).toString(); + } else if (!basedir.isAbsolute() && file.isAbsolute()) { + return basedir.toAbsolutePath().relativize(file.normalize()).toString(); + } else { + return basedir.normalize().relativize(file.normalize()).toString(); + } + } + + public static String getRelative(String basedir, Path file) { + return getRelative(Paths.get(basedir), file); } /** @@ -77,30 +88,11 @@ public class PathUtil * * @param basedir the basedir. * @param child the child path (can be a full path) - * @return the relative path to the child. (NOTE: this path will NOT start with a {@link File#separator} character) + * @return the relative path to the child. (NOTE: this path will NOT start with a file separator character) */ public static String getRelative( String basedir, String child ) { - if ( basedir.endsWith( "/" ) || basedir.endsWith( "\\" ) ) - { - basedir = basedir.substring( 0, basedir.length() - 1 ); - } - - if ( child.startsWith( basedir ) ) - { - // simple solution. - return child.substring( basedir.length() + 1 ); - } - - String absoluteBasedir = new File( basedir ).getAbsolutePath(); - if ( child.startsWith( absoluteBasedir ) ) - { - // resolved basedir solution. - return child.substring( absoluteBasedir.length() + 1 ); - } - // File is not within basedir. - throw new IllegalStateException( - "Unable to obtain relative path of file " + child + ", it is not within basedir " + basedir + "." ); + return getRelative(basedir, Paths.get(child)); } } diff --git a/archiva-modules/archiva-base/archiva-common/src/test/java/org/apache/archiva/common/utils/PathUtilTest.java b/archiva-modules/archiva-base/archiva-common/src/test/java/org/apache/archiva/common/utils/PathUtilTest.java index 5dc75f6a4..5fcbebcba 100644 --- a/archiva-modules/archiva-base/archiva-common/src/test/java/org/apache/archiva/common/utils/PathUtilTest.java +++ b/archiva-modules/archiva-base/archiva-common/src/test/java/org/apache/archiva/common/utils/PathUtilTest.java @@ -19,11 +19,11 @@ package org.apache.archiva.common.utils; * under the License. */ +import junit.framework.TestCase; import org.apache.commons.lang.StringUtils; -import java.io.File; - -import junit.framework.TestCase; +import java.nio.file.Path; +import java.nio.file.Paths; /** * PathUtilTest @@ -47,9 +47,9 @@ public class PathUtilTest public void testToUrlRelativePath() { - File workingDir = new File( "." ); + Path workingDir = Paths.get( "" ); - String workingDirname = StringUtils.replaceChars( workingDir.getAbsolutePath(), '\\', '/' ); + String workingDirname = StringUtils.replaceChars( workingDir.toAbsolutePath().toString(), '\\', '/' ); // Some JVM's retain the "." at the end of the path. Drop it. if ( workingDirname.endsWith( "/." ) ) @@ -70,9 +70,9 @@ public class PathUtilTest public void testToUrlUsingFileUrl() { - File workingDir = new File( "." ); + Path workingDir = Paths.get( "." ); - String workingDirname = StringUtils.replaceChars( workingDir.getAbsolutePath(), '\\', '/' ); + String workingDirname = StringUtils.replaceChars( workingDir.toAbsolutePath().toString(), '\\', '/' ); // Some JVM's retain the "." at the end of the path. Drop it. if ( workingDirname.endsWith( "/." ) ) diff --git a/archiva-modules/archiva-base/archiva-common/src/test/java/org/apache/archiva/common/utils/ResourceUtils.java b/archiva-modules/archiva-base/archiva-common/src/test/java/org/apache/archiva/common/utils/ResourceUtils.java index 5ba7912ca..6241bc485 100644 --- a/archiva-modules/archiva-base/archiva-common/src/test/java/org/apache/archiva/common/utils/ResourceUtils.java +++ b/archiva-modules/archiva-base/archiva-common/src/test/java/org/apache/archiva/common/utils/ResourceUtils.java @@ -21,9 +21,10 @@ package org.apache.archiva.common.utils; import org.apache.commons.lang.StringUtils; -import java.io.File; import java.io.IOException; import java.net.URL; +import java.nio.file.Path; +import java.nio.file.Paths; /** * ResourceUtils @@ -42,7 +43,7 @@ public class ResourceUtils * @param resourcePath the path to the resource relative to the root of the classpath * @return File a file object pointing to the resource on the classpath or null if the resource cannot be found */ - public static File getResource( String resourcePath ) + public static Path getResource(String resourcePath ) throws IOException { return getResource( resourcePath, null ); @@ -61,10 +62,10 @@ public class ResourceUtils * @param classloader the classloader who's classpath should be searched for the resource * @return File a file object pointing to the resource on the classpath or null if the resource cannot be found */ - public static File getResource( String resourcePath, ClassLoader classloader ) + public static Path getResource( String resourcePath, ClassLoader classloader ) throws IOException { - File testResource = null; + Path testResource = null; if ( StringUtils.isNotBlank( resourcePath ) ) { @@ -76,7 +77,7 @@ public class ResourceUtils { throw new IOException( "Could not find test resource at path '" + resourcePath + "'" ); } - testResource = new File( resourceUrl.getFile() ); + testResource = Paths.get( resourceUrl.getFile() ); } return testResource; diff --git a/archiva-modules/archiva-base/archiva-converter/src/main/java/org/apache/archiva/converter/legacy/DefaultLegacyRepositoryConverter.java b/archiva-modules/archiva-base/archiva-converter/src/main/java/org/apache/archiva/converter/legacy/DefaultLegacyRepositoryConverter.java index a0e8497c8..ff04828e3 100644 --- a/archiva-modules/archiva-base/archiva-converter/src/main/java/org/apache/archiva/converter/legacy/DefaultLegacyRepositoryConverter.java +++ b/archiva-modules/archiva-base/archiva-converter/src/main/java/org/apache/archiva/converter/legacy/DefaultLegacyRepositoryConverter.java @@ -88,7 +88,7 @@ public class DefaultLegacyRepositoryConverter { try { - String defaultRepositoryUrl = PathUtil.toUrl( repositoryDirectory.toFile() ); + String defaultRepositoryUrl = PathUtil.toUrl( repositoryDirectory ); ManagedRepository legacyRepository = new ManagedRepository(); legacyRepository.setId( "legacy" ); diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/metadata/MetadataTools.java b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/metadata/MetadataTools.java index 7b6b95bd8..8706759a9 100644 --- a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/metadata/MetadataTools.java +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/metadata/MetadataTools.java @@ -30,12 +30,7 @@ import org.apache.archiva.configuration.ConfigurationNames; import org.apache.archiva.configuration.FileTypes; import org.apache.archiva.configuration.ProxyConnectorConfiguration; import org.apache.archiva.maven2.metadata.MavenMetadataReader; -import org.apache.archiva.model.ArchivaRepositoryMetadata; -import org.apache.archiva.model.ArtifactReference; -import org.apache.archiva.model.Plugin; -import org.apache.archiva.model.ProjectReference; -import org.apache.archiva.model.SnapshotVersion; -import org.apache.archiva.model.VersionedReference; +import org.apache.archiva.model.*; import org.apache.archiva.redback.components.registry.Registry; import org.apache.archiva.redback.components.registry.RegistryListener; import org.apache.archiva.repository.ContentNotFoundException; @@ -60,19 +55,7 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.text.ParseException; import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.Collection; -import java.util.Collections; -import java.util.Date; -import java.util.HashMap; -import java.util.HashSet; -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.*; import java.util.regex.Matcher; import java.util.stream.Stream; @@ -964,7 +947,7 @@ public class MetadataTools try(Stream stream = Files.list(repoDir)) { String result = stream.filter( Files::isRegularFile ).map( path1 -> - PathUtil.getRelative( managedRepository.getRepoRoot(), path1.toFile() ) + PathUtil.getRelative( managedRepository.getRepoRoot(), path1 ) ).filter( filetypes::matchesArtifactPattern ).findFirst().orElse( null ); if (result!=null) { return managedRepository.toArtifactReference( result ); diff --git a/archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/archiva/webdav/ArchivaDavResourceFactory.java b/archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/archiva/webdav/ArchivaDavResourceFactory.java index 81e920eac..1f5e0be5e 100644 --- a/archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/archiva/webdav/ArchivaDavResourceFactory.java +++ b/archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/archiva/webdav/ArchivaDavResourceFactory.java @@ -707,7 +707,7 @@ public class ArchivaDavResourceFactory log.error("Could not create directory {}: {}", destDir, e.getMessage(), e); throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Could not create directory "+destDir ); } - String relPath = PathUtil.getRelative( rootDirectory.toAbsolutePath().toString(), destDir.toFile() ); + String relPath = PathUtil.getRelative( rootDirectory.toAbsolutePath().toString(), destDir ); log.debug( "Creating destination directory '{}' (current user '{}')", destDir.getFileName(), activePrincipal ); diff --git a/archiva-modules/plugins/problem-reports/src/test/java/org/apache/archiva/reports/consumers/DuplicateArtifactsConsumerTest.java b/archiva-modules/plugins/problem-reports/src/test/java/org/apache/archiva/reports/consumers/DuplicateArtifactsConsumerTest.java index 4af6bc7ca..7849536b6 100644 --- a/archiva-modules/plugins/problem-reports/src/test/java/org/apache/archiva/reports/consumers/DuplicateArtifactsConsumerTest.java +++ b/archiva-modules/plugins/problem-reports/src/test/java/org/apache/archiva/reports/consumers/DuplicateArtifactsConsumerTest.java @@ -41,8 +41,8 @@ import org.springframework.test.context.ContextConfiguration; import javax.inject.Inject; import javax.inject.Named; -import java.io.File; import java.nio.file.NoSuchFileException; +import java.nio.file.Paths; import java.util.Arrays; import java.util.Date; @@ -97,7 +97,7 @@ public class DuplicateArtifactsConsumerTest config = new ManagedRepository(); config.setId( TEST_REPO ); - config.setLocation( new File( "target/test-repository" ).getAbsolutePath() ); + config.setLocation( Paths.get( "target/test-repository" ).toAbsolutePath().toString() ); metadataRepository = mock( MetadataRepository.class ); -- 2.39.5