Turn Sone into an interface, add in-memory implementation of Sone.
[Sone.git] / src / main / java / net / pterodactylus / sone / main / SonePlugin.java
1 /*
2  * Sone - SonePlugin.java - Copyright © 2010–2012 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.main;
19
20 import java.io.File;
21 import java.util.logging.Level;
22 import java.util.logging.LogRecord;
23 import java.util.logging.Logger;
24
25 import net.pterodactylus.sone.core.Core;
26 import net.pterodactylus.sone.core.FreenetInterface;
27 import net.pterodactylus.sone.database.Database;
28 import net.pterodactylus.sone.database.memory.MemoryDatabase;
29 import net.pterodactylus.sone.fcp.FcpInterface;
30 import net.pterodactylus.sone.freenet.PluginStoreConfigurationBackend;
31 import net.pterodactylus.sone.freenet.plugin.PluginConnector;
32 import net.pterodactylus.sone.freenet.wot.IdentityManager;
33 import net.pterodactylus.sone.freenet.wot.WebOfTrustConnector;
34 import net.pterodactylus.sone.web.WebInterface;
35 import net.pterodactylus.util.config.Configuration;
36 import net.pterodactylus.util.config.ConfigurationException;
37 import net.pterodactylus.util.config.MapConfigurationBackend;
38 import net.pterodactylus.util.logging.Logging;
39 import net.pterodactylus.util.logging.LoggingListener;
40 import net.pterodactylus.util.version.Version;
41 import freenet.client.async.DatabaseDisabledException;
42 import freenet.l10n.BaseL10n.LANGUAGE;
43 import freenet.l10n.PluginL10n;
44 import freenet.pluginmanager.FredPlugin;
45 import freenet.pluginmanager.FredPluginBaseL10n;
46 import freenet.pluginmanager.FredPluginFCP;
47 import freenet.pluginmanager.FredPluginL10n;
48 import freenet.pluginmanager.FredPluginThreadless;
49 import freenet.pluginmanager.FredPluginVersioned;
50 import freenet.pluginmanager.PluginReplySender;
51 import freenet.pluginmanager.PluginRespirator;
52 import freenet.support.SimpleFieldSet;
53 import freenet.support.api.Bucket;
54
55 /**
56  * This class interfaces with Freenet. It is the class that is loaded by the
57  * node and starts up the whole Sone system.
58  *
59  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
60  */
61 public class SonePlugin implements FredPlugin, FredPluginFCP, FredPluginL10n, FredPluginBaseL10n, FredPluginThreadless, FredPluginVersioned {
62
63         static {
64                 /* initialize logging. */
65                 Logging.setup("sone");
66                 Logging.addLoggingListener(new LoggingListener() {
67
68                         @Override
69                         public void logged(LogRecord logRecord) {
70                                 Class<?> loggerClass = Logging.getLoggerClass(logRecord.getLoggerName());
71                                 int recordLevel = logRecord.getLevel().intValue();
72                                 if (recordLevel < Level.FINE.intValue()) {
73                                         freenet.support.Logger.debug(loggerClass, logRecord.getMessage(), logRecord.getThrown());
74                                 } else if (recordLevel < Level.INFO.intValue()) {
75                                         freenet.support.Logger.minor(loggerClass, logRecord.getMessage(), logRecord.getThrown());
76                                 } else if (recordLevel < Level.WARNING.intValue()) {
77                                         freenet.support.Logger.normal(loggerClass, logRecord.getMessage(), logRecord.getThrown());
78                                 } else if (recordLevel < Level.SEVERE.intValue()) {
79                                         freenet.support.Logger.warning(loggerClass, logRecord.getMessage(), logRecord.getThrown());
80                                 } else {
81                                         freenet.support.Logger.error(loggerClass, logRecord.getMessage(), logRecord.getThrown());
82                                 }
83                         }
84                 });
85         }
86
87         /** The version. */
88         public static final Version VERSION = new Version(0, 8, 1);
89
90         /** The logger. */
91         private static final Logger logger = Logging.getLogger(SonePlugin.class);
92
93         /** The plugin respirator. */
94         private PluginRespirator pluginRespirator;
95
96         /** The core. */
97         private Core core;
98
99         /** The web interface. */
100         private WebInterface webInterface;
101
102         /** The FCP interface. */
103         private FcpInterface fcpInterface;
104
105         /** The l10n helper. */
106         private PluginL10n l10n;
107
108         /** The web of trust connector. */
109         private WebOfTrustConnector webOfTrustConnector;
110
111         /** The identity manager. */
112         private IdentityManager identityManager;
113
114         //
115         // ACCESSORS
116         //
117
118         /**
119          * Returns the plugin respirator for this plugin.
120          *
121          * @return The plugin respirator
122          */
123         public PluginRespirator pluginRespirator() {
124                 return pluginRespirator;
125         }
126
127         /**
128          * Returns the core started by this plugin.
129          *
130          * @return The core
131          */
132         public Core core() {
133                 return core;
134         }
135
136         /**
137          * Returns the plugin’s l10n helper.
138          *
139          * @return The plugin’s l10n helper
140          */
141         public PluginL10n l10n() {
142                 return l10n;
143         }
144
145         //
146         // FREDPLUGIN METHODS
147         //
148
149         /**
150          * {@inheritDoc}
151          */
152         @Override
153         public void runPlugin(PluginRespirator pluginRespirator) {
154                 this.pluginRespirator = pluginRespirator;
155
156                 /* create a configuration. */
157                 Configuration oldConfiguration;
158                 Configuration newConfiguration = null;
159                 boolean firstStart = !new File("sone.properties").exists();
160                 boolean newConfig = false;
161                 try {
162                         oldConfiguration = new Configuration(new MapConfigurationBackend(new File("sone.properties"), false));
163                         newConfiguration = oldConfiguration;
164                 } catch (ConfigurationException ce1) {
165                         newConfig = true;
166                         logger.log(Level.INFO, "Could not load configuration file, trying plugin store…", ce1);
167                         try {
168                                 newConfiguration = new Configuration(new MapConfigurationBackend(new File("sone.properties"), true));
169                                 logger.log(Level.INFO, "Created new configuration file.");
170                         } catch (ConfigurationException ce2) {
171                                 logger.log(Level.SEVERE, "Could not create configuration file, using Plugin Store!", ce2);
172                         }
173                         try {
174                                 oldConfiguration = new Configuration(new PluginStoreConfigurationBackend(pluginRespirator));
175                                 logger.log(Level.INFO, "Plugin store loaded.");
176                         } catch (DatabaseDisabledException dde1) {
177                                 logger.log(Level.SEVERE, "Could not load any configuration, using empty configuration!");
178                                 oldConfiguration = new Configuration(new MapConfigurationBackend());
179                         }
180                 }
181
182                 boolean startupFailed = true;
183                 try {
184                         /* create freenet interface. */
185                         FreenetInterface freenetInterface = new FreenetInterface(pluginRespirator.getNode());
186
187                         /* create web of trust connector. */
188                         PluginConnector pluginConnector = new PluginConnector(pluginRespirator);
189                         webOfTrustConnector = new WebOfTrustConnector(pluginConnector);
190                         identityManager = new IdentityManager(webOfTrustConnector);
191                         identityManager.setContext("Sone");
192
193                         /* create Sone database. */
194                         Database soneDatabase = new MemoryDatabase();
195
196                         /* create core. */
197                         core = new Core(oldConfiguration, soneDatabase, freenetInterface, identityManager);
198
199                         /* create the web interface. */
200                         webInterface = new WebInterface(this);
201                         core.addCoreListener(webInterface);
202
203                         /* create FCP interface. */
204                         fcpInterface = new FcpInterface(core);
205                         core.setFcpInterface(fcpInterface);
206
207                         /* create the identity manager. */
208                         identityManager.addIdentityListener(core);
209
210                         /* start core! */
211                         core.start();
212                         if ((newConfiguration != null) && (oldConfiguration != newConfiguration)) {
213                                 logger.log(Level.INFO, "Setting configuration to file-based configuration.");
214                                 core.setConfiguration(newConfiguration);
215                         }
216                         webInterface.start();
217                         webInterface.setFirstStart(firstStart);
218                         webInterface.setNewConfig(newConfig);
219                         identityManager.start();
220                         startupFailed = false;
221                 } finally {
222                         if (startupFailed) {
223                                 /*
224                                  * we let the exception bubble up but shut the logging down so
225                                  * that the logfile is not swamped by the installed logging
226                                  * handlers of the failed instances.
227                                  */
228                                 Logging.shutdown();
229                         }
230                 }
231         }
232
233         /**
234          * {@inheritDoc}
235          */
236         @Override
237         public void terminate() {
238                 try {
239                         /* stop the web interface. */
240                         webInterface.stop();
241
242                         /* stop the core. */
243                         core.stop();
244
245                         /* stop the identity manager. */
246                         identityManager.stop();
247
248                         /* stop the web of trust connector. */
249                         webOfTrustConnector.stop();
250                 } catch (Throwable t1) {
251                         logger.log(Level.SEVERE, "Error while shutting down!", t1);
252                 } finally {
253                         /* shutdown logger. */
254                         Logging.shutdown();
255                 }
256         }
257
258         //
259         // INTERFACE FredPluginFCP
260         //
261
262         /**
263          * {@inheritDoc}
264          */
265         @Override
266         public void handle(PluginReplySender pluginReplySender, SimpleFieldSet parameters, Bucket data, int accessType) {
267                 fcpInterface.handle(pluginReplySender, parameters, data, accessType);
268         }
269
270         //
271         // INTERFACE FredPluginL10n
272         //
273
274         /**
275          * {@inheritDoc}
276          */
277         @Override
278         public String getString(String key) {
279                 return l10n.getBase().getString(key);
280         }
281
282         /**
283          * {@inheritDoc}
284          */
285         @Override
286         public void setLanguage(LANGUAGE newLanguage) {
287                 l10n = new PluginL10n(this, newLanguage);
288         }
289
290         //
291         // INTERFACE FredPluginBaseL10n
292         //
293
294         /**
295          * {@inheritDoc}
296          */
297         @Override
298         public String getL10nFilesBasePath() {
299                 return "i18n";
300         }
301
302         /**
303          * {@inheritDoc}
304          */
305         @Override
306         public String getL10nFilesMask() {
307                 return "sone.${lang}.properties";
308         }
309
310         /**
311          * {@inheritDoc}
312          */
313         @Override
314         public String getL10nOverrideFilesMask() {
315                 return "sone.${lang}.override.properties";
316         }
317
318         /**
319          * {@inheritDoc}
320          */
321         @Override
322         public ClassLoader getPluginClassLoader() {
323                 return SonePlugin.class.getClassLoader();
324         }
325
326         //
327         // INTERFACE FredPluginVersioned
328         //
329
330         /**
331          * {@inheritDoc}
332          */
333         @Override
334         public String getVersion() {
335                 return VERSION.toString();
336         }
337
338 }