]> source.dussan.org Git - archiva.git/commitdiff
Migrating xmltools and proxy to java.nio
authorMartin Stockhammer <martin.stockhammer@ars.de>
Fri, 8 Sep 2017 08:06:01 +0000 (10:06 +0200)
committerMartin Stockhammer <martin.stockhammer@ars.de>
Fri, 8 Sep 2017 08:06:01 +0000 (10:06 +0200)
archiva-modules/archiva-base/archiva-maven2-metadata/src/main/java/org/apache/archiva/maven2/metadata/MavenMetadataReader.java
archiva-modules/archiva-base/archiva-proxy/src/main/java/org/apache/archiva/proxy/DefaultRepositoryProxyConnectors.java
archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/archiva/proxy/AbstractProxyTestCase.java
archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/archiva/proxy/ChecksumTransferTest.java
archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/archiva/proxy/MetadataTransferTest.java
archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/archiva/proxy/WagonDelegate.java
archiva-modules/archiva-base/archiva-xml-tools/src/main/java/org/apache/archiva/xml/XMLReader.java
archiva-modules/archiva-base/archiva-xml-tools/src/test/java/org/apache/archiva/xml/AbstractArchivaXmlTestCase.java
archiva-modules/archiva-base/archiva-xml-tools/src/test/java/org/apache/archiva/xml/LatinEntityResolutionReaderTest.java
archiva-modules/archiva-base/archiva-xml-tools/src/test/java/org/apache/archiva/xml/XMLReaderTest.java

