@@ -24,6 +24,7 @@ import org.apache.archiva.repository.RepositoryEventListener; | |||
import org.apache.archiva.repository.RepositoryType; | |||
import java.net.URI; | |||
import java.nio.file.Path; | |||
import java.util.Collection; | |||
public interface ArchivaIndexManager { | |||
@@ -95,4 +96,10 @@ public interface ArchivaIndexManager { | |||
* @throws IndexCreationFailedException | |||
*/ | |||
ArchivaIndexingContext move(ArchivaIndexingContext context, Repository repo) throws IndexCreationFailedException; | |||
/** | |||
* Returns the local path where the index is stored. | |||
* @return | |||
*/ | |||
public void updateLocalIndexPath(Repository repo); | |||
} |
@@ -19,17 +19,29 @@ package org.apache.archiva.indexer; | |||
* under the License. | |||
*/ | |||
import org.apache.archiva.common.utils.PathUtil; | |||
import org.apache.archiva.repository.Repository; | |||
import org.apache.archiva.repository.RepositoryEvent; | |||
import org.apache.archiva.repository.RepositoryType; | |||
import org.apache.archiva.repository.features.IndexCreationFeature; | |||
import org.apache.commons.lang.StringUtils; | |||
import org.slf4j.Logger; | |||
import org.slf4j.LoggerFactory; | |||
import org.springframework.stereotype.Service; | |||
import java.io.IOException; | |||
import java.net.URI; | |||
import java.nio.file.Files; | |||
import java.nio.file.Path; | |||
import java.util.Collection; | |||
@Service("indexManager#none") | |||
public class GenericIndexManager implements ArchivaIndexManager { | |||
private final Logger log = LoggerFactory.getLogger(GenericIndexManager.class); | |||
public static final String DEFAULT_INDEXER_DIR = ".indexer"; | |||
@Override | |||
public void pack(ArchivaIndexingContext context) { | |||
@@ -75,4 +87,43 @@ public class GenericIndexManager implements ArchivaIndexManager { | |||
return null; | |||
} | |||
@Override | |||
public void updateLocalIndexPath(Repository repo) { | |||
if (repo.supportsFeature(IndexCreationFeature.class)) { | |||
IndexCreationFeature icf = repo.getFeature(IndexCreationFeature.class).get(); | |||
try { | |||
icf.setLocalIndexPath(getIndexPath(repo)); | |||
} catch (IOException e) { | |||
log.error("Could not set local index path for {}. New URI: {}", repo.getId(), icf.getIndexPath()); | |||
} | |||
} | |||
} | |||
private Path getIndexPath(Repository repo) throws IOException { | |||
IndexCreationFeature icf = repo.getFeature(IndexCreationFeature.class).get(); | |||
Path repoDir = repo.getLocalPath(); | |||
URI indexDir = icf.getIndexPath(); | |||
Path indexDirectory = null; | |||
if ( ! StringUtils.isEmpty(indexDir.toString( ) ) ) | |||
{ | |||
indexDirectory = PathUtil.getPathFromUri( indexDir ); | |||
// not absolute so create it in repository directory | |||
if ( !indexDirectory.isAbsolute( ) ) | |||
{ | |||
indexDirectory = repoDir.resolve( indexDirectory ); | |||
} | |||
} | |||
else | |||
{ | |||
indexDirectory = repoDir.resolve( DEFAULT_INDEXER_DIR); | |||
} | |||
if ( !Files.exists( indexDirectory ) ) | |||
{ | |||
Files.createDirectories( indexDirectory ); | |||
} | |||
return indexDirectory; | |||
} | |||
} |
@@ -29,12 +29,12 @@ import java.time.LocalDateTime; | |||
public class RepositoryEvent<T> { | |||
final EventType type; | |||
final String repo; | |||
final Repository repo; | |||
final T value; | |||
final T oldValue; | |||
final LocalDateTime instant; | |||
public RepositoryEvent(EventType type, String repo, T oldValue, T value) { | |||
public RepositoryEvent(EventType type, Repository repo, T oldValue, T value) { | |||
this.type = type; | |||
this.repo = repo; | |||
this.value = value; | |||
@@ -47,19 +47,19 @@ public class RepositoryEvent<T> { | |||
} | |||
EventType getType() { | |||
public EventType getType() { | |||
return type; | |||
}; | |||
String getRepositoryId() { | |||
public Repository getRepository() { | |||
return repo; | |||
}; | |||
T getValue() { | |||
public T getValue() { | |||
return value; | |||
} | |||
T getOldValue() { | |||
public T getOldValue() { | |||
return oldValue; | |||
} | |||
@@ -19,6 +19,7 @@ package org.apache.archiva.repository.features; | |||
* under the License. | |||
*/ | |||
import org.apache.archiva.repository.Repository; | |||
import org.apache.archiva.repository.RepositoryEvent; | |||
import java.net.URI; | |||
@@ -29,7 +30,7 @@ public class IndexCreationEvent extends RepositoryEvent<URI> { | |||
URI_CHANGE | |||
} | |||
IndexCreationEvent(String repo, URI oldValue, URI value) { | |||
IndexCreationEvent(Repository repo, URI oldValue, URI value) { | |||
super(Index.URI_CHANGE, repo, oldValue, value); | |||
} | |||
} |
@@ -26,6 +26,7 @@ import org.apache.commons.lang.StringUtils; | |||
import java.net.URI; | |||
import java.net.URISyntaxException; | |||
import java.nio.file.Path; | |||
import java.util.ArrayList; | |||
import java.util.EventListener; | |||
import java.util.List; | |||
@@ -42,9 +43,11 @@ public class IndexCreationFeature extends AbstractFeature implements RepositoryF | |||
private URI indexPath; | |||
private String repo; | |||
private Path localIndexPath; | |||
public IndexCreationFeature(String repoId, RepositoryEventListener listener) { | |||
private Repository repo; | |||
public IndexCreationFeature(Repository repoId, RepositoryEventListener listener) { | |||
super(listener); | |||
this.repo = repoId; | |||
try | |||
@@ -109,4 +112,12 @@ public class IndexCreationFeature extends AbstractFeature implements RepositoryF | |||
public boolean hasIndex() { | |||
return this.indexPath!=null && !StringUtils.isEmpty( this.indexPath.getPath() ); | |||
} | |||
public Path getLocalIndexPath() { | |||
return localIndexPath; | |||
} | |||
public void setLocalIndexPath(Path localIndexPath) { | |||
this.localIndexPath = localIndexPath; | |||
} | |||
} |
@@ -64,7 +64,7 @@ public class BasicManagedRepository extends AbstractManagedRepository | |||
} | |||
private void initFeatures() { | |||
IndexCreationFeature indexCreationFeature = new IndexCreationFeature(this.getId(), this); | |||
IndexCreationFeature indexCreationFeature = new IndexCreationFeature(this, this); | |||
addFeature( artifactCleanupFeature ); | |||
addFeature( indexCreationFeature ); | |||
addFeature( stagingRepositoryFeature ); |
@@ -19,19 +19,14 @@ package org.apache.archiva.repository; | |||
* under the License. | |||
*/ | |||
import org.apache.archiva.configuration.ArchivaConfiguration; | |||
import org.apache.archiva.configuration.Configuration; | |||
import org.apache.archiva.configuration.ConfigurationEvent; | |||
import org.apache.archiva.configuration.ConfigurationListener; | |||
import org.apache.archiva.configuration.IndeterminateConfigurationException; | |||
import org.apache.archiva.configuration.ManagedRepositoryConfiguration; | |||
import org.apache.archiva.configuration.RemoteRepositoryConfiguration; | |||
import org.apache.archiva.configuration.*; | |||
import org.apache.archiva.indexer.*; | |||
import org.apache.archiva.metadata.model.facets.AuditEvent; | |||
import org.apache.archiva.redback.components.registry.RegistryException; | |||
import org.apache.archiva.repository.features.ArtifactCleanupFeature; | |||
import org.apache.archiva.repository.features.IndexCreationEvent; | |||
import org.apache.archiva.repository.features.IndexCreationFeature; | |||
import org.apache.archiva.repository.features.StagingRepositoryFeature; | |||
import org.apache.commons.lang.StringUtils; | |||
import org.slf4j.Logger; | |||
import org.slf4j.LoggerFactory; | |||
import org.springframework.stereotype.Service; | |||
@@ -40,7 +35,6 @@ import javax.annotation.PostConstruct; | |||
import javax.annotation.PreDestroy; | |||
import javax.inject.Inject; | |||
import javax.inject.Named; | |||
import java.io.IOException; | |||
import java.util.*; | |||
import java.util.concurrent.locks.ReentrantReadWriteLock; | |||
import java.util.stream.Collectors; | |||
@@ -239,6 +233,7 @@ public class RepositoryRegistry implements ConfigurationListener, RepositoryEven | |||
ArchivaIndexManager idxManager = getIndexManager(editableRepo.getType()); | |||
try { | |||
editableRepo.setIndexingContext(idxManager.createContext(editableRepo)); | |||
idxManager.updateLocalIndexPath(editableRepo); | |||
} catch (IndexCreationFailedException e) { | |||
throw new RepositoryException("Could not create index for repository "+editableRepo.getId()+": "+e.getMessage(),e); | |||
} | |||
@@ -855,6 +850,21 @@ public class RepositoryRegistry implements ConfigurationListener, RepositoryEven | |||
} | |||
private void doRemoveRepo(RemoteRepository repo, Configuration configuration) { | |||
repo.close(); | |||
RemoteRepositoryConfiguration cfg = configuration.findRemoteRepositoryById(repo.getId()); | |||
if (cfg != null) { | |||
configuration.removeRemoteRepository(cfg); | |||
} | |||
List<ProxyConnectorConfiguration> proxyConnectors = new ArrayList<>(configuration.getProxyConnectors()); | |||
for (ProxyConnectorConfiguration proxyConnector : proxyConnectors) { | |||
if (StringUtils.equals(proxyConnector.getTargetRepoId(), repo.getId())) { | |||
configuration.removeProxyConnector(proxyConnector); | |||
} | |||
} | |||
} | |||
/** | |||
* Removes the remote repository from the registry and configuration. | |||
* The change is saved to the configuration immediately. | |||
@@ -864,23 +874,18 @@ public class RepositoryRegistry implements ConfigurationListener, RepositoryEven | |||
*/ | |||
public void removeRepository( RemoteRepository remoteRepository ) throws RepositoryException | |||
{ | |||
final String id = remoteRepository.getId(); | |||
RemoteRepository repo = getRemoteRepository( id ); | |||
if (repo!=null) { | |||
rwLock.writeLock().lock(); | |||
try { | |||
repo = remoteRepositories.remove( id ); | |||
if (repo!=null) { | |||
repo.close(); | |||
Configuration configuration = getArchivaConfiguration().getConfiguration(); | |||
RemoteRepositoryConfiguration cfg = configuration.findRemoteRepositoryById( id ); | |||
if (cfg!=null) { | |||
configuration.removeRemoteRepository( cfg ); | |||
} | |||
doRemoveRepo(repo, configuration); | |||
getArchivaConfiguration().save( configuration ); | |||
} | |||
} | |||
catch ( RegistryException | IndeterminateConfigurationException e ) | |||
{ | |||
@@ -904,11 +909,7 @@ public class RepositoryRegistry implements ConfigurationListener, RepositoryEven | |||
try { | |||
repo = remoteRepositories.remove( id ); | |||
if (repo!=null) { | |||
repo.close(); | |||
RemoteRepositoryConfiguration cfg = configuration.findRemoteRepositoryById( id ); | |||
if (cfg!=null) { | |||
configuration.removeRemoteRepository( cfg ); | |||
} | |||
doRemoveRepo(repo, configuration); | |||
} | |||
} finally | |||
{ | |||
@@ -1017,13 +1018,18 @@ public class RepositoryRegistry implements ConfigurationListener, RepositoryEven | |||
@Override | |||
public <T> void raise(RepositoryEvent<T> event) { | |||
if (event.getType().equals(IndexCreationEvent.Index.URI_CHANGE)) { | |||
if (managedRepositories.containsKey(event.getRepositoryId()) || | |||
remoteRepositories.containsKey(event.getRepositoryId())) { | |||
EditableRepository repo = (EditableRepository) getRepository(event.getRepositoryId()); | |||
if (managedRepositories.containsKey(event.getRepository().getId()) || | |||
remoteRepositories.containsKey(event.getRepository().getId())) { | |||
EditableRepository repo = (EditableRepository) event.getRepository(); | |||
if (repo != null && repo.getIndexingContext()!=null) { | |||
try { | |||
ArchivaIndexingContext newCtx = getIndexManager(repo.getType()).move(repo.getIndexingContext(), repo); | |||
repo.setIndexingContext(newCtx); | |||
ArchivaIndexManager idxmgr = getIndexManager(repo.getType()); | |||
if (idxmgr != null) { | |||
ArchivaIndexingContext newCtx = idxmgr.move(repo.getIndexingContext(), repo); | |||
repo.setIndexingContext(newCtx); | |||
idxmgr.updateLocalIndexPath(repo); | |||
} | |||
} catch (IndexCreationFailedException e) { | |||
log.error("Could not move index to new directory {}", e.getMessage(), e); | |||
} |
@@ -82,4 +82,9 @@ public class ArchivaIndexManagerMock implements ArchivaIndexManager { | |||
public ArchivaIndexingContext move(ArchivaIndexingContext context, Repository repo) throws IndexCreationFailedException { | |||
return null; | |||
} | |||
@Override | |||
public void updateLocalIndexPath(Repository repo) { | |||
} | |||
} |