Set Freenet interface in core.
[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         /** Interface to freenet. */
50         private FreenetInterface freenetInterface;
51
52         /** The local Sones. */
53         private final Set<Sone> localSones = new HashSet<Sone>();
54
55         /**
56          * Creates a new core.
57          */
58         public Core() {
59                 super("Sone Core");
60         }
61
62         //
63         // ACCESSORS
64         //
65
66         /**
67          * Sets the configuration of the core.
68          *
69          * @param configuration
70          *            The configuration of the core
71          * @return This core (for method chaining)
72          */
73         public Core configuration(Configuration configuration) {
74                 this.configuration = configuration;
75                 return this;
76         }
77
78         /**
79          * Sets the Freenet interface to use.
80          *
81          * @param freenetInterface
82          *            The Freenet interface to use
83          * @return This core (for method chaining)
84          */
85         public Core freenetInterface(FreenetInterface freenetInterface) {
86                 this.freenetInterface = freenetInterface;
87                 return this;
88         }
89
90         //
91         // ACTIONS
92         //
93
94         //
95         // SERVICE METHODS
96         //
97
98         /**
99          * {@inheritDoc}
100          */
101         @Override
102         protected void serviceStart() {
103                 loadConfiguration();
104         }
105
106         //
107         // PRIVATE METHODS
108         //
109
110         /**
111          * Loads the configuration.
112          */
113         private void loadConfiguration() {
114                 logger.entering(Core.class.getName(), "loadConfiguration()");
115
116                 /* get names of all local Sones. */
117                 String allSoneNamesString = configuration.getStringValue("Sone/Names").getValue("");
118                 List<String> allSoneNames;
119                 try {
120                         allSoneNames = StringEscaper.parseLine(allSoneNamesString);
121                 } catch (TextException te1) {
122                         logger.log(Level.WARNING, "Could not parse Sone names: “" + allSoneNamesString + "”", te1);
123                         allSoneNames = Collections.emptyList();
124                 }
125
126                 /* parse local Sones. */
127                 for (String soneName : allSoneNames) {
128                         String insertUri = configuration.getStringValue("Sone/Name." + soneName + "/InsertURI").getValue(null);
129                         String requestUri = configuration.getStringValue("Sone/Name." + soneName + "/RequestURI").getValue(null);
130                         try {
131                                 localSones.add(new Sone(new FreenetURI(requestUri), new FreenetURI(insertUri)));
132                         } catch (MalformedURLException mue1) {
133                                 logger.log(Level.WARNING, "Could not create Sone from requestUri (“" + requestUri + "”) and insertUri (“" + insertUri + "”)!", mue1);
134                         }
135                 }
136
137                 logger.exiting(Core.class.getName(), "loadConfiguration()");
138         }
139
140 }