]> source.dussan.org Git - archiva.git/commitdiff
Fixing Index context creation
authorMartin Stockhammer <martin_s@apache.org>
Fri, 4 Jun 2021 15:44:30 +0000 (17:44 +0200)
committerMartin Stockhammer <martin_s@apache.org>
Fri, 4 Jun 2021 15:44:30 +0000 (17:44 +0200)
13 files changed:
archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/archiva/common/utils/FileUtils.java
archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/main/resources/archetype-resources/src/test/java/RepositoryProviderMock.java
archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/repository/mock/RepositoryProviderMock.java
archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/main/java/org/apache/archiva/admin/repository/group/DefaultRepositoryGroupAdmin.java
archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/RepositoryProvider.java
archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/base/ArchivaRepositoryRegistry.java
archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/base/RepositoryGroupHandler.java
archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/mock/RepositoryProviderMock.java
archiva-modules/archiva-maven/archiva-maven-indexer/src/main/java/org/apache/archiva/indexer/maven/MavenIndexManager.java
archiva-modules/archiva-maven/archiva-maven-proxy/src/test/java/org/apache/archiva/repository/mock/RepositoryProviderMock.java
archiva-modules/archiva-maven/archiva-maven-repository/src/main/java/org/apache/archiva/repository/maven/MavenRepositoryProvider.java
archiva-modules/archiva-maven/archiva-maven-scheduler/src/test/java/org/apache/archiva/scheduler/indexing/maven/DownloadRemoteIndexTaskTest.java
archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/RepositoryServletBrowseTest.java

index 3dc95432b779e839fdddcda3d511817721e14095..3ce13536a7f1ca533722d3add0506ee09e2de8f1 100644 (file)
@@ -23,9 +23,11 @@ import org.slf4j.LoggerFactory;
 
 import java.io.*;
 import java.nio.charset.Charset;
+import java.nio.file.CopyOption;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
 import java.nio.file.StandardOpenOption;
 import java.util.Comparator;
 import java.util.Optional;
@@ -218,4 +220,22 @@ public class FileUtils {
             });
         }
     }
+
+
+    public static void copyContent(Path srcDir, Path destinationDir, boolean overwrite) throws IOException
+    {
+        CopyOption[] copyOption = overwrite ? new CopyOption[]{StandardCopyOption.REPLACE_EXISTING} : new CopyOption[]{};
+        Files.walk(srcDir).forEach(a -> {
+            Path b = destinationDir.resolve(srcDir.relativize( a ));
+            try {
+                Files.copy(a, b, copyOption);
+            } catch (IOException e) {
+                log.error( "Could not copy file {} to {}: {}", a, b, e.getMessage( ) );
+            }
+        });
+    }
+
+    public static void copyContent(Path srcDir, Path destinationDir) throws IOException {
+        copyContent( srcDir, destinationDir, true );
+    }
 }
index 6f1907ef015865bdc7b93e064991e08780340eb0..8abed28c5730f241c98dd5e83204cb8676757485 100644 (file)
@@ -22,6 +22,8 @@ package $package;
 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
 import org.apache.archiva.configuration.RepositoryGroupConfiguration;
+import org.apache.archiva.event.EventHandler;
+import org.apache.archiva.event.EventType;
 import org.apache.archiva.repository.base.BasicManagedRepository;
 import org.apache.archiva.repository.base.BasicRemoteRepository;
 import org.apache.archiva.repository.EditableManagedRepository;
@@ -37,6 +39,7 @@ import org.apache.archiva.repository.RepositoryException;
 import org.apache.archiva.repository.RepositoryProvider;
 import org.apache.archiva.repository.RepositoryType;
 import org.apache.archiva.event.Event;
+import org.apache.archiva.repository.event.RepositoryEvent;
 import org.apache.archiva.repository.features.ArtifactCleanupFeature;
 import org.apache.archiva.repository.features.IndexCreationFeature;
 import org.apache.archiva.repository.features.RemoteIndexFeature;
@@ -280,5 +283,8 @@ public class RepositoryProviderMock implements RepositoryProvider
 
     }
 
-
+    public void addRepositoryEventHandler( EventHandler<? super RepositoryEvent> eventHandler )
+    {
+        // do nothing
+    }
 }
index eaa94e32f38e0e10e1531838c74b28f0cf3928b4..d11008a687a968b87fc49f5628a5ca5ab679f0a3 100644 (file)
@@ -22,6 +22,7 @@ package org.apache.archiva.repository.mock;
 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
 import org.apache.archiva.configuration.RepositoryGroupConfiguration;
+import org.apache.archiva.event.EventHandler;
 import org.apache.archiva.repository.base.BasicManagedRepository;
 import org.apache.archiva.repository.base.BasicRemoteRepository;
 import org.apache.archiva.repository.EditableManagedRepository;
@@ -37,6 +38,7 @@ import org.apache.archiva.repository.RepositoryException;
 import org.apache.archiva.repository.RepositoryGroup;
 import org.apache.archiva.repository.RepositoryProvider;
 import org.apache.archiva.repository.RepositoryType;
+import org.apache.archiva.repository.event.RepositoryEvent;
 import org.apache.archiva.repository.features.ArtifactCleanupFeature;
 import org.apache.archiva.repository.features.IndexCreationFeature;
 import org.apache.archiva.repository.features.RemoteIndexFeature;
@@ -253,6 +255,12 @@ public class RepositoryProviderMock implements RepositoryProvider
         return null;
     }
 
+    @Override
+    public void addRepositoryEventHandler( EventHandler<? super RepositoryEvent> eventHandler )
+    {
+        // do nothing
+    }
+
 
     @Override
     public RemoteRepositoryConfiguration getRemoteConfiguration( RemoteRepository remoteRepository )
index 13f3ce6f9f91c959806efff945e252e41b870b70..a6276a84411ca3aa053574345c38d5dca4211840 100644 (file)
@@ -76,15 +76,9 @@ public class DefaultRepositoryGroupAdmin
     @Inject
     private ManagedRepositoryAdmin managedRepositoryAdmin;
 
-    @Inject
-    @Named("mergedRemoteIndexesScheduler#default")
-    private MergedRemoteIndexesScheduler mergedRemoteIndexesScheduler;
-
     @Inject
     private RepositoryRegistry repositoryRegistry;
 
-    private Path groupsDirectory;
-
     @PostConstruct
     public void baseInit() {
         this.repositoryRegistry.registerEventHandler( EventType.ROOT, this );
@@ -92,29 +86,6 @@ public class DefaultRepositoryGroupAdmin
 
     public void initialize()
     {
-        String appServerBase = getRegistry().getString( "appserver.base" );
-        groupsDirectory = Paths.get( appServerBase, "groups" );
-        if ( !Files.exists(groupsDirectory) )
-        {
-            Files.exists(groupsDirectory);
-        }
-
-        for ( org.apache.archiva.repository.RepositoryGroup repositoryGroup : repositoryRegistry.getRepositoryGroups() )
-        {
-            mergedRemoteIndexesScheduler.schedule( repositoryGroup,
-                                                   getMergedIndexDirectory( repositoryGroup.getId() ));
-            // create the directory for each group if not exists
-            Path groupPath = groupsDirectory.resolve(repositoryGroup.getId() );
-            if ( !Files.exists(groupPath) )
-            {
-                try {
-                    Files.createDirectories(groupPath);
-                } catch (IOException e) {
-                    log.error("Could not create directory {}", groupPath);
-                }
-            }
-        }
-
     }
 
 
@@ -167,7 +138,6 @@ public class DefaultRepositoryGroupAdmin
         }
 
         triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.ADD_REPO_GROUP, auditInformation );
-        mergedRemoteIndexesScheduler.schedule( repositoryRegistry.getRepositoryGroup( repositoryGroup.getId()), getMergedIndexDirectory( repositoryGroup.getId() ) );
         return Boolean.TRUE;
     }
 
@@ -183,10 +153,7 @@ public class DefaultRepositoryGroupAdmin
             log.error("Removal of repository group {} failed: {}", repositoryGroup.getId(), e.getMessage(), e);
             throw new RepositoryAdminException("Removal of repository failed: " + e.getMessage(), e);
         }
-        mergedRemoteIndexesScheduler.unschedule(
-            repositoryGroup );
         triggerAuditEvent( repositoryGroupId, null, AuditEvent.DELETE_REPO_GROUP, auditInformation );
-
         return Boolean.TRUE;
     }
 
@@ -222,8 +189,6 @@ public class DefaultRepositoryGroupAdmin
         }
 
         org.apache.archiva.repository.RepositoryGroup rg = repositoryRegistry.getRepositoryGroup( repositoryGroup.getId( ) );
-        mergedRemoteIndexesScheduler.unschedule( rg );
-        mergedRemoteIndexesScheduler.schedule( rg, getMergedIndexDirectory( repositoryGroup.getId() ) );
         triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.MODIFY_REPO_GROUP, auditInformation );
         return Boolean.TRUE;
     }
index 434c41deb2a5ed4f66b91598ec9c16875a464331..813acb61de0098117aceb5f2622270c323c26325 100644 (file)
@@ -23,6 +23,7 @@ import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
 import org.apache.archiva.configuration.RepositoryGroupConfiguration;
 import org.apache.archiva.event.EventHandler;
+import org.apache.archiva.repository.event.RepositoryEvent;
 
 import java.io.IOException;
 import java.util.Set;
@@ -180,4 +181,12 @@ public interface RepositoryProvider extends EventHandler
      * @throws RepositoryException if the data cannot be converted
      */
     RepositoryGroupConfiguration getRepositoryGroupConfiguration(RepositoryGroup repositoryGroup) throws RepositoryException;
+
+    /**
+     * This event handler is registered to all newly created repositories immediately after creation. This may be needed by
+     * some components to get events right after creation of the instance.
+     *
+     * @param eventHandler the event handler instance
+     */
+    void addRepositoryEventHandler( EventHandler<? super RepositoryEvent> eventHandler);
 }
index 6a3d0e0b39fc2233288ef994390ca1b9782c6a9e..24943609202a1afad4ec25ad8377027afba94b17 100644 (file)
@@ -19,12 +19,25 @@ package org.apache.archiva.repository.base;
  * under the License.
  */
 
-import org.apache.archiva.configuration.*;
+import org.apache.archiva.components.registry.RegistryException;
+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.ProxyConnectorConfiguration;
+import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
+import org.apache.archiva.configuration.RepositoryGroupConfiguration;
 import org.apache.archiva.event.Event;
+import org.apache.archiva.event.EventHandler;
 import org.apache.archiva.event.EventManager;
 import org.apache.archiva.event.EventType;
-import org.apache.archiva.indexer.*;
-import org.apache.archiva.components.registry.RegistryException;
+import org.apache.archiva.indexer.ArchivaIndexManager;
+import org.apache.archiva.indexer.ArchivaIndexingContext;
+import org.apache.archiva.indexer.IndexCreationFailedException;
+import org.apache.archiva.indexer.IndexManagerFactory;
+import org.apache.archiva.indexer.IndexUpdateFailedException;
 import org.apache.archiva.repository.EditableManagedRepository;
 import org.apache.archiva.repository.EditableRemoteRepository;
 import org.apache.archiva.repository.EditableRepository;
@@ -38,8 +51,10 @@ import org.apache.archiva.repository.RepositoryProvider;
 import org.apache.archiva.repository.RepositoryRegistry;
 import org.apache.archiva.repository.RepositoryType;
 import org.apache.archiva.repository.UnsupportedRepositoryTypeException;
-import org.apache.archiva.repository.event.*;
-import org.apache.archiva.event.EventHandler;
+import org.apache.archiva.repository.event.LifecycleEvent;
+import org.apache.archiva.repository.event.RepositoryEvent;
+import org.apache.archiva.repository.event.RepositoryIndexEvent;
+import org.apache.archiva.repository.event.RepositoryRegistryEvent;
 import org.apache.archiva.repository.features.IndexCreationFeature;
 import org.apache.archiva.repository.features.StagingRepositoryFeature;
 import org.apache.archiva.repository.metadata.MetadataReader;
@@ -53,7 +68,14 @@ import javax.annotation.PostConstruct;
 import javax.annotation.PreDestroy;
 import javax.inject.Inject;
 import javax.inject.Named;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 import java.util.stream.Collectors;
@@ -70,12 +92,12 @@ import java.util.stream.Stream;
  *
  * @since 3.0
  */
