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.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.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.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 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.addLoggingListener(new LoggingListener() {
63 public void logged(LogRecord logRecord) {
64 Class<?> loggerClass = Logging.getLoggerClass(logRecord.getLoggerName());
65 int recordLevel = logRecord.getLevel().intValue();
66 if (recordLevel < Level.FINE.intValue()) {
67 freenet.support.Logger.debug(loggerClass, String.format(logRecord.getMessage(), logRecord.getParameters()), logRecord.getThrown());
68 } else if (recordLevel < Level.INFO.intValue()) {
69 freenet.support.Logger.minor(loggerClass, String.format(logRecord.getMessage(), logRecord.getParameters()), logRecord.getThrown());
70 } else if (recordLevel < Level.WARNING.intValue()) {
71 freenet.support.Logger.normal(loggerClass, String.format(logRecord.getMessage(), logRecord.getParameters()), logRecord.getThrown());
72 } else if (recordLevel < Level.SEVERE.intValue()) {
73 freenet.support.Logger.warning(loggerClass, String.format(logRecord.getMessage(), logRecord.getParameters()), logRecord.getThrown());
75 freenet.support.Logger.error(loggerClass, String.format(logRecord.getMessage(), logRecord.getParameters()), logRecord.getThrown());
82 public static final Version VERSION = new Version("RC1", 0, 3, 1);
85 private static final Logger logger = Logging.getLogger(SonePlugin.class);
87 /** The plugin respirator. */
88 private PluginRespirator pluginRespirator;
93 /** The web interface. */
94 private WebInterface webInterface;
96 /** The l10n helper. */
97 private PluginL10n l10n;
99 /** The plugin store. */
100 private PluginStore pluginStore;
102 /** The identity manager. */
103 private IdentityManager identityManager;
110 * Returns the plugin respirator for this plugin.
112 * @return The plugin respirator
114 public PluginRespirator pluginRespirator() {
115 return pluginRespirator;
119 * Returns the core started by this plugin.
128 * Returns the plugin’s l10n helper.
130 * @return The plugin’s l10n helper
132 public PluginL10n l10n() {
137 // FREDPLUGIN METHODS
144 public void runPlugin(PluginRespirator pluginRespirator) {
145 this.pluginRespirator = pluginRespirator;
147 /* create a configuration. */
148 Configuration oldConfiguration;
149 Configuration newConfiguration = null;
151 oldConfiguration = new Configuration(new MapConfigurationBackend(new File("sone.properties"), false));
152 newConfiguration = oldConfiguration;
153 } catch (ConfigurationException ce1) {
154 logger.log(Level.INFO, "Could not load configuration file, trying plugin store…");
156 newConfiguration = new Configuration(new MapConfigurationBackend(new File("sone.properties"), true));
157 logger.log(Level.INFO, "Created new configuration file.");
158 } catch (ConfigurationException ce2) {
159 logger.log(Level.SEVERE, "Could not create configuration file, using Plugin Store!");
162 oldConfiguration = new Configuration(new PluginStoreConfigurationBackend(pluginRespirator));
163 logger.log(Level.INFO, "Plugin store loaded.");
164 } catch (DatabaseDisabledException dde1) {
165 logger.log(Level.SEVERE, "Could not load any configuration, using empty configuration!");
166 oldConfiguration = new Configuration(new MapConfigurationBackend());
170 /* create freenet interface. */
171 FreenetInterface freenetInterface = new FreenetInterface(pluginRespirator.getNode(), pluginRespirator.getHLSimpleClient());
173 /* create web of trust connector. */
174 PluginConnector pluginConnector = new PluginConnector(pluginRespirator);
175 WebOfTrustConnector webOfTrustConnector = new WebOfTrustConnector(pluginConnector);
176 identityManager = new IdentityManager(webOfTrustConnector);
177 identityManager.setContext("Sone");
180 core = new Core(oldConfiguration, freenetInterface, identityManager);
182 /* create the web interface. */
183 webInterface = new WebInterface(this);
184 core.addCoreListener(webInterface);
186 /* create the identity manager. */
187 identityManager.addIdentityListener(core);
190 boolean startupFailed = true;
193 if ((newConfiguration != null) && (oldConfiguration != newConfiguration)) {
194 logger.log(Level.INFO, "Setting configuration to file-based configuration.");
195 core.setConfiguration(newConfiguration);
197 webInterface.start();
198 identityManager.start();
199 startupFailed = false;
203 * we let the exception bubble up but shut the logging down so
204 * that the logfile is not swamped by the installed logging
205 * handlers of the failed instances.
216 public void terminate() {
218 /* stop the web interface. */
224 /* stop the identity manager. */
225 identityManager.stop();
227 /* TODO wait for core to stop? */
229 pluginRespirator.putStore(pluginStore);
230 } catch (DatabaseDisabledException dde1) {
231 logger.log(Level.WARNING, "Could not store plugin store, database is disabled.", dde1);
235 /* shutdown logger. */
241 // INTERFACE FredPluginL10n
248 public String getString(String key) {
249 return l10n.getBase().getString(key);
256 public void setLanguage(LANGUAGE newLanguage) {
257 l10n = new PluginL10n(this, newLanguage);
261 // INTERFACE FredPluginBaseL10n
268 public String getL10nFilesBasePath() {
276 public String getL10nFilesMask() {
277 return "sone.${lang}.properties";
284 public String getL10nOverrideFilesMask() {
285 return "sone.${lang}.override.properties";
292 public ClassLoader getPluginClassLoader() {
293 return SonePlugin.class.getClassLoader();
297 // INTERFACE FredPluginVersioned
304 public String getVersion() {
305 return VERSION.toString();