dcd99645023c7264d59af8ce599b20adbfb79d97
[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 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, "Sone");
190
191                         /* create trust updater. */
192                         WebOfTrustUpdater trustUpdater = new WebOfTrustUpdater(webOfTrustConnector);
193                         trustUpdater.init();
194
195                         /* create core. */
196                         core = new Core(oldConfiguration, freenetInterface, identityManager, trustUpdater);
197
198                         /* create the web interface. */
199                         webInterface = new WebInterface(this);
200                         core.addCoreListener(webInterface);
201
202                         /* create FCP interface. */
203                         fcpInterface = new FcpInterface(core);
204                         core.setFcpInterface(fcpInterface);
205
206                         /* create the identity manager. */
207                         identityManager.addIdentityListener(core);
208
209                         /* start core! */
210                         core.start();
211                         if ((newConfiguration != null) && (oldConfiguration != newConfiguration)) {
212                                 logger.log(Level.INFO, "Setting configuration to file-based configuration.");
213                                 core.setConfiguration(newConfiguration);
214                         }
215                         webInterface.start();
216                         webInterface.setFirstStart(firstStart);
217                         webInterface.setNewConfig(newConfig);
218                         identityManager.start();
219                         startupFailed = false;
220                 } finally {
221                         if (startupFailed) {
222                                 /*
223                                  * we let the exception bubble up but shut the logging down so
224                                  * that the logfile is not swamped by the installed logging
225                                  * handlers of the failed instances.
226                                  */
227                                 Logging.shutdown();
228                         }
229                 }
230         }
231
232         /**
233          * {@inheritDoc}
234          */
235         @Override
236         public void terminate() {
237                 try {
238                         /* stop the web interface. */
239                         webInterface.stop();
240
241                         /* stop the core. */
242                         core.stop();
243
244                         /* stop the identity manager. */
245                         identityManager.stop();
246
247                         /* stop the web of trust connector. */
248                         webOfTrustConnector.stop();
249                 } catch (Throwable t1) {
250                         logger.log(Level.SEVERE, "Error while shutting down!", t1);
251                 } finally {
252                         /* shutdown logger. */
253                         Logging.shutdown();
254                 }
255         }
256
257         //
258         // INTERFACE FredPluginFCP
259         //
260
261         /**
262          * {@inheritDoc}
263          */
264         @Override
265         public void handle(PluginReplySender pluginReplySender, SimpleFieldSet parameters, Bucket data, int accessType) {
266                 fcpInterface.handle(pluginReplySender, parameters, data, accessType);
267         }
268
269         //
270         // INTERFACE FredPluginL10n
271         //
272
273         /**
274          * {@inheritDoc}
275          */
276         @Override
277         public String getString(String key) {
278                 return l10n.getBase().getString(key);
279         }
280
281         /**
282          * {@inheritDoc}
283          */
284         @Override
285         public void setLanguage(LANGUAGE newLanguage) {
286                 l10n = new PluginL10n(this, newLanguage);
287         }
288
289         //
290         // INTERFACE FredPluginBaseL10n
291         //
292
293         /**
294          * {@inheritDoc}
295          */
296         @Override
297         public String getL10nFilesBasePath() {
298                 return "i18n";
299         }
300
301         /**
302          * {@inheritDoc}
303          */
304         @Override
305         public String getL10nFilesMask() {
306                 return "sone.${lang}.properties";
307         }
308
309         /**
310          * {@inheritDoc}
311          */
312         @Override
313         public String getL10nOverrideFilesMask() {
314                 return "sone.${lang}.override.properties";
315         }
316
317         /**
318          * {@inheritDoc}
319          */
320         @Override
321         public ClassLoader getPluginClassLoader() {
322                 return SonePlugin.class.getClassLoader();
323         }
324
325         //
326         // INTERFACE FredPluginVersioned
327         //
328
329         /**
330          * {@inheritDoc}
331          */
332         @Override
333         public String getVersion() {
334                 return VERSION.toString();
335         }
336
337 }