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