Remove core listener.
[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
231                 boolean startupFailed = true;
232                 try {
233
234                         /* start core! */
235                         core.start();
236                         if ((newConfiguration != null) && (oldConfiguration != newConfiguration)) {
237                                 logger.log(Level.INFO, "Setting configuration to file-based configuration.");
238                                 core.setConfiguration(newConfiguration);
239                         }
240                         webInterface.start();
241                         webInterface.setFirstStart(firstStart);
242                         webInterface.setNewConfig(newConfig);
243                         startupFailed = false;
244                 } finally {
245                         if (startupFailed) {
246                                 /*
247                                  * we let the exception bubble up but shut the logging down so
248                                  * that the logfile is not swamped by the installed logging
249                                  * handlers of the failed instances.
250                                  */
251                                 Logging.shutdown();
252                         }
253                 }
254         }
255
256         /**
257          * {@inheritDoc}
258          */
259         @Override
260         public void terminate() {
261                 try {
262                         /* stop the web interface. */
263                         webInterface.stop();
264
265                         /* stop the core. */
266                         core.stop();
267
268                         /* stop the web of trust connector. */
269                         webOfTrustConnector.stop();
270                 } catch (Throwable t1) {
271                         logger.log(Level.SEVERE, "Error while shutting down!", t1);
272                 } finally {
273                         /* shutdown logger. */
274                         Logging.shutdown();
275                 }
276         }
277
278         //
279         // INTERFACE FredPluginFCP
280         //
281
282         /**
283          * {@inheritDoc}
284          */
285         @Override
286         public void handle(PluginReplySender pluginReplySender, SimpleFieldSet parameters, Bucket data, int accessType) {
287                 fcpInterface.handle(pluginReplySender, parameters, data, accessType);
288         }
289
290         //
291         // INTERFACE FredPluginL10n
292         //
293
294         /**
295          * {@inheritDoc}
296          */
297         @Override
298         public String getString(String key) {
299                 return l10n.getBase().getString(key);
300         }
301
302         /**
303          * {@inheritDoc}
304          */
305         @Override
306         public void setLanguage(LANGUAGE newLanguage) {
307                 l10n = new PluginL10n(this, newLanguage);
308         }
309
310         //
311         // INTERFACE FredPluginBaseL10n
312         //
313
314         /**
315          * {@inheritDoc}
316          */
317         @Override
318         public String getL10nFilesBasePath() {
319                 return "i18n";
320         }
321
322         /**
323          * {@inheritDoc}
324          */
325         @Override
326         public String getL10nFilesMask() {
327                 return "sone.${lang}.properties";
328         }
329
330         /**
331          * {@inheritDoc}
332          */
333         @Override
334         public String getL10nOverrideFilesMask() {
335                 return "sone.${lang}.override.properties";
336         }
337
338         /**
339          * {@inheritDoc}
340          */
341         @Override
342         public ClassLoader getPluginClassLoader() {
343                 return SonePlugin.class.getClassLoader();
344         }
345
346         //
347         // INTERFACE FredPluginVersioned
348         //
349
350         /**
351          * {@inheritDoc}
352          */
353         @Override
354         public String getVersion() {
355                 return VERSION.toString();
356         }
357
358 }