index 22eb836e9f3b00c8e51031b136f214a732dbe7ba..023b8eaa27ab42e33dec0fa39a81495a894eb0dd 100644 (file)
@@ -75,7 +75,7 @@ public class MavenMetadataReader
         throws XMLException
     {
 
-        XMLReader xml = new XMLReader( "metadata", metadataFile.toFile() );
+        XMLReader xml = new XMLReader( "metadata", metadataFile );
         // invoke this to remove namespaces, see MRM-1136
         xml.removeNamespaces();
 
index dc100f0ca48b70dc4cf2c1eb6c4aeb9bb8e9f380..e4e60fe73145a4e0df3110f7bf5311452436e3a2 100644 (file)
@@ -28,22 +28,11 @@ import org.apache.archiva.common.filelock.FileLockException;
 import org.apache.archiva.common.filelock.FileLockManager;
 import org.apache.archiva.common.filelock.FileLockTimeoutException;
 import org.apache.archiva.common.filelock.Lock;
-import org.apache.archiva.configuration.ArchivaConfiguration;
-import org.apache.archiva.configuration.Configuration;
-import org.apache.archiva.configuration.ConfigurationNames;
-import org.apache.archiva.configuration.NetworkProxyConfiguration;
-import org.apache.archiva.configuration.ProxyConnectorConfiguration;
-import org.apache.archiva.configuration.ProxyConnectorRuleConfiguration;
+import org.apache.archiva.configuration.*;
 import org.apache.archiva.model.ArtifactReference;
 import org.apache.archiva.model.Keys;
 import org.apache.archiva.model.RepositoryURL;
-import org.apache.archiva.policies.DownloadErrorPolicy;
-import org.apache.archiva.policies.DownloadPolicy;
-import org.apache.archiva.policies.PolicyConfigurationException;
-import org.apache.archiva.policies.PolicyViolationException;
-import org.apache.archiva.policies.PostDownloadPolicy;
-import org.apache.archiva.policies.PreDownloadPolicy;
-import org.apache.archiva.policies.ProxyDownloadException;
+import org.apache.archiva.policies.*;
 import org.apache.archiva.policies.urlcache.UrlFailureCache;
 import org.apache.archiva.proxy.common.WagonFactory;
 import org.apache.archiva.proxy.common.WagonFactoryException;
@@ -54,11 +43,7 @@ import org.apache.archiva.proxy.model.RepositoryProxyConnectors;
 import org.apache.archiva.redback.components.registry.Registry;
 import org.apache.archiva.redback.components.registry.RegistryListener;
 import org.apache.archiva.redback.components.taskqueue.TaskQueueException;
-import org.apache.archiva.repository.ManagedRepositoryContent;
-import org.apache.archiva.repository.RemoteRepositoryContent;
-import org.apache.archiva.repository.RepositoryContentFactory;
-import org.apache.archiva.repository.RepositoryException;
-import org.apache.archiva.repository.RepositoryNotFoundException;
+import org.apache.archiva.repository.*;
 import org.apache.archiva.repository.metadata.MetadataTools;
 import org.apache.archiva.repository.metadata.RepositoryMetadataException;
 import org.apache.archiva.scheduler.ArchivaTaskScheduler;
@@ -84,18 +69,12 @@ 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.util.ArrayList;
-import java.util.Collections;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 import java.util.Map.Entry;
-import java.util.Properties;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.TimeUnit;
index 124e7a58b9acef0567416ec96dd33621c3306844..a8ffb633675e874690508a3253ddbc5f78bc0bb3 100644 (file)
@@ -53,7 +53,6 @@ import org.springframework.test.context.ContextConfiguration;
 
 import javax.inject.Inject;
 import java.io.BufferedReader;
-import java.io.File;
 import java.io.FileReader;
 import java.io.IOException;
 import java.nio.charset.Charset;
@@ -63,12 +62,9 @@ import java.nio.file.Paths;
 import java.nio.file.attribute.FileTime;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
-import java.util.ArrayList;
-import java.util.Calendar;
-import java.util.Collection;
-import java.util.Date;
-import java.util.Locale;
+import java.util.*;
 import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
 
 import static org.junit.Assert.*;
 
@@ -168,11 +164,11 @@ public abstract class AbstractProxyTestCase
 
         // Setup target (proxied to) repository.
         saveRemoteRepositoryConfig( ID_PROXIED1, "Proxied Repository 1",
-                                    new File( REPOPATH_PROXIED1 ).toURL().toExternalForm(), "default" );
+                                    Paths.get( REPOPATH_PROXIED1 ).toUri().toURL().toExternalForm(), "default" );
 
         // Setup target (proxied to) repository.
         saveRemoteRepositoryConfig( ID_PROXIED2, "Proxied Repository 2",
-                                    new File( REPOPATH_PROXIED2 ).toURL().toExternalForm(), "default" );
+                                    Paths.get( REPOPATH_PROXIED2 ).toUri().toURL().toExternalForm(), "default" );
 
         // Setup the proxy handler.
         //proxyHandler = applicationContext.getBean (RepositoryProxyConnectors) lookup( RepositoryProxyConnectors.class.getName() );
@@ -270,15 +266,19 @@ public abstract class AbstractProxyTestCase
             return;
         }
 
-        Collection<File> tmpFiles =
-            org.apache.commons.io.FileUtils.listFiles( workingDir.toFile(), new String[]{ "tmp" }, false );
-        if ( !tmpFiles.isEmpty() )
+        Collection<Path> tmpFiles = null;
+        try {
+            tmpFiles = Files.list(workingDir).filter(path -> Files.isRegularFile(path) && path.getFileName().toString().endsWith(".tmp")).collect(Collectors.toList());
+        } catch (IOException e) {
+            log.error("Could not retrieve tmpFiles {}", workingDir);
+        }
+        if ( tmpFiles!=null && !tmpFiles.isEmpty() )
         {
             StringBuilder emsg = new StringBuilder();
             emsg.append( "Found Temp Files in dir: " ).append( workingDir.toString() );
-            for ( File tfile : tmpFiles )
+            for ( Path tfile : tmpFiles )
             {
-                emsg.append( "\n   " ).append( tfile.getName() );
+                emsg.append( "\n   " ).append( tfile.getFileName().toString());
             }
             fail( emsg.toString() );
         }
