diff options
author | Martin Stockhammer <martin_s@apache.org> | 2019-10-01 23:11:54 +0200 |
---|---|---|
committer | Martin Stockhammer <martin_s@apache.org> | 2019-10-01 23:11:54 +0200 |
commit | 54148d4fc22487389bf39c52eaff821aed8c994c (patch) | |
tree | 9453cd5e525002192ded4ed25b806e9ce061d8d4 /archiva-modules/archiva-base/archiva-repository-api | |
parent | 9d1cf5687f96b719b5aba3a8e6a5282b1f1205c8 (diff) | |
download | archiva-54148d4fc22487389bf39c52eaff821aed8c994c.tar.gz archiva-54148d4fc22487389bf39c52eaff821aed8c994c.zip |
Refactoring event API
Diffstat (limited to 'archiva-modules/archiva-base/archiva-repository-api')
10 files changed, 94 insertions, 49 deletions
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<O> { +public class Event extends EventObject { + + public static final EventType<Event> ANY = new EventType(null, "ANY"); Event previous; - final O originator; - final EventType type; + final Object originator; + final EventType<? extends Event> type; final LocalDateTime instant; - public <OO extends O> Event(EventType type, OO originator) { + public Event(EventType<? extends Event> type, Object originator) { + super(originator); this.originator = originator; this.type = type; this.instant = LocalDateTime.now(); } - private <OO> Event(Event<OO> 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<? extends Event> getType() { return type; }; @@ -49,11 +54,11 @@ public class Event<O> { return instant; } - public O getOriginator() { + public Object getOriginator() { return originator; } - public <NO> Event<NO> 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<T extends Event> { + + private final String name; + private final EventType<? super T> superType; + + public EventType(EventType<? super T> superType, String name) { + this.name = name; + this.superType = superType; + } + + public String name() { + return name; + } + + public EventType<? super T> getSuperType() { + return superType; + } + + + public static List<EventType<?>> fetchSuperTypes(EventType<?> type) { + List<EventType<?>> 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<O> extends RepositoryValueEvent<O, URI> { - - public enum Index implements EventType { - INDEX_URI_CHANGE, PACKED_INDEX_URI_CHANGE - } +public class IndexCreationEvent extends RepositoryValueEvent<URI> { - IndexCreationEvent(Repository repo, O origin, URI oldValue, URI value) { - super(Index.INDEX_URI_CHANGE, origin, repo, oldValue, value); - } + public static EventType<IndexCreationEvent> ANY = new EventType<>(RepositoryValueEvent.ANY, "REPOSITORY.VALUE.INDEX"); + public static EventType<IndexCreationEvent> INDEX_URI_CHANGED = new EventType<>(ANY, "REPOSITORY.VALUE.INDEX.URI_CHANGED"); + public static EventType<IndexCreationEvent> PACKED_INDEX_URI_CHANGED = new EventType<>(ANY, "REPOSITORY.VALUE.INDEX.PACKED_URI_CHANGED"); - IndexCreationEvent(Index type, O origin, Repository repo, URI oldValue, URI value) { + IndexCreationEvent(EventType<? extends IndexCreationEvent> type, Object origin, Repository repo, URI oldValue, URI value) { super(type, origin, repo, oldValue, value); } public static final <O> 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 <O> 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<O> extends RepositoryEvent<O> { +public class LifecycleEvent extends RepositoryEvent { - public enum LifecycleEventType implements EventType { - REGISTERED,UNREGISTERED,UPDATED - } + public static EventType<LifecycleEvent> ANY = new EventType<>(RepositoryEvent.ANY, "REPOSITORY.LIFECYCLE"); + public static EventType<LifecycleEvent> REGISTERED = new EventType<>(ANY, "REPOSITORY.LIFECYCLE.REGISTERED"); + public static EventType<LifecycleEvent> UNREGISTERED = new EventType<>(ANY, "REPOSITORY.LIFECYCLE.UNREGISTERED"); + public static EventType<LifecycleEvent> UPDATED = new EventType<>(ANY, "REPOSITORY.LIFECYCLE.UPDATED"); - public <OO extends O> LifecycleEvent(LifecycleEventType type, OO origin, Repository repository) { + public LifecycleEvent(EventType<? extends LifecycleEvent> 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<O> extends Event<O> { +public class RepositoryEvent extends Event { + + public static final EventType<RepositoryEvent> ANY = new EventType<>(Event.ANY, "REPOSITORY.UPDATED"); private final Repository repository; - public <OO extends O> RepositoryEvent(EventType type, OO origin, Repository repository) { + public RepositoryEvent(EventType<? extends RepositoryEvent> type, Object origin, Repository repository) { super(type, origin); this.repository = repository; } @@ -33,4 +35,9 @@ public class RepositoryEvent<O> extends Event<O> { public Repository getRepository() { return repository; } + + @Override + public EventType<? extends RepositoryEvent> getType() { + return (EventType<? extends RepositoryEvent>) 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<T extends Event> { - 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 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); + <T extends Event> void register(EventType<T> type, RepositoryEventListener<? super T> listener); - void register(RepositoryEventListener listener, EventType type); - - void register(RepositoryEventListener listener, Set<? extends EventType> types); - - void unregister(RepositoryEventListener listener); + <T extends Event> void unregister(EventType<T> type, RepositoryEventListener<? super T> 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<O> extends Event<O> { +public class RepositoryRegistryEvent extends Event { - public enum RegistryEventType implements EventType { - RELOADED,DESTROYED - } + public static EventType<RepositoryRegistryEvent> ANY = new EventType(Event.ANY, "REGISTRY"); + public static EventType<RepositoryRegistryEvent> RELOADED = new EventType(ANY, "REGISTRY.RELOADED"); + public static EventType<RepositoryRegistryEvent> DESTROYED = new EventType(ANY, "REGISTRY.DESTROYED"); - public <OO extends O> RepositoryRegistryEvent(RegistryEventType type, OO origin) { + public RepositoryRegistryEvent(EventType<? extends RepositoryRegistryEvent> 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 <V> */ -public class RepositoryValueEvent<O, V> extends RepositoryEvent<O> { +public class RepositoryValueEvent<V> extends RepositoryEvent { + + public static final EventType<RepositoryValueEvent<?>> 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<? extends RepositoryValueEvent<V>> type, Object origin, Repository repo, V oldValue, V value) { super(type, origin, repo); this.value = value; this.oldValue = oldValue; |