2 * Sone - SonePlugin.java - Copyright © 2010–2012 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.main;
21 import java.util.logging.Level;
22 import java.util.logging.LogRecord;
23 import java.util.logging.Logger;
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;
55 * This class interfaces with Freenet. It is the class that is loaded by the
56 * node and starts up the whole Sone system.
58 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
60 public class SonePlugin implements FredPlugin, FredPluginFCP, FredPluginL10n, FredPluginBaseL10n, FredPluginThreadless, FredPluginVersioned {
63 /* initialize logging. */
64 Logging.setup("sone");
65 Logging.addLoggingListener(new LoggingListener() {
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());
80 freenet.support.Logger.error(loggerClass, logRecord.getMessage(), logRecord.getThrown());
87 public static final Version VERSION = new Version(0, 8, 2);
90 private static final Logger logger = Logging.getLogger(SonePlugin.class);
92 /** The plugin respirator. */
93 private PluginRespirator pluginRespirator;
98 /** The web interface. */
99 private WebInterface webInterface;
101 /** The FCP interface. */
102 private FcpInterface fcpInterface;
104 /** The l10n helper. */
105 private PluginL10n l10n;
107 /** The web of trust connector. */
108 private WebOfTrustConnector webOfTrustConnector;
110 /** The identity manager. */
111 private IdentityManager identityManager;
118 * Returns the plugin respirator for this plugin.
120 * @return The plugin respirator
122 public PluginRespirator pluginRespirator() {
123 return pluginRespirator;
127 * Returns the core started by this plugin.
136 * Returns the plugin’s l10n helper.
138 * @return The plugin’s l10n helper
140 public PluginL10n l10n() {
145 // FREDPLUGIN METHODS
152 public void runPlugin(PluginRespirator pluginRespirator) {
153 this.pluginRespirator = pluginRespirator;
155 /* create a configuration. */
156 Configuration oldConfiguration;
157 Configuration newConfiguration = null;
158 boolean firstStart = !new File("sone.properties").exists();
159 boolean newConfig = false;
161 oldConfiguration = new Configuration(new MapConfigurationBackend(new File("sone.properties"), false));
162 newConfiguration = oldConfiguration;
163 } catch (ConfigurationException ce1) {
165 logger.log(Level.INFO, "Could not load configuration file, trying plugin store…", ce1);
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);
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());
181 boolean startupFailed = true;
183 /* create freenet interface. */
184 FreenetInterface freenetInterface = new FreenetInterface(pluginRespirator.getNode());
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");
192 /* create trust updater. */
193 WebOfTrustUpdater trustUpdater = new WebOfTrustUpdater(webOfTrustConnector);
197 core = new Core(oldConfiguration, freenetInterface, identityManager, trustUpdater);
199 /* create the web interface. */
200 webInterface = new WebInterface(this);
201 core.addCoreListener(webInterface);
203 /* create FCP interface. */
204 fcpInterface = new FcpInterface(core);
205 core.setFcpInterface(fcpInterface);
207 /* create the identity manager. */
208 identityManager.addIdentityListener(core);
212 if ((newConfiguration != null) && (oldConfiguration != newConfiguration)) {
213 logger.log(Level.INFO, "Setting configuration to file-based configuration.");
214 core.setConfiguration(newConfiguration);
216 webInterface.start();
217 webInterface.setFirstStart(firstStart);
218 webInterface.setNewConfig(newConfig);
219 identityManager.start();
220 startupFailed = false;
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.
237 public void terminate() {
239 /* stop the web interface. */
245 /* stop the identity manager. */
246 identityManager.stop();
248 /* stop the web of trust connector. */
249 webOfTrustConnector.stop();
250 } catch (Throwable t1) {
251 logger.log(Level.SEVERE, "Error while shutting down!", t1);
253 /* shutdown logger. */
259 // INTERFACE FredPluginFCP
266 public void handle(PluginReplySender pluginReplySender, SimpleFieldSet parameters, Bucket data, int accessType) {
267 fcpInterface.handle(pluginReplySender, parameters, data, accessType);
271 // INTERFACE FredPluginL10n
278 public String getString(String key) {
279 return l10n.getBase().getString(key);
286 public void setLanguage(LANGUAGE newLanguage) {
287 l10n = new PluginL10n(this, newLanguage);
291 // INTERFACE FredPluginBaseL10n
298 public String getL10nFilesBasePath() {
306 public String getL10nFilesMask() {
307 return "sone.${lang}.properties";
314 public String getL10nOverrideFilesMask() {
315 return "sone.${lang}.override.properties";
322 public ClassLoader getPluginClassLoader() {
323 return SonePlugin.class.getClassLoader();
327 // INTERFACE FredPluginVersioned
334 public String getVersion() {
335 return VERSION.toString();