@@ -300,44 +300,43 @@ public abstract class AbstractProxyTestCase
             throw new IOException( "Source directory doesn't exists (" + sourceDirectory.toAbsolutePath() + ")." );
         }
 
-        File[] files = sourceDirectory.toFile().listFiles();
+        Path[] files = Files.list(sourceDirectory).filter(path -> Files.isRegularFile(path)).toArray(Path[]::new);
 
         String sourcePath = sourceDirectory.toAbsolutePath().toString();
 
         for ( int i = 0; i < files.length; i++ )
         {
-            File file = files[i];
+            Path file = files[i];
 
-            String dest = file.getAbsolutePath();
+            String dest = file.toAbsolutePath().toString();
 
             dest = dest.substring( sourcePath.length() + 1 );
 
-            File destination = new File( destDirectory.toFile(), dest );
+            Path destination = destDirectory.resolve( dest );
 
-            if ( file.isFile() )
+            if ( Files.isRegularFile(file) )
             {
-                destination = destination.getParentFile();
+                destination = destination.getParent();
 
-                org.apache.commons.io.FileUtils.copyFile( file, new File( destination, file.getName() ), false );
+                org.apache.commons.io.FileUtils.copyFile( file.toFile(), destination.resolve( file.getFileName() ).toFile(), false );
                 // TODO: Change when there is a FileUtils.copyFileToDirectory(file, destination, boolean) option
                 //FileUtils.copyFileToDirectory( file, destination );
             }
-            else if ( file.isDirectory() )
+            else if ( Files.isDirectory(file) )
             {
-                if ( !".svn".equals( file.getName() ) )
+                if ( !".svn".equals( file.getFileName().toString() ) )
                 {
-                    if ( !destination.exists() && !destination.mkdirs() )
+                    if ( !Files.exists(destination))
                     {
-                        throw new IOException(
-                            "Could not create destination directory '" + destination.getAbsolutePath() + "'." );
+                        Files.createDirectories(destination);
                     }
 
-                    copyDirectoryStructure( file.toPath(), destination.toPath() );
+                    copyDirectoryStructure( file, destination );
                 }
             }
             else
             {
-                throw new IOException( "Unknown file type: " + file.getAbsolutePath() );
+                throw new IOException( "Unknown file type: " + file.toAbsolutePath() );
             }
         }
     }
index 51e53e76575c80865c0a367764ee5457257a6d1f..e9ade0479dc7c83bbbf903212304d1d07ee0b22e 100644 (file)
@@ -30,7 +30,6 @@ import org.easymock.EasyMock;
 import org.junit.Test;
 
 import java.io.File;
-import java.nio.file.CopyOption;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
index 7d453540dc0297b6946fbe686c0867ab44e36e81..b9a5f1ac3ffb19729c3e1ca7b2bd2df2630b2c0b 100644 (file)
@@ -22,21 +22,15 @@ package org.apache.archiva.proxy;
 import org.apache.archiva.common.utils.VersionUtil;
 import org.apache.archiva.configuration.ProxyConnectorConfiguration;
 import org.apache.archiva.maven2.metadata.MavenMetadataReader;
-import org.apache.archiva.model.ArchivaRepositoryMetadata;
-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.policies.CachedFailuresPolicy;
 import org.apache.archiva.policies.ChecksumPolicy;
 import org.apache.archiva.policies.ReleasesPolicy;
 import org.apache.archiva.policies.SnapshotsPolicy;
-import org.apache.archiva.proxy.model.ProxyFetchResult;
 import org.apache.archiva.repository.metadata.MetadataTools;
 import org.apache.archiva.repository.metadata.RepositoryMetadataException;
 import org.apache.archiva.repository.metadata.RepositoryMetadataWriter;
 import org.apache.commons.lang.StringUtils;
