X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ffreenet%2FPluginStoreConfigurationBackend.java;h=a8a269743b6fe7eb5ab760408d8aa8a01cb230af;hp=e942b196df7bd5a8944a1bb5769f4d6aed3f1e25;hb=438378deab1514f0f608d975ef65f5b7aea44ccb;hpb=808da54b27184b195d784b37e107fa009dc3ea62 diff --git a/src/main/java/net/pterodactylus/sone/freenet/PluginStoreConfigurationBackend.java b/src/main/java/net/pterodactylus/sone/freenet/PluginStoreConfigurationBackend.java index e942b19..a8a2697 100644 --- a/src/main/java/net/pterodactylus/sone/freenet/PluginStoreConfigurationBackend.java +++ b/src/main/java/net/pterodactylus/sone/freenet/PluginStoreConfigurationBackend.java @@ -1,5 +1,5 @@ /* - * FreenetSone - PluginStoreConfigurationBackend.java - Copyright © 2010 David Roden + * Sone - PluginStoreConfigurationBackend.java - Copyright © 2010–2020 David Roden * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -17,29 +17,44 @@ package net.pterodactylus.sone.freenet; +import static java.util.logging.Logger.getLogger; + +import java.util.logging.Logger; + +import net.pterodactylus.util.config.AttributeNotFoundException; import net.pterodactylus.util.config.Configuration; import net.pterodactylus.util.config.ConfigurationException; import net.pterodactylus.util.config.ExtendedConfigurationBackend; +import freenet.client.async.PersistenceDisabledException; +import freenet.pluginmanager.PluginRespirator; import freenet.pluginmanager.PluginStore; /** * Backend for a {@link Configuration} that is based on a {@link PluginStore}. - * - * @author David ‘Bombe’ Roden */ public class PluginStoreConfigurationBackend implements ExtendedConfigurationBackend { + /** The logger. */ + @SuppressWarnings("unused") + private static final Logger logger = getLogger(PluginStoreConfigurationBackend.class.getName()); + + /** The plugin respirator. */ + private final PluginRespirator pluginRespirator; + /** The backing plugin store. */ private final PluginStore pluginStore; /** * Creates a new configuration backend based on a plugin store. * - * @param pluginStore - * The backing plugin store + * @param pluginRespirator + * The plugin respirator + * @throws PersistenceDisabledException + * if the plugin store is not available */ - public PluginStoreConfigurationBackend(PluginStore pluginStore) { - this.pluginStore = pluginStore; + public PluginStoreConfigurationBackend(PluginRespirator pluginRespirator) throws PersistenceDisabledException { + this.pluginRespirator = pluginRespirator; + this.pluginStore = pluginRespirator.getStore(); } /** @@ -47,6 +62,9 @@ public class PluginStoreConfigurationBackend implements ExtendedConfigurationBac */ @Override public String getValue(String attribute) throws ConfigurationException { + if (!pluginStore.strings.containsKey(attribute)) { + throw new AttributeNotFoundException(attribute); + } return pluginStore.strings.get(attribute); } @@ -56,6 +74,7 @@ public class PluginStoreConfigurationBackend implements ExtendedConfigurationBac @Override public void putValue(String attribute, String value) throws ConfigurationException { pluginStore.strings.put(attribute, value); + save(); } /** @@ -63,6 +82,9 @@ public class PluginStoreConfigurationBackend implements ExtendedConfigurationBac */ @Override public Boolean getBooleanValue(String attribute) throws ConfigurationException { + if (!pluginStore.booleans.containsKey(attribute)) { + throw new AttributeNotFoundException(attribute); + } return pluginStore.booleans.get(attribute); } @@ -72,6 +94,7 @@ public class PluginStoreConfigurationBackend implements ExtendedConfigurationBac @Override public void setBooleanValue(String attribute, Boolean value) throws ConfigurationException { pluginStore.booleans.put(attribute, value); + save(); } /** @@ -79,6 +102,9 @@ public class PluginStoreConfigurationBackend implements ExtendedConfigurationBac */ @Override public Double getDoubleValue(String attribute) throws ConfigurationException { + if (!pluginStore.strings.containsKey(attribute)) { + throw new AttributeNotFoundException(attribute); + } String stringValue = pluginStore.strings.get(attribute); if (stringValue == null) { return null; @@ -96,6 +122,7 @@ public class PluginStoreConfigurationBackend implements ExtendedConfigurationBac @Override public void setDoubleValue(String attribute, Double value) throws ConfigurationException { pluginStore.strings.put(attribute, String.valueOf(value)); + save(); } /** @@ -103,6 +130,9 @@ public class PluginStoreConfigurationBackend implements ExtendedConfigurationBac */ @Override public Integer getIntegerValue(String attribute) throws ConfigurationException { + if (!pluginStore.integers.containsKey(attribute)) { + throw new AttributeNotFoundException(attribute); + } return pluginStore.integers.get(attribute); } @@ -112,6 +142,7 @@ public class PluginStoreConfigurationBackend implements ExtendedConfigurationBac @Override public void setIntegerValue(String attribute, Integer value) throws ConfigurationException { pluginStore.integers.put(attribute, value); + save(); } /** @@ -119,6 +150,9 @@ public class PluginStoreConfigurationBackend implements ExtendedConfigurationBac */ @Override public Long getLongValue(String attribute) throws ConfigurationException { + if (!pluginStore.longs.containsKey(attribute)) { + throw new AttributeNotFoundException(attribute); + } return pluginStore.longs.get(attribute); } @@ -128,6 +162,19 @@ public class PluginStoreConfigurationBackend implements ExtendedConfigurationBac @Override public void setLongValue(String attribute, Long value) throws ConfigurationException { pluginStore.longs.put(attribute, value); + save(); + } + + /** + * {@inheritDoc} + */ + @Override + public void save() throws ConfigurationException { + try { + pluginRespirator.putStore(pluginStore); + } catch (PersistenceDisabledException pde1) { + throw new ConfigurationException("Could not store plugin store, persistence is disabled.", pde1); + } } }