Improve build and local deployment
[lgpl/argeo-commons.git] / org.argeo.init / src / org / argeo / init / logging / ThinLoggerFinder.java
index 4147534dd2695abcdc393f7c988ef49965ddc4df..3fa2bc868848baa2ba1528cdd189e4f958a137c6 100644 (file)
@@ -1,35 +1,51 @@
 package org.argeo.init.logging;
 
+import java.io.Serializable;
 import java.lang.System.Logger;
 import java.lang.System.LoggerFinder;
 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<Map<String, Object>>, Supplier<Flow.Publisher<Map<String, Serializable>>> {
        private static ThinLogging logging;
        private static ThinJavaUtilLogging javaUtilLogging;
 
        public ThinLoggerFinder() {
                if (logging != null)
                        throw new IllegalStateException("Only one logging can be initialised.");
-               init();
+//             init();
        }
 
        @Override
        public Logger getLogger(String name, Module module) {
-               return logging.getLogger(name, module);
+               lazyInit();
+               Objects.requireNonNull(name);
+               Logger logger = logging.getLogger(name, module);
+               Objects.requireNonNull(logger);
+               return logger;
        }
 
        private static void init() {
                logging = new ThinLogging();
+               reloadConfiguration();
+       }
 
-               Map<String, String> configuration = new HashMap<>();
+       /** Reload configuration form system properties */
+       public static void reloadConfiguration() {
+               if (logging == null)
+                       return;
+               Map<String, Object> configuration = new HashMap<>();
                for (Object key : System.getProperties().keySet()) {
                        Objects.requireNonNull(key);
                        String property = key.toString();
@@ -37,11 +53,12 @@ public class ThinLoggerFinder extends LoggerFinder {
                                        || property.equals(ThinLogging.DEFAULT_LEVEL_PROPERTY))
                                configuration.put(property, System.getProperty(property));
                }
-               logging.update(configuration);
+               logging.accept(configuration);
        }
 
        /**
-        * Falls back to java.util.logging if thin logging was not already initialised.
+        * Falls back to java.util.logging if thin logging was not already initialised
+        * by the {@link LoggerFinder} mechanism.
         */
        public static void lazyInit() {
                if (logging != null)
@@ -53,10 +70,25 @@ public class ThinLoggerFinder extends LoggerFinder {
                javaUtilLogging.readConfiguration(logging.getLevels());
        }
 
-       public static void update(Map<String, String> configuration) {
+       static Consumer<Map<String, Object>> getConfigurationConsumer() {
+               Objects.requireNonNull(logging);
+               return logging;
+       }
+
+       static Flow.Publisher<Map<String, Serializable>> getLogEntryPublisher() {
+               Objects.requireNonNull(logging);
+               return logging.getLogEntryPublisher();
+       }
+
+       @Override
+       public Publisher<Map<String, Serializable>> get() {
+               return getLogEntryPublisher();
+       }
+
+       static void update(Map<String, Object> configuration) {
                if (logging == null)
                        throw new IllegalStateException("Thin logging must be initialized first");
-               logging.update(configuration);
+               logging.accept(configuration);
                if (javaUtilLogging != null)
                        javaUtilLogging.readConfiguration(logging.getLevels());
        }
@@ -64,4 +96,17 @@ public class ThinLoggerFinder extends LoggerFinder {
        static Logger getLogger(String name) {
                return logging.getLogger(name, null);
        }
+
+       @Override
+       public void accept(Map<String, Object> t) {
+               if (logging != null) {
+                       // delegate to thin logging
+                       logging.accept(t);
+               } else {
+                       // ignore
+                       // TODO try to congure Java logging ?
+               }
+
+       }
+
 }