From d26a4ba8f367608590167fb4a866f1f0202ad1aa Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Tue, 28 Jun 2022 08:13:06 +0200 Subject: [PATCH] Experiment with Java preferences. --- .../argeo/init/osgi/OsgiRuntimeContext.java | 16 ++-- .../init/prefs/SystemRootPreferences.java | 83 +++++++++++++++++++ .../init/prefs/ThinPreferencesFactory.java | 42 ++++++++++ 3 files changed, 131 insertions(+), 10 deletions(-) create mode 100644 org.argeo.init/src/org/argeo/init/prefs/SystemRootPreferences.java create mode 100644 org.argeo.init/src/org/argeo/init/prefs/ThinPreferencesFactory.java 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 64c588d19..186577b4d 100644 --- a/org.argeo.init/src/org/argeo/init/osgi/OsgiRuntimeContext.java +++ b/org.argeo.init/src/org/argeo/init/osgi/OsgiRuntimeContext.java @@ -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 loggingConfigurationSr; - @SuppressWarnings("rawtypes") - private ServiceRegistration 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 index 000000000..202100a49 --- /dev/null +++ b/org.argeo.init/src/org/argeo/init/prefs/SystemRootPreferences.java @@ -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 { + private CompletableFuture 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 index 000000000..8b3356c9c --- /dev/null +++ b/org.argeo.init/src/org/argeo/init/prefs/ThinPreferencesFactory.java @@ -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 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); + } + } +} -- 2.30.2