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