From: Mathieu Baudier Date: Mon, 4 Mar 2024 18:23:04 +0000 (+0100) Subject: Make publication of thin logger more robust X-Git-Tag: v2.3.28~36 X-Git-Url: https://git.argeo.org/?p=lgpl%2Fargeo-commons.git;a=commitdiff_plain;h=a63b0d9025acf74e96dfd2e462b4c4d5a534d74e Make publication of thin logger more robust --- diff --git a/org.argeo.init/src/org/argeo/init/logging/ThinLoggerFinder.java b/org.argeo.init/src/org/argeo/init/logging/ThinLoggerFinder.java index e60d22fba..5be74fef9 100644 --- a/org.argeo.init/src/org/argeo/init/logging/ThinLoggerFinder.java +++ b/org.argeo.init/src/org/argeo/init/logging/ThinLoggerFinder.java @@ -7,14 +7,17 @@ import java.util.HashMap; import java.util.Map; import java.util.Objects; import java.util.concurrent.Flow; +import java.util.concurrent.Flow.Publisher; import java.util.function.Consumer; +import java.util.function.Supplier; /** * Factory for Java system logging. As it has to be a public class in order to * be exposed as a service provider, it is also the main entry point for the * thin logging system, via static methos. */ -public class ThinLoggerFinder extends LoggerFinder { +public class ThinLoggerFinder extends LoggerFinder + implements Consumer>, Supplier>> { private static ThinLogging logging; private static ThinJavaUtilLogging javaUtilLogging; @@ -64,16 +67,21 @@ public class ThinLoggerFinder extends LoggerFinder { javaUtilLogging.readConfiguration(logging.getLevels()); } - public static Consumer> getConfigurationConsumer() { + static Consumer> getConfigurationConsumer() { Objects.requireNonNull(logging); return logging; } - public static Flow.Publisher> getLogEntryPublisher() { + static Flow.Publisher> getLogEntryPublisher() { Objects.requireNonNull(logging); return logging.getLogEntryPublisher(); } + @Override + public Publisher> get() { + return getLogEntryPublisher(); + } + static void update(Map configuration) { if (logging == null) throw new IllegalStateException("Thin logging must be initialized first"); @@ -85,4 +93,17 @@ public class ThinLoggerFinder extends LoggerFinder { static Logger getLogger(String name) { return logging.getLogger(name, null); } + + @Override + public void accept(Map t) { + if (logging != null) { + // delegate to thin logging + logging.accept(t); + } else { + // ignore + // TODO try to congure Java logging ? + } + + } + } diff --git a/org.argeo.init/src/org/argeo/init/osgi/OsgiRuntimeContext.java b/org.argeo.init/src/org/argeo/init/osgi/OsgiRuntimeContext.java index 186577b4d..8afe05922 100644 --- a/org.argeo.init/src/org/argeo/init/osgi/OsgiRuntimeContext.java +++ b/org.argeo.init/src/org/argeo/init/osgi/OsgiRuntimeContext.java @@ -1,5 +1,6 @@ package org.argeo.init.osgi; +import java.io.Serializable; import java.lang.System.LoggerFinder; import java.util.Collections; import java.util.Hashtable; @@ -9,9 +10,9 @@ import java.util.Optional; import java.util.ServiceLoader; import java.util.concurrent.Flow; import java.util.function.Consumer; +import java.util.function.Supplier; import org.argeo.init.RuntimeContext; -import org.argeo.init.logging.ThinLoggerFinder; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.BundleException; @@ -63,14 +64,22 @@ public class OsgiRuntimeContext implements RuntimeContext, AutoCloseable { // bundleContext.registerService(AbstractPreferences.class, systemRootPreferences, new Hashtable<>()); // Make sure LoggerFinder has been searched for, since it is lazily loaded - LoggerFinder.getLoggerFinder(); - - // logging - bundleContext.registerService(Consumer.class, ThinLoggerFinder.getConfigurationConsumer(), - new Hashtable<>(Collections.singletonMap(Constants.SERVICE_PID, "argeo.logging.configuration"))); - bundleContext.registerService(Flow.Publisher.class, ThinLoggerFinder.getLogEntryPublisher(), - new Hashtable<>(Collections.singletonMap(Constants.SERVICE_PID, "argeo.logging.publisher"))); - + LoggerFinder loggerFinder = LoggerFinder.getLoggerFinder(); + + if (loggerFinder instanceof Consumer && loggerFinder instanceof Supplier) { + @SuppressWarnings("unchecked") + Consumer> consumer = (Consumer>) loggerFinder; + // ThinLoggerFinder.getConfigurationConsumer() + // ThinLoggerFinder.getLogEntryPublisher() + + @SuppressWarnings("unchecked") + Supplier>> supplier = (Supplier>>) loggerFinder; + // logging + bundleContext.registerService(Consumer.class, consumer, + new Hashtable<>(Collections.singletonMap(Constants.SERVICE_PID, "argeo.logging.configuration"))); + bundleContext.registerService(Flow.Publisher.class, supplier.get(), + new Hashtable<>(Collections.singletonMap(Constants.SERVICE_PID, "argeo.logging.publisher"))); + } osgiBoot = new OsgiBoot(bundleContext); osgiBoot.bootstrap(config);