-import org.apache.maven.wagon.ResourceDoesNotExistException;
 import org.apache.maven.wagon.TransferFailedException;
 import org.custommonkey.xmlunit.DetailedDiff;
 import org.custommonkey.xmlunit.Diff;
index 56edc448dc9bbe36f4c74f7c65683b60b3576643..702fe99c72b23b910882abb4bb00552cd5eff37b 100644 (file)
@@ -19,7 +19,6 @@ package org.apache.archiva.proxy;
  * under the License.
  */
 
-import org.apache.commons.io.FileUtils;
 import org.apache.maven.wagon.ConnectionException;
 import org.apache.maven.wagon.ResourceDoesNotExistException;
 import org.apache.maven.wagon.TransferFailedException;
@@ -39,6 +38,8 @@ import org.springframework.stereotype.Service;
 import java.io.File;
 import java.io.IOException;
 import java.nio.charset.Charset;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.List;
 
 /**
@@ -60,7 +61,7 @@ public class WagonDelegate
     {
         log.debug( ".get({}, {})", resourceName, destination );
         delegate.get( resourceName, destination );
-        create( destination );
+        create( destination.toPath() );
     }
 
     @Override
@@ -70,7 +71,7 @@ public class WagonDelegate
         log.info( ".getIfNewer({}, {}, {})", resourceName, destination, timestamp );
 
         boolean result = delegate.getIfNewer( resourceName, destination, timestamp );
-        createIfMissing( destination );
+        createIfMissing( destination.toPath() );
         return result;
     }
 
@@ -254,28 +255,27 @@ public class WagonDelegate
         contentToGet = content;
     }
 
-    private void createIfMissing( File destination )
+    private void createIfMissing( Path destination )
     {
         // since the mock won't actually copy a file, create an empty one to simulate file existence
-        if ( !destination.exists() )
+        if ( !Files.exists(destination) )
         {
             create( destination );
         }
     }
 
-    private void create( File destination )
+    private void create( Path destination )
     {
         try
         {
-            destination.getParentFile().mkdirs();
+            Files.createDirectories(destination.getParent());
             if ( contentToGet == null )
             {
-                destination.createNewFile();
+                Files.createFile(destination);
             }
             else
             {
-                FileUtils.writeStringToFile( new File( destination.getAbsolutePath() ), contentToGet,
-                                             Charset.defaultCharset() );
+                org.apache.archiva.common.utils.FileUtils.writeStringToFile(destination, Charset.defaultCharset(), contentToGet);
             }
         }
         catch ( IOException e )
index d889e92d8e5d8d7a75dab98f702e6be61350a614..5d3f1cd0e4be49f2ce1e28aacbc3e1297747b9ee 100644 (file)
@@ -20,28 +20,18 @@ package org.apache.archiva.xml;
  */
 
 import org.apache.commons.lang.StringUtils;
-import org.dom4j.Attribute;
-import org.dom4j.Document;
-import org.dom4j.DocumentException;
-import org.dom4j.Element;
-import org.dom4j.Namespace;
-import org.dom4j.Node;
-import org.dom4j.QName;
-import org.dom4j.XPath;
+import org.dom4j.*;
 import org.dom4j.io.SAXReader;
 
