]> source.dussan.org Git - archiva.git/commitdiff
Switching some more files to java.nio
authorMartin Stockhammer <martin.stockhammer@ars.de>
Wed, 20 Sep 2017 19:39:27 +0000 (21:39 +0200)
committerMartin Stockhammer <martin.stockhammer@ars.de>
Wed, 20 Sep 2017 19:39:27 +0000 (21:39 +0200)
archiva-cli/src/main/java/org/apache/archiva/cli/ArchivaCli.java
archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/archiva/common/utils/BaseFile.java
archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/archiva/common/utils/PathUtil.java
archiva-modules/archiva-base/archiva-common/src/test/java/org/apache/archiva/common/utils/PathUtilTest.java
archiva-modules/archiva-base/archiva-common/src/test/java/org/apache/archiva/common/utils/ResourceUtils.java
archiva-modules/archiva-base/archiva-converter/src/main/java/org/apache/archiva/converter/legacy/DefaultLegacyRepositoryConverter.java
archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/metadata/MetadataTools.java
archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/archiva/webdav/ArchivaDavResourceFactory.java
archiva-modules/plugins/problem-reports/src/test/java/org/apache/archiva/reports/consumers/DuplicateArtifactsConsumerTest.java

index f853db81d399664ff26c2eb9130f245037720f0d..dad00f651b31fd33a7c7dfc1fd75b20597edc7eb 100644 (file)
@@ -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 );
 
index 622c284d5e7dd17e5866c58cb2b3270609d30525..bc2db2a9fc4073bd2ae7ccc5140687dd6edb3f6e 100644 (file)
@@ -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 )
index ddafc3e0fe0930cc87623cd61c44a89f6ffac349..e029e0d289d71bf74d507633015247d85bffefbf 100644 (file)
@@ -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));
     }
 }
index 5dc75f6a4ea73742967b95295e8fbb7f1b08d235..5fcbebcba3e24e94318ca904f0732f39e97cef52 100644 (file)
@@ -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( "/." ) )
index 5ba7912cae3710f1f34197ff946dae02753983d7..6241bc4852aaf7686b4ff61a308744425f6c88de 100644 (file)
@@ -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;
 
 /**
  * <code>ResourceUtils</code>
@@ -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;
index a0e8497c8d8c4dd4e4c4c381183cc1b31bbb7a8f..ff04828e388b62daf89dcc648cfcf6774b91a463 100644 (file)
@@ -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" );
index 7b6b95bd8897e43d138ecf3b7ff9e863743f9fd0..8706759a9e5658209048ade4e1a7785f09cafacf 100644 (file)
@@ -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<Path> 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 );
index 81e920eace62f33f2b5c37f4601084e356cdeb1b..1f5e0be5e125ad0aac085471caae76db65898f34 100644 (file)
@@ -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 );
index 4af6bc7ca5a7feb3623eca78f8dc105d9f256d3e..7849536b618066e9fd1b4075187cf8aabb6ea30c 100644 (file)
@@ -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 );