Add method to store the configuration when the core is stopped.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
1 /*
2  * FreenetSone - Core.java - Copyright © 2010 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.core;
19
20 import java.net.MalformedURLException;
21 import java.util.Collections;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Set;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27
28 import net.pterodactylus.sone.data.Sone;
29 import net.pterodactylus.util.config.Configuration;
30 import net.pterodactylus.util.config.ConfigurationException;
31 import net.pterodactylus.util.logging.Logging;
32 import net.pterodactylus.util.service.AbstractService;
33 import net.pterodactylus.util.text.StringEscaper;
34 import net.pterodactylus.util.text.TextException;
35 import freenet.keys.FreenetURI;
36
37 /**
38  * The Sone core.
39  *
40  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41  */
42 public class Core extends AbstractService {
43
44         /** The logger. */
45         private static final Logger logger = Logging.getLogger(Core.class);
46
47         /** The configuration. */
48         private Configuration configuration;
49
50         /** Interface to freenet. */
51         private FreenetInterface freenetInterface;
52
53         /** The local Sones. */
54         private final Set<Sone> localSones = new HashSet<Sone>();
55
56         /**
57          * Creates a new core.
58          */
59         public Core() {
60                 super("Sone Core");
61         }
62
63         //
64         // ACCESSORS
65         //
66
67         /**
68          * Sets the configuration of the core.
69          *
70          * @param configuration
71          *            The configuration of the core
72          * @return This core (for method chaining)
73          */
74         public Core configuration(Configuration configuration) {
75                 this.configuration = configuration;
76                 return this;
77         }
78
79         /**
80          * Sets the Freenet interface to use.
81          *
82          * @param freenetInterface
83          *            The Freenet interface to use
84          * @return This core (for method chaining)
85          */
86         public Core freenetInterface(FreenetInterface freenetInterface) {
87                 this.freenetInterface = freenetInterface;
88                 return this;
89         }
90
91         /**
92          * Returns the local Sones.
93          *
94          * @return The local Sones
95          */
96         public Set<Sone> localSones() {
97                 return localSones;
98         }
99
100         //
101         // ACTIONS
102         //
103
104         //
105         // SERVICE METHODS
106         //
107
108         /**
109          * {@inheritDoc}
110          */
111         @Override
112         protected void serviceStart() {
113                 loadConfiguration();
114         }
115
116         /**
117          * {@inheritDoc}
118          */
119         @Override
120         protected void serviceStop() {
121                 saveConfiguration();
122         }
123
124         //
125         // PRIVATE METHODS
126         //
127
128         /**
129          * Loads the configuration.
130          */
131         private void loadConfiguration() {
132                 logger.entering(Core.class.getName(), "loadConfiguration()");
133
134                 /* get names of all local Sones. */
135                 String allSoneNamesString = configuration.getStringValue("Sone/Names").getValue(null);
136                 if (allSoneNamesString == null) {
137                         allSoneNamesString = "";
138                 }
139                 List<String> allSoneNames;
140                 try {
141                         allSoneNames = StringEscaper.parseLine(allSoneNamesString);
142                 } catch (TextException te1) {
143                         logger.log(Level.WARNING, "Could not parse Sone names: “" + allSoneNamesString + "”", te1);
144                         allSoneNames = Collections.emptyList();
145                 }
146
147                 /* parse local Sones. */
148                 logger.log(Level.INFO, "Loading %d Sones…", allSoneNames.size());
149                 for (String soneName : allSoneNames) {
150                         String insertUri = configuration.getStringValue("Sone/Name." + soneName + "/InsertURI").getValue(null);
151                         String requestUri = configuration.getStringValue("Sone/Name." + soneName + "/RequestURI").getValue(null);
152                         try {
153                                 localSones.add(new Sone(soneName, new FreenetURI(requestUri), new FreenetURI(insertUri)));
154                         } catch (MalformedURLException mue1) {
155                                 logger.log(Level.WARNING, "Could not create Sone from requestUri (“" + requestUri + "”) and insertUri (“" + insertUri + "”)!", mue1);
156                         }
157                 }
158
159                 logger.exiting(Core.class.getName(), "loadConfiguration()");
160         }
161
162         /**
163          * Saves the configuraiton.
164          */
165         private void saveConfiguration() {
166
167                 /* get the names of all Sones. */
168                 Set<String> soneNames = new HashSet<String>();
169                 for (Sone sone : localSones) {
170                         soneNames.add(sone.getName());
171                 }
172                 String soneNamesString = StringEscaper.escapeWords(soneNames);
173
174                 logger.log(Level.INFO, "Storing %d Sones…", soneNames.size());
175                 try {
176                         /* store names of all Sones. */
177                         configuration.getStringValue("Sone/Names").setValue(soneNamesString);
178
179                         /* store all Sones. */
180                         for (Sone sone : localSones) {
181                                 configuration.getStringValue("Sone/Name." + sone.getName() + "/RequestURI").setValue(sone.getRequestUri().toString());
182                                 configuration.getStringValue("Sone/Name." + sone.getName() + "/InsertURI").setValue(sone.getInsertUri().toString());
183                         }
184                 } catch (ConfigurationException ce1) {
185                         logger.log(Level.WARNING, "Could not store configuration!", ce1);
186                 }
187         }
188
189 }