2 * FreenetSone - SonePlugin.java - Copyright © 2010 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.Collections;
22 import java.util.logging.Level;
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.freenet.PluginStoreConfigurationBackend;
28 import net.pterodactylus.sone.freenet.wot.IdentityManager;
29 import net.pterodactylus.sone.freenet.wot.PluginConnector;
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.config.XMLConfigurationBackend;
36 import net.pterodactylus.util.logging.Logging;
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 import freenet.pluginmanager.PluginStore;
50 * This class interfaces with Freenet. It is the class that is loaded by the
51 * node and starts up the whole Sone system.
53 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
55 public class SonePlugin implements FredPlugin, FredPluginL10n, FredPluginBaseL10n, FredPluginThreadless, FredPluginVersioned {
58 /* initialize logging. */
59 Logging.setup("sone");
60 Logging.setupConsoleLogging();
62 * Logging.addLoggingListener(new LoggingListener() {
63 * @Override public void logged(LogRecord logRecord) { Class<?>
64 * loggerClass = Logging.getLoggerClass(logRecord.getLoggerName()); int
65 * recordLevel = logRecord.getLevel().intValue(); if (recordLevel <
66 * Level.FINE.intValue()) { freenet.support.Logger.debug(loggerClass,
67 * String.format(logRecord.getMessage(), logRecord.getParameters()),
68 * logRecord.getThrown()); } else if (recordLevel <
69 * Level.INFO.intValue()) { freenet.support.Logger.minor(loggerClass,
70 * String.format(logRecord.getMessage(), logRecord.getParameters()),
71 * logRecord.getThrown()); } else if (recordLevel <
72 * Level.WARNING.intValue()) {
73 * freenet.support.Logger.normal(loggerClass,
74 * String.format(logRecord.getMessage(), logRecord.getParameters()),
75 * logRecord.getThrown()); } else if (recordLevel <
76 * Level.SEVERE.intValue()) {
77 * freenet.support.Logger.warning(loggerClass,
78 * String.format(logRecord.getMessage(), logRecord.getParameters()),
79 * logRecord.getThrown()); } else {
80 * freenet.support.Logger.error(loggerClass,
81 * String.format(logRecord.getMessage(), logRecord.getParameters()),
82 * logRecord.getThrown()); } } });
87 public static final Version VERSION = new Version("RC1", 0, 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 l10n helper. */
102 private PluginL10n l10n;
104 /** The plugin store. */
105 private PluginStore pluginStore;
107 /** The identity manager. */
108 private IdentityManager identityManager;
115 * Returns the plugin respirator for this plugin.
117 * @return The plugin respirator
119 public PluginRespirator pluginRespirator() {
120 return pluginRespirator;
124 * Returns the core started by this plugin.
133 * Returns the plugin’s l10n helper.
135 * @return The plugin’s l10n helper
137 public PluginL10n l10n() {
142 // FREDPLUGIN METHODS
149 public void runPlugin(PluginRespirator pluginRespirator) {
150 this.pluginRespirator = pluginRespirator;
152 /* create a configuration. */
153 Configuration configuration;
155 configuration = new Configuration(new PluginStoreConfigurationBackend(pluginStore = pluginRespirator.getStore()));
156 } catch (DatabaseDisabledException dde1) {
157 logger.log(Level.WARNING, "Could not load plugin store, using XML files.");
159 configuration = new Configuration(new XMLConfigurationBackend(new File("sone.xml"), true));
160 } catch (ConfigurationException ce1) {
161 logger.log(Level.SEVERE, "Could not load or create the “sone.xml” configuration file!");
162 configuration = new Configuration(new MapConfigurationBackend(Collections.<String, String> emptyMap()));
166 /* create freenet interface. */
167 FreenetInterface freenetInterface = new FreenetInterface(pluginRespirator.getNode(), pluginRespirator.getHLSimpleClient());
169 /* create web of trust connector. */
170 PluginConnector pluginConnector = new PluginConnector(pluginRespirator);
171 WebOfTrustConnector webOfTrustConnector = new WebOfTrustConnector(pluginConnector);
172 identityManager = new IdentityManager(webOfTrustConnector);
173 identityManager.setContext("Sone");
175 /* create the web interface. */
176 webInterface = new WebInterface(this);
179 core = new Core(configuration, freenetInterface, identityManager);
181 /* create the identity manager. */
182 identityManager.addIdentityListener(core);
185 boolean startupFailed = true;
188 webInterface.start();
189 identityManager.start();
190 startupFailed = false;
194 * we let the exception bubble up but shut the logging down so
195 * that the logfile is not swamped by the installed logging
196 * handlers of the failed instances.
207 public void terminate() {
209 /* stop the web interface. */
215 /* stop the identity manager. */
216 identityManager.stop();
218 /* TODO wait for core to stop? */
220 pluginRespirator.putStore(pluginStore);
221 } catch (DatabaseDisabledException dde1) {
222 logger.log(Level.WARNING, "Could not store plugin store, database is disabled.", dde1);
226 /* shutdown logger. */
232 // INTERFACE FredPluginL10n
239 public String getString(String key) {
240 return l10n.getBase().getString(key);
247 public void setLanguage(LANGUAGE newLanguage) {
248 l10n = new PluginL10n(this, newLanguage);
252 // INTERFACE FredPluginBaseL10n
259 public String getL10nFilesBasePath() {
267 public String getL10nFilesMask() {
268 return "sone.${lang}.properties";
275 public String getL10nOverrideFilesMask() {
276 return "sone.${lang}.override.properties";
283 public ClassLoader getPluginClassLoader() {
284 return SonePlugin.class.getClassLoader();
288 // INTERFACE FredPluginVersioned
295 public String getVersion() {
296 return VERSION.toString();