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