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.fcp.FcpInterface;
31 import net.pterodactylus.sone.freenet.PluginStoreConfigurationBackend;
32 import net.pterodactylus.sone.freenet.wot.Context;
33 import net.pterodactylus.sone.freenet.wot.WebOfTrustConnector;
34 import net.pterodactylus.sone.web.WebInterface;
35 import net.pterodactylus.util.config.Configuration;
36 import net.pterodactylus.util.config.ConfigurationException;
37 import net.pterodactylus.util.config.MapConfigurationBackend;
38 import net.pterodactylus.util.version.Version;
40 import com.google.common.base.Optional;
41 import com.google.common.cache.CacheBuilder;
42 import com.google.common.cache.CacheLoader;
43 import com.google.common.cache.LoadingCache;
44 import com.google.common.eventbus.EventBus;
45 import com.google.inject.AbstractModule;
46 import com.google.inject.Guice;
47 import com.google.inject.Injector;
48 import com.google.inject.TypeLiteral;
49 import com.google.inject.matcher.Matchers;
50 import com.google.inject.spi.InjectionListener;
51 import com.google.inject.spi.TypeEncounter;
52 import com.google.inject.spi.TypeListener;
54 import freenet.client.async.PersistenceDisabledException;
55 import freenet.l10n.BaseL10n.LANGUAGE;
56 import freenet.l10n.PluginL10n;
57 import freenet.node.Node;
58 import freenet.pluginmanager.FredPlugin;
59 import freenet.pluginmanager.FredPluginBaseL10n;
60 import freenet.pluginmanager.FredPluginFCP;
61 import freenet.pluginmanager.FredPluginL10n;
62 import freenet.pluginmanager.FredPluginThreadless;
63 import freenet.pluginmanager.FredPluginVersioned;
64 import freenet.pluginmanager.PluginReplySender;
65 import freenet.pluginmanager.PluginRespirator;
66 import freenet.support.SimpleFieldSet;
67 import freenet.support.api.Bucket;
70 * This class interfaces with Freenet. It is the class that is loaded by the
71 * node and starts up the whole Sone system.
73 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
75 public class SonePlugin implements FredPlugin, FredPluginFCP, FredPluginL10n, FredPluginBaseL10n, FredPluginThreadless, FredPluginVersioned {
77 private static final Logger soneLogger = getLogger("net.pterodactylus.sone");
80 /* initialize logging. */
81 soneLogger.setUseParentHandlers(false);
82 soneLogger.addHandler(new Handler() {
83 private final LoadingCache<String, Class<?>> classCache = CacheBuilder.newBuilder()
84 .build(new CacheLoader<String, Class<?>>() {
86 public Class<?> load(String key) throws Exception {
87 return Class.forName(key);
92 public void publish(LogRecord logRecord) {
93 int recordLevel = logRecord.getLevel().intValue();
94 Class<?> loggingClass = classCache.getUnchecked(logRecord.getLoggerName());
95 if (recordLevel < Level.FINE.intValue()) {
96 freenet.support.Logger.debug(loggingClass, logRecord.getMessage(), logRecord.getThrown());
97 } else if (recordLevel < Level.INFO.intValue()) {
98 freenet.support.Logger.minor(loggingClass, logRecord.getMessage(), logRecord.getThrown());
99 } else if (recordLevel < Level.WARNING.intValue()) {
100 freenet.support.Logger.normal(loggingClass, logRecord.getMessage(), logRecord.getThrown());
101 } else if (recordLevel < Level.SEVERE.intValue()) {
102 freenet.support.Logger.warning(loggingClass, logRecord.getMessage(), logRecord.getThrown());
104 freenet.support.Logger.error(loggingClass, logRecord.getMessage(), logRecord.getThrown());
109 public void flush() {
113 public void close() {
119 public static final Version VERSION = new Version(0, 9, 3);
121 /** The current year at time of release. */
122 private static final int YEAR = 2015;
123 private static final String SONE_HOMEPAGE = "USK@nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI,DuQSUZiI~agF8c-6tjsFFGuZ8eICrzWCILB60nT8KKo,AQACAAE/sone/";
124 private static final int LATEST_EDITION = 70;
127 private static final Logger logger = getLogger(SonePlugin.class.getName());
129 /** The plugin respirator. */
130 private PluginRespirator pluginRespirator;
135 /** The web interface. */
136 private WebInterface webInterface;
138 /** The FCP interface. */
139 private FcpInterface fcpInterface;
141 /** The l10n helper. */
142 private PluginL10n l10n;
144 /** The web of trust connector. */
145 private WebOfTrustConnector webOfTrustConnector;
152 * Returns the plugin respirator for this plugin.
154 * @return The plugin respirator
156 public PluginRespirator pluginRespirator() {
157 return pluginRespirator;
161 * Returns the core started by this plugin.
170 * Returns the plugin’s l10n helper.
172 * @return The plugin’s l10n helper
174 public PluginL10n l10n() {
178 public static int getYear() {
182 public static String getHomepage() {
183 return SONE_HOMEPAGE + LATEST_EDITION;
187 // FREDPLUGIN METHODS
194 public void runPlugin(PluginRespirator pluginRespirator) {
195 this.pluginRespirator = pluginRespirator;
197 /* create a configuration. */
198 Configuration oldConfiguration;
199 Configuration newConfiguration = null;
200 boolean firstStart = !new File("sone.properties").exists();
201 boolean newConfig = false;
203 oldConfiguration = new Configuration(new MapConfigurationBackend(new File("sone.properties"), false));
204 newConfiguration = oldConfiguration;
205 } catch (ConfigurationException ce1) {
207 logger.log(Level.INFO, "Could not load configuration file, trying plugin store…", ce1);
209 newConfiguration = new Configuration(new MapConfigurationBackend(new File("sone.properties"), true));
210 logger.log(Level.INFO, "Created new configuration file.");
211 } catch (ConfigurationException ce2) {
212 logger.log(Level.SEVERE, "Could not create configuration file, using Plugin Store!", ce2);
215 oldConfiguration = new Configuration(new PluginStoreConfigurationBackend(pluginRespirator));
216 logger.log(Level.INFO, "Plugin store loaded.");
217 } catch (PersistenceDisabledException pde1) {
218 logger.log(Level.SEVERE, "Could not load any configuration, using empty configuration!");
219 oldConfiguration = new Configuration(new MapConfigurationBackend());
223 final Configuration startConfiguration;
224 if ((newConfiguration != null) && (oldConfiguration != newConfiguration)) {
225 logger.log(Level.INFO, "Setting configuration to file-based configuration.");
226 startConfiguration = newConfiguration;
228 startConfiguration = oldConfiguration;
230 final EventBus eventBus = new EventBus();
232 /* Freenet injector configuration. */
233 AbstractModule freenetModule = new AbstractModule() {
236 @SuppressWarnings("synthetic-access")
237 protected void configure() {
238 bind(PluginRespirator.class).toInstance(SonePlugin.this.pluginRespirator);
239 bind(Node.class).toInstance(SonePlugin.this.pluginRespirator.getNode());
242 /* Sone injector configuration. */
243 AbstractModule soneModule = new AbstractModule() {
246 protected void configure() {
247 bind(EventBus.class).toInstance(eventBus);
248 bind(Configuration.class).toInstance(startConfiguration);
249 Context context = new Context("Sone");
250 bind(Context.class).toInstance(context);
251 bind(getOptionalContextTypeLiteral()).toInstance(of(context));
252 bind(SonePlugin.class).toInstance(SonePlugin.this);
253 bindListener(Matchers.any(), new TypeListener() {
256 public <I> void hear(TypeLiteral<I> typeLiteral, TypeEncounter<I> typeEncounter) {
257 typeEncounter.register(new InjectionListener<I>() {
260 public void afterInjection(I injectee) {
261 eventBus.register(injectee);
268 private TypeLiteral<Optional<Context>> getOptionalContextTypeLiteral() {
269 return new TypeLiteral<Optional<Context>>() {
274 Injector injector = Guice.createInjector(freenetModule, soneModule);
275 core = injector.getInstance(Core.class);
277 /* create web of trust connector. */
278 webOfTrustConnector = injector.getInstance(WebOfTrustConnector.class);
280 /* create FCP interface. */
281 fcpInterface = injector.getInstance(FcpInterface.class);
283 /* create the web interface. */
284 webInterface = injector.getInstance(WebInterface.class);
288 webInterface.start();
289 webInterface.setFirstStart(firstStart);
290 webInterface.setNewConfig(newConfig);
297 public void terminate() {
298 deregisterLoggerHandlers();
300 /* stop the web interface. */
306 /* stop the web of trust connector. */
307 webOfTrustConnector.stop();
308 } catch (Throwable t1) {
309 logger.log(Level.SEVERE, "Error while shutting down!", t1);
313 private void deregisterLoggerHandlers() {
314 for (Handler handler : soneLogger.getHandlers()) {
315 soneLogger.removeHandler(handler);
320 // INTERFACE FredPluginFCP
327 public void handle(PluginReplySender pluginReplySender, SimpleFieldSet parameters, Bucket data, int accessType) {
328 fcpInterface.handle(pluginReplySender, parameters, data, accessType);
332 // INTERFACE FredPluginL10n
339 public String getString(String key) {
340 return l10n.getBase().getString(key);
347 public void setLanguage(LANGUAGE newLanguage) {
348 l10n = new PluginL10n(this, newLanguage);
352 // INTERFACE FredPluginBaseL10n
359 public String getL10nFilesBasePath() {
367 public String getL10nFilesMask() {
368 return "sone.${lang}.properties";
375 public String getL10nOverrideFilesMask() {
376 return "sone.${lang}.override.properties";
383 public ClassLoader getPluginClassLoader() {
384 return SonePlugin.class.getClassLoader();
388 // INTERFACE FredPluginVersioned
395 public String getVersion() {
396 return VERSION.toString();