84e813e19fdf09ab4d6bb1f67eb8ab05009e4e41
[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.logging.Logging;
31 import net.pterodactylus.util.service.AbstractService;
32 import net.pterodactylus.util.text.StringEscaper;
33 import net.pterodactylus.util.text.TextException;
34 import freenet.keys.FreenetURI;
35
36 /**
37  * The Sone core.
38  *
39  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40  */
41 public class Core extends AbstractService {
42
43         /** The logger. */
44         private static final Logger logger = Logging.getLogger(Core.class);
45
46         /** The configuration. */
47         private Configuration configuration;
48
49         /** The local Sones. */
50         private final Set<Sone> localSones = new HashSet<Sone>();
51
52         /**
53          * Creates a new core.
54          */
55         public Core() {
56                 super("Sone Core");
57         }
58
59         //
60         // ACCESSORS
61         //
62
63         /**
64          * Sets the configuration of the core.
65          *
66          * @param configuration
67          *            The configuration of the core
68          * @return This core (for method chaining)
69          */
70         public Core configuration(Configuration configuration) {
71                 this.configuration = configuration;
72                 return this;
73         }
74
75         //
76         // ACTIONS
77         //
78
79         //
80         // SERVICE METHODS
81         //
82
83         /**
84          * {@inheritDoc}
85          */
86         @Override
87         protected void serviceStart() {
88                 loadConfiguration();
89         }
90
91         //
92         // PRIVATE METHODS
93         //
94
95         /**
96          * Loads the configuration.
97          */
98         private void loadConfiguration() {
99                 logger.entering(Core.class.getName(), "loadConfiguration()");
100
101                 /* get names of all local Sones. */
102                 String allSoneNamesString = configuration.getStringValue("Sone/Names").getValue("");
103                 List<String> allSoneNames;
104                 try {
105                         allSoneNames = StringEscaper.parseLine(allSoneNamesString);
106                 } catch (TextException te1) {
107                         logger.log(Level.WARNING, "Could not parse Sone names: “" + allSoneNamesString + "”", te1);
108                         allSoneNames = Collections.emptyList();
109                 }
110
111                 /* parse local Sones. */
112                 for (String soneName : allSoneNames) {
113                         String insertUri = configuration.getStringValue("Sone/Name." + soneName + "/InsertURI").getValue(null);
114                         String requestUri = configuration.getStringValue("Sone/Name." + soneName + "/RequestURI").getValue(null);
115                         try {
116                                 localSones.add(new Sone(new FreenetURI(requestUri), new FreenetURI(insertUri)));
117                         } catch (MalformedURLException mue1) {
118                                 logger.log(Level.WARNING, "Could not create Sone from requestUri (“" + requestUri + "”) and insertUri (“" + insertUri + "”)!", mue1);
119                         }
120                 }
121
122                 logger.exiting(Core.class.getName(), "loadConfiguration()");
123         }
124
125 }