]> source.dussan.org Git - archiva.git/commitdiff
First part in moving to java.nio
authorMartin Stockhammer <martin_s@apache.org>
Sat, 2 Sep 2017 11:14:33 +0000 (13:14 +0200)
committerMartin Stockhammer <martin_s@apache.org>
Sat, 2 Sep 2017 11:18:15 +0000 (13:18 +0200)
17 files changed:
archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/archiva/common/utils/FileUtils.java [new file with mode: 0644]
archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/archiva/consumers/core/repository/AbstractRepositoryPurge.java
archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/archiva/consumers/core/repository/DaysOldRepositoryPurge.java
archiva-modules/archiva-base/archiva-proxy/src/main/java/org/apache/archiva/proxy/DefaultRepositoryProxyConnectors.java
archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/ManagedRepositoryContent.java
archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/metadata/MetadataTools.java
archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/metadata/RepositoryMetadataWriter.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultBrowseService.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRepositoriesService.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/utils/ArtifactBuilder.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/ArtifactContentEntriesTests.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/utils/ArtifactBuilderTest.java
archiva-modules/archiva-web/archiva-web-common/src/main/java/org/apache/archiva/web/api/DefaultFileUploadService.java
archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/archiva/webdav/ArchivaDavResourceFactory.java
archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/Maven2RepositoryStorage.java
archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/repository/content/maven2/ManagedDefaultRepositoryContent.java
archiva-modules/plugins/stage-repository-merge/src/main/java/org/apache/archiva/stagerepository/merge/Maven2RepositoryMerger.java

diff --git a/archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/archiva/common/utils/FileUtils.java b/archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/archiva/common/utils/FileUtils.java
new file mode 100644 (file)
index 0000000..45a68ec
--- /dev/null
@@ -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
+        }
+
+
+    }
+}
index 9b1615f7664cb872620f004b08d12d28ee0912ca..44b7daa19102d19170df8a583f60cea7186ee4b0 100644 (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 )
                 {
index 2231ffc190b0c66729e14fe7e838079706f782a1..f893ca5115762dcb41d37d2b20cda9daab6f27ec 100644 (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 );
         }
index 1397c7eb5c87b4536cf4cf684b89361db62218df..3e8aab8c52c254a784cd598c61c4eb30bc981158 100644 (file)
@@ -672,7 +672,7 @@ public class DefaultRepositoryProxyConnectors
 
     private File toLocalFile( ManagedRepositoryContent repository, ArtifactReference artifact )
     {
-        return repository.toFile( artifact );
+        return repository.toFile( artifact ).toFile();
     }
 
     /**
index 13d75edc8b5f690a423937923cd3f3f5e954f2b8..643f3e98b39f7c442c9c5c22cfcbc62dc8e94c44 100644 (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
index 06038e960f7b06236a0e3dbfa7282447bc38a36b..71258fc082206fa350ffe1d5eb49305a399eee94 100644 (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;
     }
index 850210eae2462046a49680b67d0bcbf402fbb1e1..0e553f634a90f9e962585aabe020d393517ee94e 100644 (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
         {
index e2efea41961163d11f410f7cbd668d77edc96f83..4fc786fbb400da36bec588c87e7f5817e80af4b5 100644 (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();
index fc78a9a942106e8035f97b79e4849e5c577a8d3d..629a44d6c1d30a355c4ba2bc26841a76a574c672 100644 (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 );
     }
index b01bacf8dede9943634515e07d9dbb56f2eeab85..8ad379b1b90d7467c0895cfc864f4f9319e891ff 100644 (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() );
     }
 
 }
index 461cb80182f2b5fb150eec69243ead21cc47a572..8e692012b7c35d09a49f13a450d4b33bc346657b 100644 (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 =
index 8b67c43c6b06af5d19511f06052d836d1b2265b0..0fb6e917e8cf48080c77dac2555595cba6f1b79d 100644 (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" );
     }
 }
index af0164d9e623387c07a19bffe87e062f1bf05e47..b272489c13cca29dd9537d6154b8dfa6c9e86df3 100644 (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 )
         {
index d4958b113b62bd756b87c78b06abbd5054835125..3f4aeecfc77ebf3d8b6f1d6a6e9d8f0926041cfa 100644 (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 );
index efdb46087a50680d291fe69ad0dc1663f3973f99..37c1b91864702f73145acb4433b3278d9d367900 100644 (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 );
             }
index aa4ca8a5adc79a1207a7603ca8abf0cc752e707d..03bdfe0d9090f51549a0983b4219f229ca6ea2cd 100644 (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 ) );
     }
 
     /**
index 7a974db3ce6e9e9c397636836e1ac61fad6f9285..d209098cfc838639cf41920dc5cee849f159d72c 100644 (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 )