import org.apache.archiva.admin.model.RepositoryAdminException;
import org.apache.archiva.admin.model.beans.RepositoryGroup;
-import java.io.File;
+import java.nio.file.Path;
import java.util.List;
import java.util.Map;
Map<String, List<String>> getRepositoryToGroupMap()
throws RepositoryAdminException;
- File getMergedIndexDirectory( String repositoryGroupId );
+ Path getMergedIndexDirectory(String repositoryGroupId );
}
import javax.annotation.PostConstruct;
import javax.inject.Inject;
-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.Arrays;
import java.util.Collections;
@Inject
private MergedRemoteIndexesScheduler mergedRemoteIndexesScheduler;
- private File groupsDirectory;
+ private Path groupsDirectory;
@PostConstruct
public void initialize()
{
String appServerBase = getRegistry().getString( "appserver.base" );
- groupsDirectory = new File( appServerBase + File.separatorChar + "groups" );
- if ( !groupsDirectory.exists() )
+ groupsDirectory = Paths.get( appServerBase, "groups" );
+ if ( !Files.exists(groupsDirectory) )
{
- groupsDirectory.mkdirs();
+ Files.exists(groupsDirectory);
}
try
for ( RepositoryGroup repositoryGroup : getRepositoriesGroups() )
{
mergedRemoteIndexesScheduler.schedule( repositoryGroup,
- getMergedIndexDirectory( repositoryGroup.getId() ).toPath() );
+ getMergedIndexDirectory( repositoryGroup.getId() ));
// create the directory for each group if not exists
- File groupPath = new File( groupsDirectory, repositoryGroup.getId() );
- if ( !groupPath.exists() )
+ Path groupPath = groupsDirectory.resolve(repositoryGroup.getId() );
+ if ( !Files.exists(groupPath) )
{
- groupPath.mkdirs();
+ try {
+ Files.createDirectories(groupPath);
+ } catch (IOException e) {
+ log.error("Could not create directory {}", groupPath);
+ }
}
}
}
@Override
- public File getMergedIndexDirectory( String repositoryGroupId )
+ public Path getMergedIndexDirectory( String repositoryGroupId )
{
- return new File( groupsDirectory, repositoryGroupId );
+ return groupsDirectory.resolve( repositoryGroupId );
}
@Override
configuration.addRepositoryGroup( repositoryGroupConfiguration );
saveConfiguration( configuration );
triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.ADD_REPO_GROUP, auditInformation );
- mergedRemoteIndexesScheduler.schedule( repositoryGroup, getMergedIndexDirectory( repositoryGroup.getId() ).toPath() );
+ mergedRemoteIndexesScheduler.schedule( repositoryGroup, getMergedIndexDirectory( repositoryGroup.getId() ) );
return Boolean.TRUE;
}
triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.MODIFY_REPO_GROUP, auditInformation );
}
mergedRemoteIndexesScheduler.unschedule( repositoryGroup );
- mergedRemoteIndexesScheduler.schedule( repositoryGroup, getMergedIndexDirectory( repositoryGroup.getId() ).toPath() );
+ mergedRemoteIndexesScheduler.schedule( repositoryGroup, getMergedIndexDirectory( repositoryGroup.getId() ) );
return Boolean.TRUE;
}
import javax.annotation.PreDestroy;
import javax.inject.Inject;
import javax.inject.Named;
-import java.io.File;
import java.io.IOException;
-import java.net.MalformedURLException;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.*;
/**
* FIXME review the staging mechanism to have a per user session one
if ( deleteContent )
{
// TODO could be async ? as directory can be huge
- File dir = new File( repository.getLocation() );
- if ( !FileUtils.deleteQuietly( dir ) )
- {
- throw new RepositoryAdminException( "Cannot delete repository " + dir );
- }
+ Path dir = Paths.get( repository.getLocation() );
+ org.apache.archiva.common.utils.FileUtils.deleteQuietly( dir );
}
// olamy: copy list for reading as a unit test in webapp fail with ConcurrentModificationException
}
// delete directory too as only content is deleted
- File indexDirectory = indexingContext.getIndexDirectoryFile();
- FileUtils.deleteDirectory( indexDirectory );
+ Path indexDirectory = indexingContext.getIndexDirectoryFile().toPath();
+ org.apache.archiva.common.utils.FileUtils.deleteDirectory( indexDirectory );
createIndexContext( managedRepository );
}
throws RepositoryAdminException, IOException
{
// Normalize the path
- File file = new File( repository.getLocation() );
+ Path file = Paths.get( repository.getLocation() );
if ( !file.isAbsolute() )
{
// add appserver.base/repositories
- file = new File( getRegistry().getString( "appserver.base" ) + File.separatorChar + "repositories",
+ file = Paths.get( getRegistry().getString( "appserver.base" ),"repositories",
repository.getLocation() );
}
- repository.setLocation( file.getCanonicalPath() );
- if ( !file.exists() )
+ repository.setLocation( file.normalize().toString() );
+ if ( !Files.exists(file) )
{
- file.mkdirs();
+ Files.createDirectories(file);
}
- if ( !file.exists() || !file.isDirectory() )
+ if ( !Files.exists(file) || !Files.isDirectory(file) )
{
throw new RepositoryAdminException(
"Unable to add repository - no write access, can not create the root directory: " + file );
}
// take care first about repository location as can be relative
- File repositoryDirectory = new File( repository.getLocation() );
+ Path repositoryDirectory = Paths.get( repository.getLocation() );
if ( !repositoryDirectory.isAbsolute() )
{
repositoryDirectory =
- new File( getRegistry().getString( "appserver.base" ) + File.separatorChar + "repositories",
+ Paths.get( getRegistry().getString( "appserver.base" ), "repositories",
repository.getLocation() );
}
- if ( !repositoryDirectory.exists() )
+ if ( !Files.exists(repositoryDirectory) )
{
- repositoryDirectory.mkdirs();
+ try {
+ Files.createDirectories(repositoryDirectory);
+ } catch (IOException e) {
+ log.error("Could not create directory {}", repositoryDirectory);
+ }
}
try
String indexDir = repository.getIndexDirectory();
//File managedRepository = new File( repository.getLocation() );
- File indexDirectory = null;
+ Path indexDirectory = null;
if ( StringUtils.isNotBlank( indexDir ) )
{
- indexDirectory = new File( repository.getIndexDirectory() );
+ indexDirectory = Paths.get( repository.getIndexDirectory() );
// not absolute so create it in repository directory
if ( !indexDirectory.isAbsolute() )
{
- indexDirectory = new File( repositoryDirectory, repository.getIndexDirectory() );
+ indexDirectory = repositoryDirectory.resolve(repository.getIndexDirectory() );
}
- repository.setIndexDirectory( indexDirectory.getAbsolutePath() );
+ repository.setIndexDirectory( indexDirectory.toAbsolutePath().toString() );
}
else
{
- indexDirectory = new File( repositoryDirectory, ".indexer" );
+ indexDirectory = repositoryDirectory.resolve(".indexer" );
if ( !repositoryDirectory.isAbsolute() )
{
- indexDirectory = new File( repositoryDirectory, ".indexer" );
+ indexDirectory = repositoryDirectory.resolve( ".indexer" );
}
- repository.setIndexDirectory( indexDirectory.getAbsolutePath() );
+ repository.setIndexDirectory( indexDirectory.toAbsolutePath().toString() );
}
- if ( !indexDirectory.exists() )
+ if ( !Files.exists(indexDirectory) )
{
- indexDirectory.mkdirs();
+ Files.createDirectories(indexDirectory);
}
context = indexer.getIndexingContexts().get( repository.getId() );
{
try
{
- context = indexer.addIndexingContext( repository.getId(), repository.getId(), repositoryDirectory,
- indexDirectory,
- repositoryDirectory.toURI().toURL().toExternalForm(),
- indexDirectory.toURI().toURL().toString(), indexCreators );
+ context = indexer.addIndexingContext( repository.getId(), repository.getId(), repositoryDirectory.toFile(),
+ indexDirectory.toFile(),
+ repositoryDirectory.toUri().toURL().toExternalForm(),
+ indexDirectory.toUri().toURL().toString(), indexCreators );
context.setSearchable( repository.isScanned() );
}
// delete it first then recreate it.
log.warn( "the index of repository {} is too old we have to delete and recreate it", //
repository.getId() );
- FileUtils.deleteDirectory( indexDirectory );
- context = indexer.addIndexingContext( repository.getId(), repository.getId(), repositoryDirectory,
- indexDirectory,
- repositoryDirectory.toURI().toURL().toExternalForm(),
- indexDirectory.toURI().toURL().toString(), indexCreators );
+ org.apache.archiva.common.utils.FileUtils.deleteDirectory( indexDirectory );
+ context = indexer.addIndexingContext( repository.getId(), repository.getId(), repositoryDirectory.toFile(),
+ indexDirectory.toFile(),
+ repositoryDirectory.toUri().toURL().toExternalForm(),
+ indexDirectory.toUri().toURL().toString(), indexCreators );
context.setSearchable( repository.isScanned() );
}
if ( StringUtils.isNotBlank( repository.getIndexDir() ) )
{
- File indexDir = new File( repository.getIndexDir() );
+ Path indexDir = Paths.get( repository.getIndexDir() );
// in case of absolute dir do not use the same
if ( indexDir.isAbsolute() )
{
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Inject;
-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.HashMap;
import java.util.List;
return indexingContext;
}
// create remote repository path
- File repoDir = new File( appServerBase, "data/remotes/" + remoteRepository.getId() );
- if ( !repoDir.exists() )
+ Path repoDir = Paths.get( appServerBase, "data/remotes/" + remoteRepository.getId() );
+ if ( !Files.exists(repoDir) )
{
- repoDir.mkdirs();
+ Files.createDirectories(repoDir);
}
- File indexDirectory = null;
+ Path indexDirectory = null;
// is there configured indexDirectory ?
String indexDirectoryPath = remoteRepository.getIndexDirectory();
if ( StringUtils.isNotBlank( indexDirectoryPath ) )
{
- if ( new File( indexDirectoryPath ).isAbsolute() )
- {
- indexDirectory = new File( indexDirectoryPath );
- }
- else
- {
- indexDirectory = new File( repoDir, indexDirectoryPath );
- }
+ repoDir.resolve( indexDirectoryPath );
}
// if not configured use a default value
if ( indexDirectory == null )
{
- indexDirectory = new File( repoDir, ".index" );
+ indexDirectory = repoDir.resolve(".index" );
}
- if ( !indexDirectory.exists() )
+ if ( !Files.exists(indexDirectory) )
{
- indexDirectory.mkdirs();
+ Files.createDirectories(indexDirectory);
}
try
{
- return indexer.addIndexingContext( contextKey, remoteRepository.getId(), repoDir, indexDirectory,
+ return indexer.addIndexingContext( contextKey, remoteRepository.getId(), repoDir.toFile(), indexDirectory.toFile(),
remoteRepository.getUrl(), calculateIndexRemoteUrl( remoteRepository ),
indexCreators );
}
// delete it first then recreate it.
log.warn( "the index of repository {} is too old we have to delete and recreate it", //
remoteRepository.getId() );
- FileUtils.deleteDirectory( indexDirectory );
- return indexer.addIndexingContext( contextKey, remoteRepository.getId(), repoDir, indexDirectory,
+ org.apache.archiva.common.utils.FileUtils.deleteDirectory( indexDirectory );
+ return indexer.addIndexingContext( contextKey, remoteRepository.getId(), repoDir.toFile(), indexDirectory.toFile(),
remoteRepository.getUrl(), calculateIndexRemoteUrl( remoteRepository ),
indexCreators );
import org.apache.archiva.scheduler.MergedRemoteIndexesScheduler;
import org.springframework.stereotype.Service;
-import java.io.File;
import java.nio.file.Path;
/**
import org.apache.archiva.redback.users.User;
import org.apache.archiva.redback.users.memory.SimpleUser;
import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
-import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.springframework.test.context.ContextConfiguration;
import javax.inject.Inject;
-import javax.inject.Named;
-import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
repoLocation + "/.index", false, 1, 2, true, false );
}
- protected File clearRepoLocation( String path )
+ protected Path clearRepoLocation(String path )
throws Exception
{
- File repoDir = new File( path );
- if ( repoDir.exists() )
+ Path repoDir = Paths.get( path );
+ if ( Files.exists(repoDir) )
{
- FileUtils.deleteDirectory( repoDir );
+ org.apache.archiva.common.utils.FileUtils.deleteDirectory( repoDir );
}
- assertFalse( repoDir.exists() );
+ assertFalse( Files.exists(repoDir) );
return repoDir;
}
import org.junit.Test;
import javax.inject.Inject;
-import java.io.File;
+import java.nio.file.Paths;
import java.util.Arrays;
/**
try
{
ManagedRepository managedRepositoryOne =
- getTestManagedRepository( "test-new-one", APPSERVER_BASE_PATH + File.separator + "test-new-one" );
+ getTestManagedRepository( "test-new-one", Paths.get(APPSERVER_BASE_PATH,"test-new-one" ).toString());
ManagedRepository managedRepositoryTwo =
- getTestManagedRepository( "test-new-two", APPSERVER_BASE_PATH + File.separator + "test-new-two" );
+ getTestManagedRepository( "test-new-two", Paths.get(APPSERVER_BASE_PATH, "test-new-two" ).toString());
managedRepositoryAdmin.addManagedRepository( managedRepositoryOne, false, getFakeAuditInformation() );
try
{
ManagedRepository managedRepositoryOne =
- getTestManagedRepository( "test-new-one", APPSERVER_BASE_PATH + File.separator + "test-new-one" );
+ getTestManagedRepository( "test-new-one", Paths.get(APPSERVER_BASE_PATH,"test-new-one" ).toString());
ManagedRepository managedRepositoryTwo =
- getTestManagedRepository( "test-new-two", APPSERVER_BASE_PATH + File.separator + "test-new-two" );
+ getTestManagedRepository( "test-new-two", Paths.get(APPSERVER_BASE_PATH, "test-new-two" ).toString());
managedRepositoryAdmin.addManagedRepository( managedRepositoryOne, false, getFakeAuditInformation() );
try
{
ManagedRepository managedRepositoryOne =
- getTestManagedRepository( "test-new-one", APPSERVER_BASE_PATH + File.separator + "test-new-one" );
+ getTestManagedRepository( "test-new-one", Paths.get(APPSERVER_BASE_PATH , "test-new-one" ).toString());
ManagedRepository managedRepositoryTwo =
- getTestManagedRepository( "test-new-two", APPSERVER_BASE_PATH + File.separator + "test-new-two" );
+ getTestManagedRepository( "test-new-two", Paths.get(APPSERVER_BASE_PATH ,"test-new-two" ).toString());
managedRepositoryAdmin.addManagedRepository( managedRepositoryOne, false, getFakeAuditInformation() );
public void testAddGroupWithInvalidMergedIndexTtl() throws Exception {
try {
ManagedRepository managedRepositoryOne =
- getTestManagedRepository( "test-new-one", APPSERVER_BASE_PATH + File.separator + "test-new-one" );
+ getTestManagedRepository( "test-new-one", Paths.get(APPSERVER_BASE_PATH , "test-new-one" ).toString());
ManagedRepository managedRepositoryTwo =
- getTestManagedRepository( "test-new-two", APPSERVER_BASE_PATH + File.separator + "test-new-two" );
+ getTestManagedRepository( "test-new-two", Paths.get(APPSERVER_BASE_PATH , "test-new-two" ).toString());
managedRepositoryAdmin.addManagedRepository( managedRepositoryOne, false, getFakeAuditInformation() );
public void testAddAndUpdateGroupWithInvalidMergedIndexTtl() throws Exception {
try {
ManagedRepository managedRepositoryOne =
- getTestManagedRepository( "test-new-one", APPSERVER_BASE_PATH + File.separator + "test-new-one" );
+ getTestManagedRepository( "test-new-one", Paths.get(APPSERVER_BASE_PATH , "test-new-one" ).toString());
ManagedRepository managedRepositoryTwo =
- getTestManagedRepository( "test-new-two", APPSERVER_BASE_PATH + File.separator + "test-new-two" );
+ getTestManagedRepository( "test-new-two", Paths.get(APPSERVER_BASE_PATH , "test-new-two" ).toString());
managedRepositoryAdmin.addManagedRepository( managedRepositoryOne, false, getFakeAuditInformation() );
import org.apache.archiva.security.common.ArchivaRoleConstants;
import org.junit.Test;
-import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.List;
/**
String repoId = "test-new-one";
- String repoLocation = APPSERVER_BASE_PATH + File.separator + repoId;
+ String repoLocation = Paths.get(APPSERVER_BASE_PATH, repoId).toString();
@Test
public void getAllManagedRepos()
{
mockAuditListener.clearEvents();
- File repoDir = clearRepoLocation( repoLocation );
+ Path repoDir = clearRepoLocation( repoLocation );
List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
assertNotNull( repos );
managedRepositoryAdmin.deleteManagedRepository( repoId, getFakeAuditInformation(), false );
// deleteContents false
- assertTrue( repoDir.exists() );
+ assertTrue( Files.exists(repoDir) );
repos = managedRepositoryAdmin.getManagedRepositories();
assertNotNull( repos );
throws Exception
{
- File repoDir = clearRepoLocation( repoLocation );
+ Path repoDir = clearRepoLocation( repoLocation );
mockAuditListener.clearEvents();
List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
repo = managedRepositoryAdmin.getManagedRepository( repoId );
assertNotNull( repo );
assertEquals( newName, repo.getName() );
- assertEquals( new File( repoLocation ).getCanonicalPath(), new File( repo.getLocation() ).getCanonicalPath() );
- assertTrue( new File( repoLocation ).exists() );
+ assertEquals( Paths.get(repoLocation).normalize(), Paths.get(repo.getLocation() ).normalize() );
+ assertTrue( Files.exists( Paths.get(repoLocation )));
assertEquals( description, repo.getDescription() );
assertTrue( repo.isSkipPackedIndexCreation() );
managedRepositoryAdmin.deleteManagedRepository( repo.getId(), getFakeAuditInformation(), false );
// check deleteContents false
- assertTrue( repoDir.exists() );
+ assertTrue( Files.exists(repoDir) );
assertTemplateRoleNotExists( repoId );
throws Exception
{
- File repoDir = clearRepoLocation( repoLocation );
+ Path repoDir = clearRepoLocation( repoLocation );
mockAuditListener.clearEvents();
List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
assertTemplateRoleExists( repoId );
- assertTrue( repoDir.exists() );
+ assertTrue( Files.exists(repoDir) );
assertNotNull( managedRepositoryAdmin.getManagedRepository( repoId + STAGE_REPO_ID_END ) );
assertTemplateRoleExists( repoId + STAGE_REPO_ID_END );
- assertTrue( new File( repoLocation + STAGE_REPO_ID_END ).exists() );
+ assertTrue( Files.exists(Paths.get( repoLocation + STAGE_REPO_ID_END )) );
managedRepositoryAdmin.deleteManagedRepository( repoId, getFakeAuditInformation(), true );
- assertFalse( repoDir.exists() );
+ assertFalse( Files.exists(repoDir) );
- assertFalse( new File( repoLocation + STAGE_REPO_ID_END ).exists() );
+ assertFalse( Files.exists(Paths.get( repoLocation + STAGE_REPO_ID_END )) );
assertTemplateRoleNotExists( repoId + STAGE_REPO_ID_END );
throws Exception
{
- String stageRepoLocation = APPSERVER_BASE_PATH + File.separator + repoId;
+ String stageRepoLocation = Paths.get(APPSERVER_BASE_PATH, repoId).toString();
- File repoDir = clearRepoLocation( repoLocation );
+ Path repoDir = clearRepoLocation( repoLocation );
mockAuditListener.clearEvents();
List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
assertTemplateRoleExists( repoId );
- assertFalse( new File( repoLocation + STAGE_REPO_ID_END ).exists() );
+ assertFalse( Files.exists(Paths.get( repoLocation + STAGE_REPO_ID_END )) );
assertTemplateRoleNotExists( repoId + STAGE_REPO_ID_END );
repo = managedRepositoryAdmin.getManagedRepository( repoId );
assertNotNull( repo );
assertEquals( newName, repo.getName() );
- assertEquals( new File( repoLocation ).getCanonicalPath(), new File( repo.getLocation() ).getCanonicalPath() );
- assertTrue( new File( repoLocation ).exists() );
+ assertEquals( Paths.get( repoLocation ).normalize(), Paths.get( repo.getLocation() ).normalize() );
+ assertTrue( Files.exists( Paths.get(repoLocation )) );
assertEquals( getTestManagedRepository( repoId, repoLocation ).getCronExpression(), repo.getCronExpression() );
assertEquals( getTestManagedRepository( repoId, repoLocation ).getLayout(), repo.getLayout() );
assertEquals( getTestManagedRepository( repoId, repoLocation ).getId(), repo.getId() );
assertTemplateRoleExists( repoId );
- assertTrue( new File( stageRepoLocation + STAGE_REPO_ID_END ).exists() );
+ assertTrue( Files.exists(Paths.get( stageRepoLocation + STAGE_REPO_ID_END )) );
assertTemplateRoleExists( repoId + STAGE_REPO_ID_END );
managedRepositoryAdmin.deleteManagedRepository( repo.getId(), getFakeAuditInformation(), false );
// check deleteContents false
- assertTrue( repoDir.exists() );
+ assertTrue( Files.exists(repoDir) );
assertTemplateRoleNotExists( repoId );
- assertTrue( new File( stageRepoLocation + STAGE_REPO_ID_END ).exists() );
+ assertTrue( Files.exists( Paths.get(stageRepoLocation + STAGE_REPO_ID_END )) );
assertTemplateRoleNotExists( repoId + STAGE_REPO_ID_END );
mockAuditListener.clearEvents();
- new File( repoLocation + STAGE_REPO_ID_END ).delete();
- assertFalse( new File( repoLocation + STAGE_REPO_ID_END ).exists() );
+ Files.deleteIfExists(Paths.get( repoLocation + STAGE_REPO_ID_END ));
+ assertFalse( Files.exists(Paths.get( repoLocation + STAGE_REPO_ID_END )) );
}
//----------------------------------