🔀 Merge branch 'release/v82'
[Sone.git] / main / java / net / pterodactylus / sone / main / SonePlugin.java
1 /*
2  * Sone - SonePlugin.java - Copyright Â© 2010 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, String.format(logRecord.getMessage(), logRecord.getParameters()), logRecord.getThrown());
72                                 } else if (recordLevel < Level.INFO.intValue()) {
73                                         freenet.support.Logger.minor(loggerClass, String.format(logRecord.getMessage(), logRecord.getParameters()), logRecord.getThrown());
74                                 } else if (recordLevel < Level.WARNING.intValue()) {
75                                         freenet.support.Logger.normal(loggerClass, String.format(logRecord.getMessage(), logRecord.getParameters()), logRecord.getThrown());
76                                 } else if (recordLevel < Level.SEVERE.intValue()) {
77                                         freenet.support.Logger.warning(loggerClass, String.format(logRecord.getMessage(), logRecord.getParameters()), logRecord.getThrown());
78                                 } else {
79                                         freenet.support.Logger.error(loggerClass, String.format(logRecord.getMessage(), logRecord.getParameters()), logRecord.getThrown());
80                                 }
81                         }
82                 });
83         }
84
85         /** The version. */
86         public static final Version VERSION = new Version(0, 6, 7);
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 identity manager. */
107         private IdentityManager identityManager;
108
109         //
110         // ACCESSORS
111         //
112
113         /**
114          * Returns the plugin respirator for this plugin.
115          *
116          * @return The plugin respirator
117          */
118         public PluginRespirator pluginRespirator() {
119                 return pluginRespirator;
120         }
121
122         /**
123          * Returns the core started by this plugin.
124          *
125          * @return The core
126          */
127         public Core core() {
128                 return core;
129         }
130
131         /**
132          * Returns the plugin’s l10n helper.
133          *
134          * @return The plugin’s l10n helper
135          */
136         public PluginL10n l10n() {
137                 return l10n;
138         }
139
140         //
141         // FREDPLUGIN METHODS
142         //
143
144         /**
145          * {@inheritDoc}
146          */
147         @Override
148         public void runPlugin(@SuppressWarnings("hiding") PluginRespirator pluginRespirator) {
149                 this.pluginRespirator = pluginRespirator;
150
151                 /* create a configuration. */
152                 Configuration oldConfiguration;
153                 Configuration newConfiguration = null;
154                 boolean firstStart = !new File("sone.properties").exists();
155                 boolean newConfig = false;
156                 try {
157                         oldConfiguration = new Configuration(new MapConfigurationBackend(new File("sone.properties"), false));
158                         newConfiguration = oldConfiguration;
159                 } catch (ConfigurationException ce1) {
160                         newConfig = true;
161                         logger.log(Level.INFO, "Could not load configuration file, trying plugin store…", ce1);
162                         try {
163                                 newConfiguration = new Configuration(new MapConfigurationBackend(new File("sone.properties"), true));
164                                 logger.log(Level.INFO, "Created new configuration file.");
165                         } catch (ConfigurationException ce2) {
166                                 logger.log(Level.SEVERE, "Could not create configuration file, using Plugin Store!", ce2);
167                         }
168                         try {
169                                 oldConfiguration = new Configuration(new PluginStoreConfigurationBackend(pluginRespirator));
170                                 logger.log(Level.INFO, "Plugin store loaded.");
171                         } catch (DatabaseDisabledException dde1) {
172                                 logger.log(Level.SEVERE, "Could not load any configuration, using empty configuration!");
173                                 oldConfiguration = new Configuration(new MapConfigurationBackend());
174                         }
175                 }
176
177                 boolean startupFailed = true;
178                 try {
179                         /* create freenet interface. */
180                         FreenetInterface freenetInterface = new FreenetInterface(pluginRespirator.getNode());
181
182                         /* create web of trust connector. */
183                         PluginConnector pluginConnector = new PluginConnector(pluginRespirator);
184                         WebOfTrustConnector webOfTrustConnector = new WebOfTrustConnector(pluginConnector);
185                         identityManager = new IdentityManager(webOfTrustConnector);
186                         identityManager.setContext("Sone");
187
188                         /* create core. */
189                         core = new Core(oldConfiguration, freenetInterface, identityManager);
190
191                         /* create the web interface. */
192                         webInterface = new WebInterface(this);
193                         core.addCoreListener(webInterface);
194
195                         /* create FCP interface. */
196                         fcpInterface = new FcpInterface(core);
197                         core.setFcpInterface(fcpInterface);
198
199                         /* create the identity manager. */
200                         identityManager.addIdentityListener(core);
201
202                         /* start core! */
203                         core.start();
204                         if ((newConfiguration != null) && (oldConfiguration != newConfiguration)) {
205                                 logger.log(Level.INFO, "Setting configuration to file-based configuration.");
206                                 core.setConfiguration(newConfiguration);
207                         }
208                         webInterface.start();
209                         webInterface.setFirstStart(firstStart);
210                         webInterface.setNewConfig(newConfig);
211                         identityManager.start();
212                         startupFailed = false;
213                 } finally {
214                         if (startupFailed) {
215                                 /*
216                                  * we let the exception bubble up but shut the logging down so
217                                  * that the logfile is not swamped by the installed logging
218                                  * handlers of the failed instances.
219                                  */
220                                 Logging.shutdown();
221                         }
222                 }
223         }
224
225         /**
226          * {@inheritDoc}
227          */
228         @Override
229         public void terminate() {
230                 try {
231                         /* stop the web interface. */
232                         webInterface.stop();
233
234                         /* stop the core. */
235                         core.stop();
236
237                         /* stop the identity manager. */
238                         identityManager.stop();
239                 } catch (Throwable t1) {
240                         logger.log(Level.SEVERE, "Error while shutting down!", t1);
241                 } finally {
242                         /* shutdown logger. */
243                         Logging.shutdown();
244                 }
245         }
246
247         //
248         // INTERFACE FredPluginFCP
249         //
250
251         /**
252          * {@inheritDoc}
253          */
254         @Override
255         public void handle(PluginReplySender pluginReplySender, SimpleFieldSet parameters, Bucket data, int accessType) {
256                 fcpInterface.handle(pluginReplySender, parameters, data, accessType);
257         }
258
259         //
260         // INTERFACE FredPluginL10n
261         //
262
263         /**
264          * {@inheritDoc}
265          */
266         @Override
267         public String getString(String key) {
268                 return l10n.getBase().getString(key);
269         }
270
271         /**
272          * {@inheritDoc}
273          */
274         @Override
275         public void setLanguage(LANGUAGE newLanguage) {
276                 l10n = new PluginL10n(this, newLanguage);
277         }
278
279         //
280         // INTERFACE FredPluginBaseL10n
281         //
282
283         /**
284          * {@inheritDoc}
285          */
286         @Override
287         public String getL10nFilesBasePath() {
288                 return "i18n";
289         }
290
291         /**
292          * {@inheritDoc}
293          */
294         @Override
295         public String getL10nFilesMask() {
296                 return "sone.${lang}.properties";
297         }
298
299         /**
300          * {@inheritDoc}
301          */
302         @Override
303         public String getL10nOverrideFilesMask() {
304                 return "sone.${lang}.override.properties";
305         }
306
307         /**
308          * {@inheritDoc}
309          */
310         @Override
311         public ClassLoader getPluginClassLoader() {
312                 return SonePlugin.class.getClassLoader();
313         }
314
315         //
316         // INTERFACE FredPluginVersioned
317         //
318
319         /**
320          * {@inheritDoc}
321          */
322         @Override
323         public String getVersion() {
324                 return VERSION.toString();
325         }
326
327 }