Use the new client layer from fred.
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / PluginStoreConfigurationBackend.java
1 /*
2  * Sone - PluginStoreConfigurationBackend.java - Copyright © 2010–2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.freenet;
19
20 import java.util.logging.Logger;
21
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.PersistenceDisabledException;
28 import freenet.pluginmanager.PluginRespirator;
29 import freenet.pluginmanager.PluginStore;
30
31 /**
32  * Backend for a {@link Configuration} that is based on a {@link PluginStore}.
33  *
34  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
35  */
36 public class PluginStoreConfigurationBackend implements ExtendedConfigurationBackend {
37
38         /** The logger. */
39         @SuppressWarnings("unused")
40         private static final Logger logger = Logging.getLogger(PluginStoreConfigurationBackend.class);
41
42         /** The plugin respirator. */
43         private final PluginRespirator pluginRespirator;
44
45         /** The backing plugin store. */
46         private final PluginStore pluginStore;
47
48         /**
49          * Creates a new configuration backend based on a plugin store.
50          *
51          * @param pluginRespirator
52          *            The plugin respirator
53          * @throws PersistenceDisabledException
54          *             if the plugin store is not available
55          */
56         public PluginStoreConfigurationBackend(PluginRespirator pluginRespirator) throws PersistenceDisabledException {
57                 this.pluginRespirator = pluginRespirator;
58                 this.pluginStore = pluginRespirator.getStore();
59         }
60
61         /**
62          * {@inheritDoc}
63          */
64         @Override
65         public String getValue(String attribute) throws ConfigurationException {
66                 if (!pluginStore.strings.containsKey(attribute)) {
67                         throw new AttributeNotFoundException(attribute);
68                 }
69                 return pluginStore.strings.get(attribute);
70         }
71
72         /**
73          * {@inheritDoc}
74          */
75         @Override
76         public void putValue(String attribute, String value) throws ConfigurationException {
77                 pluginStore.strings.put(attribute, value);
78                 save();
79         }
80
81         /**
82          * {@inheritDoc}
83          */
84         @Override
85         public Boolean getBooleanValue(String attribute) throws ConfigurationException {
86                 if (!pluginStore.booleans.containsKey(attribute)) {
87                         throw new AttributeNotFoundException(attribute);
88                 }
89                 return pluginStore.booleans.get(attribute);
90         }
91
92         /**
93          * {@inheritDoc}
94          */
95         @Override
96         public void setBooleanValue(String attribute, Boolean value) throws ConfigurationException {
97                 pluginStore.booleans.put(attribute, value);
98                 save();
99         }
100
101         /**
102          * {@inheritDoc}
103          */
104         @Override
105         public Double getDoubleValue(String attribute) throws ConfigurationException {
106                 if (!pluginStore.strings.containsKey(attribute)) {
107                         throw new AttributeNotFoundException(attribute);
108                 }
109                 String stringValue = pluginStore.strings.get(attribute);
110                 if (stringValue == null) {
111                         return null;
112                 }
113                 try {
114                         return Double.valueOf(pluginStore.strings.get(attribute));
115                 } catch (NumberFormatException nfe1) {
116                         throw new ConfigurationException("Could not parse “" + stringValue + "”.", nfe1);
117                 }
118         }
119
120         /**
121          * {@inheritDoc}
122          */
123         @Override
124         public void setDoubleValue(String attribute, Double value) throws ConfigurationException {
125                 pluginStore.strings.put(attribute, String.valueOf(value));
126                 save();
127         }
128
129         /**
130          * {@inheritDoc}
131          */
132         @Override
133         public Integer getIntegerValue(String attribute) throws ConfigurationException {
134                 if (!pluginStore.integers.containsKey(attribute)) {
135                         throw new AttributeNotFoundException(attribute);
136                 }
137                 return pluginStore.integers.get(attribute);
138         }
139
140         /**
141          * {@inheritDoc}
142          */
143         @Override
144         public void setIntegerValue(String attribute, Integer value) throws ConfigurationException {
145                 pluginStore.integers.put(attribute, value);
146                 save();
147         }
148
149         /**
150          * {@inheritDoc}
151          */
152         @Override
153         public Long getLongValue(String attribute) throws ConfigurationException {
154                 if (!pluginStore.longs.containsKey(attribute)) {
155                         throw new AttributeNotFoundException(attribute);
156                 }
157                 return pluginStore.longs.get(attribute);
158         }
159
160         /**
161          * {@inheritDoc}
162          */
163         @Override
164         public void setLongValue(String attribute, Long value) throws ConfigurationException {
165                 pluginStore.longs.put(attribute, value);
166                 save();
167         }
168
169         /**
170          * {@inheritDoc}
171          */
172         @Override
173         public void save() throws ConfigurationException {
174                 try {
175                         pluginRespirator.putStore(pluginStore);
176                 } catch (PersistenceDisabledException pde1) {
177                         throw new ConfigurationException("Could not store plugin store, persistence is disabled.", pde1);
178                 }
179         }
180
181 }