Abandon logging that does not work with Freenet’s logger at all
[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.PersistenceDisabledException;
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(PluginStoreConfigurationBackend.class.getName());
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 PersistenceDisabledException
55          *             if the plugin store is not available
56          */
57         public PluginStoreConfigurationBackend(PluginRespirator pluginRespirator) throws PersistenceDisabledException {
58                 this.pluginRespirator = pluginRespirator;
59                 this.pluginStore = pluginRespirator.getStore();
60         }
61
62         /**
63          * {@inheritDoc}
64          */
65         @Override
66         public String getValue(String attribute) throws ConfigurationException {
67                 if (!pluginStore.strings.containsKey(attribute)) {
68                         throw new AttributeNotFoundException(attribute);
69                 }
70                 return pluginStore.strings.get(attribute);
71         }
72
73         /**
74          * {@inheritDoc}
75          */
76         @Override
77         public void putValue(String attribute, String value) throws ConfigurationException {
78                 pluginStore.strings.put(attribute, value);
79                 save();
80         }
81
82         /**
83          * {@inheritDoc}
84          */
85         @Override
86         public Boolean getBooleanValue(String attribute) throws ConfigurationException {
87                 if (!pluginStore.booleans.containsKey(attribute)) {
88                         throw new AttributeNotFoundException(attribute);
89                 }
90                 return pluginStore.booleans.get(attribute);
91         }
92
93         /**
94          * {@inheritDoc}
95          */
96         @Override
97         public void setBooleanValue(String attribute, Boolean value) throws ConfigurationException {
98                 pluginStore.booleans.put(attribute, value);
99                 save();
100         }
101
102         /**
103          * {@inheritDoc}
104          */
105         @Override
106         public Double getDoubleValue(String attribute) throws ConfigurationException {
107                 if (!pluginStore.strings.containsKey(attribute)) {
108                         throw new AttributeNotFoundException(attribute);
109                 }
110                 String stringValue = pluginStore.strings.get(attribute);
111                 if (stringValue == null) {
112                         return null;
113                 }
114                 try {
115                         return Double.valueOf(pluginStore.strings.get(attribute));
116                 } catch (NumberFormatException nfe1) {
117                         throw new ConfigurationException("Could not parse “" + stringValue + "”.", nfe1);
118                 }
119         }
120
121         /**
122          * {@inheritDoc}
123          */
124         @Override
125         public void setDoubleValue(String attribute, Double value) throws ConfigurationException {
126                 pluginStore.strings.put(attribute, String.valueOf(value));
127                 save();
128         }
129
130         /**
131          * {@inheritDoc}
132          */
133         @Override
134         public Integer getIntegerValue(String attribute) throws ConfigurationException {
135                 if (!pluginStore.integers.containsKey(attribute)) {
136                         throw new AttributeNotFoundException(attribute);
137                 }
138                 return pluginStore.integers.get(attribute);
139         }
140
141         /**
142          * {@inheritDoc}
143          */
144         @Override
145         public void setIntegerValue(String attribute, Integer value) throws ConfigurationException {
146                 pluginStore.integers.put(attribute, value);
147                 save();
148         }
149
150         /**
151          * {@inheritDoc}
152          */
153         @Override
154         public Long getLongValue(String attribute) throws ConfigurationException {
155                 if (!pluginStore.longs.containsKey(attribute)) {
156                         throw new AttributeNotFoundException(attribute);
157                 }
158                 return pluginStore.longs.get(attribute);
159         }
160
161         /**
162          * {@inheritDoc}
163          */
164         @Override
165         public void setLongValue(String attribute, Long value) throws ConfigurationException {
166                 pluginStore.longs.put(attribute, value);
167                 save();
168         }
169
170         /**
171          * {@inheritDoc}
172          */
173         @Override
174         public void save() throws ConfigurationException {
175                 try {
176                         pluginRespirator.putStore(pluginStore);
177                 } catch (PersistenceDisabledException pde1) {
178                         throw new ConfigurationException("Could not store plugin store, persistence is disabled.", pde1);
179                 }
180         }
181
182 }