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.database.Database;
29 import net.pterodactylus.sone.database.memory.MemoryDatabase;
30 import net.pterodactylus.sone.fcp.FcpInterface;
31 import net.pterodactylus.sone.freenet.PluginStoreConfigurationBackend;
32 import net.pterodactylus.sone.freenet.plugin.PluginConnector;
33 import net.pterodactylus.sone.freenet.wot.IdentityManager;
34 import net.pterodactylus.sone.freenet.wot.WebOfTrustConnector;
35 import net.pterodactylus.sone.web.WebInterface;
36 import net.pterodactylus.util.config.Configuration;
37 import net.pterodactylus.util.config.ConfigurationException;
38 import net.pterodactylus.util.config.MapConfigurationBackend;
39 import net.pterodactylus.util.logging.Logging;
40 import net.pterodactylus.util.logging.LoggingListener;
41 import net.pterodactylus.util.version.Version;
42 import freenet.client.async.DatabaseDisabledException;
43 import freenet.l10n.BaseL10n.LANGUAGE;
44 import freenet.l10n.PluginL10n;
45 import freenet.pluginmanager.FredPlugin;
46 import freenet.pluginmanager.FredPluginBaseL10n;
47 import freenet.pluginmanager.FredPluginFCP;
48 import freenet.pluginmanager.FredPluginL10n;
49 import freenet.pluginmanager.FredPluginThreadless;
50 import freenet.pluginmanager.FredPluginVersioned;
51 import freenet.pluginmanager.PluginReplySender;
52 import freenet.pluginmanager.PluginRespirator;
53 import freenet.support.SimpleFieldSet;
54 import freenet.support.api.Bucket;
57 * This class interfaces with Freenet. It is the class that is loaded by the
58 * node and starts up the whole Sone system.
60 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
62 public class SonePlugin implements FredPlugin, FredPluginFCP, FredPluginL10n, FredPluginBaseL10n, FredPluginThreadless, FredPluginVersioned {
65 /* initialize logging. */
66 Logging.setup("sone");
67 Logging.addLoggingListener(new LoggingListener() {
70 public void logged(LogRecord logRecord) {
71 Class<?> loggerClass = Logging.getLoggerClass(logRecord.getLoggerName());
72 int recordLevel = logRecord.getLevel().intValue();
73 if (recordLevel < Level.FINE.intValue()) {
74 freenet.support.Logger.debug(loggerClass, logRecord.getMessage(), logRecord.getThrown());
75 } else if (recordLevel < Level.INFO.intValue()) {
76 freenet.support.Logger.minor(loggerClass, logRecord.getMessage(), logRecord.getThrown());
77 } else if (recordLevel < Level.WARNING.intValue()) {
78 freenet.support.Logger.normal(loggerClass, logRecord.getMessage(), logRecord.getThrown());
79 } else if (recordLevel < Level.SEVERE.intValue()) {
80 freenet.support.Logger.warning(loggerClass, logRecord.getMessage(), logRecord.getThrown());
82 freenet.support.Logger.error(loggerClass, logRecord.getMessage(), logRecord.getThrown());
89 public static final Version VERSION = new Version(0, 8, 4);
92 private static final Logger logger = Logging.getLogger(SonePlugin.class);
94 /** The plugin respirator. */
95 private PluginRespirator pluginRespirator;
100 /** The web interface. */
101 private WebInterface webInterface;
103 /** The FCP interface. */
104 private FcpInterface fcpInterface;
106 /** The l10n helper. */
107 private PluginL10n l10n;
109 /** The web of trust connector. */
110 private WebOfTrustConnector webOfTrustConnector;
112 /** The identity manager. */
113 private IdentityManager identityManager;
120 * Returns the plugin respirator for this plugin.
122 * @return The plugin respirator
124 public PluginRespirator pluginRespirator() {
125 return pluginRespirator;
129 * Returns the core started by this plugin.
138 * Returns the plugin’s l10n helper.
140 * @return The plugin’s l10n helper
142 public PluginL10n l10n() {
147 // FREDPLUGIN METHODS
154 public void runPlugin(PluginRespirator pluginRespirator) {
155 this.pluginRespirator = pluginRespirator;
157 /* create a configuration. */
158 Configuration oldConfiguration;
159 Configuration newConfiguration = null;
160 boolean firstStart = !new File("sone.properties").exists();
161 boolean newConfig = false;
163 oldConfiguration = new Configuration(new MapConfigurationBackend(new File("sone.properties"), false));
164 newConfiguration = oldConfiguration;
165 } catch (ConfigurationException ce1) {
167 logger.log(Level.INFO, "Could not load configuration file, trying plugin store…", ce1);
169 newConfiguration = new Configuration(new MapConfigurationBackend(new File("sone.properties"), true));
170 logger.log(Level.INFO, "Created new configuration file.");
171 } catch (ConfigurationException ce2) {
172 logger.log(Level.SEVERE, "Could not create configuration file, using Plugin Store!", ce2);
175 oldConfiguration = new Configuration(new PluginStoreConfigurationBackend(pluginRespirator));
176 logger.log(Level.INFO, "Plugin store loaded.");
177 } catch (DatabaseDisabledException dde1) {
178 logger.log(Level.SEVERE, "Could not load any configuration, using empty configuration!");
179 oldConfiguration = new Configuration(new MapConfigurationBackend());
183 boolean startupFailed = true;
185 /* create freenet interface. */
186 FreenetInterface freenetInterface = new FreenetInterface(pluginRespirator.getNode());
188 /* create web of trust connector. */
189 PluginConnector pluginConnector = new PluginConnector(pluginRespirator);
190 webOfTrustConnector = new WebOfTrustConnector(pluginConnector);
191 identityManager = new IdentityManager(webOfTrustConnector, "Sone");
193 /* create Sone database. */
194 Database soneDatabase = new MemoryDatabase();
196 /* create trust updater. */
197 WebOfTrustUpdater trustUpdater = new WebOfTrustUpdater(webOfTrustConnector);
201 core = new Core(oldConfiguration, soneDatabase, freenetInterface, identityManager, trustUpdater);
203 /* create the web interface. */
204 webInterface = new WebInterface(this);
205 core.addCoreListener(webInterface);
207 /* create FCP interface. */
208 fcpInterface = new FcpInterface(core);
209 core.setFcpInterface(fcpInterface);
211 /* create the identity manager. */
212 identityManager.addIdentityListener(core);
216 if ((newConfiguration != null) && (oldConfiguration != newConfiguration)) {
217 logger.log(Level.INFO, "Setting configuration to file-based configuration.");
218 core.setConfiguration(newConfiguration);
220 webInterface.start();
221 webInterface.setFirstStart(firstStart);
222 webInterface.setNewConfig(newConfig);
223 identityManager.start();
224 startupFailed = false;
228 * we let the exception bubble up but shut the logging down so
229 * that the logfile is not swamped by the installed logging
230 * handlers of the failed instances.
241 public void terminate() {
243 /* stop the web interface. */
249 /* stop the identity manager. */
250 identityManager.stop();
252 /* stop the web of trust connector. */
253 webOfTrustConnector.stop();
254 } catch (Throwable t1) {
255 logger.log(Level.SEVERE, "Error while shutting down!", t1);
257 /* shutdown logger. */
263 // INTERFACE FredPluginFCP
270 public void handle(PluginReplySender pluginReplySender, SimpleFieldSet parameters, Bucket data, int accessType) {
271 fcpInterface.handle(pluginReplySender, parameters, data, accessType);
275 // INTERFACE FredPluginL10n
282 public String getString(String key) {
283 return l10n.getBase().getString(key);
290 public void setLanguage(LANGUAGE newLanguage) {
291 l10n = new PluginL10n(this, newLanguage);
295 // INTERFACE FredPluginBaseL10n
302 public String getL10nFilesBasePath() {
310 public String getL10nFilesMask() {
311 return "sone.${lang}.properties";
318 public String getL10nOverrideFilesMask() {
319 return "sone.${lang}.override.properties";
326 public ClassLoader getPluginClassLoader() {
327 return SonePlugin.class.getClassLoader();
331 // INTERFACE FredPluginVersioned
338 public String getVersion() {
339 return VERSION.toString();