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