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.database.Database;
28 import net.pterodactylus.sone.database.memory.MemoryDatabase;
29 import net.pterodactylus.sone.fcp.FcpInterface;
30 import net.pterodactylus.sone.freenet.PluginStoreConfigurationBackend;
31 import net.pterodactylus.sone.freenet.plugin.PluginConnector;
32 import net.pterodactylus.sone.freenet.wot.IdentityManager;
33 import net.pterodactylus.sone.freenet.wot.WebOfTrustConnector;
34 import net.pterodactylus.sone.web.WebInterface;
35 import net.pterodactylus.util.config.Configuration;
36 import net.pterodactylus.util.config.ConfigurationException;
37 import net.pterodactylus.util.config.MapConfigurationBackend;
38 import net.pterodactylus.util.logging.Logging;
39 import net.pterodactylus.util.logging.LoggingListener;
40 import net.pterodactylus.util.version.Version;
41 import freenet.client.async.DatabaseDisabledException;
42 import freenet.l10n.BaseL10n.LANGUAGE;
43 import freenet.l10n.PluginL10n;
44 import freenet.pluginmanager.FredPlugin;
45 import freenet.pluginmanager.FredPluginBaseL10n;
46 import freenet.pluginmanager.FredPluginFCP;
47 import freenet.pluginmanager.FredPluginL10n;
48 import freenet.pluginmanager.FredPluginThreadless;
49 import freenet.pluginmanager.FredPluginVersioned;
50 import freenet.pluginmanager.PluginReplySender;
51 import freenet.pluginmanager.PluginRespirator;
52 import freenet.support.SimpleFieldSet;
53 import freenet.support.api.Bucket;
56 * This class interfaces with Freenet. It is the class that is loaded by the
57 * node and starts up the whole Sone system.
59 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
61 public class SonePlugin implements FredPlugin, FredPluginFCP, FredPluginL10n, FredPluginBaseL10n, FredPluginThreadless, FredPluginVersioned {
64 /* initialize logging. */
65 Logging.setup("sone");
66 Logging.addLoggingListener(new LoggingListener() {
69 public void logged(LogRecord logRecord) {
70 Class<?> loggerClass = Logging.getLoggerClass(logRecord.getLoggerName());
71 int recordLevel = logRecord.getLevel().intValue();
72 if (recordLevel < Level.FINE.intValue()) {
73 freenet.support.Logger.debug(loggerClass, logRecord.getMessage(), logRecord.getThrown());
74 } else if (recordLevel < Level.INFO.intValue()) {
75 freenet.support.Logger.minor(loggerClass, logRecord.getMessage(), logRecord.getThrown());
76 } else if (recordLevel < Level.WARNING.intValue()) {
77 freenet.support.Logger.normal(loggerClass, logRecord.getMessage(), logRecord.getThrown());
78 } else if (recordLevel < Level.SEVERE.intValue()) {
79 freenet.support.Logger.warning(loggerClass, logRecord.getMessage(), logRecord.getThrown());
81 freenet.support.Logger.error(loggerClass, logRecord.getMessage(), logRecord.getThrown());
88 public static final Version VERSION = new Version(0, 8, 1);
91 private static final Logger logger = Logging.getLogger(SonePlugin.class);
93 /** The plugin respirator. */
94 private PluginRespirator pluginRespirator;
99 /** The web interface. */
100 private WebInterface webInterface;
102 /** The FCP interface. */
103 private FcpInterface fcpInterface;
105 /** The l10n helper. */
106 private PluginL10n l10n;
108 /** The web of trust connector. */
109 private WebOfTrustConnector webOfTrustConnector;
111 /** The identity manager. */
112 private IdentityManager identityManager;
119 * Returns the plugin respirator for this plugin.
121 * @return The plugin respirator
123 public PluginRespirator pluginRespirator() {
124 return pluginRespirator;
128 * Returns the core started by this plugin.
137 * Returns the plugin’s l10n helper.
139 * @return The plugin’s l10n helper
141 public PluginL10n l10n() {
146 // FREDPLUGIN METHODS
153 public void runPlugin(PluginRespirator pluginRespirator) {
154 this.pluginRespirator = pluginRespirator;
156 /* create a configuration. */
157 Configuration oldConfiguration;
158 Configuration newConfiguration = null;
159 boolean firstStart = !new File("sone.properties").exists();
160 boolean newConfig = false;
162 oldConfiguration = new Configuration(new MapConfigurationBackend(new File("sone.properties"), false));
163 newConfiguration = oldConfiguration;
164 } catch (ConfigurationException ce1) {
166 logger.log(Level.INFO, "Could not load configuration file, trying plugin store…", ce1);
168 newConfiguration = new Configuration(new MapConfigurationBackend(new File("sone.properties"), true));
169 logger.log(Level.INFO, "Created new configuration file.");
170 } catch (ConfigurationException ce2) {
171 logger.log(Level.SEVERE, "Could not create configuration file, using Plugin Store!", ce2);
174 oldConfiguration = new Configuration(new PluginStoreConfigurationBackend(pluginRespirator));
175 logger.log(Level.INFO, "Plugin store loaded.");
176 } catch (DatabaseDisabledException dde1) {
177 logger.log(Level.SEVERE, "Could not load any configuration, using empty configuration!");
178 oldConfiguration = new Configuration(new MapConfigurationBackend());
182 boolean startupFailed = true;
184 /* create freenet interface. */
185 FreenetInterface freenetInterface = new FreenetInterface(pluginRespirator.getNode());
187 /* create web of trust connector. */
188 PluginConnector pluginConnector = new PluginConnector(pluginRespirator);
189 webOfTrustConnector = new WebOfTrustConnector(pluginConnector);
190 identityManager = new IdentityManager(webOfTrustConnector);
191 identityManager.setContext("Sone");
193 /* create Sone database. */
194 Database soneDatabase = new MemoryDatabase();
197 core = new Core(oldConfiguration, soneDatabase, freenetInterface, identityManager);
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();