-import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.nio.charset.Charset;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.*;
 
 /**
  * XMLReader - a set of common xml utility methods for reading content out of an xml file.
@@ -56,27 +46,27 @@ public class XMLReader
 
     private Map<String, String> namespaceMap = new HashMap<>();
 
-    public XMLReader( String type, File file )
+    public XMLReader( String type, Path file )
         throws XMLException
     {
-        if ( !file.exists() )
+        if ( !Files.exists(file) )
         {
-            throw new XMLException( "file does not exist: " + file.getAbsolutePath() );
+            throw new XMLException( "file does not exist: " + file.toAbsolutePath() );
         }
 
-        if ( !file.isFile() )
+        if ( !Files.isRegularFile(file) )
         {
-            throw new XMLException( "path is not a file: " + file.getAbsolutePath() );
+            throw new XMLException( "path is not a file: " + file.toAbsolutePath() );
         }
 
-        if ( !file.canRead() )
+        if ( !Files.isReadable(file) )
         {
-            throw new XMLException( "Cannot read xml file due to permissions: " + file.getAbsolutePath() );
+            throw new XMLException( "Cannot read xml file due to permissions: " + file.toAbsolutePath() );
         }
 
         try
         {
-            init( type, file.toURL() );
+            init( type, file.toUri().toURL() );
         }
         catch ( MalformedURLException e )
         {
index 5f340053f1437f7a268eb19cbd27101c78914aa1..83155b45375a2329d4acbaea5239b5c3e4832169 100644 (file)
@@ -19,12 +19,14 @@ package org.apache.archiva.xml;
  * under the License.
  */
 
-import java.io.File;
-
 import junit.framework.TestCase;
 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
 import org.junit.runner.RunWith;
 
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
 /**
  * AbstractArchivaXmlTestCase 
  *
@@ -42,17 +44,17 @@ public abstract class AbstractArchivaXmlTestCase
 
     protected static final String INFINITE_ARCHIVA = "The " + INFIN + " Archiva";
 
-    protected File getExampleXml( String filename )
+    protected Path getExampleXml(String filename )
     {
-        File examplesDir = new File( "src/test/examples" );
-        if ( !examplesDir.exists() )
+        Path examplesDir = Paths.get("src/test/examples");
+        if ( !Files.exists(examplesDir) )
         {
-            fail( "Missing the examples directory: " + examplesDir.getAbsolutePath() );
+            fail( "Missing the examples directory: " + examplesDir.toAbsolutePath() );
         }
-        File exampleFile = new File( examplesDir, filename );
-        if ( !exampleFile.exists() )
+        Path exampleFile = examplesDir.resolve( filename );
+        if ( !Files.exists(exampleFile) )
         {
-            fail( "Missing the example xml file: " + exampleFile.getAbsolutePath() );
+            fail( "Missing the example xml file: " + exampleFile.toAbsolutePath() );
         }
         return exampleFile;
     }
index b97e828fba247496fadd18b9d4c0795a388ec60b..c3c7dd0def5013fd5e32196585683e28eaa67fd3 100644 (file)
@@ -25,7 +25,6 @@ import org.junit.Assert;
 import org.junit.Test;
 
 import java.io.BufferedReader;
-import java.io.File;
 import java.io.FileReader;
 import java.io.IOException;
 import java.io.InputStream;
@@ -34,6 +33,7 @@ import java.io.Reader;
 import java.io.StringWriter;
 import java.net.URL;
 import java.nio.charset.Charset;
+import java.nio.file.Path;
 
 /**
  * LatinEntityResolutionReaderTest
@@ -79,8 +79,8 @@ public class LatinEntityResolutionReaderTest
     private String toStringFromExample( String examplePath )
         throws IOException
     {
-        File exampleFile = getExampleXml( examplePath );
-        FileReader fileReader = new FileReader( exampleFile );
+        Path exampleFile = getExampleXml( examplePath );
+        FileReader fileReader = new FileReader( exampleFile.toFile() );
         BufferedReader lineReader = new BufferedReader( fileReader );
         StringBuilder sb = new StringBuilder();
 
@@ -105,9 +105,9 @@ public class LatinEntityResolutionReaderTest
     {
         try
         {
-            File inputFile = getExampleXml( sourcePath );
+            Path inputFile = getExampleXml( sourcePath );
 
-            FileReader fileReader = new FileReader( inputFile );
+            FileReader fileReader = new FileReader( inputFile.toFile() );
             LatinEntityResolutionReader testReader = new LatinEntityResolutionReader( fileReader );
 
             String actualOutput = toStringFromReader( testReader, bufsize );
@@ -125,9 +125,9 @@ public class LatinEntityResolutionReaderTest
     {
         try
         {
-            File inputFile = getExampleXml( sourcePath );
+            Path inputFile = getExampleXml( sourcePath );
 
-            FileReader fileReader = new FileReader( inputFile );
+            FileReader fileReader = new FileReader( inputFile.toFile() );
             LatinEntityResolutionReader testReader = new LatinEntityResolutionReader( fileReader );
 
             String actualOutput = toStringFromReader( testReader, bufSize );
@@ -208,11 +208,11 @@ public class LatinEntityResolutionReaderTest
     public void testReaderLeftOver()
         throws IOException
     {
-        File inputFile = getExampleXml( "maven-metadata-leftover.xml" );
+        Path inputFile = getExampleXml( "maven-metadata-leftover.xml" );
         //Bits from RepositoryMetadataReader.read
         InputStream in = null;
         SAXReader reader = new SAXReader();
-        URL url = inputFile.toURL();
+        URL url = inputFile.toUri().toURL();
         in = url.openStream();
         InputStreamReader inReader = new InputStreamReader( in, Charset.forName( "UTF-8" ) );
         LatinEntityResolutionReader latinReader = new LatinEntityResolutionReader( inReader );
index 05c9855f2c069082ad8065f67191e4ae4b5e4ca9..fa99a4afa4e0d52021b4b4e1cb6aa017ff682b22 100644 (file)
@@ -19,7 +19,7 @@ package org.apache.archiva.xml;
  * under the License.
  */
 
