Experiment with Java preferences.
authorMathieu Baudier <mbaudier@argeo.org>
Tue, 28 Jun 2022 06:13:06 +0000 (08:13 +0200)
committerMathieu Baudier <mbaudier@argeo.org>
Tue, 28 Jun 2022 06:13:06 +0000 (08:13 +0200)
org.argeo.init/src/org/argeo/init/osgi/OsgiRuntimeContext.java
org.argeo.init/src/org/argeo/init/prefs/SystemRootPreferences.java [new file with mode: 0644]
org.argeo.init/src/org/argeo/init/prefs/ThinPreferencesFactory.java [new file with mode: 0644]

index 64c588d19a347a0ac90204fc36faa336d59eff97..186577b4dba40dda3ee7169b0d2bb4c863489c10 100644 (file)
@@ -16,7 +16,6 @@ import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleException;
 import org.osgi.framework.Constants;
-import org.osgi.framework.ServiceRegistration;
 import org.osgi.framework.launch.Framework;
 import org.osgi.framework.launch.FrameworkFactory;
 
@@ -26,11 +25,6 @@ public class OsgiRuntimeContext implements RuntimeContext, AutoCloseable {
        private Framework framework;
        private OsgiBoot osgiBoot;
 
-       @SuppressWarnings("rawtypes")
-       private ServiceRegistration<Consumer> loggingConfigurationSr;
-       @SuppressWarnings("rawtypes")
-       private ServiceRegistration<Flow.Publisher> logEntryPublisherSr;
-
        /**
         * Constructor to use when the runtime context will create the OSGi
         * {@link Framework}.
@@ -64,15 +58,17 @@ public class OsgiRuntimeContext implements RuntimeContext, AutoCloseable {
        }
 
        public void start(BundleContext bundleContext) {
+               // preferences
+//             SystemRootPreferences systemRootPreferences = ThinPreferencesFactory.getInstance().getSystemRootPreferences();
+//             bundleContext.registerService(AbstractPreferences.class, systemRootPreferences, new Hashtable<>());
+
                // Make sure LoggerFinder has been searched for, since it is lazily loaded
                LoggerFinder.getLoggerFinder();
 
                // logging
-               loggingConfigurationSr = bundleContext.registerService(Consumer.class,
-                               ThinLoggerFinder.getConfigurationConsumer(),
+               bundleContext.registerService(Consumer.class, ThinLoggerFinder.getConfigurationConsumer(),
                                new Hashtable<>(Collections.singletonMap(Constants.SERVICE_PID, "argeo.logging.configuration")));
-               logEntryPublisherSr = bundleContext.registerService(Flow.Publisher.class,
-                               ThinLoggerFinder.getLogEntryPublisher(),
+               bundleContext.registerService(Flow.Publisher.class, ThinLoggerFinder.getLogEntryPublisher(),
                                new Hashtable<>(Collections.singletonMap(Constants.SERVICE_PID, "argeo.logging.publisher")));
 
                osgiBoot = new OsgiBoot(bundleContext);
diff --git a/org.argeo.init/src/org/argeo/init/prefs/SystemRootPreferences.java b/org.argeo.init/src/org/argeo/init/prefs/SystemRootPreferences.java
new file mode 100644 (file)
index 0000000..202100a
--- /dev/null
@@ -0,0 +1,83 @@
+package org.argeo.init.prefs;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.function.Consumer;
+import java.util.prefs.AbstractPreferences;
+import java.util.prefs.BackingStoreException;
+
+public class SystemRootPreferences extends AbstractPreferences implements Consumer<AbstractPreferences> {
+       private CompletableFuture<AbstractPreferences> singleChild;
+
+       protected SystemRootPreferences() {
+               super(null, "");
+       }
+
+       @Override
+       public void accept(AbstractPreferences t) {
+               this.singleChild.complete(t);
+       }
+
+       /*
+        * ABSTRACT PREFERENCES
+        */
+
+       @Override
+       protected void putSpi(String key, String value) {
+               throw new UnsupportedOperationException();
+       }
+
+       @Override
+       protected String getSpi(String key) {
+               throw new UnsupportedOperationException();
+       }
+
+       @Override
+       protected void removeSpi(String key) {
+               throw new UnsupportedOperationException();
+       }
+
+       @Override
+       protected void removeNodeSpi() throws BackingStoreException {
+               throw new UnsupportedOperationException();
+       }
+
+       @Override
+       protected String[] keysSpi() throws BackingStoreException {
+               return new String[0];
+       }
+
+       /** Will block. */
+       @Override
+       protected String[] childrenNamesSpi() throws BackingStoreException {
+               String childName;
+               try {
+                       childName = singleChild.get().name();
+               } catch (InterruptedException | ExecutionException e) {
+                       throw new IllegalStateException("Cannot get child preferences name", e);
+               }
+               return new String[] { childName };
+       }
+
+       @Override
+       protected AbstractPreferences childSpi(String name) {
+               String childName;
+               try {
+                       childName = singleChild.get().name();
+               } catch (InterruptedException | ExecutionException e) {
+                       throw new IllegalStateException("Cannot get child preferences name", e);
+               }
+               if (!childName.equals(name))
+                       throw new IllegalArgumentException("Child name is " + childName + ", not " + name);
+               return null;
+       }
+
+       @Override
+       protected void syncSpi() throws BackingStoreException {
+       }
+
+       @Override
+       protected void flushSpi() throws BackingStoreException {
+       }
+
+}
diff --git a/org.argeo.init/src/org/argeo/init/prefs/ThinPreferencesFactory.java b/org.argeo.init/src/org/argeo/init/prefs/ThinPreferencesFactory.java
new file mode 100644 (file)
index 0000000..8b3356c
--- /dev/null
@@ -0,0 +1,42 @@
+package org.argeo.init.prefs;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.prefs.Preferences;
+import java.util.prefs.PreferencesFactory;
+
+public class ThinPreferencesFactory implements PreferencesFactory {
+       private static CompletableFuture<ThinPreferencesFactory> INSTANCE = new CompletableFuture<>();
+
+       private SystemRootPreferences systemRootPreferences;
+
+       public ThinPreferencesFactory() {
+               systemRootPreferences = new SystemRootPreferences();
+               if (INSTANCE.isDone())
+                       throw new IllegalStateException(
+                                       "There is already a " + ThinPreferencesFactory.class.getName() + " instance.");
+               INSTANCE.complete(this);
+       }
+
+       @Override
+       public Preferences systemRoot() {
+               return systemRootPreferences;
+       }
+
+       @Override
+       public Preferences userRoot() {
+               throw new UnsupportedOperationException();
+       }
+
+       public SystemRootPreferences getSystemRootPreferences() {
+               return systemRootPreferences;
+       }
+
+       public static ThinPreferencesFactory getInstance() {
+               try {
+                       return INSTANCE.get();
+               } catch (InterruptedException | ExecutionException e) {
+                       throw new IllegalStateException("Cannot get " + ThinPreferencesFactory.class.getName() + " instance.", e);
+               }
+       }
+}