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