Browse Source

Switching some more files to java.nio

pull/46/head
Martin Stockhammer 6 years ago
parent
commit
5806dc2980

+ 1
- 2
archiva-cli/src/main/java/org/apache/archiva/cli/ArchivaCli.java View 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 );


+ 3
- 2
archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/archiva/common/utils/BaseFile.java View 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 )

+ 21
- 29
archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/archiva/common/utils/PathUtil.java View 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));
}
}

+ 7
- 7
archiva-modules/archiva-base/archiva-common/src/test/java/org/apache/archiva/common/utils/PathUtilTest.java View 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( "/." ) )

+ 6
- 5
archiva-modules/archiva-base/archiva-common/src/test/java/org/apache/archiva/common/utils/ResourceUtils.java View 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;

+ 1
- 1
archiva-modules/archiva-base/archiva-converter/src/main/java/org/apache/archiva/converter/legacy/DefaultLegacyRepositoryConverter.java View 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" );

+ 3
- 20
archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/metadata/MetadataTools.java View 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 );

+ 1
- 1
archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/archiva/webdav/ArchivaDavResourceFactory.java View 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 );

+ 2
- 2
archiva-modules/plugins/problem-reports/src/test/java/org/apache/archiva/reports/consumers/DuplicateArtifactsConsumerTest.java View 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 );


Loading…
Cancel
Save