From 54148d4fc22487389bf39c52eaff821aed8c994c Mon Sep 17 00:00:00 2001 From: Martin Stockhammer Date: Tue, 1 Oct 2019 23:11:54 +0200 Subject: [PATCH] Refactoring event API --- .../archiva/proxy/ArchivaProxyRegistry.java | 18 ++-- .../apache/archiva/repository/Repository.java | 4 +- .../archiva/repository/events/Event.java | 21 +++-- .../archiva/repository/events/EventType.java | 43 ++++++++- .../repository/events/IndexCreationEvent.java | 19 ++-- .../repository/events/LifecycleEvent.java | 11 ++- .../repository/events/RepositoryEvent.java | 11 ++- .../events/RepositoryEventListener.java | 8 +- ...andler.java => RepositoryEventSource.java} | 10 +- .../events/RepositoryRegistryEvent.java | 10 +- .../events/RepositoryValueEvent.java | 6 +- .../repository/AbstractRepository.java | 47 ++++------ .../repository/RepositoryRegistry.java | 91 ++++++++----------- 13 files changed, 160 insertions(+), 139 deletions(-) rename archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/events/{RepositoryEventHandler.java => RepositoryEventSource.java} (77%) diff --git a/archiva-modules/archiva-base/archiva-proxy/src/main/java/org/apache/archiva/proxy/ArchivaProxyRegistry.java b/archiva-modules/archiva-base/archiva-proxy/src/main/java/org/apache/archiva/proxy/ArchivaProxyRegistry.java index b59091a4c..4fb20b7af 100644 --- a/archiva-modules/archiva-base/archiva-proxy/src/main/java/org/apache/archiva/proxy/ArchivaProxyRegistry.java +++ b/archiva-modules/archiva-base/archiva-proxy/src/main/java/org/apache/archiva/proxy/ArchivaProxyRegistry.java @@ -19,15 +19,19 @@ package org.apache.archiva.proxy; * under the License. */ -import org.apache.archiva.configuration.*; +import org.apache.archiva.configuration.ArchivaConfiguration; +import org.apache.archiva.configuration.NetworkProxyConfiguration; +import org.apache.archiva.configuration.ProxyConnectorConfiguration; import org.apache.archiva.policies.Policy; import org.apache.archiva.policies.PolicyOption; import org.apache.archiva.policies.PolicyUtil; import org.apache.archiva.proxy.model.NetworkProxy; import org.apache.archiva.proxy.model.ProxyConnector; import org.apache.archiva.proxy.model.RepositoryProxyHandler; -import org.apache.archiva.repository.*; -import org.apache.archiva.repository.events.Event; +import org.apache.archiva.repository.ManagedRepository; +import org.apache.archiva.repository.RemoteRepository; +import org.apache.archiva.repository.RepositoryRegistry; +import org.apache.archiva.repository.RepositoryType; import org.apache.archiva.repository.events.RepositoryEventListener; import org.apache.archiva.repository.events.RepositoryRegistryEvent; import org.slf4j.Logger; @@ -47,7 +51,7 @@ import java.util.stream.Collectors; */ @SuppressWarnings( "SpringJavaInjectionPointsAutowiringInspection" ) @Service("proxyRegistry#default") -public class ArchivaProxyRegistry implements ProxyRegistry, RepositoryEventListener { +public class ArchivaProxyRegistry implements ProxyRegistry, RepositoryEventListener { private static final Logger log = LoggerFactory.getLogger(ArchivaProxyRegistry.class); @@ -80,7 +84,7 @@ public class ArchivaProxyRegistry implements ProxyRegistry, RepositoryEventListe updateHandler(); updateConnectors(); updateNetworkProxies(); - repositoryRegistry.register(this, RepositoryRegistryEvent.RegistryEventType.RELOADED); + repositoryRegistry.register(RepositoryRegistryEvent.RELOADED, this); } private ArchivaConfiguration getArchivaConfiguration() { @@ -213,9 +217,9 @@ public class ArchivaProxyRegistry implements ProxyRegistry, RepositoryEventListe } @Override - public void raise(Event event) { + public void raise(RepositoryRegistryEvent event) { log.debug("Reload happened, updating proxy list"); - if (event.getType()== RepositoryRegistryEvent.RegistryEventType.RELOADED) { + if (event.getType()== RepositoryRegistryEvent.RELOADED) { init(); } } diff --git a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/Repository.java b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/Repository.java index b26627f10..96cb597e3 100644 --- a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/Repository.java +++ b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/Repository.java @@ -20,7 +20,7 @@ package org.apache.archiva.repository; */ import org.apache.archiva.indexer.ArchivaIndexingContext; -import org.apache.archiva.repository.events.RepositoryEventHandler; +import org.apache.archiva.repository.events.RepositoryEventSource; import org.apache.archiva.repository.storage.RepositoryStorage; import org.apache.archiva.repository.features.RepositoryFeature; import org.apache.archiva.repository.storage.StorageAsset; @@ -35,7 +35,7 @@ import java.util.Set; * * Created by Martin Stockhammer on 21.09.17. */ -public interface Repository extends RepositoryEventHandler, RepositoryStorage { +public interface Repository extends RepositoryEventSource, RepositoryStorage { /** * Return the identifier of the repository. Repository identifier should be unique at least diff --git a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/events/Event.java b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/events/Event.java index 7ba7f34b0..4b0120776 100644 --- a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/events/Event.java +++ b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/events/Event.java @@ -20,28 +20,33 @@ package org.apache.archiva.repository.events; */ import java.time.LocalDateTime; +import java.util.EventObject; -public class Event { +public class Event extends EventObject { + + public static final EventType ANY = new EventType(null, "ANY"); Event previous; - final O originator; - final EventType type; + final Object originator; + final EventType type; final LocalDateTime instant; - public Event(EventType type, OO originator) { + public Event(EventType type, Object originator) { + super(originator); this.originator = originator; this.type = type; this.instant = LocalDateTime.now(); } - private Event(Event previous, O originator) { + private Event(Event previous, Object originator) { + super(originator); this.previous = previous; this.originator = originator; this.type = previous.getType(); this.instant = previous.getInstant(); } - public EventType getType() { + public EventType getType() { return type; }; @@ -49,11 +54,11 @@ public class Event { return instant; } - public O getOriginator() { + public Object getOriginator() { return originator; } - public Event recreate(NO newOrigin) { + public Event recreate(Object newOrigin) { return new Event(this, newOrigin); } diff --git a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/events/EventType.java b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/events/EventType.java index 5004a1526..4162f9ac1 100644 --- a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/events/EventType.java +++ b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/events/EventType.java @@ -19,7 +19,46 @@ package org.apache.archiva.repository.events; * under the License. */ -public interface EventType { +import java.util.ArrayList; +import java.util.List; - String name(); +public class EventType { + + private final String name; + private final EventType superType; + + public EventType(EventType superType, String name) { + this.name = name; + this.superType = superType; + } + + public String name() { + return name; + } + + public EventType getSuperType() { + return superType; + } + + + public static List> fetchSuperTypes(EventType type) { + List> typeList = new ArrayList<>(); + EventType cType = type; + while (cType!=null) { + typeList.add(cType); + cType = cType.getSuperType(); + } + return typeList; + } + + public static boolean isInstanceOf(EventType type, EventType baseType) { + EventType cType = type; + while(cType!=null) { + if (cType == baseType) { + return true; + } + cType = cType.getSuperType(); + } + return false; + } } diff --git a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/events/IndexCreationEvent.java b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/events/IndexCreationEvent.java index 847b679a9..9a2183470 100644 --- a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/events/IndexCreationEvent.java +++ b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/events/IndexCreationEvent.java @@ -19,29 +19,26 @@ package org.apache.archiva.repository.events; * under the License. */ + import org.apache.archiva.repository.Repository; import java.net.URI; -public class IndexCreationEvent extends RepositoryValueEvent { +public class IndexCreationEvent extends RepositoryValueEvent { - public enum Index implements EventType { - INDEX_URI_CHANGE, PACKED_INDEX_URI_CHANGE - } + public static EventType ANY = new EventType<>(RepositoryValueEvent.ANY, "REPOSITORY.VALUE.INDEX"); + public static EventType INDEX_URI_CHANGED = new EventType<>(ANY, "REPOSITORY.VALUE.INDEX.URI_CHANGED"); + public static EventType PACKED_INDEX_URI_CHANGED = new EventType<>(ANY, "REPOSITORY.VALUE.INDEX.PACKED_URI_CHANGED"); - IndexCreationEvent(Repository repo, O origin, URI oldValue, URI value) { - super(Index.INDEX_URI_CHANGE, origin, repo, oldValue, value); - } - - IndexCreationEvent(Index type, O origin, Repository repo, URI oldValue, URI value) { + IndexCreationEvent(EventType type, Object origin, Repository repo, URI oldValue, URI value) { super(type, origin, repo, oldValue, value); } public static final IndexCreationEvent indexUriChange(O origin, Repository repo, URI oldValue, URI newValue) { - return new IndexCreationEvent(Index.INDEX_URI_CHANGE, origin, repo, oldValue, newValue); + return new IndexCreationEvent(INDEX_URI_CHANGED, origin, repo, oldValue, newValue); } public static final IndexCreationEvent packedIndexUriChange(O origin, Repository repo, URI oldValue, URI newValue) { - return new IndexCreationEvent(Index.PACKED_INDEX_URI_CHANGE, origin, repo, oldValue, newValue); + return new IndexCreationEvent(PACKED_INDEX_URI_CHANGED, origin, repo, oldValue, newValue); } } diff --git a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/events/LifecycleEvent.java b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/events/LifecycleEvent.java index 2c55dd842..9197b580a 100644 --- a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/events/LifecycleEvent.java +++ b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/events/LifecycleEvent.java @@ -21,13 +21,14 @@ package org.apache.archiva.repository.events; import org.apache.archiva.repository.Repository; -public class LifecycleEvent extends RepositoryEvent { +public class LifecycleEvent extends RepositoryEvent { - public enum LifecycleEventType implements EventType { - REGISTERED,UNREGISTERED,UPDATED - } + public static EventType ANY = new EventType<>(RepositoryEvent.ANY, "REPOSITORY.LIFECYCLE"); + public static EventType REGISTERED = new EventType<>(ANY, "REPOSITORY.LIFECYCLE.REGISTERED"); + public static EventType UNREGISTERED = new EventType<>(ANY, "REPOSITORY.LIFECYCLE.UNREGISTERED"); + public static EventType UPDATED = new EventType<>(ANY, "REPOSITORY.LIFECYCLE.UPDATED"); - public LifecycleEvent(LifecycleEventType type, OO origin, Repository repository) { + public LifecycleEvent(EventType type, Object origin, Repository repository) { super(type, origin, repository); } } diff --git a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/events/RepositoryEvent.java b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/events/RepositoryEvent.java index 7c97b8762..24ea42c31 100644 --- a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/events/RepositoryEvent.java +++ b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/events/RepositoryEvent.java @@ -21,11 +21,13 @@ package org.apache.archiva.repository.events; import org.apache.archiva.repository.Repository; -public class RepositoryEvent extends Event { +public class RepositoryEvent extends Event { + + public static final EventType ANY = new EventType<>(Event.ANY, "REPOSITORY.UPDATED"); private final Repository repository; - public RepositoryEvent(EventType type, OO origin, Repository repository) { + public RepositoryEvent(EventType type, Object origin, Repository repository) { super(type, origin); this.repository = repository; } @@ -33,4 +35,9 @@ public class RepositoryEvent extends Event { public Repository getRepository() { return repository; } + + @Override + public EventType getType() { + return (EventType) super.getType(); + } } diff --git a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/events/RepositoryEventListener.java b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/events/RepositoryEventListener.java index 1f0b2039d..ffedc4207 100644 --- a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/events/RepositoryEventListener.java +++ b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/events/RepositoryEventListener.java @@ -19,12 +19,10 @@ package org.apache.archiva.repository.events; * under the License. */ -import org.apache.archiva.repository.events.RepositoryValueEvent; - /** - * Listener that accepts repository events. + * A listener that accepts repository events. */ -public interface RepositoryEventListener { +public interface RepositoryEventListener { - void raise(Event event); + void raise(T event); } diff --git a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/events/RepositoryEventHandler.java b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/events/RepositoryEventSource.java similarity index 77% rename from archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/events/RepositoryEventHandler.java rename to archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/events/RepositoryEventSource.java index 123ffb289..f26eb782e 100644 --- a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/events/RepositoryEventHandler.java +++ b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/events/RepositoryEventSource.java @@ -26,15 +26,11 @@ import java.util.Set; * Listeners may register for all events that are raised or only to a subset of events. * */ -public interface RepositoryEventHandler { +public interface RepositoryEventSource { - void register(RepositoryEventListener listener); + void register(EventType type, RepositoryEventListener listener); - void register(RepositoryEventListener listener, EventType type); - - void register(RepositoryEventListener listener, Set types); - - void unregister(RepositoryEventListener listener); + void unregister(EventType type, RepositoryEventListener listener); void clearListeners(); } diff --git a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/events/RepositoryRegistryEvent.java b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/events/RepositoryRegistryEvent.java index 1a138cf4d..2b9ac7668 100644 --- a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/events/RepositoryRegistryEvent.java +++ b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/events/RepositoryRegistryEvent.java @@ -19,13 +19,13 @@ package org.apache.archiva.repository.events; * under the License. */ -public class RepositoryRegistryEvent extends Event { +public class RepositoryRegistryEvent extends Event { - public enum RegistryEventType implements EventType { - RELOADED,DESTROYED - } + public static EventType ANY = new EventType(Event.ANY, "REGISTRY"); + public static EventType RELOADED = new EventType(ANY, "REGISTRY.RELOADED"); + public static EventType DESTROYED = new EventType(ANY, "REGISTRY.DESTROYED"); - public RepositoryRegistryEvent(RegistryEventType type, OO origin) { + public RepositoryRegistryEvent(EventType type, Object origin) { super(type, origin); } } diff --git a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/events/RepositoryValueEvent.java b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/events/RepositoryValueEvent.java index 6081717e6..b5f8146a7 100644 --- a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/events/RepositoryValueEvent.java +++ b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/events/RepositoryValueEvent.java @@ -26,12 +26,14 @@ import org.apache.archiva.repository.Repository; * * @param */ -public class RepositoryValueEvent extends RepositoryEvent { +public class RepositoryValueEvent extends RepositoryEvent { + + public static final EventType> ANY = new EventType(RepositoryEvent.ANY, "REPOSITORY.VALUE.UPDATED"); final V value; final V oldValue; - public RepositoryValueEvent(EventType type, O origin, Repository repo, V oldValue, V value) { + public RepositoryValueEvent(EventType> type, Object origin, Repository repo, V oldValue, V value) { super(type, origin, repo); this.value = value; this.oldValue = oldValue; diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/AbstractRepository.java b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/AbstractRepository.java index 0f71880c8..5a5aa6c4c 100644 --- a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/AbstractRepository.java +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/AbstractRepository.java @@ -56,7 +56,7 @@ import java.util.function.Consumer; * No features are provided. Capabilities and features must be implemented by concrete classes. * */ -public abstract class AbstractRepository implements EditableRepository, RepositoryEventListener +public abstract class AbstractRepository implements EditableRepository, RepositoryEventListener { @@ -79,8 +79,8 @@ public abstract class AbstractRepository implements EditableRepository, Reposito String schedulingDefinition = "0 0 02 * * ?"; private String layout = "default"; public static final CronDefinition CRON_DEFINITION = CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ); - private List listeners = new ArrayList<>(); - private Map> listenerTypeMap = new HashMap<>(); + + private Map, List>> listenerTypeMap = new HashMap<>(); Map>, RepositoryFeature> featureMap = new HashMap<>( ); @@ -334,14 +334,16 @@ public abstract class AbstractRepository implements EditableRepository, Reposito } @Override - public void raise(Event event) { - callListeners(event, listeners); - if (listenerTypeMap.containsKey(event.getType())) { - callListeners(event, listenerTypeMap.get(event.getType())); + public void raise(RepositoryEvent event) { + final EventType currentType = event.getType(); + for (EventType type : listenerTypeMap.keySet()) { + if (EventType.isInstanceOf(currentType, type)) { + callListeners(event, listenerTypeMap.get(type)); + } } } - private void callListeners(Event event, List evtListeners) { + private void callListeners(RepositoryEvent event, List> evtListeners) { for(RepositoryEventListener listener : evtListeners) { try { listener.raise(event.recreate(this)); @@ -353,15 +355,14 @@ public abstract class AbstractRepository implements EditableRepository, Reposito } @Override - public void register(RepositoryEventListener listener) { - if (!this.listeners.contains(listener)) { - this.listeners.add(listener); + public void register(EventType eventType, RepositoryEventListener listener) { + if (!EventType.isInstanceOf(eventType, RepositoryEvent.ANY)) { + throw new IllegalArgumentException("Can only register RepositoryEvent Handlers"); } - } + final RepositoryEventListener myListener = (RepositoryEventListener) listener; + final EventType type = (EventType) eventType; - @Override - public void register(RepositoryEventListener listener, EventType type) { - List listeners; + List> listeners; if (listenerTypeMap.containsKey(type)) { listeners = listenerTypeMap.get(type); } else { @@ -369,29 +370,19 @@ public abstract class AbstractRepository implements EditableRepository, Reposito listenerTypeMap.put(type, listeners); } if (!listeners.contains(listener)) { - listeners.add(listener); - } - - } - - @Override - public void register(RepositoryEventListener listener, Set types) { - for (EventType type : types) { - register(listener, type); + listeners.add(myListener); } } @Override - public void unregister(RepositoryEventListener listener) { - listeners.remove(listener); - for (List listeners : listenerTypeMap.values()) { + public void unregister(EventType type, RepositoryEventListener listener) { + for (List> listeners : listenerTypeMap.values()) { listeners.remove(listener); } } @Override public void clearListeners() { - this.listeners.clear(); this.listenerTypeMap.clear(); } diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/RepositoryRegistry.java b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/RepositoryRegistry.java index 28ffc5a01..07e7300e5 100644 --- a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/RepositoryRegistry.java +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/RepositoryRegistry.java @@ -53,7 +53,7 @@ import static org.apache.archiva.indexer.ArchivaIndexManager.DEFAULT_INDEX_PATH; * @since 3.0 */ @Service("repositoryRegistry") -public class RepositoryRegistry implements ConfigurationListener, RepositoryEventHandler, RepositoryEventListener { +public class RepositoryRegistry implements ConfigurationListener, RepositoryEventSource, RepositoryEventListener { private static final Logger log = LoggerFactory.getLogger(RepositoryRegistry.class); @@ -73,8 +73,7 @@ public class RepositoryRegistry implements ConfigurationListener, RepositoryEven @Named("repositoryContentFactory#default") RepositoryContentFactory repositoryContentFactory; - private List listeners = new ArrayList<>(); - private Map> typeListenerMap = new HashMap<>(); + private Map, List>> typeListenerMap = new HashMap<>(); private Map managedRepositories = new HashMap<>(); @@ -111,7 +110,7 @@ public class RepositoryRegistry implements ConfigurationListener, RepositoryEven } finally { rwLock.writeLock().unlock(); } - pushEvent(new RepositoryRegistryEvent<>(RepositoryRegistryEvent.RegistryEventType.RELOADED, this)); + pushEvent(new RepositoryRegistryEvent(RepositoryRegistryEvent.RELOADED, this)); } @PreDestroy @@ -124,7 +123,7 @@ public class RepositoryRegistry implements ConfigurationListener, RepositoryEven repo.close(); } remoteRepositories.clear(); - pushEvent(new RepositoryRegistryEvent<>(RepositoryRegistryEvent.RegistryEventType.DESTROYED, this)); + pushEvent(new RepositoryRegistryEvent(RepositoryRegistryEvent.DESTROYED, this)); } @@ -182,7 +181,7 @@ public class RepositoryRegistry implements ConfigurationListener, RepositoryEven private ManagedRepository createNewManagedRepository(RepositoryProvider provider, ManagedRepositoryConfiguration cfg) throws RepositoryException { log.debug("Creating repo {}", cfg.getId()); ManagedRepository repo = provider.createManagedInstance(cfg); - repo.register(this); + repo.register(RepositoryEvent.ANY, this); updateRepositoryReferences(provider, repo, cfg, null); return repo; @@ -205,7 +204,7 @@ public class RepositoryRegistry implements ConfigurationListener, RepositoryEven if (configuration != null) { replaceOrAddRepositoryConfig(provider.getManagedConfiguration(stageRepo), configuration); } - pushEvent(new LifecycleEvent(LifecycleEvent.LifecycleEventType.REGISTERED, this, stageRepo)); + pushEvent(new LifecycleEvent(LifecycleEvent.REGISTERED, this, stageRepo)); } feature.setStagingRepository(stageRepo); } @@ -222,7 +221,7 @@ public class RepositoryRegistry implements ConfigurationListener, RepositoryEven createIndexingContext(editableRepo); } } - repo.register(this); + repo.register(RepositoryEvent.ANY, this); } public ArchivaIndexManager getIndexManager(RepositoryType type) { @@ -297,7 +296,7 @@ public class RepositoryRegistry implements ConfigurationListener, RepositoryEven createIndexingContext(editableRepo); } } - repo.register(this); + repo.register(RepositoryEvent.ANY, this); } private Map getRepositorGroupsFromConfig() { @@ -332,7 +331,7 @@ public class RepositoryRegistry implements ConfigurationListener, RepositoryEven private RepositoryGroup createNewRepositoryGroup(RepositoryProvider provider, RepositoryGroupConfiguration config) throws RepositoryException { RepositoryGroup repositoryGroup = provider.createRepositoryGroup(config); - repositoryGroup.register(this); + repositoryGroup.register(RepositoryEvent.ANY, this); updateRepositoryReferences(provider, repositoryGroup, config); return repositoryGroup; } @@ -511,9 +510,9 @@ public class RepositoryRegistry implements ConfigurationListener, RepositoryEven configuration.addManagedRepository(newCfg); saveConfiguration(configuration); if (originRepo != managedRepository) { - pushEvent(new LifecycleEvent<>(LifecycleEvent.LifecycleEventType.REGISTERED, this, managedRepository)); + pushEvent(new LifecycleEvent(LifecycleEvent.REGISTERED, this, managedRepository)); } else { - pushEvent(new LifecycleEvent<>(LifecycleEvent.LifecycleEventType.UPDATED, this, managedRepository)); + pushEvent(new LifecycleEvent(LifecycleEvent.UPDATED, this, managedRepository)); } return managedRepository; } catch (Exception e) { @@ -595,9 +594,9 @@ public class RepositoryRegistry implements ConfigurationListener, RepositoryEven updateRepositoryReferences(getProvider(repoType), repo, managedRepositoryConfiguration, configuration); replaceOrAddRepositoryConfig(managedRepositoryConfiguration, configuration); if (registeredNew) { - pushEvent(new LifecycleEvent<>(LifecycleEvent.LifecycleEventType.REGISTERED, this, repo)); + pushEvent(new LifecycleEvent(LifecycleEvent.REGISTERED, this, repo)); } else { - pushEvent(new LifecycleEvent<>(LifecycleEvent.LifecycleEventType.UPDATED, this, repo)); + pushEvent(new LifecycleEvent(LifecycleEvent.UPDATED, this, repo)); } return repo; } finally { @@ -780,9 +779,9 @@ public class RepositoryRegistry implements ConfigurationListener, RepositoryEven } configuration.addRemoteRepository(newCfg); if (remoteRepository != originRepo) { - pushEvent(new LifecycleEvent<>(LifecycleEvent.LifecycleEventType.REGISTERED, this, remoteRepository)); + pushEvent(new LifecycleEvent(LifecycleEvent.REGISTERED, this, remoteRepository)); } else { - pushEvent(new LifecycleEvent<>(LifecycleEvent.LifecycleEventType.UPDATED, this, remoteRepository)); + pushEvent(new LifecycleEvent(LifecycleEvent.UPDATED, this, remoteRepository)); } return remoteRepository; } catch (Exception e) { @@ -896,9 +895,9 @@ public class RepositoryRegistry implements ConfigurationListener, RepositoryEven updateRepositoryReferences(getProvider(repoType), repo, remoteRepositoryConfiguration, configuration); replaceOrAddRepositoryConfig(remoteRepositoryConfiguration, configuration); if (registeredNew) { - pushEvent(new LifecycleEvent(LifecycleEvent.LifecycleEventType.REGISTERED, this, repo)); + pushEvent(new LifecycleEvent(LifecycleEvent.REGISTERED, this, repo)); } else { - pushEvent(new LifecycleEvent(LifecycleEvent.LifecycleEventType.UPDATED, this, repo)); + pushEvent(new LifecycleEvent(LifecycleEvent.UPDATED, this, repo)); } return repo; } finally { @@ -958,7 +957,7 @@ public class RepositoryRegistry implements ConfigurationListener, RepositoryEven } saveConfiguration(configuration); } - pushEvent(new LifecycleEvent<>(LifecycleEvent.LifecycleEventType.UNREGISTERED, this, repo)); + pushEvent(new LifecycleEvent(LifecycleEvent.UNREGISTERED, this, repo)); } catch (RegistryException | IndeterminateConfigurationException e) { // Rollback log.error("Could not save config after repository removal: {}", e.getMessage(), e); @@ -995,7 +994,7 @@ public class RepositoryRegistry implements ConfigurationListener, RepositoryEven configuration.removeManagedRepository(cfg); } } - pushEvent(new LifecycleEvent<>(LifecycleEvent.LifecycleEventType.UNREGISTERED, this, repo)); + pushEvent(new LifecycleEvent(LifecycleEvent.UNREGISTERED, this, repo)); } finally { rwLock.writeLock().unlock(); } @@ -1102,7 +1101,7 @@ public class RepositoryRegistry implements ConfigurationListener, RepositoryEven doRemoveRepo(repo, configuration); saveConfiguration(configuration); } - pushEvent(new LifecycleEvent<>(LifecycleEvent.LifecycleEventType.UNREGISTERED, this, repo)); + pushEvent(new LifecycleEvent(LifecycleEvent.UNREGISTERED, this, repo)); } catch (RegistryException | IndeterminateConfigurationException e) { // Rollback log.error("Could not save config after repository removal: {}", e.getMessage(), e); @@ -1127,7 +1126,7 @@ public class RepositoryRegistry implements ConfigurationListener, RepositoryEven if (repo != null) { doRemoveRepo(repo, configuration); } - pushEvent(new LifecycleEvent<>(LifecycleEvent.LifecycleEventType.UNREGISTERED, this, repo)); + pushEvent(new LifecycleEvent(LifecycleEvent.UNREGISTERED, this, repo)); } finally { rwLock.writeLock().unlock(); } @@ -1172,7 +1171,7 @@ public class RepositoryRegistry implements ConfigurationListener, RepositoryEven ManagedRepositoryConfiguration cfg = provider.getManagedConfiguration(repo); cfg.setId(newId); ManagedRepository cloned = provider.createManagedInstance(cfg); - cloned.register(this); + cloned.register(RepositoryEvent.ANY, this); return cloned; } @@ -1201,7 +1200,7 @@ public class RepositoryRegistry implements ConfigurationListener, RepositoryEven RemoteRepositoryConfiguration cfg = provider.getRemoteConfiguration(repo); cfg.setId(newId); RemoteRepository cloned = provider.createRemoteInstance(cfg); - cloned.register(this); + cloned.register(RepositoryEvent.ANY, this); return cloned; } @@ -1216,15 +1215,8 @@ public class RepositoryRegistry implements ConfigurationListener, RepositoryEven @Override - public void register(RepositoryEventListener listener) { - if (!this.listeners.contains(listener)) { - this.listeners.add(listener); - } - } - - @Override - public void register(RepositoryEventListener listener, EventType type) { - List listeners; + public void register(EventType type, RepositoryEventListener listener) { + List> listeners; if (typeListenerMap.containsKey(type)) { listeners = typeListenerMap.get(type); } else { @@ -1236,28 +1228,19 @@ public class RepositoryRegistry implements ConfigurationListener, RepositoryEven } } - @Override - public void register(RepositoryEventListener listener, Set types) { - for (EventType type : types) { - register(listener, type); - } - } @Override - public void unregister(RepositoryEventListener listener) { - this.listeners.remove(listener); - for (List listeners : typeListenerMap.values()) { + public void unregister(EventType type, RepositoryEventListener listener) { + for (List> listeners : typeListenerMap.values()) { listeners.remove(listener); } } @Override public void clearListeners() { - this.listeners.clear(); this.typeListenerMap.clear(); } - @SuppressWarnings("unchecked") @Override public void raise(Event event) { // To avoid event cycles: @@ -1302,20 +1285,18 @@ public class RepositoryRegistry implements ConfigurationListener, RepositoryEven } } - private void pushEvent(Event event) { - callListeners(event, listeners); - if (typeListenerMap.containsKey(event.getType())) { - callListeners(event, typeListenerMap.get(event.getType())); + private void pushEvent(Event event) { + final EventType currentType = event.getType(); + for (EventType type : typeListenerMap.keySet()) { + if (EventType.isInstanceOf(currentType, type)) { + callListeners(event, typeListenerMap.get(type)); + } } } - private void callListeners(final Event event, final List evtListeners) { - for (RepositoryEventListener listener : evtListeners) { - try { - listener.raise(event); - } catch (Throwable e) { - log.error("Could not raise event {} on listener {}: {}", event, listener, e.getMessage()); - } + private void callListeners(Event event, List> listeners) { + for (RepositoryEventListener listener : listeners) { + listener.raise(event); } }