From 09a7a95b251d15c1876c0f55ce263b7ed4d68d4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Prante?= Date: Mon, 15 Jan 2024 11:54:12 +0100 Subject: [PATCH] refactoring to event manager services, new subproject for syslog with netty dependencies --- event-common/build.gradle | 1 - event-common/src/main/java/module-info.java | 9 +-- ...ger.java => ClockEventManagerService.java} | 23 +++--- .../xbib/event/clock/ClockEventService.java | 4 +- .../org/xbib/event/common/EventManager.java | 81 +++++++++---------- .../event/common/EventManagerService.java | 11 +++ ...r.java => GenericEventManagerService.java} | 15 +++- ...ava => FileFollowEventManagerService.java} | 22 +++-- ...ager.java => PathEventManagerService.java} | 28 ++++--- .../org/xbib/event/path/PathEventService.java | 4 +- ...ger.java => TimerEventManagerService.java} | 47 ++++++----- .../java/org/xbib/event/EventManagerTest.java | 6 +- .../event/clock/ClockEventManagerTest.java | 3 +- .../path/FileFollowEventManagerTest.java | 3 +- .../event/timer/TimerEventManagerTest.java | 2 +- event-net-http/build.gradle | 3 + event-syslog/build.gradle | 5 ++ event-syslog/src/main/java/module-info.java | 17 ++++ .../xbib/event/syslog/CEFMessageParser.java | 0 .../event/syslog/DefaultSyslogMessage.java | 0 .../event/syslog/DefaultSyslogRequest.java | 0 .../org/xbib/event/syslog/EncoderHelper.java | 0 .../java/org/xbib/event/syslog/Message.java | 0 .../org/xbib/event/syslog/MessageEncoder.java | 0 .../org/xbib/event/syslog/MessageKey.java | 0 .../org/xbib/event/syslog/MessageParser.java | 0 .../org/xbib/event/syslog/MessageType.java | 0 .../java/org/xbib/event/syslog/Priority.java | 0 .../event/syslog/RFC3164MessageParser.java | 0 .../event/syslog/RFC5424MessageParser.java | 0 .../syslog/SyslogEventManagerService.java | 20 +++-- .../xbib/event/syslog/SyslogFrameDecoder.java | 0 .../event/syslog/SyslogIdleStateHandler.java | 0 .../org/xbib/event/syslog/SyslogMessage.java | 0 .../event/syslog/SyslogMessageHandler.java | 0 .../xbib/event/syslog/SyslogMessageKey.java | 0 .../org/xbib/event/syslog/SyslogRequest.java | 0 .../org/xbib/event/syslog/SyslogService.java | 0 .../event/syslog/TCPSyslogMessageDecoder.java | 0 .../event/syslog/UDPSyslogMessageDecoder.java | 0 .../org.xbib.event.common.EventManagerService | 1 + .../event/syslog/test}/SyslogServiceTest.java | 5 +- gradle.properties | 2 +- settings.gradle | 1 + 44 files changed, 190 insertions(+), 123 deletions(-) rename event-common/src/main/java/org/xbib/event/clock/{ClockEventManager.java => ClockEventManagerService.java} (81%) create mode 100644 event-common/src/main/java/org/xbib/event/common/EventManagerService.java rename event-common/src/main/java/org/xbib/event/generic/{GenericEventManager.java => GenericEventManagerService.java} (73%) rename event-common/src/main/java/org/xbib/event/path/{FileFollowEventManager.java => FileFollowEventManagerService.java} (78%) rename event-common/src/main/java/org/xbib/event/path/{PathEventManager.java => PathEventManagerService.java} (90%) rename event-common/src/main/java/org/xbib/event/timer/{TimerEventManager.java => TimerEventManagerService.java} (67%) create mode 100644 event-net-http/build.gradle create mode 100644 event-syslog/build.gradle create mode 100644 event-syslog/src/main/java/module-info.java rename {event-common => event-syslog}/src/main/java/org/xbib/event/syslog/CEFMessageParser.java (100%) rename {event-common => event-syslog}/src/main/java/org/xbib/event/syslog/DefaultSyslogMessage.java (100%) rename {event-common => event-syslog}/src/main/java/org/xbib/event/syslog/DefaultSyslogRequest.java (100%) rename {event-common => event-syslog}/src/main/java/org/xbib/event/syslog/EncoderHelper.java (100%) rename {event-common => event-syslog}/src/main/java/org/xbib/event/syslog/Message.java (100%) rename {event-common => event-syslog}/src/main/java/org/xbib/event/syslog/MessageEncoder.java (100%) rename {event-common => event-syslog}/src/main/java/org/xbib/event/syslog/MessageKey.java (100%) rename {event-common => event-syslog}/src/main/java/org/xbib/event/syslog/MessageParser.java (100%) rename {event-common => event-syslog}/src/main/java/org/xbib/event/syslog/MessageType.java (100%) rename {event-common => event-syslog}/src/main/java/org/xbib/event/syslog/Priority.java (100%) rename {event-common => event-syslog}/src/main/java/org/xbib/event/syslog/RFC3164MessageParser.java (100%) rename {event-common => event-syslog}/src/main/java/org/xbib/event/syslog/RFC5424MessageParser.java (100%) rename event-common/src/main/java/org/xbib/event/syslog/SyslogEventManager.java => event-syslog/src/main/java/org/xbib/event/syslog/SyslogEventManagerService.java (65%) rename {event-common => event-syslog}/src/main/java/org/xbib/event/syslog/SyslogFrameDecoder.java (100%) rename {event-common => event-syslog}/src/main/java/org/xbib/event/syslog/SyslogIdleStateHandler.java (100%) rename {event-common => event-syslog}/src/main/java/org/xbib/event/syslog/SyslogMessage.java (100%) rename {event-common => event-syslog}/src/main/java/org/xbib/event/syslog/SyslogMessageHandler.java (100%) rename {event-common => event-syslog}/src/main/java/org/xbib/event/syslog/SyslogMessageKey.java (100%) rename {event-common => event-syslog}/src/main/java/org/xbib/event/syslog/SyslogRequest.java (100%) rename {event-common => event-syslog}/src/main/java/org/xbib/event/syslog/SyslogService.java (100%) rename {event-common => event-syslog}/src/main/java/org/xbib/event/syslog/TCPSyslogMessageDecoder.java (100%) rename {event-common => event-syslog}/src/main/java/org/xbib/event/syslog/UDPSyslogMessageDecoder.java (100%) create mode 100644 event-syslog/src/main/resources/META-INF/services/org.xbib.event.common.EventManagerService rename {event-common/src/test/java/org/xbib/event/syslog => event-syslog/src/test/java/org/xbib/event/syslog/test}/SyslogServiceTest.java (77%) diff --git a/event-common/build.gradle b/event-common/build.gradle index 25e5dcf..87c059c 100644 --- a/event-common/build.gradle +++ b/event-common/build.gradle @@ -6,5 +6,4 @@ dependencies { implementation libs.time implementation libs.datastructures.common implementation libs.datastructures.json.tiny - implementation libs.netty.handler } diff --git a/event-common/src/main/java/module-info.java b/event-common/src/main/java/module-info.java index 9d554a4..0abcdf6 100644 --- a/event-common/src/main/java/module-info.java +++ b/event-common/src/main/java/module-info.java @@ -1,3 +1,5 @@ +import org.xbib.event.common.EventManagerService; + module org.xbib.event.common { exports org.xbib.event.bus; exports org.xbib.event.clock; @@ -6,9 +8,9 @@ module org.xbib.event.common { exports org.xbib.event.log; exports org.xbib.event.path; exports org.xbib.event.persistence; - exports org.xbib.event.syslog; exports org.xbib.event.timer; exports org.xbib.event.wal; + uses EventManagerService; requires org.xbib.event.api; requires org.xbib.settings.api; requires org.xbib.settings.datastructures.json; @@ -16,10 +18,5 @@ module org.xbib.event.common { requires org.xbib.time; requires org.xbib.datastructures.common; requires org.xbib.datastructures.json.tiny; - requires io.netty.buffer; - requires io.netty.common; - requires io.netty.transport; - requires io.netty.handler; - requires io.netty.codec; requires java.logging; } diff --git a/event-common/src/main/java/org/xbib/event/clock/ClockEventManager.java b/event-common/src/main/java/org/xbib/event/clock/ClockEventManagerService.java similarity index 81% rename from event-common/src/main/java/org/xbib/event/clock/ClockEventManager.java rename to event-common/src/main/java/org/xbib/event/clock/ClockEventManagerService.java index 1b8760e..b48265d 100644 --- a/event-common/src/main/java/org/xbib/event/clock/ClockEventManager.java +++ b/event-common/src/main/java/org/xbib/event/clock/ClockEventManagerService.java @@ -1,7 +1,7 @@ package org.xbib.event.clock; -import org.xbib.event.bus.AsyncEventBus; import org.xbib.event.bus.EventBus; +import org.xbib.event.common.EventManagerService; import org.xbib.settings.Settings; import org.xbib.time.schedule.CronExpression; import org.xbib.time.schedule.CronSchedule; @@ -11,27 +11,25 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ThreadFactory; import java.util.logging.Level; import java.util.logging.Logger; -public class ClockEventManager implements Closeable { +public class ClockEventManagerService implements EventManagerService, Closeable { - private static final Logger logger = Logger.getLogger(ClockEventManager.class.getName()); + private static final Logger logger = Logger.getLogger(ClockEventManagerService.class.getName()); - private final CronSchedule cronSchedule; + private CronSchedule cronSchedule; - private final List suspended; + private List suspended; - public ClockEventManager(Settings settings) { - this(settings, new AsyncEventBus(Executors.newSingleThreadExecutor()), ClockEventManager.class.getClassLoader()); + public ClockEventManagerService() { } - public ClockEventManager(Settings settings, - EventBus eventBus, - ClassLoader classLoader) { + public ClockEventManagerService init(Settings settings, EventBus eventBus, ClassLoader classLoader, ExecutorService executorService) { this.suspended = new ArrayList<>(); ThreadFactory threadFactory = new ThreadFactory() { int n = 1; @@ -40,9 +38,9 @@ public class ClockEventManager implements Closeable { return new Thread(r, "clock-event-manager-" + (n++)); } }; - ScheduledExecutorService executorService = + ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(settings.getAsInt("pool.size", 2), threadFactory); - this.cronSchedule = new CronSchedule<>(executorService); + this.cronSchedule = new CronSchedule<>(scheduledExecutorService); for (Map.Entry clockEventService : settings.getGroups("event.clock").entrySet()) { String name = clockEventService.getKey(); Settings entrySettings = clockEventService.getValue(); @@ -69,6 +67,7 @@ public class ClockEventManager implements Closeable { } logger.log(Level.INFO, "entries = " + cronSchedule.getEntries()); cronSchedule.start(); + return this; } public List getSuspended() { diff --git a/event-common/src/main/java/org/xbib/event/clock/ClockEventService.java b/event-common/src/main/java/org/xbib/event/clock/ClockEventService.java index ed5ab08..34504d6 100644 --- a/event-common/src/main/java/org/xbib/event/clock/ClockEventService.java +++ b/event-common/src/main/java/org/xbib/event/clock/ClockEventService.java @@ -10,7 +10,7 @@ public class ClockEventService implements Callable { private static final Logger logger = Logger.getLogger(ClockEventService.class.getName()); - private final ClockEventManager manager; + private final ClockEventManagerService manager; private final EventBus eventBus; @@ -18,7 +18,7 @@ public class ClockEventService implements Callable { private final Class eventClass; - public ClockEventService(ClockEventManager manager, + public ClockEventService(ClockEventManagerService manager, EventBus eventBus, String name, Class eventClass) { diff --git a/event-common/src/main/java/org/xbib/event/common/EventManager.java b/event-common/src/main/java/org/xbib/event/common/EventManager.java index e23135a..29fd348 100644 --- a/event-common/src/main/java/org/xbib/event/common/EventManager.java +++ b/event-common/src/main/java/org/xbib/event/common/EventManager.java @@ -1,22 +1,22 @@ package org.xbib.event.common; +import java.util.HashMap; +import java.util.Map; import java.util.ServiceLoader; import org.xbib.event.EventConsumer; import org.xbib.event.bus.AsyncEventBus; import org.xbib.event.bus.SubscriberExceptionContext; import org.xbib.event.bus.SubscriberExceptionHandler; -import org.xbib.event.clock.ClockEventManager; -import org.xbib.event.generic.GenericEventManager; -import org.xbib.event.path.FileFollowEventManager; -import org.xbib.event.path.PathEventManager; -import org.xbib.event.syslog.SyslogEventManager; -import org.xbib.event.timer.TimerEventManager; +import org.xbib.event.clock.ClockEventManagerService; +import org.xbib.event.generic.GenericEventManagerService; +import org.xbib.event.path.FileFollowEventManagerService; +import org.xbib.event.path.PathEventManagerService; +import org.xbib.event.timer.TimerEventManagerService; import org.xbib.settings.Settings; import java.io.Closeable; import java.io.IOException; -import java.time.ZoneId; import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -31,54 +31,53 @@ public final class EventManager { private final Builder builder; - private final GenericEventManager genericEventManager; - - private final ClockEventManager clockEventManager; - - private final TimerEventManager timerEventManager; - - private final FileFollowEventManager fileFollowEventManager; - - private final PathEventManager pathEventManager; - - private final SyslogEventManager syslogEventManager; + private final Map, EventManagerService> services; private EventManager(Builder builder) { this.builder = builder; - this.genericEventManager = new GenericEventManager(builder.eventBus); - this.clockEventManager = new ClockEventManager(builder.settings, builder.eventBus, builder.classLoader); - this.timerEventManager = new TimerEventManager(builder.settings, builder.eventBus, builder.classLoader, ZoneId.systemDefault()); - this.fileFollowEventManager = new FileFollowEventManager(builder.settings, builder.eventBus, builder.executorService, builder.classLoader); - this.pathEventManager = new PathEventManager(builder.settings, builder.eventBus, builder.executorService, builder.classLoader); - this.syslogEventManager = new SyslogEventManager(builder.settings, builder.eventBus); + this.services = new HashMap<>(); + services.put(GenericEventManagerService.class, new GenericEventManagerService() + .init(builder.settings, builder.eventBus, builder.classLoader, builder.executorService)); + services.put(ClockEventManagerService.class, new ClockEventManagerService() + .init(builder.settings, builder.eventBus, builder.classLoader, builder.executorService)); + services.put(TimerEventManagerService.class, new TimerEventManagerService() + .init(builder.settings, builder.eventBus, builder.classLoader, builder.executorService)); + services.put(FileFollowEventManagerService.class, new FileFollowEventManagerService() + .init(builder.settings, builder.eventBus, builder.classLoader, builder.executorService)); + services.put(PathEventManagerService.class, new PathEventManagerService() + .init(builder.settings, builder.eventBus, builder.classLoader, builder.executorService)); + for (EventManagerService service : ServiceLoader.load(EventManagerService.class)) { + services.put(service.getClass(), service.init(builder.settings, builder.eventBus, builder.classLoader, builder.executorService)); + } + logger.log(Level.INFO, "installed event service managers = " + services.keySet()); } public static Builder builder(Settings settings) { return new Builder(settings); } - public GenericEventManager getGenericEventManager() { - return genericEventManager; + public EventManagerService getEventManagerService(Class cl) { + return services.get(cl); } - public ClockEventManager getClockEventManager() { - return clockEventManager; + public GenericEventManagerService getGenericEventManagerService() { + return (GenericEventManagerService) services.get(GenericEventManagerService.class); } - public TimerEventManager getTimerEventManager() { - return timerEventManager; + public ClockEventManagerService getClockEventManagerService() { + return (ClockEventManagerService) services.get(ClockEventManagerService.class); } - public FileFollowEventManager getFileFollowEventManager() { - return fileFollowEventManager; + public TimerEventManagerService getTimerEventManagerService() { + return (TimerEventManagerService) services.get(TimerEventManagerService.class); } - public PathEventManager getPathEventManager() { - return pathEventManager; + public FileFollowEventManagerService getFileFollowEventManagerService() { + return (FileFollowEventManagerService) services.get(FileFollowEventManagerService.class); } - public SyslogEventManager getSyslogEventManager() { - return syslogEventManager; + public PathEventManagerService getPathEventManagerService() { + return (PathEventManagerService) services.get(PathEventManagerService.class); } public void close() throws IOException { @@ -87,11 +86,11 @@ public final class EventManager { closeable.close(); } } - clockEventManager.close(); - timerEventManager.close(); - fileFollowEventManager.close(); - pathEventManager.close(); - syslogEventManager.close(); + for (EventManagerService service : services.values()) { + if (service instanceof Closeable closeable) { + closeable.close(); + } + } } public static class Builder { diff --git a/event-common/src/main/java/org/xbib/event/common/EventManagerService.java b/event-common/src/main/java/org/xbib/event/common/EventManagerService.java new file mode 100644 index 0000000..0918df1 --- /dev/null +++ b/event-common/src/main/java/org/xbib/event/common/EventManagerService.java @@ -0,0 +1,11 @@ +package org.xbib.event.common; + +import org.xbib.event.bus.EventBus; +import org.xbib.settings.Settings; + +import java.util.concurrent.ExecutorService; + +public interface EventManagerService { + + EventManagerService init(Settings settings, EventBus eventBus, ClassLoader classLoader, ExecutorService executorService); +} diff --git a/event-common/src/main/java/org/xbib/event/generic/GenericEventManager.java b/event-common/src/main/java/org/xbib/event/generic/GenericEventManagerService.java similarity index 73% rename from event-common/src/main/java/org/xbib/event/generic/GenericEventManager.java rename to event-common/src/main/java/org/xbib/event/generic/GenericEventManagerService.java index 7abad9d..6c25709 100644 --- a/event-common/src/main/java/org/xbib/event/generic/GenericEventManager.java +++ b/event-common/src/main/java/org/xbib/event/generic/GenericEventManagerService.java @@ -2,17 +2,26 @@ package org.xbib.event.generic; import java.util.Set; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; import org.xbib.event.bus.AsyncEventBus; +import org.xbib.event.bus.EventBus; import org.xbib.event.bus.Subscriber; import org.xbib.event.bus.SubscriberRegistry; +import org.xbib.event.common.EventManagerService; +import org.xbib.settings.Settings; -public class GenericEventManager { +public class GenericEventManagerService implements EventManagerService { - private final AsyncEventBus eventBus; + private EventBus eventBus; - public GenericEventManager(AsyncEventBus eventBus) { + public GenericEventManagerService() { + } + + @Override + public GenericEventManagerService init(Settings settings, EventBus eventBus, ClassLoader classLoader, ExecutorService executorService) { this.eventBus = eventBus; + return this; } public void post(Object event) { diff --git a/event-common/src/main/java/org/xbib/event/path/FileFollowEventManager.java b/event-common/src/main/java/org/xbib/event/path/FileFollowEventManagerService.java similarity index 78% rename from event-common/src/main/java/org/xbib/event/path/FileFollowEventManager.java rename to event-common/src/main/java/org/xbib/event/path/FileFollowEventManagerService.java index 87dc08a..e3a6f14 100644 --- a/event-common/src/main/java/org/xbib/event/path/FileFollowEventManager.java +++ b/event-common/src/main/java/org/xbib/event/path/FileFollowEventManagerService.java @@ -1,6 +1,7 @@ package org.xbib.event.path; -import org.xbib.event.bus.AsyncEventBus; +import org.xbib.event.bus.EventBus; +import org.xbib.event.common.EventManagerService; import org.xbib.settings.Settings; import java.io.Closeable; @@ -16,17 +17,21 @@ import java.util.logging.Level; import java.util.logging.Logger; import java.util.regex.Pattern; -public class FileFollowEventManager implements Closeable { +public class FileFollowEventManagerService implements EventManagerService, Closeable { - private static final Logger logger = Logger.getLogger(FileFollowEventManager.class.getName()); + private static final Logger logger = Logger.getLogger(FileFollowEventManagerService.class.getName()); - private final Map, FileFollowEventService> eventServiceMap; + private Map, FileFollowEventService> eventServiceMap; + + public FileFollowEventManagerService() { + } @SuppressWarnings("unchecked") - public FileFollowEventManager(Settings settings, - AsyncEventBus eventBus, - ExecutorService executorService, - ClassLoader classLoader) { + @Override + public FileFollowEventManagerService init(Settings settings, + EventBus eventBus, + ClassLoader classLoader, + ExecutorService executorService) { this.eventServiceMap = new LinkedHashMap<>(); for (Map.Entry entry : settings.getGroups("event.filefollow").entrySet()) { Settings definition = entry.getValue(); @@ -49,6 +54,7 @@ public class FileFollowEventManager implements Closeable { } } } + return this; } @Override diff --git a/event-common/src/main/java/org/xbib/event/path/PathEventManager.java b/event-common/src/main/java/org/xbib/event/path/PathEventManagerService.java similarity index 90% rename from event-common/src/main/java/org/xbib/event/path/PathEventManager.java rename to event-common/src/main/java/org/xbib/event/path/PathEventManagerService.java index 1a3491c..57f9d9e 100644 --- a/event-common/src/main/java/org/xbib/event/path/PathEventManager.java +++ b/event-common/src/main/java/org/xbib/event/path/PathEventManagerService.java @@ -3,6 +3,7 @@ package org.xbib.event.path; import org.xbib.datastructures.api.TimeValue; import org.xbib.datastructures.json.tiny.Json; import org.xbib.event.bus.EventBus; +import org.xbib.event.common.EventManagerService; import org.xbib.settings.Settings; import java.io.Closeable; @@ -22,25 +23,29 @@ import java.util.logging.Level; import java.util.logging.Logger; import java.util.stream.Stream; -public class PathEventManager implements Closeable { +public class PathEventManagerService implements EventManagerService, Closeable { - private static final Logger logger = Logger.getLogger(PathEventManager.class.getName()); + private static final Logger logger = Logger.getLogger(PathEventManagerService.class.getName()); - private final EventBus eventBus; + private EventBus eventBus; - private final ExecutorService executorService; + private ExecutorService executorService; - private final Path path; + private Path path; - private final Map, PathEventService> eventServiceMap; + private Map, PathEventService> eventServiceMap; - private final List suspendedQueues; + private List suspendedQueues; + + public PathEventManagerService() { + } @SuppressWarnings("unchecked") - public PathEventManager(Settings settings, - EventBus eventBus, - ExecutorService executorService, - ClassLoader classLoader) { + @Override + public PathEventManagerService init(Settings settings, + EventBus eventBus, + ClassLoader classLoader, + ExecutorService executorService) { this.eventBus = eventBus; this.executorService = executorService; this.eventServiceMap = new LinkedHashMap<>(); @@ -64,6 +69,7 @@ public class PathEventManager implements Closeable { logger.log(Level.SEVERE, "unable to create event path service " + entry.getKey() + ", reason " + e.getMessage(), e); } } + return this; } public void createPathEventService(String name, diff --git a/event-common/src/main/java/org/xbib/event/path/PathEventService.java b/event-common/src/main/java/org/xbib/event/path/PathEventService.java index 2cb8eae..e121a0b 100644 --- a/event-common/src/main/java/org/xbib/event/path/PathEventService.java +++ b/event-common/src/main/java/org/xbib/event/path/PathEventService.java @@ -32,7 +32,7 @@ public class PathEventService implements Callable, Closeable { public static final String FAIL = "fail"; - private final PathEventManager pathEventManager; + private final PathEventManagerService pathEventManager; private final EventBus eventBus; @@ -52,7 +52,7 @@ public class PathEventService implements Callable, Closeable { private volatile boolean keepWatching; - public PathEventService(PathEventManager pathEventManager, + public PathEventService(PathEventManagerService pathEventManager, EventBus eventBus, String name, Path path, diff --git a/event-common/src/main/java/org/xbib/event/timer/TimerEventManager.java b/event-common/src/main/java/org/xbib/event/timer/TimerEventManagerService.java similarity index 67% rename from event-common/src/main/java/org/xbib/event/timer/TimerEventManager.java rename to event-common/src/main/java/org/xbib/event/timer/TimerEventManagerService.java index 2e83069..b47f0ef 100644 --- a/event-common/src/main/java/org/xbib/event/timer/TimerEventManager.java +++ b/event-common/src/main/java/org/xbib/event/timer/TimerEventManagerService.java @@ -2,6 +2,7 @@ package org.xbib.event.timer; import org.xbib.event.bus.AsyncEventBus; import org.xbib.event.bus.EventBus; +import org.xbib.event.common.EventManagerService; import org.xbib.event.persistence.FilePersistenceStore; import org.xbib.event.persistence.PersistenceStore; import org.xbib.settings.Settings; @@ -16,26 +17,27 @@ import java.time.ZoneId; import java.time.ZonedDateTime; import java.util.LinkedHashMap; import java.util.Map; +import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.logging.Level; import java.util.logging.Logger; -public class TimerEventManager implements Closeable { +public class TimerEventManagerService implements EventManagerService, Closeable { - private static final Logger logger = Logger.getLogger(TimerEventManager.class.getName()); + private static final Logger logger = Logger.getLogger(TimerEventManagerService.class.getName()); - private final Map services; + private Map services; - public TimerEventManager(Settings settings) { - this(settings, new AsyncEventBus(Executors.newSingleThreadExecutor()), - TimerEventManager.class.getClassLoader(), ZoneId.systemDefault()); + public TimerEventManagerService() { } + @SuppressWarnings("unchecked") - public TimerEventManager(Settings settings, - EventBus eventBus, - ClassLoader classLoader, - ZoneId zoneId) { + @Override + public TimerEventManagerService init(Settings settings, + EventBus eventBus, + ClassLoader classLoader, + ExecutorService executorService) { this.services = new LinkedHashMap<>(); for (Map.Entry entry : settings.getGroups("event.timer").entrySet()) { String name = entry.getKey(); @@ -44,38 +46,43 @@ public class TimerEventManager implements Closeable { try { Class eventClass = (Class) classLoader.loadClass(className); PersistenceStore persistenceStore = new FilePersistenceStore(timerSettings, name); - services.put(name, new TimerEventService(eventBus, name, eventClass, zoneId, persistenceStore)); + services.put(name, new TimerEventService(eventBus, name, eventClass, ZoneId.systemDefault(), persistenceStore)); logger.log(Level.INFO, "timer " + name + " active for timer event class " + className); } catch (Exception e) { logger.log(Level.WARNING, "unable to activate timer " + name + ", reason: " + e.getMessage(), e); } } + return this; } - public boolean put(String key, String timeSpec, Map map) throws ParseException, IOException { - if (services.containsKey(key)) { + public boolean put(String service, + String timeSpec, + Map map) throws ParseException, IOException { + if (services.containsKey(service)) { Span span = Chronic.parse(timeSpec); if (span != null) { ZonedDateTime zonedDateTime = span.getBeginCalendar(); - services.get(key).schedule(zonedDateTime.toInstant(), map); + services.get(service).schedule(zonedDateTime.toInstant(), map); logger.log(Level.INFO, "scheduled to " + zonedDateTime); } else { - logger.log(Level.INFO, "timer event key " + key + ": can not understand time spec " + timeSpec); + logger.log(Level.INFO, "timer event key " + service + ": can not understand time spec " + timeSpec); return false; } return true; } else { - logger.log(Level.SEVERE, "unknown timer event key: " + key); + logger.log(Level.SEVERE, "unknown timer event key: " + service); } return false; } - public boolean put(String key, Instant instant, Map map) throws IOException { - if (services.containsKey(key)) { - services.get(key).schedule(instant, map); + public boolean put(String service, + Instant instant, + Map map) throws IOException { + if (services.containsKey(service)) { + services.get(service).schedule(instant, map); return true; } else { - logger.log(Level.SEVERE, "unknown timer event key: " + key); + logger.log(Level.SEVERE, "unknown timer event key: " + service); } return false; } diff --git a/event-common/src/test/java/org/xbib/event/EventManagerTest.java b/event-common/src/test/java/org/xbib/event/EventManagerTest.java index 2d63318..b7b8f29 100644 --- a/event-common/src/test/java/org/xbib/event/EventManagerTest.java +++ b/event-common/src/test/java/org/xbib/event/EventManagerTest.java @@ -24,7 +24,7 @@ public class EventManagerTest { EventManager eventManager = EventManager.builder(settings) .register(consumer) .build(); - eventManager.getGenericEventManager().post(new DefaultGenericEvent(e -> { + eventManager.getGenericEventManagerService().post(new DefaultGenericEvent(e -> { logger.log(Level.INFO, "received event " + e); })); } @@ -39,7 +39,7 @@ public class EventManagerTest { .register(consumer) .build(); CompletableFuture future = new CompletableFuture<>(); - eventManager.getGenericEventManager().post(new DefaultGenericEvent(e -> { + eventManager.getGenericEventManagerService().post(new DefaultGenericEvent(e -> { logger.log(Level.INFO, "received event " + e); future.complete(e); })); @@ -59,7 +59,7 @@ public class EventManagerTest { .loadEventConsumers() .build(); CompletableFuture future = new CompletableFuture<>(); - eventManager.getGenericEventManager().post(new DefaultGenericEvent(e -> { + eventManager.getGenericEventManagerService().post(new DefaultGenericEvent(e -> { logger.log(Level.INFO, "received event " + e); }), future); GenericEvent e = future.get(); diff --git a/event-common/src/test/java/org/xbib/event/clock/ClockEventManagerTest.java b/event-common/src/test/java/org/xbib/event/clock/ClockEventManagerTest.java index 357e01c..b2dfa3c 100644 --- a/event-common/src/test/java/org/xbib/event/clock/ClockEventManagerTest.java +++ b/event-common/src/test/java/org/xbib/event/clock/ClockEventManagerTest.java @@ -19,8 +19,7 @@ public class ClockEventManagerTest { EventManager eventManager = EventManager.builder(settings) .register(clockEventConsumer) .build(); - ClockEventManager clockEventManager = eventManager.getClockEventManager(); Thread.sleep(90000L); - clockEventManager.close(); + eventManager.close(); } } diff --git a/event-common/src/test/java/org/xbib/event/path/FileFollowEventManagerTest.java b/event-common/src/test/java/org/xbib/event/path/FileFollowEventManagerTest.java index 564285f..30dd561 100644 --- a/event-common/src/test/java/org/xbib/event/path/FileFollowEventManagerTest.java +++ b/event-common/src/test/java/org/xbib/event/path/FileFollowEventManagerTest.java @@ -28,15 +28,14 @@ public class FileFollowEventManagerTest { EventManager eventManager = EventManager.builder(settings) .register(consumer) .build(); - FileFollowEventManager fileFolloeEventManager = eventManager.getFileFollowEventManager(); Thread.sleep(5000L); try (BufferedWriter bufferedWriter = Files.newBufferedWriter(path.resolve("test.txt"))) { bufferedWriter.write("Hello"); logger.log(Level.INFO, "Hello written"); } Thread.sleep(5000L); - fileFolloeEventManager.close(); Files.delete(path.resolve("test.txt")); Files.delete(path); + eventManager.close(); } } diff --git a/event-common/src/test/java/org/xbib/event/timer/TimerEventManagerTest.java b/event-common/src/test/java/org/xbib/event/timer/TimerEventManagerTest.java index 35bcda1..7e21925 100644 --- a/event-common/src/test/java/org/xbib/event/timer/TimerEventManagerTest.java +++ b/event-common/src/test/java/org/xbib/event/timer/TimerEventManagerTest.java @@ -20,7 +20,7 @@ public class TimerEventManagerTest { EventManager eventManager = EventManager.builder(settings) .register(consumer) .build(); - TimerEventManager timerEventManager = eventManager.getTimerEventManager(); + TimerEventManagerService timerEventManager = eventManager.getTimerEventManagerService(); timerEventManager.put("testtimerevent", Instant.now().plusSeconds(5L), Map.of("a", "b")); Thread.sleep(10000L); timerEventManager.close(); diff --git a/event-net-http/build.gradle b/event-net-http/build.gradle new file mode 100644 index 0000000..0ce6a16 --- /dev/null +++ b/event-net-http/build.gradle @@ -0,0 +1,3 @@ +dependencies { + +} \ No newline at end of file diff --git a/event-syslog/build.gradle b/event-syslog/build.gradle new file mode 100644 index 0000000..d65c38e --- /dev/null +++ b/event-syslog/build.gradle @@ -0,0 +1,5 @@ +dependencies { + api project(':event-common') + implementation libs.datastructures.json.tiny + implementation libs.netty.handler +} diff --git a/event-syslog/src/main/java/module-info.java b/event-syslog/src/main/java/module-info.java new file mode 100644 index 0000000..1bcbc24 --- /dev/null +++ b/event-syslog/src/main/java/module-info.java @@ -0,0 +1,17 @@ +import org.xbib.event.common.EventManagerService; +import org.xbib.event.syslog.SyslogEventManagerService; + +module org.xbib.event.syslog { + exports org.xbib.event.syslog; + provides EventManagerService with SyslogEventManagerService; + requires org.xbib.event.common; + requires org.xbib.datastructures.json.tiny; + requires io.netty.buffer; + requires io.netty.common; + requires io.netty.transport; + requires io.netty.handler; + requires io.netty.codec; + requires java.logging; + requires org.xbib.settings.api; + requires org.xbib.event.api; +} diff --git a/event-common/src/main/java/org/xbib/event/syslog/CEFMessageParser.java b/event-syslog/src/main/java/org/xbib/event/syslog/CEFMessageParser.java similarity index 100% rename from event-common/src/main/java/org/xbib/event/syslog/CEFMessageParser.java rename to event-syslog/src/main/java/org/xbib/event/syslog/CEFMessageParser.java diff --git a/event-common/src/main/java/org/xbib/event/syslog/DefaultSyslogMessage.java b/event-syslog/src/main/java/org/xbib/event/syslog/DefaultSyslogMessage.java similarity index 100% rename from event-common/src/main/java/org/xbib/event/syslog/DefaultSyslogMessage.java rename to event-syslog/src/main/java/org/xbib/event/syslog/DefaultSyslogMessage.java diff --git a/event-common/src/main/java/org/xbib/event/syslog/DefaultSyslogRequest.java b/event-syslog/src/main/java/org/xbib/event/syslog/DefaultSyslogRequest.java similarity index 100% rename from event-common/src/main/java/org/xbib/event/syslog/DefaultSyslogRequest.java rename to event-syslog/src/main/java/org/xbib/event/syslog/DefaultSyslogRequest.java diff --git a/event-common/src/main/java/org/xbib/event/syslog/EncoderHelper.java b/event-syslog/src/main/java/org/xbib/event/syslog/EncoderHelper.java similarity index 100% rename from event-common/src/main/java/org/xbib/event/syslog/EncoderHelper.java rename to event-syslog/src/main/java/org/xbib/event/syslog/EncoderHelper.java diff --git a/event-common/src/main/java/org/xbib/event/syslog/Message.java b/event-syslog/src/main/java/org/xbib/event/syslog/Message.java similarity index 100% rename from event-common/src/main/java/org/xbib/event/syslog/Message.java rename to event-syslog/src/main/java/org/xbib/event/syslog/Message.java diff --git a/event-common/src/main/java/org/xbib/event/syslog/MessageEncoder.java b/event-syslog/src/main/java/org/xbib/event/syslog/MessageEncoder.java similarity index 100% rename from event-common/src/main/java/org/xbib/event/syslog/MessageEncoder.java rename to event-syslog/src/main/java/org/xbib/event/syslog/MessageEncoder.java diff --git a/event-common/src/main/java/org/xbib/event/syslog/MessageKey.java b/event-syslog/src/main/java/org/xbib/event/syslog/MessageKey.java similarity index 100% rename from event-common/src/main/java/org/xbib/event/syslog/MessageKey.java rename to event-syslog/src/main/java/org/xbib/event/syslog/MessageKey.java diff --git a/event-common/src/main/java/org/xbib/event/syslog/MessageParser.java b/event-syslog/src/main/java/org/xbib/event/syslog/MessageParser.java similarity index 100% rename from event-common/src/main/java/org/xbib/event/syslog/MessageParser.java rename to event-syslog/src/main/java/org/xbib/event/syslog/MessageParser.java diff --git a/event-common/src/main/java/org/xbib/event/syslog/MessageType.java b/event-syslog/src/main/java/org/xbib/event/syslog/MessageType.java similarity index 100% rename from event-common/src/main/java/org/xbib/event/syslog/MessageType.java rename to event-syslog/src/main/java/org/xbib/event/syslog/MessageType.java diff --git a/event-common/src/main/java/org/xbib/event/syslog/Priority.java b/event-syslog/src/main/java/org/xbib/event/syslog/Priority.java similarity index 100% rename from event-common/src/main/java/org/xbib/event/syslog/Priority.java rename to event-syslog/src/main/java/org/xbib/event/syslog/Priority.java diff --git a/event-common/src/main/java/org/xbib/event/syslog/RFC3164MessageParser.java b/event-syslog/src/main/java/org/xbib/event/syslog/RFC3164MessageParser.java similarity index 100% rename from event-common/src/main/java/org/xbib/event/syslog/RFC3164MessageParser.java rename to event-syslog/src/main/java/org/xbib/event/syslog/RFC3164MessageParser.java diff --git a/event-common/src/main/java/org/xbib/event/syslog/RFC5424MessageParser.java b/event-syslog/src/main/java/org/xbib/event/syslog/RFC5424MessageParser.java similarity index 100% rename from event-common/src/main/java/org/xbib/event/syslog/RFC5424MessageParser.java rename to event-syslog/src/main/java/org/xbib/event/syslog/RFC5424MessageParser.java diff --git a/event-common/src/main/java/org/xbib/event/syslog/SyslogEventManager.java b/event-syslog/src/main/java/org/xbib/event/syslog/SyslogEventManagerService.java similarity index 65% rename from event-common/src/main/java/org/xbib/event/syslog/SyslogEventManager.java rename to event-syslog/src/main/java/org/xbib/event/syslog/SyslogEventManagerService.java index f667c5d..0e55cdf 100644 --- a/event-common/src/main/java/org/xbib/event/syslog/SyslogEventManager.java +++ b/event-syslog/src/main/java/org/xbib/event/syslog/SyslogEventManagerService.java @@ -1,6 +1,7 @@ package org.xbib.event.syslog; -import org.xbib.event.bus.AsyncEventBus; +import org.xbib.event.bus.EventBus; +import org.xbib.event.common.EventManagerService; import org.xbib.settings.Settings; import java.io.Closeable; @@ -8,16 +9,22 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.concurrent.ExecutorService; import java.util.logging.Level; import java.util.logging.Logger; -public class SyslogEventManager implements Closeable { - private static final Logger logger = Logger.getLogger(SyslogEventManager.class.getName()); +public class SyslogEventManagerService implements EventManagerService, Closeable { + private static final Logger logger = Logger.getLogger(SyslogEventManagerService.class.getName()); - private final List syslogServices; + private List syslogServices; - public SyslogEventManager(Settings settings, - AsyncEventBus eventBus) { + public SyslogEventManagerService() {} + + @Override + public SyslogEventManagerService init(Settings settings, + EventBus eventBus, + ClassLoader classLoader, + ExecutorService executorService) { this.syslogServices = new ArrayList<>(); for (Map.Entry entry : settings.getGroups("event.syslog").entrySet()) { Settings definition = entry.getValue(); @@ -31,6 +38,7 @@ public class SyslogEventManager implements Closeable { } } } + return this; } @Override diff --git a/event-common/src/main/java/org/xbib/event/syslog/SyslogFrameDecoder.java b/event-syslog/src/main/java/org/xbib/event/syslog/SyslogFrameDecoder.java similarity index 100% rename from event-common/src/main/java/org/xbib/event/syslog/SyslogFrameDecoder.java rename to event-syslog/src/main/java/org/xbib/event/syslog/SyslogFrameDecoder.java diff --git a/event-common/src/main/java/org/xbib/event/syslog/SyslogIdleStateHandler.java b/event-syslog/src/main/java/org/xbib/event/syslog/SyslogIdleStateHandler.java similarity index 100% rename from event-common/src/main/java/org/xbib/event/syslog/SyslogIdleStateHandler.java rename to event-syslog/src/main/java/org/xbib/event/syslog/SyslogIdleStateHandler.java diff --git a/event-common/src/main/java/org/xbib/event/syslog/SyslogMessage.java b/event-syslog/src/main/java/org/xbib/event/syslog/SyslogMessage.java similarity index 100% rename from event-common/src/main/java/org/xbib/event/syslog/SyslogMessage.java rename to event-syslog/src/main/java/org/xbib/event/syslog/SyslogMessage.java diff --git a/event-common/src/main/java/org/xbib/event/syslog/SyslogMessageHandler.java b/event-syslog/src/main/java/org/xbib/event/syslog/SyslogMessageHandler.java similarity index 100% rename from event-common/src/main/java/org/xbib/event/syslog/SyslogMessageHandler.java rename to event-syslog/src/main/java/org/xbib/event/syslog/SyslogMessageHandler.java diff --git a/event-common/src/main/java/org/xbib/event/syslog/SyslogMessageKey.java b/event-syslog/src/main/java/org/xbib/event/syslog/SyslogMessageKey.java similarity index 100% rename from event-common/src/main/java/org/xbib/event/syslog/SyslogMessageKey.java rename to event-syslog/src/main/java/org/xbib/event/syslog/SyslogMessageKey.java diff --git a/event-common/src/main/java/org/xbib/event/syslog/SyslogRequest.java b/event-syslog/src/main/java/org/xbib/event/syslog/SyslogRequest.java similarity index 100% rename from event-common/src/main/java/org/xbib/event/syslog/SyslogRequest.java rename to event-syslog/src/main/java/org/xbib/event/syslog/SyslogRequest.java diff --git a/event-common/src/main/java/org/xbib/event/syslog/SyslogService.java b/event-syslog/src/main/java/org/xbib/event/syslog/SyslogService.java similarity index 100% rename from event-common/src/main/java/org/xbib/event/syslog/SyslogService.java rename to event-syslog/src/main/java/org/xbib/event/syslog/SyslogService.java diff --git a/event-common/src/main/java/org/xbib/event/syslog/TCPSyslogMessageDecoder.java b/event-syslog/src/main/java/org/xbib/event/syslog/TCPSyslogMessageDecoder.java similarity index 100% rename from event-common/src/main/java/org/xbib/event/syslog/TCPSyslogMessageDecoder.java rename to event-syslog/src/main/java/org/xbib/event/syslog/TCPSyslogMessageDecoder.java diff --git a/event-common/src/main/java/org/xbib/event/syslog/UDPSyslogMessageDecoder.java b/event-syslog/src/main/java/org/xbib/event/syslog/UDPSyslogMessageDecoder.java similarity index 100% rename from event-common/src/main/java/org/xbib/event/syslog/UDPSyslogMessageDecoder.java rename to event-syslog/src/main/java/org/xbib/event/syslog/UDPSyslogMessageDecoder.java diff --git a/event-syslog/src/main/resources/META-INF/services/org.xbib.event.common.EventManagerService b/event-syslog/src/main/resources/META-INF/services/org.xbib.event.common.EventManagerService new file mode 100644 index 0000000..f51b17a --- /dev/null +++ b/event-syslog/src/main/resources/META-INF/services/org.xbib.event.common.EventManagerService @@ -0,0 +1 @@ +org.xbib.event.syslog.SyslogEventManagerService \ No newline at end of file diff --git a/event-common/src/test/java/org/xbib/event/syslog/SyslogServiceTest.java b/event-syslog/src/test/java/org/xbib/event/syslog/test/SyslogServiceTest.java similarity index 77% rename from event-common/src/test/java/org/xbib/event/syslog/SyslogServiceTest.java rename to event-syslog/src/test/java/org/xbib/event/syslog/test/SyslogServiceTest.java index 0ab3f81..b4cf5ea 100644 --- a/event-common/src/test/java/org/xbib/event/syslog/SyslogServiceTest.java +++ b/event-syslog/src/test/java/org/xbib/event/syslog/test/SyslogServiceTest.java @@ -1,6 +1,7 @@ -package org.xbib.event.syslog; +package org.xbib.event.syslog.test; import org.junit.jupiter.api.Test; +import org.xbib.event.syslog.SyslogService; import org.xbib.settings.Settings; import java.io.IOException; @@ -14,7 +15,7 @@ public class SyslogServiceTest { .build(); SyslogService syslogService = new SyslogService(settings, null); syslogService.startTcp(); - Thread.sleep(60000L); + Thread.sleep(60000L); // send a syslog from console syslogService.close(); } } diff --git a/gradle.properties b/gradle.properties index a3444c9..56c2845 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ group = org.xbib name = event -version = 0.1.0 +version = 0.2.0 diff --git a/settings.gradle b/settings.gradle index 314e9a0..3e051ae 100644 --- a/settings.gradle +++ b/settings.gradle @@ -45,3 +45,4 @@ include 'event-async' include 'event-common' include 'event-loop' include 'event-net-http' +include 'event-syslog'