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