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, 4);
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, "Sone");
192
193                         /* create Sone database. */
194                         Database soneDatabase = new MemoryDatabase();
195
196                         /* create trust updater. */
197                         WebOfTrustUpdater trustUpdater = new WebOfTrustUpdater(webOfTrustConnector);
198                         trustUpdater.init();
199
200                         /* create core. */
201                         core = new Core(oldConfiguration, soneDatabase, freenetInterface, identityManager, trustUpdater);
202
203                         /* create the web interface. */
204                         webInterface = new WebInterface(this);
205                         core.addCoreListener(webInterface);
206
207                         /* create FCP interface. */
208                         fcpInterface = new FcpInterface(core);
209                         core.setFcpInterface(fcpInterface);
210
211                         /* create the identity manager. */
212                         identityManager.addIdentityListener(core);
213
214                         /* start core! */
215                         core.start();
216                         if ((newConfiguration != null) && (oldConfiguration != newConfiguration)) {
217                                 logger.log(Level.INFO, "Setting configuration to file-based configuration.");
218                                 core.setConfiguration(newConfiguration);
219                         }
220                         webInterface.start();
221                         webInterface.setFirstStart(firstStart);
222                         webInterface.setNewConfig(newConfig);
223                         identityManager.start();
224                         startupFailed = false;
225                 } finally {
226                         if (startupFailed) {
227                                 /*
228                                  * we let the exception bubble up but shut the logging down so
229                                  * that the logfile is not swamped by the installed logging
230                                  * handlers of the failed instances.
231                                  */
232                                 Logging.shutdown();
233                         }
234                 }
235         }
236
237         /**
238          * {@inheritDoc}
239          */
240         @Override
241         public void terminate() {
242                 try {
243                         /* stop the web interface. */
244                         webInterface.stop();
245
246                         /* stop the core. */
247                         core.stop();
248
249                         /* stop the identity manager. */
250                         identityManager.stop();
251
252                         /* stop the web of trust connector. */
253                         webOfTrustConnector.stop();
254                 } catch (Throwable t1) {
255                         logger.log(Level.SEVERE, "Error while shutting down!", t1);
256                 } finally {
257                         /* shutdown logger. */
258                         Logging.shutdown();
259                 }
260         }
261
262         //
263         // INTERFACE FredPluginFCP
264         //
265
266         /**
267          * {@inheritDoc}
268          */
269         @Override
270         public void handle(PluginReplySender pluginReplySender, SimpleFieldSet parameters, Bucket data, int accessType) {
271                 fcpInterface.handle(pluginReplySender, parameters, data, accessType);
272         }
273
274         //
275         // INTERFACE FredPluginL10n
276         //
277
278         /**
279          * {@inheritDoc}
280          */
281         @Override
282         public String getString(String key) {
283                 return l10n.getBase().getString(key);
284         }
285
286         /**
287          * {@inheritDoc}
288          */
289         @Override
290         public void setLanguage(LANGUAGE newLanguage) {
291                 l10n = new PluginL10n(this, newLanguage);
292         }
293
294         //
295         // INTERFACE FredPluginBaseL10n
296         //
297
298         /**
299          * {@inheritDoc}
300          */
301         @Override
302         public String getL10nFilesBasePath() {
303                 return "i18n";
304         }
305
306         /**
307          * {@inheritDoc}
308          */
309         @Override
310         public String getL10nFilesMask() {
311                 return "sone.${lang}.properties";
312         }
313
314         /**
315          * {@inheritDoc}
316          */
317         @Override
318         public String getL10nOverrideFilesMask() {
319                 return "sone.${lang}.override.properties";
320         }
321
322         /**
323          * {@inheritDoc}
324          */
325         @Override
326         public ClassLoader getPluginClassLoader() {
327                 return SonePlugin.class.getClassLoader();
328         }
329
330         //
331         // INTERFACE FredPluginVersioned
332         //
333
334         /**
335          * {@inheritDoc}
336          */
337         @Override
338         public String getVersion() {
339                 return VERSION.toString();
340         }
341
342 }