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