Migrating xmltools and proxy to java.nio

This commit is contained in:
Martin Stockhammer 2017-09-08 10:06:01 +02:00
parent 5437dfd6de
commit 98715c57ea
10 changed files with 81 additions and 118 deletions

View File

@ -75,7 +75,7 @@ public static ArchivaRepositoryMetadata read( Path metadataFile )
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();

View File

@ -28,22 +28,11 @@
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.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 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;

View File

@ -53,7 +53,6 @@
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.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 void setUp()
// 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 @@ protected void assertNoTempFiles( Path expectedFile )
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 @@ protected void copyDirectoryStructure( Path sourceDirectory, Path destDirectory
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() );
}
}
}

View File

@ -30,7 +30,6 @@
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;

View File

@ -22,21 +22,15 @@
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;

View File

@ -19,7 +19,6 @@
* 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 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 void get( String resourceName, File destination )
{
log.debug( ".get({}, {})", resourceName, destination );
delegate.get( resourceName, destination );
create( destination );
create( destination.toPath() );
}
@Override
@ -70,7 +71,7 @@ public boolean getIfNewer( String resourceName, File destination, long timestamp
log.info( ".getIfNewer({}, {}, {})", resourceName, destination, timestamp );
boolean result = delegate.getIfNewer( resourceName, destination, timestamp );
createIfMissing( destination );
createIfMissing( destination.toPath() );
return result;
}
@ -254,28 +255,27 @@ void setContentToGet( String content )
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 )

View File

@ -20,28 +20,18 @@
*/
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 )
{

View File

@ -19,12 +19,14 @@
* 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;
}

View File

@ -25,7 +25,6 @@
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.StringWriter;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Path;
/**
* LatinEntityResolutionReaderTest
@ -79,8 +79,8 @@ private String toStringFromReader( Reader input, int bufsize )
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 void assertProperRead( String sourcePath, String expectedPath, int bufsiz
{
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 @@ private void assertProperRead( StringBuilder expected, String sourcePath, int bu
{
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 void testReaderHugeBufsize()
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 );

View File

@ -19,7 +19,7 @@
* under the License.
*/
import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
@ -55,7 +55,7 @@ private void assertElementTexts( List<Element> elementList, String[] expectedTex
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 void testNoPrologBasicRead()
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 void testNoPrologEntitiesRead()
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 void testNoPrologUtf8Read()
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 void testPrologUtf8Read()
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" );