-import java.io.File;
+import java.nio.file.Path;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -55,7 +55,7 @@ public class XMLReaderTest
     public void testNoPrologBasicRead()
         throws XMLException
     {
-        File xmlFile = getExampleXml( "no-prolog-basic.xml" );
+        Path xmlFile = getExampleXml( "no-prolog-basic.xml" );
         XMLReader reader = new XMLReader( "basic", xmlFile );
 
         List<Element> fruits = reader.getElementList( "//basic/fruits/fruit" );
@@ -66,7 +66,7 @@ public class XMLReaderTest
     public void testNoPrologEntitiesRead()
         throws XMLException
     {
-        File xmlFile = getExampleXml( "no-prolog-with-entities.xml" );
+        Path xmlFile = getExampleXml( "no-prolog-with-entities.xml" );
         XMLReader reader = new XMLReader( "basic", xmlFile );
 
         List<Element> names = reader.getElementList( "//basic/names/name" );
@@ -77,7 +77,7 @@ public class XMLReaderTest
     public void testNoPrologUtf8Read()
         throws XMLException
     {
-        File xmlFile = getExampleXml( "no-prolog-with-utf8.xml" );
+        Path xmlFile = getExampleXml( "no-prolog-with-utf8.xml" );
         XMLReader reader = new XMLReader( "basic", xmlFile );
 
         List<Element> names = reader.getElementList( "//basic/names/name" );
@@ -88,7 +88,7 @@ public class XMLReaderTest
     public void testPrologUtf8Read()
         throws XMLException
     {
-        File xmlFile = getExampleXml( "prolog-with-utf8.xml" );
+        Path xmlFile = getExampleXml( "prolog-with-utf8.xml" );
         XMLReader reader = new XMLReader( "basic", xmlFile );
 
         List<Element> names = reader.getElementList( "//basic/names/name" );
@@ -100,8 +100,8 @@ public class XMLReaderTest
     public void testProxiedMetadataRead()
         throws XMLException
     {
-        File xmlFile = getExampleXml( "maven-metadata-codehaus-snapshots.xml" );
-        XMLReader reader = new XMLReader( "metadata", xmlFile );        
+        Path xmlFile = getExampleXml( "maven-metadata-codehaus-snapshots.xml" );
+        XMLReader reader = new XMLReader( "metadata", xmlFile );
         reader.removeNamespaces();
         
         Element groupId = reader.getElement( "//metadata/groupId" );