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();
68 public String getValue(String attribute) throws ConfigurationException {
69 if (!pluginStore.strings.containsKey(attribute)) {
70 throw new AttributeNotFoundException(attribute);
72 return pluginStore.strings.get(attribute);
79 public void putValue(String attribute, String value) throws ConfigurationException {
80 pluginStore.strings.put(attribute, value);
88 public Boolean getBooleanValue(String attribute) throws ConfigurationException {
89 if (!pluginStore.booleans.containsKey(attribute)) {
90 throw new AttributeNotFoundException(attribute);
92 return pluginStore.booleans.get(attribute);
99 public void setBooleanValue(String attribute, Boolean value) throws ConfigurationException {
100 pluginStore.booleans.put(attribute, value);
108 public Double getDoubleValue(String attribute) throws ConfigurationException {
109 if (!pluginStore.strings.containsKey(attribute)) {
110 throw new AttributeNotFoundException(attribute);
112 String stringValue = pluginStore.strings.get(attribute);
113 if (stringValue == null) {
117 return Double.valueOf(pluginStore.strings.get(attribute));
118 } catch (NumberFormatException nfe1) {
119 throw new ConfigurationException("Could not parse “" + stringValue + "”.", nfe1);
127 public void setDoubleValue(String attribute, Double value) throws ConfigurationException {
128 pluginStore.strings.put(attribute, String.valueOf(value));
136 public Integer getIntegerValue(String attribute) throws ConfigurationException {
137 if (!pluginStore.integers.containsKey(attribute)) {
138 throw new AttributeNotFoundException(attribute);
140 return pluginStore.integers.get(attribute);
147 public void setIntegerValue(String attribute, Integer value) throws ConfigurationException {
148 pluginStore.integers.put(attribute, value);
156 public Long getLongValue(String attribute) throws ConfigurationException {
157 if (!pluginStore.longs.containsKey(attribute)) {
158 throw new AttributeNotFoundException(attribute);
160 return pluginStore.longs.get(attribute);
167 public void setLongValue(String attribute, Long value) throws ConfigurationException {
168 pluginStore.longs.put(attribute, value);
176 public void save() throws ConfigurationException {
178 pluginRespirator.putStore(pluginStore);
179 } catch (DatabaseDisabledException dde1) {
180 throw new ConfigurationException("Could not store plugin store, database is disabled.", dde1);