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