-@Service("repositoryRegistry")
+@Service( "repositoryRegistry" )
 public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHandler<Event>,
     RepositoryRegistry
 {
 
-    private static final Logger log = LoggerFactory.getLogger(RepositoryRegistry.class);
+    private static final Logger log = LoggerFactory.getLogger( RepositoryRegistry.class );
     private final ConfigurationHandler configurationHandler;
 
     /**
@@ -91,21 +113,20 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa
     List<MetadataReader> metadataReaderList;
 
     @Inject
-    @Named("repositoryContentFactory#default")
+    @Named( "repositoryContentFactory#default" )
     RepositoryContentFactory repositoryContentFactory;
 
 
-
     private final EventManager eventManager;
 
 
-    private Map<String, ManagedRepository> managedRepositories = new HashMap<>();
-    private Map<String, ManagedRepository> uManagedRepository = Collections.unmodifiableMap(managedRepositories);
+    private Map<String, ManagedRepository> managedRepositories = new HashMap<>( );
+    private Map<String, ManagedRepository> uManagedRepository = Collections.unmodifiableMap( managedRepositories );
 
-    private Map<String, RemoteRepository> remoteRepositories = new HashMap<>();
-    private Map<String, RemoteRepository> uRemoteRepositories = Collections.unmodifiableMap(remoteRepositories);
+    private Map<String, RemoteRepository> remoteRepositories = new HashMap<>( );
+    private Map<String, RemoteRepository> uRemoteRepositories = Collections.unmodifiableMap( remoteRepositories );
 
-    private ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
+    private ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock( );
 
     private RepositoryGroupHandler groupHandler;
 
@@ -114,21 +135,25 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa
     private AtomicBoolean remote_initialized = new AtomicBoolean( false );
 
 
-    public ArchivaRepositoryRegistry(ConfigurationHandler configurationHandler) {
-        this.eventManager = new EventManager(this);
+    public ArchivaRepositoryRegistry( ConfigurationHandler configurationHandler )
+    {
+        this.eventManager = new EventManager( this );
         this.configurationHandler = configurationHandler;
     }
 
     @Override
-    public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration ) {
+    public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
+    {
         this.configurationHandler.setArchivaConfiguration( archivaConfiguration );
     }
 
     @PostConstruct
-    private void initialize() {
-        rwLock.writeLock().lock();
-        try {
-            log.debug("Initializing repository registry");
+    private void initialize( )
+    {
+        rwLock.writeLock( ).lock( );
+        try
+        {
+            log.debug( "Initializing repository registry" );
             updateManagedRepositoriesFromConfig( );
             pushEvent( new RepositoryRegistryEvent( RepositoryRegistryEvent.MANAGED_REPOS_INITIALIZED, this ) );
             managed_initialized.set( true );
@@ -136,248 +161,311 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa
             pushEvent( new RepositoryRegistryEvent( RepositoryRegistryEvent.REMOTE_REPOS_INITIALIZED, this ) );
             remote_initialized.set( true );
 
-            initializeRepositoryGroups();
-            this.configurationHandler.addListener(this);
-        } finally {
-            rwLock.writeLock().unlock();
+            initializeRepositoryGroups( );
+
+            for ( RepositoryProvider provider : repositoryProviders )
+            {
+                provider.addRepositoryEventHandler( this );
+            }
+            this.configurationHandler.addListener( this );
         }
-        pushEvent(new RepositoryRegistryEvent(RepositoryRegistryEvent.RELOADED, this));
-        if (managed_initialized.get() && remote_initialized.get() && groups_initalized.get( )) {
+        finally
+        {
+            rwLock.writeLock( ).unlock( );
+        }
+        pushEvent( new RepositoryRegistryEvent( RepositoryRegistryEvent.RELOADED, this ) );
+        if ( managed_initialized.get( ) && remote_initialized.get( ) && groups_initalized.get( ) )
+        {
             pushEvent( new RepositoryRegistryEvent( RepositoryRegistryEvent.INITIALIZED, this ) );
         }
     }
 
-    private void initializeRepositoryGroups() {
-        if (this.groupHandler!=null) {
-            this.groupHandler.initializeFromConfig();
+    private void initializeRepositoryGroups( )
+    {
+        if ( this.groupHandler != null )
+        {
+            this.groupHandler.initializeFromConfig( );
             this.groups_initalized.set( true );
             pushEvent( new RepositoryRegistryEvent( RepositoryRegistryEvent.GROUPS_INITIALIZED, this ) );
         }
     }
 
-    public void registerGroupHandler(RepositoryGroupHandler groupHandler) {
+    public void registerGroupHandler( RepositoryGroupHandler groupHandler )
+    {
         this.groupHandler = groupHandler;
-        initializeRepositoryGroups();
-        if (managed_initialized.get() && remote_initialized.get() && groups_initalized.get( )) {
+        initializeRepositoryGroups( );
+        if ( managed_initialized.get( ) && remote_initialized.get( ) && groups_initalized.get( ) )
+        {
             pushEvent( new RepositoryRegistryEvent( RepositoryRegistryEvent.INITIALIZED, this ) );
         }
     }
 
 
     @PreDestroy
-    public void destroy() {
-        for (ManagedRepository rep : managedRepositories.values()) {
-            rep.close();
+    public void destroy( )
+    {
+        for ( ManagedRepository rep : managedRepositories.values( ) )
+        {
+            rep.close( );
         }
-        managedRepositories.clear();
-        for (RemoteRepository repo : remoteRepositories.values()) {
-            repo.close();
+        managedRepositories.clear( );
+        for ( RemoteRepository repo : remoteRepositories.values( ) )
+        {
+            repo.close( );
         }
-        remoteRepositories.clear();
-        groupHandler.close();
-        pushEvent(new RepositoryRegistryEvent(RepositoryRegistryEvent.DESTROYED, this));
+        remoteRepositories.clear( );
+        groupHandler.close( );
+        pushEvent( new RepositoryRegistryEvent( RepositoryRegistryEvent.DESTROYED, this ) );
     }
 
 
-    protected Map<RepositoryType, RepositoryProvider> getRepositoryProviderMap() {
-        Map<RepositoryType, RepositoryProvider> map = new HashMap<>();
-        if (repositoryProviders != null) {
-            for (RepositoryProvider provider : repositoryProviders) {
-                for (RepositoryType type : provider.provides()) {
-                    map.put(type, provider);
+    protected Map<RepositoryType, RepositoryProvider> getRepositoryProviderMap( )
+    {
+        Map<RepositoryType, RepositoryProvider> map = new HashMap<>( );
+        if ( repositoryProviders != null )
+        {
+            for ( RepositoryProvider provider : repositoryProviders )
+            {
+                for ( RepositoryType type : provider.provides( ) )
+                {
+                    map.put( type, provider );
                 }
             }
         }
         return map;
     }
 
-    protected RepositoryProvider getProvider(RepositoryType type) throws RepositoryException
+    protected RepositoryProvider getProvider( RepositoryType type ) throws RepositoryException
     {
-        return repositoryProviders.stream().filter(repositoryProvider -> repositoryProvider.provides().contains(type)).findFirst().orElseThrow(() -> new RepositoryException("Repository type cannot be handled: " + type));
+        return repositoryProviders.stream( ).filter( repositoryProvider -> repositoryProvider.provides( ).contains( type ) ).findFirst( ).orElseThrow( ( ) -> new RepositoryException( "Repository type cannot be handled: " + type ) );
     }
 
     /*
      * Updates the repositories
      */
-    private void updateManagedRepositoriesFromConfig() {
-        try {
+    private void updateManagedRepositoriesFromConfig( )
+    {
+        try
+        {
 
-            Set<String> configRepoIds = new HashSet<>();
+            Set<String> configRepoIds = new HashSet<>( );
             List<ManagedRepositoryConfiguration> managedRepoConfigs =
-                    configurationHandler.getBaseConfiguration().getManagedRepositories();
+                configurationHandler.getBaseConfiguration( ).getManagedRepositories( );
 
-            if (managedRepoConfigs == null) {
+            if ( managedRepoConfigs == null )
+            {
                 return;
             }
 
-            for (ManagedRepositoryConfiguration repoConfig : managedRepoConfigs) {
-                ManagedRepository repo = putRepository(repoConfig, null);
-                configRepoIds.add(repoConfig.getId());
-                if (repo.supportsFeature(StagingRepositoryFeature.class)) {
-                    StagingRepositoryFeature stagF = repo.getFeature(StagingRepositoryFeature.class).get();
-                    if (stagF.getStagingRepository() != null) {
-                        configRepoIds.add(stagF.getStagingRepository().getId());
+            for ( ManagedRepositoryConfiguration repoConfig : managedRepoConfigs )
+            {
+                ManagedRepository repo = putRepository( repoConfig, null );
+                configRepoIds.add( repoConfig.getId( ) );
+                if ( repo.supportsFeature( StagingRepositoryFeature.class ) )
+                {
+                    StagingRepositoryFeature stagF = repo.getFeature( StagingRepositoryFeature.class ).get( );
+                    if ( stagF.getStagingRepository( ) != null )
+                    {
+                        configRepoIds.add( stagF.getStagingRepository( ).getId( ) );
                     }
                 }
             }
-            List<String> toRemove = managedRepositories.keySet().stream().filter(id -> !configRepoIds.contains(id)).collect(Collectors.toList());
-            for (String id : toRemove) {
-                ManagedRepository removed = managedRepositories.remove(id);
-                removed.close();
+            List<String> toRemove = managedRepositories.keySet( ).stream( ).filter( id -> !configRepoIds.contains( id ) ).collect( Collectors.toList( ) );
+            for ( String id : toRemove )
+            {
+                ManagedRepository removed = managedRepositories.remove( id );
+                removed.close( );
             }
-        } catch (Throwable e) {
-            log.error("Could not initialize repositories from config: {}", e.getMessage(), e);
+        }
+        catch ( Throwable e )
+        {
+            log.error( "Could not initialize repositories from config: {}", e.getMessage( ), e );
             return;
         }
     }
 
-    private ManagedRepository createNewManagedRepository(RepositoryProvider provider, ManagedRepositoryConfiguration cfg) throws RepositoryException {
-        log.debug("Creating repo {}", cfg.getId());
-        ManagedRepository repo = provider.createManagedInstance(cfg);
-        repo.registerEventHandler(RepositoryEvent.ANY,  this);
-        updateRepositoryReferences(provider, repo, cfg, null);
+    private ManagedRepository createNewManagedRepository( RepositoryProvider provider, ManagedRepositoryConfiguration cfg ) throws RepositoryException
+    {
+        log.debug( "Creating repo {}", cfg.getId( ) );
+        ManagedRepository repo = provider.createManagedInstance( cfg );
+        repo.registerEventHandler( RepositoryEvent.ANY, this );
+        updateRepositoryReferences( provider, repo, cfg, null );
         return repo;
 
     }
 
-    private String getStagingId(String repoId) {
+    private String getStagingId( String repoId )
+    {
         return repoId + StagingRepositoryFeature.STAGING_REPO_POSTFIX;
     }
 
-    @SuppressWarnings("unchecked")
-    private void updateRepositoryReferences(RepositoryProvider provider, ManagedRepository repo, ManagedRepositoryConfiguration cfg, Configuration configuration) throws RepositoryException {
-        log.debug("Updating references of repo {}", repo.getId());
-        if (repo.supportsFeature(StagingRepositoryFeature.class)) {
-            StagingRepositoryFeature feature = repo.getFeature(StagingRepositoryFeature.class).get();
-            if (feature.isStageRepoNeeded() && feature.getStagingRepository() == null) {
-                ManagedRepository stageRepo = getManagedRepository(getStagingId(repo.getId()));
-                if (stageRepo == null) {
-                    stageRepo = getStagingRepository(provider, cfg, configuration);
-                    managedRepositories.put(stageRepo.getId(), stageRepo);
-                    if (configuration != null) {
-                        replaceOrAddRepositoryConfig(provider.getManagedConfiguration(stageRepo), configuration);
+    @SuppressWarnings( "unchecked" )
+    private void updateRepositoryReferences( RepositoryProvider provider, ManagedRepository repo, ManagedRepositoryConfiguration cfg, Configuration configuration ) throws RepositoryException
+    {
+        log.debug( "Updating references of repo {}", repo.getId( ) );
+        if ( repo.supportsFeature( StagingRepositoryFeature.class ) )
+        {
+            StagingRepositoryFeature feature = repo.getFeature( StagingRepositoryFeature.class ).get( );
+            if ( feature.isStageRepoNeeded( ) && feature.getStagingRepository( ) == null )
+            {
+                ManagedRepository stageRepo = getManagedRepository( getStagingId( repo.getId( ) ) );
+                if ( stageRepo == null )
+                {
+                    stageRepo = getStagingRepository( provider, cfg, configuration );
+                    managedRepositories.put( stageRepo.getId( ), stageRepo );
+                    if ( configuration != null )
+                    {
+                        replaceOrAddRepositoryConfig( provider.getManagedConfiguration( stageRepo ), configuration );
                     }
-                    pushEvent(new LifecycleEvent(LifecycleEvent.REGISTERED, this, stageRepo));
+                    pushEvent( new LifecycleEvent( LifecycleEvent.REGISTERED, this, stageRepo ) );
                 }
-                feature.setStagingRepository(stageRepo);
+                feature.setStagingRepository( stageRepo );
             }
         }
-        if (repo instanceof EditableManagedRepository ) {
+        if ( repo instanceof EditableManagedRepository )
+        {
             EditableManagedRepository editableRepo = (EditableManagedRepository) repo;
-            if (repo.getContent() == null) {
-                editableRepo.setContent(repositoryContentFactory.getManagedRepositoryContent(repo));
-                editableRepo.getContent().setRepository(editableRepo);
+            if ( repo.getContent( ) == null )
+            {
+                editableRepo.setContent( repositoryContentFactory.getManagedRepositoryContent( repo ) );
+                editableRepo.getContent( ).setRepository( editableRepo );
             }
-            log.debug("Index repo: " + repo.hasIndex());
-            if (repo.hasIndex() && ( repo.getIndexingContext() == null || !repo.getIndexingContext().isOpen() )) {
-                log.debug("Creating indexing context for {}", repo.getId());
-                createIndexingContext(editableRepo);
+            log.debug( "Index repo: " + repo.hasIndex( ) );
+            if ( repo.hasIndex( ) && ( repo.getIndexingContext( ) == null || !repo.getIndexingContext( ).isOpen( ) ) )
+            {
+                log.debug( "Creating indexing context for {}", repo.getId( ) );
+                createIndexingContext( editableRepo );
             }
         }
-        repo.registerEventHandler(RepositoryEvent.ANY, this);
+        repo.registerEventHandler( RepositoryEvent.ANY, this );
     }
 
     @Override
-    public ArchivaIndexManager getIndexManager( RepositoryType type ) {
-        return indexManagerFactory.getIndexManager(type);
+    public ArchivaIndexManager getIndexManager( RepositoryType type )
+    {
+        return indexManagerFactory.getIndexManager( type );
     }
 
     @Override
     public MetadataReader getMetadataReader( final RepositoryType type ) throws UnsupportedRepositoryTypeException
     {
-        if (metadataReaderList!=null) {
+        if ( metadataReaderList != null )
+        {
             return metadataReaderList.stream( ).filter( mr -> mr.isValidForType( type ) ).findFirst( ).orElseThrow( ( ) -> new UnsupportedRepositoryTypeException( type ) );
-        } else {
+        }
+        else
+        {
             throw new UnsupportedRepositoryTypeException( type );
         }
     }
 
-    private void createIndexingContext( EditableRepository editableRepo) throws RepositoryException {
-        if (editableRepo.supportsFeature(IndexCreationFeature.class)) {
-            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);
+    private void createIndexingContext( EditableRepository editableRepo ) throws RepositoryException
+    {
+        if ( editableRepo.supportsFeature( IndexCreationFeature.class ) )
+        {
+            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 );
             }
         }
     }
 
-    private ManagedRepository getStagingRepository(RepositoryProvider provider, ManagedRepositoryConfiguration baseRepoCfg, Configuration configuration) throws RepositoryException {
-        ManagedRepository stageRepo = getManagedRepository(getStagingId(baseRepoCfg.getId()));
-        if (stageRepo == null) {
-            stageRepo = provider.createStagingInstance(baseRepoCfg);
-            if (stageRepo.supportsFeature(StagingRepositoryFeature.class)) {
-                stageRepo.getFeature(StagingRepositoryFeature.class).get().setStageRepoNeeded(false);
+    private ManagedRepository getStagingRepository( RepositoryProvider provider, ManagedRepositoryConfiguration baseRepoCfg, Configuration configuration ) throws RepositoryException
+    {
+        ManagedRepository stageRepo = getManagedRepository( getStagingId( baseRepoCfg.getId( ) ) );
+        if ( stageRepo == null )
+        {
+            stageRepo = provider.createStagingInstance( baseRepoCfg );
+            if ( stageRepo.supportsFeature( StagingRepositoryFeature.class ) )
+            {
+                stageRepo.getFeature( StagingRepositoryFeature.class ).get( ).setStageRepoNeeded( false );
             }
-            ManagedRepositoryConfiguration stageCfg = provider.getManagedConfiguration(stageRepo);
-            updateRepositoryReferences(provider, stageRepo, stageCfg, configuration);
+            ManagedRepositoryConfiguration stageCfg = provider.getManagedConfiguration( stageRepo );
+            updateRepositoryReferences( provider, stageRepo, stageCfg, configuration );
         }
         return stageRepo;
     }
 
 
-    private void updateRemoteRepositoriesFromConfig() {
-        try {
+    private void updateRemoteRepositoriesFromConfig( )
+    {
+        try
+        {
             List<RemoteRepositoryConfiguration> remoteRepoConfigs =
-                    configurationHandler.getBaseConfiguration().getRemoteRepositories();
+                configurationHandler.getBaseConfiguration( ).getRemoteRepositories( );
 
-            if (remoteRepoConfigs == null) {
+            if ( remoteRepoConfigs == null )
+            {
                 return;
             }
-            Set<String> repoIds = new HashSet<>();
-            for (RemoteRepositoryConfiguration repoConfig : remoteRepoConfigs) {
-                putRepository(repoConfig, null);
-                repoIds.add(repoConfig.getId());
+            Set<String> repoIds = new HashSet<>( );
+            for ( RemoteRepositoryConfiguration repoConfig : remoteRepoConfigs )
+            {
+                putRepository( repoConfig, null );
+                repoIds.add( repoConfig.getId( ) );
             }
 
-            List<String> toRemove = remoteRepositories.keySet().stream().filter(id -> !repoIds.contains(id)).collect(Collectors.toList());
-            for (String id : toRemove) {
-                RemoteRepository removed = remoteRepositories.remove(id);
-                removed.close();
+            List<String> toRemove = remoteRepositories.keySet( ).stream( ).filter( id -> !repoIds.contains( id ) ).collect( Collectors.toList( ) );
+            for ( String id : toRemove )
+            {
+                RemoteRepository removed = remoteRepositories.remove( id );
+                removed.close( );
             }
 
-        } catch (Throwable e) {
-            log.error("Could not initialize remote repositories from config: {}", e.getMessage(), e);
+        }
+        catch ( Throwable e )
+        {
+            log.error( "Could not initialize remote repositories from config: {}", e.getMessage( ), e );
             return;
         }
     }
 
-    private RemoteRepository createNewRemoteRepository(RepositoryProvider provider, RemoteRepositoryConfiguration cfg) throws RepositoryException {
-        log.debug("Creating remote repo {}", cfg.getId());
-        RemoteRepository repo = provider.createRemoteInstance(cfg);
-        updateRepositoryReferences(provider, repo, cfg, null);
+    private RemoteRepository createNewRemoteRepository( RepositoryProvider provider, RemoteRepositoryConfiguration cfg ) throws RepositoryException
+    {
+        log.debug( "Creating remote repo {}", cfg.getId( ) );
+        RemoteRepository repo = provider.createRemoteInstance( cfg );
+        updateRepositoryReferences( provider, repo, cfg, null );
         return repo;
 
     }
 
-    private void updateRepositoryReferences(RepositoryProvider provider, RemoteRepository repo, RemoteRepositoryConfiguration cfg, Configuration configuration) throws RepositoryException {
-        if (repo instanceof EditableRemoteRepository && repo.getContent() == null) {
+    private void updateRepositoryReferences( RepositoryProvider provider, RemoteRepository repo, RemoteRepositoryConfiguration cfg, Configuration configuration ) throws RepositoryException
+    {
+        if ( repo instanceof EditableRemoteRepository && repo.getContent( ) == null )
+        {
             EditableRemoteRepository editableRepo = (EditableRemoteRepository) repo;
-            editableRepo.setContent(repositoryContentFactory.getRemoteRepositoryContent(repo));
-            if (repo.supportsFeature(IndexCreationFeature.class) && repo.getIndexingContext() == null) {
-                createIndexingContext(editableRepo);
+            editableRepo.setContent( repositoryContentFactory.getRemoteRepositoryContent( repo ) );
+            if ( repo.supportsFeature( IndexCreationFeature.class ) && repo.getIndexingContext( ) == null )
+            {
+                createIndexingContext( editableRepo );
             }
         }
-        repo.registerEventHandler(RepositoryEvent.ANY, this);
+        repo.registerEventHandler( RepositoryEvent.ANY, this );
     }
 
 
-
-
     /**
      * Returns all repositories that are registered. There is no defined order of the returned repositories.
      *
      * @return a list of managed and remote repositories
      */
     @Override
-    public Collection<Repository> getRepositories( ) {
-        rwLock.readLock().lock();
-        try {
-            return Stream.concat(managedRepositories.values().stream(), remoteRepositories.values().stream()).collect(Collectors.toList());
-        } finally {
-            rwLock.readLock().unlock();
+    public Collection<Repository> getRepositories( )
+    {
+        rwLock.readLock( ).lock( );
+        try
+        {
+            return Stream.concat( managedRepositories.values( ).stream( ), remoteRepositories.values( ).stream( ) ).collect( Collectors.toList( ) );
+        }
+        finally
+        {
+            rwLock.readLock( ).unlock( );
         }
     }
 
@@ -387,12 +475,16 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa
      * @return a list of managed repositories
      */
     @Override
-    public Collection<ManagedRepository> getManagedRepositories( ) {
-        rwLock.readLock().lock();
-        try {
-            return uManagedRepository.values();
-        } finally {
-            rwLock.readLock().unlock();
+    public Collection<ManagedRepository> getManagedRepositories( )
+    {
+        rwLock.readLock( ).lock( );
+        try
+        {
+            return uManagedRepository.values( );
+        }
+        finally
+        {
+            rwLock.readLock( ).unlock( );
         }
     }
 
@@ -402,22 +494,30 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa
      * @return a list of remote repositories
      */
     @Override
-    public Collection<RemoteRepository> getRemoteRepositories( ) {
-        rwLock.readLock().lock();
-        try {
-            return uRemoteRepositories.values();
-        } finally {
-            rwLock.readLock().unlock();
+    public Collection<RemoteRepository> getRemoteRepositories( )
+    {
+        rwLock.readLock( ).lock( );
+        try
+        {
+            return uRemoteRepositories.values( );
+        }
+        finally
+        {
+            rwLock.readLock( ).unlock( );
         }
     }
 
     @Override
-    public Collection<RepositoryGroup> getRepositoryGroups( ) {
-        rwLock.readLock().lock();
-        try {
+    public Collection<RepositoryGroup> getRepositoryGroups( )
+    {
+        rwLock.readLock( ).lock( );
+        try
+        {
             return groupHandler.getRepositoryGroups( );
-        } finally {
-            rwLock.readLock().unlock();
+        }
+        finally
+        {
+            rwLock.readLock( ).unlock( );
         }
     }
 
@@ -429,23 +529,34 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa
      * @return the repository if found, otherwise null
      */
     @Override
-    public Repository getRepository( String repoId ) {
-        rwLock.readLock().lock();
-        try {
-            log.debug("getRepository {}", repoId);
-            if (managedRepositories.containsKey(repoId)) {
-                log.debug("Managed repo");
-                return managedRepositories.get(repoId);
-            } else if (remoteRepositories.containsKey(repoId)) {
-                log.debug("Remote repo");
-                return remoteRepositories.get(repoId);
-            } else if (groupHandler.hasRepositoryGroup(repoId)) {
-                return groupHandler.getRepositoryGroup(repoId);
-            } else {
+    public Repository getRepository( String repoId )
+    {
+        rwLock.readLock( ).lock( );
+        try
+        {
+            log.debug( "getRepository {}", repoId );
+            if ( managedRepositories.containsKey( repoId ) )
+            {
+                log.debug( "Managed repo" );
+                return managedRepositories.get( repoId );
+            }
+            else if ( remoteRepositories.containsKey( repoId ) )
+            {
+                log.debug( "Remote repo" );
+                return remoteRepositories.get( repoId );
+            }
+            else if ( groupHandler.hasRepositoryGroup( repoId ) )
+            {
+                return groupHandler.getRepositoryGroup( repoId );
+            }
+            else
+            {
                 return null;
             }
-        } finally {
-            rwLock.readLock().unlock();
+        }
+        finally
+        {
+            rwLock.readLock( ).unlock( );
         }
     }
 
@@ -457,12 +568,16 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa
      * @return the managed repository if found, otherwise null
      */
     @Override
-    public ManagedRepository getManagedRepository( String repoId ) {
-        rwLock.readLock().lock();
-        try {
-            return managedRepositories.get(repoId);
-        } finally {
-            rwLock.readLock().unlock();
+    public ManagedRepository getManagedRepository( String repoId )
+    {
+        rwLock.readLock( ).lock( );
+        try
+        {
+            return managedRepositories.get( repoId );
+        }
+        finally
+        {
+            rwLock.readLock( ).unlock( );
         }
     }
 
@@ -474,27 +589,36 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa
      * @return the remote repository if found, otherwise null
      */
     @Override
-    public RemoteRepository getRemoteRepository( String repoId ) {
-        rwLock.readLock().lock();
-        try {
-            return remoteRepositories.get(repoId);
-        } finally {
-            rwLock.readLock().unlock();
+    public RemoteRepository getRemoteRepository( String repoId )
+    {
+        rwLock.readLock( ).lock( );
+        try
+        {
+            return remoteRepositories.get( repoId );
+        }
+        finally
+        {
+            rwLock.readLock( ).unlock( );
         }
     }
 
     @Override
-    public RepositoryGroup getRepositoryGroup( String groupId ) {
-        rwLock.readLock().lock();
-        try {
-            return groupHandler.getRepositoryGroup(groupId);
-        } finally {
-            rwLock.readLock().unlock();
+    public RepositoryGroup getRepositoryGroup( String groupId )
+    {
+        rwLock.readLock( ).lock( );
+        try
+        {
+            return groupHandler.getRepositoryGroup( groupId );
+        }
+        finally
+        {
+            rwLock.readLock( ).unlock( );
         }
     }
 
-    protected void saveConfiguration(Configuration configuration) throws IndeterminateConfigurationException, RegistryException {
-        configurationHandler.save(configuration, ConfigurationHandler.REGISTRY_EVENT_TAG );
+    protected void saveConfiguration( Configuration configuration ) throws IndeterminateConfigurationException, RegistryException
+    {
+        configurationHandler.save( configuration, ConfigurationHandler.REGISTRY_EVENT_TAG );
     }
 
     /**
@@ -506,46 +630,62 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa
      * @throws RepositoryException if the new repository could not be saved to the configuration.
      */
     @Override
-    public ManagedRepository putRepository( ManagedRepository managedRepository ) throws RepositoryException {
-        rwLock.writeLock().lock();
-        try {
-            final String id = managedRepository.getId();
-            if (remoteRepositories.containsKey(id)) {
-                throw new RepositoryException("There exists a remote repository with id " + id + ". Could not update with managed repository.");
+    public ManagedRepository putRepository( ManagedRepository managedRepository ) throws RepositoryException
+    {
+        rwLock.writeLock( ).lock( );
+        try
+        {
+            final String id = managedRepository.getId( );
+            if ( remoteRepositories.containsKey( id ) )
+            {
+                throw new RepositoryException( "There exists a remote repository with id " + id + ". Could not update with managed repository." );
             }
-            ManagedRepository originRepo = managedRepositories.put(id, managedRepository);
-            try {
-                if (originRepo != null && originRepo != managedRepository) {
-                    originRepo.close();
+            ManagedRepository originRepo = managedRepositories.put( id, managedRepository );
+            try
+            {
+                if ( originRepo != null && originRepo != managedRepository )
+                {
+                    originRepo.close( );
                 }
-                RepositoryProvider provider = getProvider(managedRepository.getType());
-                ManagedRepositoryConfiguration newCfg = provider.getManagedConfiguration(managedRepository);
-                Configuration configuration = configurationHandler.getBaseConfiguration();
-                updateRepositoryReferences(provider, managedRepository, newCfg, configuration);
-                ManagedRepositoryConfiguration oldCfg = configuration.findManagedRepositoryById(id);
-                if (oldCfg != null) {
-                    configuration.removeManagedRepository(oldCfg);
+                RepositoryProvider provider = getProvider( managedRepository.getType( ) );
+                ManagedRepositoryConfiguration newCfg = provider.getManagedConfiguration( managedRepository );
+                Configuration configuration = configurationHandler.getBaseConfiguration( );
+                updateRepositoryReferences( provider, managedRepository, newCfg, configuration );
+                ManagedRepositoryConfiguration oldCfg = configuration.findManagedRepositoryById( id );
+                if ( oldCfg != null )
+                {
+                    configuration.removeManagedRepository( oldCfg );
                 }
-                configuration.addManagedRepository(newCfg);
-                saveConfiguration(configuration);
-                if (originRepo != managedRepository) {
-                    pushEvent(new LifecycleEvent(LifecycleEvent.REGISTERED, this, managedRepository));
-                } else {
-                    pushEvent(new LifecycleEvent(LifecycleEvent.UPDATED, this, managedRepository));
+                configuration.addManagedRepository( newCfg );
+                saveConfiguration( configuration );
+                if ( originRepo != managedRepository )
+                {
+                    pushEvent( new LifecycleEvent( LifecycleEvent.REGISTERED, this, managedRepository ) );
+                }
+                else
+                {
+                    pushEvent( new LifecycleEvent( LifecycleEvent.UPDATED, this, managedRepository ) );
                 }
                 return managedRepository;
-            } catch (Exception e) {
+            }
+            catch ( Exception e )
+            {
                 // Rollback only partly, because repository is closed already
-                if (originRepo != null) {
-                    managedRepositories.put(id, originRepo);
-                } else {
-                    managedRepositories.remove(id);
+                if ( originRepo != null )
+                {
+                    managedRepositories.put( id, originRepo );
                 }
-                log.error("Exception during configuration update {}", e.getMessage(), e);
-                throw new RepositoryException("Could not save the configuration" + (e.getMessage() == null ? "" : ": " + e.getMessage()));
+                else
+                {
+                    managedRepositories.remove( id );
+                }
+                log.error( "Exception during configuration update {}", e.getMessage( ), e );
+                throw new RepositoryException( "Could not save the configuration" + ( e.getMessage( ) == null ? "" : ": " + e.getMessage( ) ) );
             }
-        } finally {
-            rwLock.writeLock().unlock();
+        }
+        finally
+        {
+            rwLock.writeLock( ).unlock( );
         }
     }
 
@@ -558,27 +698,35 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa
      * @throws RepositoryException if an error occurs, or the configuration is not valid.
      */
     @Override
-    public ManagedRepository putRepository( ManagedRepositoryConfiguration managedRepositoryConfiguration ) throws RepositoryException {
-        rwLock.writeLock().lock();
-        try {
-            final String id = managedRepositoryConfiguration.getId();
-            final RepositoryType repositoryType = RepositoryType.valueOf(managedRepositoryConfiguration.getType());
-            Configuration configuration = configurationHandler.getBaseConfiguration();
-            ManagedRepository repo = managedRepositories.get(id);
-            ManagedRepositoryConfiguration oldCfg = repo != null ? getProvider(repositoryType).getManagedConfiguration(repo) : null;
-            repo = putRepository(managedRepositoryConfiguration, configuration);
-            try {
-                saveConfiguration(configuration);
-            } catch (IndeterminateConfigurationException | RegistryException e) {
-                if (oldCfg != null) {
-                    getProvider(repositoryType).updateManagedInstance((EditableManagedRepository) repo, oldCfg);
+    public ManagedRepository putRepository( ManagedRepositoryConfiguration managedRepositoryConfiguration ) throws RepositoryException
+    {
+        rwLock.writeLock( ).lock( );
+        try
+        {
+            final String id = managedRepositoryConfiguration.getId( );
+            final RepositoryType repositoryType = RepositoryType.valueOf( managedRepositoryConfiguration.getType( ) );
+            Configuration configuration = configurationHandler.getBaseConfiguration( );
+            ManagedRepository repo = managedRepositories.get( id );
+            ManagedRepositoryConfiguration oldCfg = repo != null ? getProvider( repositoryType ).getManagedConfiguration( repo ) : null;
+            repo = putRepository( managedRepositoryConfiguration, configuration );
+            try
+            {
+                saveConfiguration( configuration );
+            }
+            catch ( IndeterminateConfigurationException | RegistryException e )
+            {
+                if ( oldCfg != null )
+                {
+                    getProvider( repositoryType ).updateManagedInstance( (EditableManagedRepository) repo, oldCfg );
                 }
-                log.error("Could not save the configuration for repository {}: {}", id, e.getMessage(), e);
-                throw new RepositoryException("Could not save the configuration for repository " + id + ": " + e.getMessage());
+                log.error( "Could not save the configuration for repository {}: {}", id, e.getMessage( ), e );
+                throw new RepositoryException( "Could not save the configuration for repository " + id + ": " + e.getMessage( ) );
             }
             return repo;
-        } finally {
-            rwLock.writeLock().unlock();
+        }
+        finally
+        {
+            rwLock.writeLock( ).unlock( );
         }
 
     }
@@ -593,35 +741,48 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa
      * @throws RepositoryException if the configuration cannot be saved or updated
      */
     @Override
-    public ManagedRepository putRepository( ManagedRepositoryConfiguration managedRepositoryConfiguration, Configuration configuration ) throws RepositoryException {
-        rwLock.writeLock().lock();
-        try {
-            final String id = managedRepositoryConfiguration.getId();
-            final RepositoryType repoType = RepositoryType.valueOf(managedRepositoryConfiguration.getType());
+    public ManagedRepository putRepository( ManagedRepositoryConfiguration managedRepositoryConfiguration, Configuration configuration ) throws RepositoryException
+    {
+        rwLock.writeLock( ).lock( );
+        try
+        {
+            final String id = managedRepositoryConfiguration.getId( );
+            final RepositoryType repoType = RepositoryType.valueOf( managedRepositoryConfiguration.getType( ) );
             ManagedRepository repo;
             boolean registeredNew = false;
-            repo = managedRepositories.get(id);
-            if (repo != null && repo.isOpen()) {
-                if (repo instanceof EditableManagedRepository) {
-                    getProvider(repoType).updateManagedInstance((EditableManagedRepository) repo, managedRepositoryConfiguration);
-                } else {
-                    throw new RepositoryException("The repository is not editable " + id);
+            repo = managedRepositories.get( id );
+            if ( repo != null && repo.isOpen( ) )
+            {
+                if ( repo instanceof EditableManagedRepository )
+                {
+                    getProvider( repoType ).updateManagedInstance( (EditableManagedRepository) repo, managedRepositoryConfiguration );
                 }
-            } else {
-                repo = getProvider(repoType).createManagedInstance(managedRepositoryConfiguration);
-                managedRepositories.put(id, repo);
+                else
+                {
+                    throw new RepositoryException( "The repository is not editable " + id );
+                }
+            }
+            else
+            {
+                repo = getProvider( repoType ).createManagedInstance( managedRepositoryConfiguration );
+                managedRepositories.put( id, repo );
                 registeredNew = true;
             }
-            updateRepositoryReferences(getProvider(repoType), repo, managedRepositoryConfiguration, configuration);
-            replaceOrAddRepositoryConfig(managedRepositoryConfiguration, configuration);
-            if (registeredNew) {
-                pushEvent(new LifecycleEvent(LifecycleEvent.REGISTERED, this, repo));
-            } else {
-                pushEvent(new LifecycleEvent(LifecycleEvent.UPDATED, this, repo));
+            updateRepositoryReferences( getProvider( repoType ), repo, managedRepositoryConfiguration, configuration );
+            replaceOrAddRepositoryConfig( managedRepositoryConfiguration, configuration );
+            if ( registeredNew )
+            {
+                pushEvent( new LifecycleEvent( LifecycleEvent.REGISTERED, this, repo ) );
+            }
+            else
+            {
+                pushEvent( new LifecycleEvent( LifecycleEvent.UPDATED, this, repo ) );
             }
             return repo;
-        } finally {
-            rwLock.writeLock().unlock();
+        }
+        finally
+        {
+            rwLock.writeLock( ).unlock( );
         }
     }
 
@@ -635,15 +796,20 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa
      * @throws RepositoryException if the new repository group could not be saved to the configuration.
      */
     @Override
-    public RepositoryGroup putRepositoryGroup( RepositoryGroup repositoryGroup ) throws RepositoryException {
-        rwLock.writeLock().lock();
-        try {
-            if (this.groupHandler==null) {
+    public RepositoryGroup putRepositoryGroup( RepositoryGroup repositoryGroup ) throws RepositoryException
+    {
+        rwLock.writeLock( ).lock( );
+        try
+        {
+            if ( this.groupHandler == null )
+            {
                 throw new RepositoryException( "Fatal error. RepositoryGroupHandler not registered!" );
             }
             return this.groupHandler.putRepositoryGroup( repositoryGroup );
-        } finally {
-            rwLock.writeLock().unlock();
+        }
+        finally
+        {
+            rwLock.writeLock( ).unlock( );
         }
     }
 
@@ -656,12 +822,16 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa
      * @throws RepositoryException if an error occurs, or the configuration is not valid.
      */
     @Override
-    public RepositoryGroup putRepositoryGroup( RepositoryGroupConfiguration repositoryGroupConfiguration ) throws RepositoryException {
-        rwLock.writeLock().lock();
-        try {
+    public RepositoryGroup putRepositoryGroup( RepositoryGroupConfiguration repositoryGroupConfiguration ) throws RepositoryException
+    {
+        rwLock.writeLock( ).lock( );
+        try
+        {
             return groupHandler.putRepositoryGroup( repositoryGroupConfiguration );
-        } finally {
-            rwLock.writeLock().unlock();
+        }
+        finally
+        {
+            rwLock.writeLock( ).unlock( );
         }
 
     }
@@ -676,91 +846,121 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa
      * @throws RepositoryException if the configuration cannot be saved or updated
      */
     @Override
-    public RepositoryGroup putRepositoryGroup( RepositoryGroupConfiguration repositoryGroupConfiguration, Configuration configuration ) throws RepositoryException {
-        rwLock.writeLock().lock();
-        try {
+    public RepositoryGroup putRepositoryGroup( RepositoryGroupConfiguration repositoryGroupConfiguration, Configuration configuration ) throws RepositoryException
+    {
+        rwLock.writeLock( ).lock( );
+        try
+        {
             return groupHandler.putRepositoryGroup( repositoryGroupConfiguration, configuration );
-        } finally {
-            rwLock.writeLock().unlock();
+        }
+        finally
+        {
+            rwLock.writeLock( ).unlock( );
         }
     }
 
-    private void replaceOrAddRepositoryConfig(ManagedRepositoryConfiguration managedRepositoryConfiguration, Configuration configuration) {
-        if (configuration != null) {
-            ManagedRepositoryConfiguration oldCfg = configuration.findManagedRepositoryById(managedRepositoryConfiguration.getId());
-            if (oldCfg != null) {
-                configuration.removeManagedRepository(oldCfg);
+    private void replaceOrAddRepositoryConfig( ManagedRepositoryConfiguration managedRepositoryConfiguration, Configuration configuration )
+    {
+        if ( configuration != null )
+        {
+            ManagedRepositoryConfiguration oldCfg = configuration.findManagedRepositoryById( managedRepositoryConfiguration.getId( ) );
+            if ( oldCfg != null )
+            {
+                configuration.removeManagedRepository( oldCfg );
             }
-            configuration.addManagedRepository(managedRepositoryConfiguration);
+            configuration.addManagedRepository( managedRepositoryConfiguration );
         }
     }
 
-    private void replaceOrAddRepositoryConfig(RemoteRepositoryConfiguration remoteRepositoryConfiguration, Configuration configuration) {
-        if (configuration != null) {
-            RemoteRepositoryConfiguration oldCfg = configuration.findRemoteRepositoryById(remoteRepositoryConfiguration.getId());
-            if (oldCfg != null) {
-                configuration.removeRemoteRepository(oldCfg);
+    private void replaceOrAddRepositoryConfig( RemoteRepositoryConfiguration remoteRepositoryConfiguration, Configuration configuration )
+    {
+        if ( configuration != null )
+        {
+            RemoteRepositoryConfiguration oldCfg = configuration.findRemoteRepositoryById( remoteRepositoryConfiguration.getId( ) );
+            if ( oldCfg != null )
+            {
+                configuration.removeRemoteRepository( oldCfg );
             }
-            configuration.addRemoteRepository(remoteRepositoryConfiguration);
+            configuration.addRemoteRepository( remoteRepositoryConfiguration );
         }
     }
 
-    private void replaceOrAddRepositoryConfig(RepositoryGroupConfiguration repositoryGroupConfiguration, Configuration configuration) {
-        RepositoryGroupConfiguration oldCfg = configuration.findRepositoryGroupById(repositoryGroupConfiguration.getId());
-        if (oldCfg != null) {
-            configuration.removeRepositoryGroup(oldCfg);
+    private void replaceOrAddRepositoryConfig( RepositoryGroupConfiguration repositoryGroupConfiguration, Configuration configuration )
+    {
+        RepositoryGroupConfiguration oldCfg = configuration.findRepositoryGroupById( repositoryGroupConfiguration.getId( ) );
+        if ( oldCfg != null )
+        {
+            configuration.removeRepositoryGroup( oldCfg );
         }
-        configuration.addRepositoryGroup(repositoryGroupConfiguration);
+        configuration.addRepositoryGroup( repositoryGroupConfiguration );
     }
 
     @Override
-    public RemoteRepository putRepository( RemoteRepository remoteRepository, Configuration configuration ) throws RepositoryException {
-        rwLock.writeLock().lock();
-        try {
-            final String id = remoteRepository.getId();
-            if (managedRepositories.containsKey(id)) {
-                throw new RepositoryException("There exists a managed repository with id " + id + ". Could not update with remote repository.");
+    public RemoteRepository putRepository( RemoteRepository remoteRepository, Configuration configuration ) throws RepositoryException
+    {
+        rwLock.writeLock( ).lock( );
+        try
+        {
+            final String id = remoteRepository.getId( );
+            if ( managedRepositories.containsKey( id ) )
+            {
+                throw new RepositoryException( "There exists a managed repository with id " + id + ". Could not update with remote repository." );
             }
-            RemoteRepository originRepo = remoteRepositories.put(id, remoteRepository);
+            RemoteRepository originRepo = remoteRepositories.put( id, remoteRepository );
             RemoteRepositoryConfiguration oldCfg = null;
             RemoteRepositoryConfiguration newCfg;
-            try {
-                if (originRepo != null && originRepo != remoteRepository) {
-                    originRepo.close();
+            try
+            {
+                if ( originRepo != null && originRepo != remoteRepository )
+                {
+                    originRepo.close( );
+                }
+                final RepositoryProvider provider = getProvider( remoteRepository.getType( ) );
+                newCfg = provider.getRemoteConfiguration( remoteRepository );
+                updateRepositoryReferences( provider, remoteRepository, newCfg, configuration );
+                oldCfg = configuration.findRemoteRepositoryById( id );
+                if ( oldCfg != null )
+                {
+                    configuration.removeRemoteRepository( oldCfg );
                 }
-                final RepositoryProvider provider = getProvider(remoteRepository.getType());
-                newCfg = provider.getRemoteConfiguration(remoteRepository);
-                updateRepositoryReferences(provider, remoteRepository, newCfg, configuration);
-                oldCfg = configuration.findRemoteRepositoryById(id);
-                if (oldCfg != null) {
-                    configuration.removeRemoteRepository(oldCfg);
+                configuration.addRemoteRepository( newCfg );
+                if ( remoteRepository != originRepo )
+                {
+                    pushEvent( new LifecycleEvent( LifecycleEvent.REGISTERED, this, remoteRepository ) );
                 }
-                configuration.addRemoteRepository(newCfg);
-                if (remoteRepository != originRepo) {
-                    pushEvent(new LifecycleEvent(LifecycleEvent.REGISTERED, this, remoteRepository));
-                } else {
-                    pushEvent(new LifecycleEvent(LifecycleEvent.UPDATED, this, remoteRepository));
+                else
+                {
+                    pushEvent( new LifecycleEvent( LifecycleEvent.UPDATED, this, remoteRepository ) );
                 }
                 return remoteRepository;
-            } catch (Exception e) {
+            }
+            catch ( Exception e )
+            {
                 // Rollback
-                if (originRepo != null) {
-                    remoteRepositories.put(id, originRepo);
-                } else {
-                    remoteRepositories.remove(id);
+                if ( originRepo != null )
+                {
+                    remoteRepositories.put( id, originRepo );
                 }
-                if (oldCfg != null) {
-                    RemoteRepositoryConfiguration cfg = configuration.findRemoteRepositoryById(id);
-                    if (cfg != null) {
-                        configuration.removeRemoteRepository(cfg);
-                        configuration.addRemoteRepository(oldCfg);
+                else
+                {
+                    remoteRepositories.remove( id );
+                }
+                if ( oldCfg != null )
+                {
+                    RemoteRepositoryConfiguration cfg = configuration.findRemoteRepositoryById( id );
+                    if ( cfg != null )
+                    {
+                        configuration.removeRemoteRepository( cfg );
+                        configuration.addRemoteRepository( oldCfg );
                     }
                 }
-                log.error("Error while adding remote repository {}", e.getMessage(), e);
-                throw new RepositoryException("Could not save the configuration" + (e.getMessage() == null ? "" : ": " + e.getMessage()));
+                log.error( "Error while adding remote repository {}", e.getMessage( ), e );
+                throw new RepositoryException( "Could not save the configuration" + ( e.getMessage( ) == null ? "" : ": " + e.getMessage( ) ) );
             }
-        } finally {
-            rwLock.writeLock().unlock();
+        }
+        finally
+        {
+            rwLock.writeLock( ).unlock( );
         }
     }
 
@@ -772,20 +972,27 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa
      * @throws RepositoryException if an error occurs during configuration save
      */
     @Override
-    public RemoteRepository putRepository( RemoteRepository remoteRepository ) throws RepositoryException {
-        rwLock.writeLock().lock();
-        try {
-            Configuration configuration = configurationHandler.getBaseConfiguration();
-            try {
-                RemoteRepository repo = putRepository(remoteRepository, configuration);
-                saveConfiguration(configuration);
+    public RemoteRepository putRepository( RemoteRepository remoteRepository ) throws RepositoryException
+    {
+        rwLock.writeLock( ).lock( );
+        try
+        {
+            Configuration configuration = configurationHandler.getBaseConfiguration( );
+            try
+            {
+                RemoteRepository repo = putRepository( remoteRepository, configuration );
+                saveConfiguration( configuration );
                 return repo;
-            } catch (RegistryException | IndeterminateConfigurationException e) {
-                log.error("Error while saving remote repository {}", e.getMessage(), e);
-                throw new RepositoryException("Could not save the configuration" + (e.getMessage() == null ? "" : ": " + e.getMessage()));
             }
-        } finally {
-            rwLock.writeLock().unlock();
+            catch ( RegistryException | IndeterminateConfigurationException e )
+            {
+                log.error( "Error while saving remote repository {}", e.getMessage( ), e );
+                throw new RepositoryException( "Could not save the configuration" + ( e.getMessage( ) == null ? "" : ": " + e.getMessage( ) ) );
+            }
+        }
+        finally
+        {
+            rwLock.writeLock( ).unlock( );
         }
     }
 
@@ -798,27 +1005,35 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa
      * @throws RepositoryException if an error occurs, or the configuration is not valid.
      */
     @Override
-    public RemoteRepository putRepository( RemoteRepositoryConfiguration remoteRepositoryConfiguration ) throws RepositoryException {
-        rwLock.writeLock().lock();
-        try {
-            final String id = remoteRepositoryConfiguration.getId();
-            final RepositoryType repositoryType = RepositoryType.valueOf(remoteRepositoryConfiguration.getType());
-            Configuration configuration = configurationHandler.getBaseConfiguration();
-            RemoteRepository repo = remoteRepositories.get(id);
-            RemoteRepositoryConfiguration oldCfg = repo != null ? getProvider(repositoryType).getRemoteConfiguration(repo) : null;
-            repo = putRepository(remoteRepositoryConfiguration, configuration);
-            try {
-                saveConfiguration(configuration);
-            } catch (IndeterminateConfigurationException | RegistryException e) {
-                if (oldCfg != null) {
-                    getProvider(repositoryType).updateRemoteInstance((EditableRemoteRepository) repo, oldCfg);
+    public RemoteRepository putRepository( RemoteRepositoryConfiguration remoteRepositoryConfiguration ) throws RepositoryException
+    {
+        rwLock.writeLock( ).lock( );
+        try
+        {
+            final String id = remoteRepositoryConfiguration.getId( );
+            final RepositoryType repositoryType = RepositoryType.valueOf( remoteRepositoryConfiguration.getType( ) );
+            Configuration configuration = configurationHandler.getBaseConfiguration( );
+            RemoteRepository repo = remoteRepositories.get( id );
+            RemoteRepositoryConfiguration oldCfg = repo != null ? getProvider( repositoryType ).getRemoteConfiguration( repo ) : null;
+            repo = putRepository( remoteRepositoryConfiguration, configuration );
+            try
+            {
+                saveConfiguration( configuration );
+            }
+            catch ( IndeterminateConfigurationException | RegistryException e )
+            {
+                if ( oldCfg != null )
+                {
+                    getProvider( repositoryType ).updateRemoteInstance( (EditableRemoteRepository) repo, oldCfg );
                 }
-                log.error("Could not save the configuration for repository {}: {}", id, e.getMessage(), e);
-                throw new RepositoryException("Could not save the configuration for repository " + id + ": " + e.getMessage());
+                log.error( "Could not save the configuration for repository {}: {}", id, e.getMessage( ), e );
+                throw new RepositoryException( "Could not save the configuration for repository " + id + ": " + e.getMessage( ) );
             }
             return repo;
-        } finally {
-            rwLock.writeLock().unlock();
+        }
+        finally
+        {
+            rwLock.writeLock( ).unlock( );
         }
 
     }
@@ -833,63 +1048,87 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa
      * @throws RepositoryException if the configuration cannot be saved or updated
      */
     @Override
-    @SuppressWarnings("unchecked")
-    public RemoteRepository putRepository( RemoteRepositoryConfiguration remoteRepositoryConfiguration, Configuration configuration ) throws RepositoryException {
-        rwLock.writeLock().lock();
-        try {
-            final String id = remoteRepositoryConfiguration.getId();
-            final RepositoryType repoType = RepositoryType.valueOf(remoteRepositoryConfiguration.getType());
+    @SuppressWarnings( "unchecked" )
+    public RemoteRepository putRepository( RemoteRepositoryConfiguration remoteRepositoryConfiguration, Configuration configuration ) throws RepositoryException
+    {
+        rwLock.writeLock( ).lock( );
+        try
+        {
+            final String id = remoteRepositoryConfiguration.getId( );
+            final RepositoryType repoType = RepositoryType.valueOf( remoteRepositoryConfiguration.getType( ) );
             RemoteRepository repo;
             boolean registeredNew = false;
-            repo = remoteRepositories.get(id);
-            if (repo != null && repo.isOpen()) {
-                if (repo instanceof EditableRemoteRepository) {
-                    getProvider(repoType).updateRemoteInstance((EditableRemoteRepository) repo, remoteRepositoryConfiguration);
-                } else {
-                    throw new RepositoryException("The repository is not editable " + id);
+            repo = remoteRepositories.get( id );
+            if ( repo != null && repo.isOpen( ) )
+            {
+                if ( repo instanceof EditableRemoteRepository )
+                {
+                    getProvider( repoType ).updateRemoteInstance( (EditableRemoteRepository) repo, remoteRepositoryConfiguration );
                 }
-            } else {
-                repo = getProvider(repoType).createRemoteInstance(remoteRepositoryConfiguration);
-                remoteRepositories.put(id, repo);
+                else
+                {
+                    throw new RepositoryException( "The repository is not editable " + id );
+                }
+            }
+            else
+            {
+                repo = getProvider( repoType ).createRemoteInstance( remoteRepositoryConfiguration );
+                remoteRepositories.put( id, repo );
                 registeredNew = true;
             }
-            updateRepositoryReferences(getProvider(repoType), repo, remoteRepositoryConfiguration, configuration);
-            replaceOrAddRepositoryConfig(remoteRepositoryConfiguration, configuration);
-            if (registeredNew) {
-                pushEvent(new LifecycleEvent(LifecycleEvent.REGISTERED, this, repo));
-            } else {
-                pushEvent(new LifecycleEvent(LifecycleEvent.UPDATED, this, repo));
+            updateRepositoryReferences( getProvider( repoType ), repo, remoteRepositoryConfiguration, configuration );
+            replaceOrAddRepositoryConfig( remoteRepositoryConfiguration, configuration );
+            if ( registeredNew )
+            {
+                pushEvent( new LifecycleEvent( LifecycleEvent.REGISTERED, this, repo ) );
+            }
+            else
+            {
+                pushEvent( new LifecycleEvent( LifecycleEvent.UPDATED, this, repo ) );
             }
             return repo;
-        } finally {
-            rwLock.writeLock().unlock();
+        }
+        finally
+        {
+            rwLock.writeLock( ).unlock( );
         }
 
 
     }
 
     @Override
-    public void removeRepository( String repoId ) throws RepositoryException {
-        Repository repo = getRepository(repoId);
-        if (repo != null) {
-            removeRepository(repo);
+    public void removeRepository( String repoId ) throws RepositoryException
+    {
+        Repository repo = getRepository( repoId );
+        if ( repo != null )
+        {
+            removeRepository( repo );
         }
     }
 
     @Override
-    public void removeRepository( Repository repo ) throws RepositoryException {
-        if (repo == null) {
-            log.warn("Trying to remove null repository");
+    public void removeRepository( Repository repo ) throws RepositoryException
+    {
+        if ( repo == null )
+        {
+            log.warn( "Trying to remove null repository" );
             return;
         }
-        if (repo instanceof RemoteRepository) {
-            removeRepository((RemoteRepository) repo);
-        } else if (repo instanceof ManagedRepository) {
-            removeRepository((ManagedRepository) repo);
-        } else if (repo instanceof RepositoryGroup) {
-            removeRepositoryGroup((RepositoryGroup) repo);
-        } else {
-            throw new RepositoryException("Repository type not known: " + repo.getClass());
+        if ( repo instanceof RemoteRepository )
+        {
+            removeRepository( (RemoteRepository) repo );
+        }
+        else if ( repo instanceof ManagedRepository )
+        {
+            removeRepository( (ManagedRepository) repo );
+        }
+        else if ( repo instanceof RepositoryGroup )
+        {
+            removeRepositoryGroup( (RepositoryGroup) repo );
+        }
+        else
+        {
+            throw new RepositoryException( "Repository type not known: " + repo.getClass( ) );
         }
     }
 
@@ -901,61 +1140,79 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa
      * @throws RepositoryException if a error occurs during configuration save
      */
     @Override
-    public void removeRepository( ManagedRepository managedRepository ) throws RepositoryException {
-        if (managedRepository == null) {
+    public void removeRepository( ManagedRepository managedRepository ) throws RepositoryException
+    {
+        if ( managedRepository == null )
+        {
             return;
         }
-        final String id = managedRepository.getId();
-        ManagedRepository repo = getManagedRepository(id);
-        if (repo != null) {
-            rwLock.writeLock().lock();
-            try {
-                repo = managedRepositories.remove(id);
-                if (repo != null) {
-                    repo.close();
-                    this.groupHandler.removeRepositoryFromGroups(repo);
-                    Configuration configuration = configurationHandler.getBaseConfiguration();
-                    ManagedRepositoryConfiguration cfg = configuration.findManagedRepositoryById(id);
-                    if (cfg != null) {
-                        configuration.removeManagedRepository(cfg);
+        final String id = managedRepository.getId( );
+        ManagedRepository repo = getManagedRepository( id );
+        if ( repo != null )
+        {
+            rwLock.writeLock( ).lock( );
+            try
+            {
+                repo = managedRepositories.remove( id );
+                if ( repo != null )
+                {
+                    repo.close( );
+                    this.groupHandler.removeRepositoryFromGroups( repo );
+                    Configuration configuration = configurationHandler.getBaseConfiguration( );
+                    ManagedRepositoryConfiguration cfg = configuration.findManagedRepositoryById( id );
+                    if ( cfg != null )
+                    {
+                        configuration.removeManagedRepository( cfg );
                     }
-                    saveConfiguration(configuration);
+                    saveConfiguration( configuration );
                 }
-                pushEvent(new LifecycleEvent(LifecycleEvent.UNREGISTERED, this, repo));
-            } catch (RegistryException | IndeterminateConfigurationException e) {
+                pushEvent( new LifecycleEvent( LifecycleEvent.UNREGISTERED, this, repo ) );
+            }
+            catch ( RegistryException | IndeterminateConfigurationException e )
+            {
                 // Rollback
-                log.error("Could not save config after repository removal: {}", e.getMessage(), e);
-                managedRepositories.put(repo.getId(), repo);
-                throw new RepositoryException("Could not save configuration after repository removal: " + e.getMessage());
-            } finally {
-                rwLock.writeLock().unlock();
+                log.error( "Could not save config after repository removal: {}", e.getMessage( ), e );
+                managedRepositories.put( repo.getId( ), repo );
+                throw new RepositoryException( "Could not save configuration after repository removal: " + e.getMessage( ) );
+            }
+            finally
+            {
+                rwLock.writeLock( ).unlock( );
             }
         }
     }
 
 
     @Override
-    public void removeRepository( ManagedRepository managedRepository, Configuration configuration ) throws RepositoryException {
-        if (managedRepository == null) {
+    public void removeRepository( ManagedRepository managedRepository, Configuration configuration ) throws RepositoryException
+    {
+        if ( managedRepository == null )
+        {
             return;
         }
-        final String id = managedRepository.getId();
-        ManagedRepository repo = getManagedRepository(id);
-        if (repo != null) {
-            rwLock.writeLock().lock();
-            try {
-                repo = managedRepositories.remove(id);
-                if (repo != null) {
-                    repo.close();
-                    this.groupHandler.removeRepositoryFromGroups(repo);
-                    ManagedRepositoryConfiguration cfg = configuration.findManagedRepositoryById(id);
-                    if (cfg != null) {
-                        configuration.removeManagedRepository(cfg);
+        final String id = managedRepository.getId( );
+        ManagedRepository repo = getManagedRepository( id );
+        if ( repo != null )
+        {
+            rwLock.writeLock( ).lock( );
+            try
+            {
+                repo = managedRepositories.remove( id );
+                if ( repo != null )
+                {
+                    repo.close( );
+                    this.groupHandler.removeRepositoryFromGroups( repo );
+                    ManagedRepositoryConfiguration cfg = configuration.findManagedRepositoryById( id );
+                    if ( cfg != null )
+                    {
+                        configuration.removeManagedRepository( cfg );
                     }
                 }
-                pushEvent(new LifecycleEvent(LifecycleEvent.UNREGISTERED, this, repo));
-            } finally {
-                rwLock.writeLock().unlock();
+                pushEvent( new LifecycleEvent( LifecycleEvent.UNREGISTERED, this, repo ) );
+            }
+            finally
+            {
+                rwLock.writeLock( ).unlock( );
             }
         }
 
@@ -970,47 +1227,63 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa
      * @throws RepositoryException if a error occurs during configuration save
      */
     @Override
-    public void removeRepositoryGroup( RepositoryGroup repositoryGroup ) throws RepositoryException {
-        if (repositoryGroup == null) {
+    public void removeRepositoryGroup( RepositoryGroup repositoryGroup ) throws RepositoryException
+    {
+        if ( repositoryGroup == null )
+        {
             return;
         }
-        final String id = repositoryGroup.getId();
-        if (groupHandler.hasRepositoryGroup( id )) {
-            rwLock.writeLock().lock();
-            try {
-            groupHandler.removeRepositoryGroup( id );
-            } finally {
-                rwLock.writeLock().unlock();
+        final String id = repositoryGroup.getId( );
+        if ( groupHandler.hasRepositoryGroup( id ) )
+        {
+            rwLock.writeLock( ).lock( );
+            try
+            {
+                groupHandler.removeRepositoryGroup( id );
+            }
+            finally
+            {
+                rwLock.writeLock( ).unlock( );
             }
         }
     }
 
     @Override
-    public void removeRepositoryGroup( RepositoryGroup repositoryGroup, Configuration configuration ) throws RepositoryException {
-        if (repositoryGroup == null) {
+    public void removeRepositoryGroup( RepositoryGroup repositoryGroup, Configuration configuration ) throws RepositoryException
+    {
+        if ( repositoryGroup == null )
+        {
             return;
         }
-        final String id = repositoryGroup.getId();
-        if (groupHandler.hasRepositoryGroup( id )) {
-            rwLock.writeLock().lock();
-            try {
+        final String id = repositoryGroup.getId( );
+        if ( groupHandler.hasRepositoryGroup( id ) )
+        {
+            rwLock.writeLock( ).lock( );
+            try
+            {
                 groupHandler.removeRepositoryGroup( id, configuration );
-            } finally {
-                rwLock.writeLock().unlock();
+            }
+            finally
+            {
+                rwLock.writeLock( ).unlock( );
             }
         }
     }
 
-    private void doRemoveRepo(RemoteRepository repo, Configuration configuration) {
-        repo.close();
-        RemoteRepositoryConfiguration cfg = configuration.findRemoteRepositoryById(repo.getId());
-        if (cfg != null) {
-            configuration.removeRemoteRepository(cfg);
+    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);
+        List<ProxyConnectorConfiguration> proxyConnectors = new ArrayList<>( configuration.getProxyConnectors( ) );
+        for ( ProxyConnectorConfiguration proxyConnector : proxyConnectors )
+        {
+            if ( StringUtils.equals( proxyConnector.getTargetRepoId( ), repo.getId( ) ) )
+            {
+                configuration.removeProxyConnector( proxyConnector );
             }
         }
     }
@@ -1023,50 +1296,66 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa
      * @throws RepositoryException if a error occurs during configuration save
      */
     @Override
-    public void removeRepository( RemoteRepository remoteRepository ) throws RepositoryException {
-        if (remoteRepository == null) {
+    public void removeRepository( RemoteRepository remoteRepository ) throws RepositoryException
+    {
+        if ( remoteRepository == null )
+        {
             return;
         }
-        final String id = remoteRepository.getId();
-        RemoteRepository repo = getRemoteRepository(id);
-        if (repo != null) {
-            rwLock.writeLock().lock();
-            try {
-                repo = remoteRepositories.remove(id);
-                if (repo != null) {
-                    Configuration configuration = configurationHandler.getBaseConfiguration();
-                    doRemoveRepo(repo, configuration);
-                    saveConfiguration(configuration);
+        final String id = remoteRepository.getId( );
+        RemoteRepository repo = getRemoteRepository( id );
+        if ( repo != null )
+        {
+            rwLock.writeLock( ).lock( );
+            try
+            {
+                repo = remoteRepositories.remove( id );
+                if ( repo != null )
+                {
+                    Configuration configuration = configurationHandler.getBaseConfiguration( );
+                    doRemoveRepo( repo, configuration );
+                    saveConfiguration( configuration );
                 }
-                pushEvent(new LifecycleEvent(LifecycleEvent.UNREGISTERED, this, repo));
-            } catch (RegistryException | IndeterminateConfigurationException e) {
+                pushEvent( new LifecycleEvent( LifecycleEvent.UNREGISTERED, this, repo ) );
+            }
+            catch ( RegistryException | IndeterminateConfigurationException e )
+            {
                 // Rollback
-                log.error("Could not save config after repository removal: {}", e.getMessage(), e);
-                remoteRepositories.put(repo.getId(), repo);
-                throw new RepositoryException("Could not save configuration after repository removal: " + e.getMessage());
-            } finally {
-                rwLock.writeLock().unlock();
+                log.error( "Could not save config after repository removal: {}", e.getMessage( ), e );
+                remoteRepositories.put( repo.getId( ), repo );
+                throw new RepositoryException( "Could not save configuration after repository removal: " + e.getMessage( ) );
+            }
+            finally
+            {
+                rwLock.writeLock( ).unlock( );
             }
         }
     }
 
     @Override
-    public void removeRepository( RemoteRepository remoteRepository, Configuration configuration ) throws RepositoryException {
-        if (remoteRepository == null) {
+    public void removeRepository( RemoteRepository remoteRepository, Configuration configuration ) throws RepositoryException
+    {
+        if ( remoteRepository == null )
+        {
             return;
         }
-        final String id = remoteRepository.getId();
-        RemoteRepository repo = getRemoteRepository(id);
-        if (repo != null) {
-            rwLock.writeLock().lock();
-            try {
-                repo = remoteRepositories.remove(id);
-                if (repo != null) {
-                    doRemoveRepo(repo, configuration);
+        final String id = remoteRepository.getId( );
+        RemoteRepository repo = getRemoteRepository( id );
+        if ( repo != null )
+        {
+            rwLock.writeLock( ).lock( );
+            try
+            {
+                repo = remoteRepositories.remove( id );
+                if ( repo != null )
+                {
+                    doRemoveRepo( repo, configuration );
                 }
-                pushEvent(new LifecycleEvent(LifecycleEvent.UNREGISTERED, this, repo));
-            } finally {
-                rwLock.writeLock().unlock();
+                pushEvent( new LifecycleEvent( LifecycleEvent.UNREGISTERED, this, repo ) );
+            }
+            finally
+            {
+                rwLock.writeLock( ).unlock( );
             }
         }
 
@@ -1076,8 +1365,9 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa
      * Reloads the registry from the configuration.
      */
     @Override
-    public void reload( ) {
-        initialize();
+    public void reload( )
+    {
+        initialize( );
     }
 
     /**
@@ -1087,11 +1377,13 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa
      * @throws IndexUpdateFailedException If the index could not be resetted.
      */
     @Override
-    public void resetIndexingContext( Repository repository ) throws IndexUpdateFailedException {
-        if (repository.hasIndex() && repository instanceof EditableRepository) {
+    public void resetIndexingContext( Repository repository ) throws IndexUpdateFailedException
+    {
+        if ( repository.hasIndex( ) && repository instanceof EditableRepository )
+        {
             EditableRepository eRepo = (EditableRepository) repository;
-            ArchivaIndexingContext newCtx = getIndexManager(repository.getType()).reset(repository.getIndexingContext());
-            eRepo.setIndexingContext(newCtx);
+            ArchivaIndexingContext newCtx = getIndexManager( repository.getType( ) ).reset( repository.getIndexingContext( ) );
+            eRepo.setIndexingContext( newCtx );
         }
     }
 
@@ -1104,26 +1396,34 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa
      * @return The cloned repository.
      */
     @Override
-    public ManagedRepository clone( ManagedRepository repo, String newId ) throws RepositoryException {
-        if (managedRepositories.containsKey(newId) || remoteRepositories.containsKey(newId)) {
-            throw new RepositoryException("The given id exists already " + newId);
-        }
-        RepositoryProvider provider = getProvider(repo.getType());
-        ManagedRepositoryConfiguration cfg = provider.getManagedConfiguration(repo);
-        cfg.setId(newId);
-        ManagedRepository cloned = provider.createManagedInstance(cfg);
-        cloned.registerEventHandler(RepositoryEvent.ANY, this);
+    public ManagedRepository clone( ManagedRepository repo, String newId ) throws RepositoryException
+    {
+        if ( managedRepositories.containsKey( newId ) || remoteRepositories.containsKey( newId ) )
+        {
+            throw new RepositoryException( "The given id exists already " + newId );
+        }
+        RepositoryProvider provider = getProvider( repo.getType( ) );
+        ManagedRepositoryConfiguration cfg = provider.getManagedConfiguration( repo );
+        cfg.setId( newId );
+        ManagedRepository cloned = provider.createManagedInstance( cfg );
+        cloned.registerEventHandler( RepositoryEvent.ANY, this );
         return cloned;
     }
 
     @Override
-    public <T extends Repository> Repository clone( T repo, String newId ) throws RepositoryException {
-        if (repo instanceof RemoteRepository) {
-            return this.clone((RemoteRepository) repo, newId);
-        } else if (repo instanceof ManagedRepository) {
-            return this.clone((ManagedRepository) repo, newId);
-        } else {
-            throw new RepositoryException("This repository class is not supported " + repo.getClass().getName());
+    public <T extends Repository> Repository clone( T repo, String newId ) throws RepositoryException
+    {
+        if ( repo instanceof RemoteRepository )
+        {
+            return this.clone( (RemoteRepository) repo, newId );
+        }
+        else if ( repo instanceof ManagedRepository )
+        {
+            return this.clone( (ManagedRepository) repo, newId );
+        }
+        else
+        {
+            throw new RepositoryException( "This repository class is not supported " + repo.getClass( ).getName( ) );
         }
     }
 
@@ -1135,101 +1435,130 @@ public class ArchivaRepositoryRegistry implements ConfigurationListener, EventHa
      * @return The cloned repository.
      */
     @Override
-    public RemoteRepository clone( RemoteRepository repo, String newId ) throws RepositoryException {
-        if (managedRepositories.containsKey(newId) || remoteRepositories.containsKey(newId)) {
-            throw new RepositoryException("The given id exists already " + newId);
-        }
-        RepositoryProvider provider = getProvider(repo.getType());
-        RemoteRepositoryConfiguration cfg = provider.getRemoteConfiguration(repo);
-        cfg.setId(newId);
-        RemoteRepository cloned = provider.createRemoteInstance(cfg);
-        cloned.registerEventHandler(RepositoryEvent.ANY, this);
+    public RemoteRepository clone( RemoteRepository repo, String newId ) throws RepositoryException
+    {
+        if ( managedRepositories.containsKey( newId ) || remoteRepositories.containsKey( newId ) )
+        {
+            throw new RepositoryException( "The given id exists already " + newId );
+        }
+        RepositoryProvider provider = getProvider( repo.getType( ) );
+        RemoteRepositoryConfiguration cfg = provider.getRemoteConfiguration( repo );
+        cfg.setId( newId );
+        RemoteRepository cloned = provider.createRemoteInstance( cfg );
+        cloned.registerEventHandler( RepositoryEvent.ANY, this );
         return cloned;
     }
 
     @Override
     public Repository getRepositoryOfAsset( StorageAsset asset )
     {
-        if (asset instanceof Repository) {
-            return (Repository)asset;
-        } else
+        if ( asset instanceof Repository )
+        {
+            return (Repository) asset;
+        }
+        else
         {
-            return getRepositories( ).stream( ).filter( r -> r.getRoot()
+            return getRepositories( ).stream( ).filter( r -> r.getRoot( )
                 .getStorage( ).equals( asset.getStorage( ) ) ).findFirst( ).orElse( null );
         }
     }
 
 
     @Override
-    public void configurationEvent(ConfigurationEvent event) {
+    public void configurationEvent( ConfigurationEvent event )
+    {
         // We ignore the event, if the save was triggered by ourself
         if ( !ConfigurationHandler.REGISTRY_EVENT_TAG.equals( event.getTag( ) ) )
         {
-            reload();
+            reload( );
         }
     }
 
 
     @Override
-    public <T extends Event> void registerEventHandler( EventType<T> type, EventHandler<? super T> eventHandler) {
-        eventManager.registerEventHandler(type, eventHandler);
+    public <T extends Event> void registerEventHandler( EventType<T> type, EventHandler<? super T> eventHandler )
+    {
+        eventManager.registerEventHandler( type, eventHandler );
     }
 
 
     @Override
-    public <T extends Event> void unregisterEventHandler(EventType<T> type, EventHandler<? super T> eventHandler) {
-        eventManager.unregisterEventHandler(type, eventHandler);
+    public <T extends Event> void unregisterEventHandler( EventType<T> type, EventHandler<? super T> eventHandler )
+    {
+        eventManager.unregisterEventHandler( type, eventHandler );
     }
 
 
     @Override
-    public void handle(Event event) {
+    public void handle( Event event )
+    {
         // To avoid event cycles:
-        if (sameOriginator(event)) {
+        if ( sameOriginator( event ) )
+        {
             return;
         }
-        if (event instanceof RepositoryIndexEvent) {
-            handleIndexCreationEvent((RepositoryIndexEvent) event);
+        if ( event instanceof RepositoryIndexEvent )
+        {
+            handleIndexCreationEvent( (RepositoryIndexEvent) event );
         }
         // We propagate all events to our listeners, but with context of repository registry
-        pushEvent(event);
+        pushEvent( event );
     }
 
-    private void handleIndexCreationEvent(RepositoryIndexEvent event) {
+    private void handleIndexCreationEvent( RepositoryIndexEvent event )
+    {
         RepositoryIndexEvent idxEvent = event;
-        if (managedRepositories.containsKey(idxEvent.getRepository().getId()) ||
-                remoteRepositories.containsKey(idxEvent.getRepository().getId())) {
-            EditableRepository repo = (EditableRepository) idxEvent.getRepository();
-            if (repo != null && repo.getIndexingContext() != null) {
-                try {
-                    ArchivaIndexManager idxmgr = getIndexManager(repo.getType());
-                    if (idxmgr != null) {
-                        ArchivaIndexingContext newCtx = idxmgr.move(repo.getIndexingContext(), repo);
-                        repo.setIndexingContext(newCtx);
-                        idxmgr.updateLocalIndexPath(repo);
-                    }
+        EditableRepository repo = (EditableRepository) idxEvent.getRepository( );
+        if ( repo != null ) {
+            ArchivaIndexManager idxmgr = getIndexManager( repo.getType( ) );
+            if ( repo.getIndexingContext( ) != null )
+            {
+                try
+                {
+                        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);
+                }
+                catch ( IndexCreationFailedException e )
+                {
+                    log.error( "Could not move index to new directory: '{}'", e.getMessage( ), e );
+                }
+            } else {
+                try
+                {
+                    ArchivaIndexingContext context = idxmgr.createContext( repo );
+                    repo.setIndexingContext( context );
+                    idxmgr.updateLocalIndexPath( repo );
+                }
+                catch ( IndexCreationFailedException e )
+                {
+                    log.error( "Could not create index:  '{}'", e.getMessage( ), e );
                 }
             }
         }
     }
 
-    private boolean sameOriginator(Event event) {
-        if (event.getSource() == this) {
+    private boolean sameOriginator( Event event )
+    {
+        if ( event.getSource( ) == this )
+        {
             return true;
-        } else if (event.hasPreviousEvent()) {
-            return sameOriginator(event.getPreviousEvent());
-        } else {
+        }
+        else if ( event.hasPreviousEvent( ) )
+        {
+            return sameOriginator( event.getPreviousEvent( ) );
+        }
+        else
+        {
             return false;
         }
     }
 
-    private void pushEvent(Event event) {
-        eventManager.fireEvent(event);
+    private void pushEvent( Event event )
+    {
+        eventManager.fireEvent( event );
     }
 
 
-
 }
index f4b02a2d650ded27248ecb5228280d0538a5c043..9a08e76ae1d25e053595e95d5271bf8fb37519aa 100644 (file)
@@ -30,6 +30,8 @@ import org.apache.archiva.repository.RepositoryGroup;
 import org.apache.archiva.repository.RepositoryProvider;
 import org.apache.archiva.repository.RepositoryType;
 import org.apache.archiva.repository.event.RepositoryEvent;
+import org.apache.archiva.repository.features.IndexCreationFeature;
+import org.apache.archiva.repository.storage.StorageAsset;
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -38,6 +40,9 @@ import org.springframework.stereotype.Service;
 import javax.annotation.PostConstruct;
 import javax.annotation.PreDestroy;
 import javax.inject.Named;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
@@ -65,6 +70,8 @@ public class RepositoryGroupHandler
 
     private Map<String, RepositoryGroup> repositoryGroups = new HashMap<>();
 
+    private Path groupsDirectory;
+
     /**
      * Creates a new instance. All dependencies are injected on the constructor.
      * @param repositoryRegistry the registry. To avoid circular dependencies via DI, this class registers itself on the registry.
@@ -84,14 +91,67 @@ public class RepositoryGroupHandler
         log.debug( "Initializing repository group handler " + repositoryRegistry.toString( ) );
         // We are registering this class on the registry. This is necessary to avoid circular dependencies via injection.
         this.repositoryRegistry.registerGroupHandler( this );
+        initializeStorage();
     }
 
     public void initializeFromConfig() {
         this.repositoryGroups.clear();
-        this.repositoryGroups.putAll( getRepositorGroupsFromConfig( ) );
+        this.repositoryGroups.putAll( getRepositoryGroupsFromConfig( ) );
+        for (RepositoryGroup group : this.repositoryGroups.values()) {
+            initializeGroup( group );
+        }
+    }
+
+    private void initializeStorage() {
+        Path baseDir = this.configurationHandler.getArchivaConfiguration( ).getRepositoryGroupBaseDir( );
+        if (!Files.exists( baseDir) ) {
+            try
+            {
+                Files.createDirectories( baseDir );
+            }
+            catch ( IOException e )
+            {
+                log.error( "Could not create group base directory: {}", e.getMessage( ), e );
+            }
+        }
+        this.groupsDirectory = baseDir;
+    }
+
+    private void initializeGroup(RepositoryGroup repositoryGroup) {
+        StorageAsset indexDirectoy = getMergedIndexDirectory( repositoryGroup );
+        if (!indexDirectoy.exists()) {
+            try
+            {
+                indexDirectoy.create( );
+            }
+            catch ( IOException e )
+            {
+                log.error( "Could not create index directory {} for group {}: {}", indexDirectoy, repositoryGroup.getId( ), e.getMessage( ) );
+            }
+        }
+        Path groupPath = groupsDirectory.resolve(repositoryGroup.getId() );
+        if ( !Files.exists(groupPath) )
+        {
+            try {
+                Files.createDirectories(groupPath);
+            } catch (IOException e) {
+                log.error("Could not create repository group directory {}", groupPath);
+            }
+        }
+        mergedRemoteIndexesScheduler.schedule( repositoryGroup,
+            indexDirectoy);
+    }
+
+    public StorageAsset getMergedIndexDirectory( RepositoryGroup group )
+    {
+        if (group!=null) {
+            return group.getFeature( IndexCreationFeature.class).get().getLocalIndexPath();
+        } else {
+            return null;
+        }
     }
 
-    public Map<String, RepositoryGroup> getRepositorGroupsFromConfig() {
+    public Map<String, RepositoryGroup> getRepositoryGroupsFromConfig() {
         try {
             List<RepositoryGroupConfiguration> repositoryGroupConfigurations =
                 this.configurationHandler.getBaseConfiguration().getRepositoryGroups();
@@ -150,6 +210,7 @@ public class RepositoryGroupHandler
             RepositoryGroup originRepoGroup = repositoryGroups.put(id, repositoryGroup);
             try {
                 if (originRepoGroup != null && originRepoGroup != repositoryGroup) {
+                    this.mergedRemoteIndexesScheduler.unschedule( originRepoGroup );
                     originRepoGroup.close();
                 }
                 RepositoryProvider provider = repositoryRegistry.getProvider( repositoryGroup.getType());
@@ -162,6 +223,7 @@ public class RepositoryGroupHandler
                 }
                 configuration.addRepositoryGroup(newCfg);
                 repositoryRegistry.saveConfiguration(configuration);
+                initializeGroup( repositoryGroup );
                 return repositoryGroup;
             } catch (Exception e) {
                 // Rollback
@@ -187,19 +249,19 @@ public class RepositoryGroupHandler
             final String id = repositoryGroupConfiguration.getId();
             final RepositoryType repositoryType = RepositoryType.valueOf(repositoryGroupConfiguration.getType());
             Configuration configuration = this.configurationHandler.getBaseConfiguration();
-            RepositoryGroup repo = repositoryGroups.get(id);
-            RepositoryGroupConfiguration oldCfg = repo != null ? repositoryRegistry.getProvider(repositoryType).getRepositoryGroupConfiguration(repo) : null;
-            repo = putRepositoryGroup(repositoryGroupConfiguration, configuration);
+            RepositoryGroup repositoryGroup = repositoryGroups.get(id);
+            RepositoryGroupConfiguration oldCfg = repositoryGroup != null ? repositoryRegistry.getProvider(repositoryType).getRepositoryGroupConfiguration(repositoryGroup) : null;
+            repositoryGroup = putRepositoryGroup(repositoryGroupConfiguration, configuration);
             try {
                 repositoryRegistry.saveConfiguration(configuration);
             } catch ( IndeterminateConfigurationException | RegistryException e) {
                 if (oldCfg != null) {
-                    repositoryRegistry.getProvider(repositoryType).updateRepositoryGroupInstance((EditableRepositoryGroup) repo, oldCfg);
+                    repositoryRegistry.getProvider(repositoryType).updateRepositoryGroupInstance((EditableRepositoryGroup) repositoryGroup, oldCfg);
                 }
                 log.error("Could not save the configuration for repository group {}: {}", id, e.getMessage(), e);
                 throw new RepositoryException("Could not save the configuration for repository group " + id + ": " + e.getMessage());
             }
-            return repo;
+            return repositoryGroup;
     }
 
     public RepositoryGroup putRepositoryGroup( RepositoryGroupConfiguration repositoryGroupConfiguration, Configuration configuration ) throws RepositoryException {
@@ -209,6 +271,7 @@ public class RepositoryGroupHandler
             setRepositoryGroupDefaults(repositoryGroupConfiguration);
             if (repositoryGroups.containsKey(id)) {
                 repo = repositoryGroups.get(id);
+                this.mergedRemoteIndexesScheduler.unschedule( repo );
                 if (repo instanceof EditableRepositoryGroup) {
                     repositoryRegistry.getProvider(repoType).updateRepositoryGroupInstance((EditableRepositoryGroup) repo, repositoryGroupConfiguration);
                 } else {
@@ -220,6 +283,7 @@ public class RepositoryGroupHandler
             }
             updateRepositoryReferences(repositoryRegistry.getProvider(repoType), repo, repositoryGroupConfiguration);
             replaceOrAddRepositoryConfig(repositoryGroupConfiguration, configuration);
+            initializeGroup( repo );
             return repo;
     }
 
@@ -263,6 +327,7 @@ public class RepositoryGroupHandler
             try {
                 repo = repositoryGroups.remove(id);
                 if (repo != null) {
+                    this.mergedRemoteIndexesScheduler.unschedule( repo );
                     repo.close();
                     Configuration configuration = this.configurationHandler.getBaseConfiguration();
                     RepositoryGroupConfiguration cfg = configuration.findRepositoryGroupById(id);
@@ -286,6 +351,7 @@ public class RepositoryGroupHandler
         if (repo != null) {
                 repo = repositoryGroups.remove(id);
                 if (repo != null) {
+                    this.mergedRemoteIndexesScheduler.unschedule( repo );
                     repo.close();
                     RepositoryGroupConfiguration cfg = configuration.findRepositoryGroupById(id);
                     if (cfg != null) {
@@ -317,6 +383,7 @@ public class RepositoryGroupHandler
         for (RepositoryGroup group : repositoryGroups.values()) {
             try
             {
+                mergedRemoteIndexesScheduler.unschedule( group );
                 group.close( );
             } catch (Throwable e) {
                 log.error( "Could not close repository group {}: {}", group.getId( ), e.getMessage( ) );
index 8297251e520fe2b099845034b701653845c16f65..90dd593e643a3ddbd7881ba2452c94d015bcc85f 100644 (file)
@@ -22,8 +22,10 @@ package org.apache.archiva.repository.mock;
 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
 import org.apache.archiva.configuration.RepositoryGroupConfiguration;
+import org.apache.archiva.event.EventHandler;
 import org.apache.archiva.repository.*;
 import org.apache.archiva.event.Event;
+import org.apache.archiva.repository.event.RepositoryEvent;
 import org.apache.archiva.repository.features.ArtifactCleanupFeature;
 import org.apache.archiva.repository.features.IndexCreationFeature;
 import org.apache.archiva.repository.features.RemoteIndexFeature;
@@ -232,6 +234,12 @@ public class RepositoryProviderMock implements RepositoryProvider
         return null;
     }
 
+    @Override
+    public void addRepositoryEventHandler( EventHandler<? super RepositoryEvent> eventHandler )
+    {
+        // do nothing
+    }
+
 
     @Override
     public RemoteRepositoryConfiguration getRemoteConfiguration( RemoteRepository remoteRepository ) throws RepositoryException
index b1231844d6fabe839746e6816144367ccacb346d..234edcd51c9a9e10fcf344621d60dbfe3ccc77d9 100644 (file)
@@ -19,6 +19,7 @@ package org.apache.archiva.indexer.maven;
  * under the License.
  */
 
+import org.apache.archiva.common.utils.FileUtils;
 import org.apache.archiva.common.utils.PathUtil;
 import org.apache.archiva.configuration.ArchivaConfiguration;
 import org.apache.archiva.indexer.ArchivaIndexManager;
@@ -38,6 +39,7 @@ import org.apache.archiva.repository.RemoteRepository;
 import org.apache.archiva.repository.Repository;
 import org.apache.archiva.repository.RepositoryType;
 import org.apache.archiva.repository.UnsupportedRepositoryTypeException;
+import org.apache.archiva.repository.storage.AssetType;
 import org.apache.archiva.repository.storage.fs.FilesystemStorage;
 import org.apache.archiva.repository.storage.RepositoryStorage;
 import org.apache.archiva.repository.storage.StorageAsset;
@@ -499,7 +501,8 @@ public class MavenIndexManager implements ArchivaIndexManager {
                 StorageAsset newPath = getIndexPath(repo);
                 IndexingContext ctx = context.getBaseContext(IndexingContext.class);
                 Path oldPath = ctx.getIndexDirectoryFile().toPath();
-                if (oldPath.equals(newPath)) {
+                Path newFilePath = newPath.getFilePath( );
+                if (oldPath.equals(newFilePath)) {
                     // Nothing to do, if path does not change
                     return context;
                 }
@@ -510,7 +513,13 @@ public class MavenIndexManager implements ArchivaIndexManager {
                     return createContext(repo);
                 } else {
                     context.close(false);
-                    Files.move(oldPath, newPath.getFilePath());
+                    if (Files.exists( newFilePath )) {
+                        FileUtils.copyContent( oldPath, newFilePath );
+                        FileUtils.deleteDirectory( oldPath );
+                    } else
+                    {
+                        Files.move( oldPath, newFilePath );
+                    }
                     return createContext(repo);
                 }
             } catch (IOException e) {
@@ -629,7 +638,7 @@ public class MavenIndexManager implements ArchivaIndexManager {
 
         if ( !indexDir.exists() )
         {
-            indexDir = storage.addAsset(indexDir.getPath(), true);
+            indexDir.create( AssetType.CONTAINER );
         }
         return indexDir;
     }
@@ -758,11 +767,23 @@ public class MavenIndexManager implements ArchivaIndexManager {
     {
         if ( rif.getIndexUri( ) == null )
         {
-            return baseUri.resolve( DEFAULT_INDEX_PATH ).toString( );
+            return baseUri.resolve( "/"+DEFAULT_INDEX_PATH ).toString( );
         }
         else
         {
-            return baseUri.resolve( rif.getIndexUri( ) ).toString( );
+            URI rifUri = rif.getIndexUri( );
+            if (rifUri.isAbsolute()) {
+                return rifUri.toString( );
+            } else
+            {
+                if (baseUri.getScheme().toLowerCase().equals( "file" )) {
+                    return Paths.get( baseUri ).resolve( rifUri.getPath() ).toUri( ).toString( );
+                } else
+                {
+                    String pathString = rifUri.getPath( ).startsWith( "/" ) ? rifUri.getPath( ) : "/" + rifUri.getPath( );
+                    return baseUri.resolve( pathString ).toString( );
+                }
+            }
         }
     }
 
index ca2477f532c2b15a9fc33cb89a028ccf16630442..e30a0f2b3d4fa412f830a8f2822e7dd7713d78fa 100644 (file)
@@ -22,6 +22,7 @@ package org.apache.archiva.repository.mock;
 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
 import org.apache.archiva.configuration.RepositoryGroupConfiguration;
+import org.apache.archiva.event.EventHandler;
 import org.apache.archiva.repository.base.BasicManagedRepository;
 import org.apache.archiva.repository.base.BasicRemoteRepository;
 import org.apache.archiva.repository.EditableManagedRepository;
@@ -37,6 +38,7 @@ import org.apache.archiva.repository.RepositoryException;
 import org.apache.archiva.repository.RepositoryGroup;
 import org.apache.archiva.repository.RepositoryProvider;
 import org.apache.archiva.repository.RepositoryType;
+import org.apache.archiva.repository.event.RepositoryEvent;
 import org.apache.archiva.repository.features.ArtifactCleanupFeature;
 import org.apache.archiva.repository.features.IndexCreationFeature;
 import org.apache.archiva.repository.features.RemoteIndexFeature;
@@ -249,6 +251,12 @@ public class RepositoryProviderMock implements RepositoryProvider
         return null;
     }
 
+    @Override
+    public void addRepositoryEventHandler( EventHandler<? super RepositoryEvent> eventHandler )
+    {
+        // do nothing
+    }
+
 
     @Override
     public RemoteRepositoryConfiguration getRemoteConfiguration( RemoteRepository remoteRepository ) throws RepositoryException
index 02b0cadacb0007dac9214485ae201e213da18c8d..2afc249bb0474768fbfe6637d82c10bde41c248b 100644 (file)
@@ -26,6 +26,9 @@ import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
 import org.apache.archiva.configuration.RepositoryGroupConfiguration;
 import org.apache.archiva.event.Event;
+import org.apache.archiva.event.EventHandler;
+import org.apache.archiva.event.EventManager;
+import org.apache.archiva.event.EventType;
 import org.apache.archiva.repository.EditableManagedRepository;
 import org.apache.archiva.repository.EditableRemoteRepository;
 import org.apache.archiva.repository.EditableRepository;
@@ -33,6 +36,7 @@ import org.apache.archiva.repository.EditableRepositoryGroup;
 import org.apache.archiva.repository.ManagedRepository;
 import org.apache.archiva.repository.ReleaseScheme;
 import org.apache.archiva.repository.RemoteRepository;
+import org.apache.archiva.repository.Repository;
 import org.apache.archiva.repository.RepositoryCredentials;
 import org.apache.archiva.repository.RepositoryException;
 import org.apache.archiva.repository.RepositoryGroup;
@@ -41,6 +45,7 @@ import org.apache.archiva.repository.RepositoryType;
 import org.apache.archiva.repository.UnsupportedURIException;
 import org.apache.archiva.repository.base.BasicManagedRepository;
 import org.apache.archiva.repository.base.PasswordCredentials;
+import org.apache.archiva.repository.event.RepositoryEvent;
 import org.apache.archiva.repository.features.ArtifactCleanupFeature;
 import org.apache.archiva.repository.features.IndexCreationFeature;
 import org.apache.archiva.repository.features.RemoteIndexFeature;
@@ -61,7 +66,9 @@ import java.nio.file.Paths;
 import java.time.Duration;
 import java.time.Period;
 import java.time.temporal.ChronoUnit;
+import java.util.ArrayList;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Set;
 import java.util.stream.Collectors;
 
@@ -81,6 +88,29 @@ public class MavenRepositoryProvider implements RepositoryProvider {
     @Inject
     private FileLockManager fileLockManager;
 
+    private class EventHandlerInfo {
+        EventType<?> type;
+        EventHandler<?> handler;
+
+        public EventHandlerInfo( EventType<?> type, EventHandler<?> handler )
+        {
+            this.type = type;
+            this.handler = handler;
+        }
+
+        public EventType<?> getType( )
+        {
+            return type;
+        }
+
+        public EventHandler<?> getHandler( )
+        {
+            return handler;
+        }
+    }
+
+    private List<EventHandler<? super RepositoryEvent>> repositoryEventHandlers = new ArrayList<>( );
+
     private static final Logger log = LoggerFactory.getLogger(MavenRepositoryProvider.class);
 
     static final Set<RepositoryType> TYPES = new HashSet<>();
@@ -100,14 +130,16 @@ public class MavenRepositoryProvider implements RepositoryProvider {
     }
 
     public MavenManagedRepository createManagedInstance(String id, String name, Path baseDir) {
-        FilesystemStorage storage = null;
+        FilesystemStorage storage;
         try {
             storage = new FilesystemStorage(baseDir.resolve(id), fileLockManager);
         } catch (IOException e) {
             log.error("Could not initialize fileystem for repository {}", id);
             throw new RuntimeException(e);
         }
-        return new MavenManagedRepository(id, name, storage);
+        MavenManagedRepository repo = new MavenManagedRepository( id, name, storage );
+        registerEventHandler( repo );
+        return repo;
     }
 
     @Override
@@ -116,14 +148,16 @@ public class MavenRepositoryProvider implements RepositoryProvider {
     }
 
     public MavenRemoteRepository createRemoteInstance(String id, String name, Path baseDir) {
-        FilesystemStorage storage = null;
+        FilesystemStorage storage;
         try {
             storage = new FilesystemStorage(baseDir.resolve(id), fileLockManager);
         } catch (IOException e) {
             log.error("Could not initialize fileystem for repository {}", id);
             throw new RuntimeException(e);
         }
-        return new MavenRemoteRepository(id, name, storage);
+        MavenRemoteRepository repo = new MavenRemoteRepository( id, name, storage );
+        registerEventHandler( repo );
+        return repo;
     }
 
     @Override
@@ -132,14 +166,22 @@ public class MavenRepositoryProvider implements RepositoryProvider {
     }
 
     public MavenRepositoryGroup createRepositoryGroup(String id, String name, Path baseDir) {
-        FilesystemStorage storage = null;
+        FilesystemStorage storage;
         try {
             storage = new FilesystemStorage(baseDir.resolve(id), fileLockManager);
         } catch (IOException e) {
             log.error("Could not initialize fileystem for repository {}", id);
             throw new RuntimeException(e);
         }
-        return new MavenRepositoryGroup(id, name, storage);
+        MavenRepositoryGroup group = new MavenRepositoryGroup( id, name, storage );
+        registerEventHandler( group );
+        return group;
+    }
+
+    private void registerEventHandler( Repository repo ) {
+        for (EventHandler<? super RepositoryEvent> eventHandler : repositoryEventHandlers) {
+            repo.registerEventHandler( RepositoryEvent.ANY, eventHandler );
+        }
     }
 
     private URI getURIFromString(String uriStr) throws RepositoryException {
@@ -166,7 +208,7 @@ public class MavenRepositoryProvider implements RepositoryProvider {
             try {
                 uri = new URI(newCfg);
             } catch (URISyntaxException e1) {
-                log.error("Could not create URI from {} -> ", uriStr, newCfg);
+                log.error("Could not create URI from {} -> {}", uriStr, newCfg);
                 throw new RepositoryException("The config entry " + uriStr + " cannot be converted to URI.");
             }
         }
@@ -421,16 +463,8 @@ public class MavenRepositoryProvider implements RepositoryProvider {
         cfg.setRetentionPeriod(artifactCleanupFeature.getRetentionPeriod().getDays());
         cfg.setDeleteReleasedSnapshots(artifactCleanupFeature.isDeleteReleasedSnapshots());
 
-        if (managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE)) {
-            cfg.setReleases(true);
-        } else {
-            cfg.setReleases(false);
-        }
-        if (managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT)) {
-            cfg.setSnapshots(true);
-        } else {
-            cfg.setSnapshots(false);
-        }
+        cfg.setReleases( managedRepository.getActiveReleaseSchemes( ).contains( ReleaseScheme.RELEASE ) );
+        cfg.setSnapshots( managedRepository.getActiveReleaseSchemes( ).contains( ReleaseScheme.SNAPSHOT ) );
         return cfg;
 
     }
@@ -450,11 +484,17 @@ public class MavenRepositoryProvider implements RepositoryProvider {
             cfg.setMergedIndexPath( indexCreationFeature.getIndexPath().toString() );
         }
         cfg.setMergedIndexTtl(repositoryGroup.getMergedIndexTTL());
-        cfg.setRepositories(repositoryGroup.getRepositories().stream().map(r -> r.getId()).collect(Collectors.toList()));
+        cfg.setRepositories(repositoryGroup.getRepositories().stream().map( Repository::getId ).collect(Collectors.toList()));
         cfg.setCronExpression(repositoryGroup.getSchedulingDefinition());
         return cfg;
     }
 
+    @Override
+    public void addRepositoryEventHandler( EventHandler<? super RepositoryEvent> eventHandler )
+    {
+        this.repositoryEventHandlers.add( eventHandler );
+    }
+
     private ManagedRepositoryConfiguration getStageRepoConfig(ManagedRepositoryConfiguration repository) {
         ManagedRepositoryConfiguration stagingRepository = new ManagedRepositoryConfiguration();
         stagingRepository.setId(repository.getId() + StagingRepositoryFeature.STAGING_REPO_POSTFIX);
@@ -470,7 +510,7 @@ public class MavenRepositoryProvider implements RepositoryProvider {
         stagingRepository.setLocation(path.substring(0, lastIndex) + "/" + stagingRepository.getId());
 
         if (StringUtils.isNotBlank(repository.getIndexDir())) {
-            Path indexDir = null;
+            Path indexDir;
             try {
                 indexDir = Paths.get(new URI(repository.getIndexDir().startsWith("file://") ? repository.getIndexDir() : "file://" + repository.getIndexDir()));
                 if (indexDir.isAbsolute()) {
@@ -488,7 +528,7 @@ public class MavenRepositoryProvider implements RepositoryProvider {
             // in case of absolute dir do not use the same
         }
         if (StringUtils.isNotBlank(repository.getPackedIndexDir())) {
-            Path packedIndexDir = null;
+            Path packedIndexDir;
             packedIndexDir = Paths.get(repository.getPackedIndexDir());
             if (packedIndexDir.isAbsolute()) {
                 Path newDir = packedIndexDir.getParent().resolve(packedIndexDir.getFileName() + StagingRepositoryFeature.STAGING_REPO_POSTFIX);
index ae71891848d9ca1f1ef374198c045fd679234316..8a4ad112e9a2fa0dc3b0590702e64659d0cf9bd6 100644 (file)
@@ -109,7 +109,7 @@ public class DownloadRemoteIndexTaskTest
         server = new Server( );
         serverConnector = new ServerConnector( server, new HttpConnectionFactory());
         server.addConnector( serverConnector );
-        createContext( server, Paths.get( "src/test/" ) );
+        createContext( server, Paths.get( "src/test" ) );
         this.server.start();
         this.port = serverConnector.getLocalPort();
         log.info( "start server on port {}", this.port );
@@ -185,13 +185,13 @@ public class DownloadRemoteIndexTaskTest
         Files.createDirectories( indexDirectory );
         remoteRepository.setLocation( new URI( "http://localhost:" + port ) );
         repoDirectory.toFile().deleteOnExit();
-        createIndexingContext( remoteRepository );
-
         RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get();
         rif.setDownloadRemoteIndex( true );
         rif.setIndexUri( new URI("http://localhost:" + port + "/index-updates/" ) );
-        IndexCreationFeature icf = remoteRepository.getFeature( IndexCreationFeature.class ).get( );
-        icf.setLocalIndexPath( remoteRepository.getAsset(  "index" ) );
+        createIndexingContext( remoteRepository );
+
+        // IndexCreationFeature icf = remoteRepository.getFeature( IndexCreationFeature.class ).get( );
+        // icf.setLocalIndexPath( remoteRepository.getAsset(  "index" ) );
         return remoteRepository;
     }
 
index a453021a410c63174fd5248516c285b236561fa2..29e51f833979ecb13c0148e4a5dfeb65fa6bfcf3 100644 (file)
@@ -67,7 +67,7 @@ public class RepositoryServletBrowseTest
 
         // dumpResponse( response );
 
-        List<String> expectedLinks = Arrays.asList( ".indexer/", "commons-lang/", "net/", "org/" );
+        List<String> expectedLinks = Arrays.asList( ".index/", ".indexer/", "commons-lang/", "net/", "org/" );
 
         Document document = Jsoup.parse( response.getContentAsString() );
         Elements elements = document.getElementsByTag( "a" );