Declare potentially-static methods as static.
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / PluginStoreConfigurationBackend.java
1 /*
2  * Sone - PluginStoreConfigurationBackend.java - Copyright © 2010–2012 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.DatabaseDisabledException;
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 DatabaseDisabledException
54          *             if the plugin store is not available
55          */
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();
61                 }
62         }
63
64         /**
65          * {@inheritDoc}
66          */
67         @Override
68         public String getValue(String attribute) throws ConfigurationException {
69                 if (!pluginStore.strings.containsKey(attribute)) {
70                         throw new AttributeNotFoundException(attribute);
71                 }
72                 return pluginStore.strings.get(attribute);
73         }
74
75         /**
76          * {@inheritDoc}
77          */
78         @Override
79         public void putValue(String attribute, String value) throws ConfigurationException {
80                 pluginStore.strings.put(attribute, value);
81                 save();
82         }
83
84         /**
85          * {@inheritDoc}
86          */
87         @Override
88         public Boolean getBooleanValue(String attribute) throws ConfigurationException {
89                 if (!pluginStore.booleans.containsKey(attribute)) {
90                         throw new AttributeNotFoundException(attribute);
91                 }
92                 return pluginStore.booleans.get(attribute);
93         }
94
95         /**
96          * {@inheritDoc}
97          */
98         @Override
99         public void setBooleanValue(String attribute, Boolean value) throws ConfigurationException {
100                 pluginStore.booleans.put(attribute, value);
101                 save();
102         }
103
104         /**
105          * {@inheritDoc}
106          */
107         @Override
108         public Double getDoubleValue(String attribute) throws ConfigurationException {
109                 if (!pluginStore.strings.containsKey(attribute)) {
110                         throw new AttributeNotFoundException(attribute);
111                 }
112                 String stringValue = pluginStore.strings.get(attribute);
113                 if (stringValue == null) {
114                         return null;
115                 }
116                 try {
117                         return Double.valueOf(pluginStore.strings.get(attribute));
118                 } catch (NumberFormatException nfe1) {
119                         throw new ConfigurationException("Could not parse “" + stringValue + "”.", nfe1);
120                 }
121         }
122
123         /**
124          * {@inheritDoc}
125          */
126         @Override
127         public void setDoubleValue(String attribute, Double value) throws ConfigurationException {
128                 pluginStore.strings.put(attribute, String.valueOf(value));
129                 save();
130         }
131
132         /**
133          * {@inheritDoc}
134          */
135         @Override
136         public Integer getIntegerValue(String attribute) throws ConfigurationException {
137                 if (!pluginStore.integers.containsKey(attribute)) {
138                         throw new AttributeNotFoundException(attribute);
139                 }
140                 return pluginStore.integers.get(attribute);
141         }
142
143         /**
144          * {@inheritDoc}
145          */
146         @Override
147         public void setIntegerValue(String attribute, Integer value) throws ConfigurationException {
148                 pluginStore.integers.put(attribute, value);
149                 save();
150         }
151
152         /**
153          * {@inheritDoc}
154          */
155         @Override
156         public Long getLongValue(String attribute) throws ConfigurationException {
157                 if (!pluginStore.longs.containsKey(attribute)) {
158                         throw new AttributeNotFoundException(attribute);
159                 }
160                 return pluginStore.longs.get(attribute);
161         }
162
163         /**
164          * {@inheritDoc}
165          */
166         @Override
167         public void setLongValue(String attribute, Long value) throws ConfigurationException {
168                 pluginStore.longs.put(attribute, value);
169                 save();
170         }
171
172         /**
173          * {@inheritDoc}
174          */
175         @Override
176         public void save() throws ConfigurationException {
177                 try {
178                         pluginRespirator.putStore(pluginStore);
179                 } catch (DatabaseDisabledException dde1) {
180                         throw new ConfigurationException("Could not store plugin store, database is disabled.", dde1);
181                 }
182         }
183
184 }