2 * FreenetSone - PluginStoreConfigurationBackend.java - Copyright © 2010 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.Level;
21 import java.util.logging.Logger;
23 import net.pterodactylus.util.config.AttributeNotFoundException;
24 import net.pterodactylus.util.config.Configuration;
25 import net.pterodactylus.util.config.ConfigurationException;
26 import net.pterodactylus.util.config.ExtendedConfigurationBackend;
27 import net.pterodactylus.util.logging.Logging;
28 import freenet.client.async.DatabaseDisabledException;
29 import freenet.pluginmanager.PluginRespirator;
30 import freenet.pluginmanager.PluginStore;
33 * Backend for a {@link Configuration} that is based on a {@link PluginStore}.
35 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37 public class PluginStoreConfigurationBackend implements ExtendedConfigurationBackend {
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();
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);
76 public void putValue(String attribute, String value) throws ConfigurationException {
77 pluginStore.strings.put(attribute, value);
85 public Boolean getBooleanValue(String attribute) throws ConfigurationException {
86 if (!pluginStore.booleans.containsKey(attribute)) {
87 throw new AttributeNotFoundException(attribute);
89 return pluginStore.booleans.get(attribute);
96 public void setBooleanValue(String attribute, Boolean value) throws ConfigurationException {
97 pluginStore.booleans.put(attribute, value);
105 public Double getDoubleValue(String attribute) throws ConfigurationException {
106 if (!pluginStore.strings.containsKey(attribute)) {
107 throw new AttributeNotFoundException(attribute);
109 String stringValue = pluginStore.strings.get(attribute);
110 if (stringValue == null) {
114 return Double.valueOf(pluginStore.strings.get(attribute));
115 } catch (NumberFormatException nfe1) {
116 throw new ConfigurationException("Could not parse “" + stringValue + "”.", nfe1);
124 public void setDoubleValue(String attribute, Double value) throws ConfigurationException {
125 pluginStore.strings.put(attribute, String.valueOf(value));
133 public Integer getIntegerValue(String attribute) throws ConfigurationException {
134 if (!pluginStore.integers.containsKey(attribute)) {
135 throw new AttributeNotFoundException(attribute);
137 return pluginStore.integers.get(attribute);
144 public void setIntegerValue(String attribute, Integer value) throws ConfigurationException {
145 pluginStore.integers.put(attribute, value);
153 public Long getLongValue(String attribute) throws ConfigurationException {
154 if (!pluginStore.longs.containsKey(attribute)) {
155 throw new AttributeNotFoundException(attribute);
157 return pluginStore.longs.get(attribute);
164 public void setLongValue(String attribute, Long value) throws ConfigurationException {
165 pluginStore.longs.put(attribute, value);
174 * Saves the configuration to the plugin store.
176 private void save() {
178 pluginRespirator.putStore(pluginStore);
179 } catch (DatabaseDisabledException dde1) {
180 logger.log(Level.WARNING, "Could not store plugin store, database is disabled.", dde1);