2 * Sone - SonePlugin.java - Copyright © 2010–2013 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;
20 import static com.google.common.base.Optional.of;
21 import static java.util.logging.Logger.getLogger;
24 import java.util.logging.Handler;
25 import java.util.logging.Level;
26 import java.util.logging.LogRecord;
27 import java.util.logging.Logger;
29 import net.pterodactylus.sone.core.Core;
30 import net.pterodactylus.sone.core.FreenetInterface;
31 import net.pterodactylus.sone.core.WebOfTrustUpdater;
32 import net.pterodactylus.sone.core.WebOfTrustUpdaterImpl;
33 import net.pterodactylus.sone.database.Database;
34 import net.pterodactylus.sone.database.PostBuilderFactory;
35 import net.pterodactylus.sone.database.PostProvider;
36 import net.pterodactylus.sone.database.PostReplyBuilderFactory;
37 import net.pterodactylus.sone.database.SoneProvider;
38 import net.pterodactylus.sone.database.memory.MemoryDatabase;
39 import net.pterodactylus.sone.fcp.FcpInterface;
40 import net.pterodactylus.sone.freenet.PluginStoreConfigurationBackend;
41 import net.pterodactylus.sone.freenet.plugin.PluginConnector;
42 import net.pterodactylus.sone.freenet.wot.Context;
43 import net.pterodactylus.sone.freenet.wot.IdentityManager;
44 import net.pterodactylus.sone.freenet.wot.IdentityManagerImpl;
45 import net.pterodactylus.sone.freenet.wot.WebOfTrustConnector;
46 import net.pterodactylus.sone.web.WebInterface;
47 import net.pterodactylus.util.config.Configuration;
48 import net.pterodactylus.util.config.ConfigurationException;
49 import net.pterodactylus.util.config.MapConfigurationBackend;
50 import net.pterodactylus.util.version.Version;
52 import com.google.common.base.Optional;
53 import com.google.common.eventbus.EventBus;
54 import com.google.inject.AbstractModule;
55 import com.google.inject.Guice;
56 import com.google.inject.Injector;
57 import com.google.inject.Singleton;
58 import com.google.inject.TypeLiteral;
59 import com.google.inject.matcher.Matchers;
60 import com.google.inject.spi.InjectionListener;
61 import com.google.inject.spi.TypeEncounter;
62 import com.google.inject.spi.TypeListener;
64 import freenet.client.async.DatabaseDisabledException;
65 import freenet.l10n.BaseL10n.LANGUAGE;
66 import freenet.l10n.PluginL10n;
67 import freenet.node.Node;
68 import freenet.pluginmanager.FredPlugin;
69 import freenet.pluginmanager.FredPluginBaseL10n;
70 import freenet.pluginmanager.FredPluginFCP;
71 import freenet.pluginmanager.FredPluginL10n;
72 import freenet.pluginmanager.FredPluginThreadless;
73 import freenet.pluginmanager.FredPluginVersioned;
74 import freenet.pluginmanager.PluginReplySender;
75 import freenet.pluginmanager.PluginRespirator;
76 import freenet.support.SimpleFieldSet;
77 import freenet.support.api.Bucket;
80 * This class interfaces with Freenet. It is the class that is loaded by the
81 * node and starts up the whole Sone system.
83 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
85 public class SonePlugin implements FredPlugin, FredPluginFCP, FredPluginL10n, FredPluginBaseL10n, FredPluginThreadless, FredPluginVersioned {
88 /* initialize logging. */
89 Logger soneLogger = getLogger("Sone");
90 soneLogger.addHandler(new Handler() {
92 public void publish(LogRecord logRecord) {
93 int recordLevel = logRecord.getLevel().intValue();
94 if (recordLevel < Level.FINE.intValue()) {
95 freenet.support.Logger.debug(logRecord.getLoggerName(), logRecord.getMessage(), logRecord.getThrown());
96 } else if (recordLevel < Level.INFO.intValue()) {
97 freenet.support.Logger.minor(logRecord.getLoggerName(), logRecord.getMessage(), logRecord.getThrown());
98 } else if (recordLevel < Level.WARNING.intValue()) {
99 freenet.support.Logger.normal(logRecord.getLoggerName(), logRecord.getMessage(), logRecord.getThrown());
100 } else if (recordLevel < Level.SEVERE.intValue()) {
101 freenet.support.Logger.warning(logRecord.getLoggerName(), logRecord.getMessage(), logRecord.getThrown());
103 freenet.support.Logger.error(logRecord.getLoggerName(), logRecord.getMessage(), logRecord.getThrown());
108 public void flush() {
112 public void close() {
118 public static final Version VERSION = new Version(0, 8, 9);
121 private static final Logger logger = getLogger("Sone.Plugin");
123 /** The plugin respirator. */
124 private PluginRespirator pluginRespirator;
129 /** The web interface. */
130 private WebInterface webInterface;
132 /** The FCP interface. */
133 private FcpInterface fcpInterface;
135 /** The l10n helper. */
136 private PluginL10n l10n;
138 /** The web of trust connector. */
139 private WebOfTrustConnector webOfTrustConnector;
146 * Returns the plugin respirator for this plugin.
148 * @return The plugin respirator
150 public PluginRespirator pluginRespirator() {
151 return pluginRespirator;
155 * Returns the core started by this plugin.
164 * Returns the plugin’s l10n helper.
166 * @return The plugin’s l10n helper
168 public PluginL10n l10n() {
173 // FREDPLUGIN METHODS
180 public void runPlugin(PluginRespirator pluginRespirator) {
181 this.pluginRespirator = pluginRespirator;
183 /* create a configuration. */
184 Configuration oldConfiguration;
185 Configuration newConfiguration = null;
186 boolean firstStart = !new File("sone.properties").exists();
187 boolean newConfig = false;
189 oldConfiguration = new Configuration(new MapConfigurationBackend(new File("sone.properties"), false));
190 newConfiguration = oldConfiguration;
191 } catch (ConfigurationException ce1) {
193 logger.log(Level.INFO, "Could not load configuration file, trying plugin store…", ce1);
195 newConfiguration = new Configuration(new MapConfigurationBackend(new File("sone.properties"), true));
196 logger.log(Level.INFO, "Created new configuration file.");
197 } catch (ConfigurationException ce2) {
198 logger.log(Level.SEVERE, "Could not create configuration file, using Plugin Store!", ce2);
201 oldConfiguration = new Configuration(new PluginStoreConfigurationBackend(pluginRespirator));
202 logger.log(Level.INFO, "Plugin store loaded.");
203 } catch (DatabaseDisabledException dde1) {
204 logger.log(Level.SEVERE, "Could not load any configuration, using empty configuration!");
205 oldConfiguration = new Configuration(new MapConfigurationBackend());
209 final Configuration startConfiguration;
210 if ((newConfiguration != null) && (oldConfiguration != newConfiguration)) {
211 logger.log(Level.INFO, "Setting configuration to file-based configuration.");
212 startConfiguration = newConfiguration;
214 startConfiguration = oldConfiguration;
216 final EventBus eventBus = new EventBus();
218 /* Freenet injector configuration. */
219 AbstractModule freenetModule = new AbstractModule() {
222 @SuppressWarnings("synthetic-access")
223 protected void configure() {
224 bind(PluginRespirator.class).toInstance(SonePlugin.this.pluginRespirator);
225 bind(Node.class).toInstance(SonePlugin.this.pluginRespirator.getNode());
228 /* Sone injector configuration. */
229 AbstractModule soneModule = new AbstractModule() {
232 protected void configure() {
233 bind(EventBus.class).toInstance(eventBus);
234 bind(Configuration.class).toInstance(startConfiguration);
235 Context context = new Context("Sone");
236 bind(Context.class).toInstance(context);
237 bind(getOptionalContextTypeLiteral()).toInstance(of(context));
238 bind(SonePlugin.class).toInstance(SonePlugin.this);
239 bindListener(Matchers.any(), new TypeListener() {
242 public <I> void hear(TypeLiteral<I> typeLiteral, TypeEncounter<I> typeEncounter) {
243 typeEncounter.register(new InjectionListener<I>() {
246 public void afterInjection(I injectee) {
247 eventBus.register(injectee);
254 private TypeLiteral<Optional<Context>> getOptionalContextTypeLiteral() {
255 return new TypeLiteral<Optional<Context>>() {
260 Injector injector = Guice.createInjector(freenetModule, soneModule);
261 core = injector.getInstance(Core.class);
263 /* create web of trust connector. */
264 webOfTrustConnector = injector.getInstance(WebOfTrustConnector.class);
266 /* create FCP interface. */
267 fcpInterface = injector.getInstance(FcpInterface.class);
269 /* create the web interface. */
270 webInterface = injector.getInstance(WebInterface.class);
274 webInterface.start();
275 webInterface.setFirstStart(firstStart);
276 webInterface.setNewConfig(newConfig);
283 public void terminate() {
285 /* stop the web interface. */
291 /* stop the web of trust connector. */
292 webOfTrustConnector.stop();
293 } catch (Throwable t1) {
294 logger.log(Level.SEVERE, "Error while shutting down!", t1);
299 // INTERFACE FredPluginFCP
306 public void handle(PluginReplySender pluginReplySender, SimpleFieldSet parameters, Bucket data, int accessType) {
307 fcpInterface.handle(pluginReplySender, parameters, data, accessType);
311 // INTERFACE FredPluginL10n
318 public String getString(String key) {
319 return l10n.getBase().getString(key);
326 public void setLanguage(LANGUAGE newLanguage) {
327 l10n = new PluginL10n(this, newLanguage);
331 // INTERFACE FredPluginBaseL10n
338 public String getL10nFilesBasePath() {
346 public String getL10nFilesMask() {
347 return "sone.${lang}.properties";
354 public String getL10nOverrideFilesMask() {
355 return "sone.${lang}.override.properties";
362 public ClassLoader getPluginClassLoader() {
363 return SonePlugin.class.getClassLoader();
367 // INTERFACE FredPluginVersioned
374 public String getVersion() {
375 return VERSION.toString();