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;
41 import com.google.common.eventbus.EventBus;
42 import com.google.inject.AbstractModule;
43 import com.google.inject.Guice;
44 import com.google.inject.Injector;
45 import com.google.inject.Singleton;
46 import com.google.inject.name.Names;
48 import freenet.client.async.DatabaseDisabledException;
49 import freenet.l10n.BaseL10n.LANGUAGE;
50 import freenet.l10n.PluginL10n;
51 import freenet.node.Node;
52 import freenet.pluginmanager.FredPlugin;
53 import freenet.pluginmanager.FredPluginBaseL10n;
54 import freenet.pluginmanager.FredPluginFCP;
55 import freenet.pluginmanager.FredPluginL10n;
56 import freenet.pluginmanager.FredPluginThreadless;
57 import freenet.pluginmanager.FredPluginVersioned;
58 import freenet.pluginmanager.PluginReplySender;
59 import freenet.pluginmanager.PluginRespirator;
60 import freenet.support.SimpleFieldSet;
61 import freenet.support.api.Bucket;
64 * This class interfaces with Freenet. It is the class that is loaded by the
65 * node and starts up the whole Sone system.
67 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
69 public class SonePlugin implements FredPlugin, FredPluginFCP, FredPluginL10n, FredPluginBaseL10n, FredPluginThreadless, FredPluginVersioned {
72 /* initialize logging. */
73 Logging.setup("sone");
74 Logging.addLoggingListener(new LoggingListener() {
77 public void logged(LogRecord logRecord) {
78 Class<?> loggerClass = Logging.getLoggerClass(logRecord.getLoggerName());
79 int recordLevel = logRecord.getLevel().intValue();
80 if (recordLevel < Level.FINE.intValue()) {
81 freenet.support.Logger.debug(loggerClass, logRecord.getMessage(), logRecord.getThrown());
82 } else if (recordLevel < Level.INFO.intValue()) {
83 freenet.support.Logger.minor(loggerClass, logRecord.getMessage(), logRecord.getThrown());
84 } else if (recordLevel < Level.WARNING.intValue()) {
85 freenet.support.Logger.normal(loggerClass, logRecord.getMessage(), logRecord.getThrown());
86 } else if (recordLevel < Level.SEVERE.intValue()) {
87 freenet.support.Logger.warning(loggerClass, logRecord.getMessage(), logRecord.getThrown());
89 freenet.support.Logger.error(loggerClass, logRecord.getMessage(), logRecord.getThrown());
96 public static final Version VERSION = new Version(0, 8, 4);
99 private static final Logger logger = Logging.getLogger(SonePlugin.class);
101 /** The plugin respirator. */
102 private PluginRespirator pluginRespirator;
107 /** The web interface. */
108 private WebInterface webInterface;
110 /** The FCP interface. */
111 private FcpInterface fcpInterface;
113 /** The l10n helper. */
114 private PluginL10n l10n;
116 /** The web of trust connector. */
117 private WebOfTrustConnector webOfTrustConnector;
124 * Returns the plugin respirator for this plugin.
126 * @return The plugin respirator
128 public PluginRespirator pluginRespirator() {
129 return pluginRespirator;
133 * Returns the core started by this plugin.
142 * Returns the plugin’s l10n helper.
144 * @return The plugin’s l10n helper
146 public PluginL10n l10n() {
151 // FREDPLUGIN METHODS
158 public void runPlugin(PluginRespirator pluginRespirator) {
159 this.pluginRespirator = pluginRespirator;
161 /* create a configuration. */
162 Configuration oldConfiguration;
163 Configuration newConfiguration = null;
164 boolean firstStart = !new File("sone.properties").exists();
165 boolean newConfig = false;
167 oldConfiguration = new Configuration(new MapConfigurationBackend(new File("sone.properties"), false));
168 newConfiguration = oldConfiguration;
169 } catch (ConfigurationException ce1) {
171 logger.log(Level.INFO, "Could not load configuration file, trying plugin store…", ce1);
173 newConfiguration = new Configuration(new MapConfigurationBackend(new File("sone.properties"), true));
174 logger.log(Level.INFO, "Created new configuration file.");
175 } catch (ConfigurationException ce2) {
176 logger.log(Level.SEVERE, "Could not create configuration file, using Plugin Store!", ce2);
179 oldConfiguration = new Configuration(new PluginStoreConfigurationBackend(pluginRespirator));
180 logger.log(Level.INFO, "Plugin store loaded.");
181 } catch (DatabaseDisabledException dde1) {
182 logger.log(Level.SEVERE, "Could not load any configuration, using empty configuration!");
183 oldConfiguration = new Configuration(new MapConfigurationBackend());
187 final Configuration startConfiguration = oldConfiguration;
188 final EventBus eventBus = new EventBus();
190 /* Freenet injector configuration. */
191 AbstractModule freenetModule = new AbstractModule() {
194 @SuppressWarnings("synthetic-access")
195 protected void configure() {
196 bind(PluginRespirator.class).toInstance(SonePlugin.this.pluginRespirator);
197 bind(Node.class).toInstance(SonePlugin.this.pluginRespirator.getNode());
200 /* Sone injector configuration. */
201 AbstractModule soneModule = new AbstractModule() {
204 protected void configure() {
205 bind(EventBus.class).toInstance(eventBus);
206 bind(Configuration.class).toInstance(startConfiguration);
207 bind(FreenetInterface.class).in(Singleton.class);
208 bind(PluginConnector.class).in(Singleton.class);
209 bind(WebOfTrustConnector.class).in(Singleton.class);
210 bind(WebOfTrustUpdater.class).in(Singleton.class);
211 bind(IdentityManager.class).in(Singleton.class);
212 bind(String.class).annotatedWith(Names.named("WebOfTrustContext")).toInstance("Sone");
213 bind(SonePlugin.class).toInstance(SonePlugin.this);
214 bind(FcpInterface.class).in(Singleton.class);
217 Injector injector = Guice.createInjector(freenetModule, soneModule);
218 core = injector.getInstance(Core.class);
220 /* create web of trust connector. */
221 webOfTrustConnector = injector.getInstance(WebOfTrustConnector.class);
223 /* create FCP interface. */
224 fcpInterface = injector.getInstance(FcpInterface.class);
225 core.setFcpInterface(fcpInterface);
227 /* create the web interface. */
228 webInterface = injector.getInstance(WebInterface.class);
229 eventBus.register(webInterface);
230 core.addCoreListener(webInterface);
232 boolean startupFailed = true;
237 if ((newConfiguration != null) && (oldConfiguration != newConfiguration)) {
238 logger.log(Level.INFO, "Setting configuration to file-based configuration.");
239 core.setConfiguration(newConfiguration);
241 webInterface.start();
242 webInterface.setFirstStart(firstStart);
243 webInterface.setNewConfig(newConfig);
244 startupFailed = false;
248 * we let the exception bubble up but shut the logging down so
249 * that the logfile is not swamped by the installed logging
250 * handlers of the failed instances.
261 public void terminate() {
263 /* stop the web interface. */
269 /* stop the web of trust connector. */
270 webOfTrustConnector.stop();
271 } catch (Throwable t1) {
272 logger.log(Level.SEVERE, "Error while shutting down!", t1);
274 /* shutdown logger. */
280 // INTERFACE FredPluginFCP
287 public void handle(PluginReplySender pluginReplySender, SimpleFieldSet parameters, Bucket data, int accessType) {
288 fcpInterface.handle(pluginReplySender, parameters, data, accessType);
292 // INTERFACE FredPluginL10n
299 public String getString(String key) {
300 return l10n.getBase().getString(key);
307 public void setLanguage(LANGUAGE newLanguage) {
308 l10n = new PluginL10n(this, newLanguage);
312 // INTERFACE FredPluginBaseL10n
319 public String getL10nFilesBasePath() {
327 public String getL10nFilesMask() {
328 return "sone.${lang}.properties";
335 public String getL10nOverrideFilesMask() {
336 return "sone.${lang}.override.properties";
343 public ClassLoader getPluginClassLoader() {
344 return SonePlugin.class.getClassLoader();
348 // INTERFACE FredPluginVersioned
355 public String getVersion() {
356 return VERSION.toString();