2 * Sone - PluginStoreConfigurationBackend.java - Copyright © 2010–2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.freenet;
20 import java.util.logging.Logger;
22 import net.pterodactylus.util.config.AttributeNotFoundException;
23 import net.pterodactylus.util.config.Configuration;
24 import net.pterodactylus.util.config.ConfigurationException;
25 import net.pterodactylus.util.config.ExtendedConfigurationBackend;
26 import net.pterodactylus.util.logging.Logging;
27 import freenet.client.async.DatabaseDisabledException;
28 import freenet.pluginmanager.PluginRespirator;
29 import freenet.pluginmanager.PluginStore;
32 * Backend for a {@link Configuration} that is based on a {@link PluginStore}.
34 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36 public class PluginStoreConfigurationBackend implements ExtendedConfigurationBackend {
39 @SuppressWarnings("unused")
40 private static final Logger logger = Logging.getLogger(PluginStoreConfigurationBackend.class);
42 /** The plugin respirator. */
43 private final PluginRespirator pluginRespirator;
45 /** The backing plugin store. */
46 private final PluginStore pluginStore;
49 * Creates a new configuration backend based on a plugin store.
51 * @param pluginRespirator
52 * The plugin respirator
53 * @throws DatabaseDisabledException
54 * if the plugin store is not available
56 public PluginStoreConfigurationBackend(PluginRespirator pluginRespirator) throws DatabaseDisabledException {
57 this.pluginRespirator = pluginRespirator;
58 this.pluginStore = pluginRespirator.getStore();
59 if (this.pluginStore == null) {
60 throw new DatabaseDisabledException();
65 public String getValue(String attribute) throws ConfigurationException {
66 if (!pluginStore.strings.containsKey(attribute)) {
67 throw new AttributeNotFoundException(attribute);
69 return pluginStore.strings.get(attribute);
73 public void putValue(String attribute, String value) throws ConfigurationException {
74 pluginStore.strings.put(attribute, value);
79 public Boolean getBooleanValue(String attribute) throws ConfigurationException {
80 if (!pluginStore.booleans.containsKey(attribute)) {
81 throw new AttributeNotFoundException(attribute);
83 return pluginStore.booleans.get(attribute);
87 public void setBooleanValue(String attribute, Boolean value) throws ConfigurationException {
88 pluginStore.booleans.put(attribute, value);
93 public Double getDoubleValue(String attribute) throws ConfigurationException {
94 if (!pluginStore.strings.containsKey(attribute)) {
95 throw new AttributeNotFoundException(attribute);
97 String stringValue = pluginStore.strings.get(attribute);
98 if (stringValue == null) {
102 return Double.valueOf(pluginStore.strings.get(attribute));
103 } catch (NumberFormatException nfe1) {
104 throw new ConfigurationException("Could not parse “" + stringValue + "”.", nfe1);
109 public void setDoubleValue(String attribute, Double value) throws ConfigurationException {
110 pluginStore.strings.put(attribute, String.valueOf(value));
115 public Integer getIntegerValue(String attribute) throws ConfigurationException {
116 if (!pluginStore.integers.containsKey(attribute)) {
117 throw new AttributeNotFoundException(attribute);
119 return pluginStore.integers.get(attribute);
123 public void setIntegerValue(String attribute, Integer value) throws ConfigurationException {
124 pluginStore.integers.put(attribute, value);
129 public Long getLongValue(String attribute) throws ConfigurationException {
130 if (!pluginStore.longs.containsKey(attribute)) {
131 throw new AttributeNotFoundException(attribute);
133 return pluginStore.longs.get(attribute);
137 public void setLongValue(String attribute, Long value) throws ConfigurationException {
138 pluginStore.longs.put(attribute, value);
143 public void save() throws ConfigurationException {
145 pluginRespirator.putStore(pluginStore);
146 } catch (DatabaseDisabledException dde1) {
147 throw new ConfigurationException("Could not store plugin store, database is disabled.", dde1);