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