2 * Sone - SonePlugin.java - Copyright © 2010–2019 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 java.util.logging.Logger.*;
22 import java.util.logging.Logger;
23 import java.util.logging.*;
25 import com.google.common.eventbus.*;
26 import net.pterodactylus.sone.core.*;
27 import net.pterodactylus.sone.fcp.*;
28 import net.pterodactylus.sone.freenet.wot.*;
29 import net.pterodactylus.sone.web.*;
31 import freenet.l10n.BaseL10n.*;
32 import freenet.l10n.*;
33 import freenet.pluginmanager.*;
34 import freenet.support.*;
35 import freenet.support.api.*;
37 import com.google.common.annotations.*;
38 import com.google.common.cache.*;
39 import com.google.inject.*;
40 import com.google.inject.name.*;
41 import kotlin.jvm.functions.*;
44 * This class interfaces with Freenet. It is the class that is loaded by the
45 * node and starts up the whole Sone system.
47 public class SonePlugin implements FredPlugin, FredPluginFCP, FredPluginL10n, FredPluginBaseL10n, FredPluginThreadless, FredPluginVersioned {
49 private static final Logger soneLogger = getLogger("net.pterodactylus.sone");
52 /* initialize logging. */
53 soneLogger.setUseParentHandlers(false);
54 soneLogger.setLevel(Level.ALL);
55 soneLogger.addHandler(new Handler() {
56 private final LoadingCache<String, Class<?>> classCache = CacheBuilder.newBuilder()
57 .build(new CacheLoader<String, Class<?>>() {
59 public Class<?> load(String key) throws Exception {
60 return SonePlugin.class.getClassLoader().loadClass(key);
65 public void publish(LogRecord logRecord) {
66 int recordLevel = logRecord.getLevel().intValue();
67 Class<?> loggingClass = classCache.getUnchecked(logRecord.getLoggerName());
68 if (recordLevel < Level.FINE.intValue()) {
69 freenet.support.Logger.debug(loggingClass, logRecord.getMessage(), logRecord.getThrown());
70 } else if (recordLevel < Level.INFO.intValue()) {
71 freenet.support.Logger.minor(loggingClass, logRecord.getMessage(), logRecord.getThrown());
72 } else if (recordLevel < Level.WARNING.intValue()) {
73 freenet.support.Logger.normal(loggingClass, logRecord.getMessage(), logRecord.getThrown());
74 } else if (recordLevel < Level.SEVERE.intValue()) {
75 freenet.support.Logger.warning(loggingClass, logRecord.getMessage(), logRecord.getThrown());
77 freenet.support.Logger.error(loggingClass, logRecord.getMessage(), logRecord.getThrown());
91 /** The current year at time of release. */
92 private static final int YEAR = 2019;
93 private static final String SONE_HOMEPAGE = "USK@nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI,DuQSUZiI~agF8c-6tjsFFGuZ8eICrzWCILB60nT8KKo,AQACAAE/sone/";
94 private static final int LATEST_EDITION = 79;
97 private static final Logger logger = getLogger(SonePlugin.class.getName());
99 private final Function1<Module[], Injector> injectorCreator;
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;
119 public SonePlugin() {
120 this(Guice::createInjector);
124 public SonePlugin(Function1<Module[], Injector> injectorCreator) {
125 this.injectorCreator = injectorCreator;
133 * Returns the plugin respirator for this plugin.
135 * @return The plugin respirator
137 public PluginRespirator pluginRespirator() {
138 return pluginRespirator;
142 * Returns the core started by this plugin.
151 * Returns the plugin’s l10n helper.
153 * @return The plugin’s l10n helper
155 public PluginL10n l10n() {
159 public static String getPluginVersion() {
160 net.pterodactylus.sone.main.Version version = VersionParserKt.getParsedVersion();
161 return (version == null) ? "unknown" : version.getNice();
164 public int getYear() {
168 public String getHomepage() {
169 return SONE_HOMEPAGE + LATEST_EDITION;
172 public static long getLatestEdition() {
173 return LATEST_EDITION;
177 // FREDPLUGIN METHODS
184 public void runPlugin(PluginRespirator pluginRespirator) {
185 this.pluginRespirator = pluginRespirator;
187 Injector injector = createInjector();
188 core = injector.getInstance(Core.class);
190 /* create web of trust connector. */
191 webOfTrustConnector = injector.getInstance(WebOfTrustConnector.class);
193 /* create FCP interface. */
194 fcpInterface = injector.getInstance(FcpInterface.class);
196 /* create the web interface. */
197 webInterface = injector.getInstance(WebInterface.class);
201 webInterface.start();
202 webInterface.setFirstStart(injector.getInstance(Key.get(Boolean.class, Names.named("FirstStart"))));
203 webInterface.setNewConfig(injector.getInstance(Key.get(Boolean.class, Names.named("NewConfig"))));
207 protected Injector createInjector() {
208 FreenetModule freenetModule = new FreenetModule(pluginRespirator);
209 AbstractModule soneModule = new SoneModule(this, new EventBus());
210 Module webInterfaceModule = new WebInterfaceModule();
212 return createInjector(freenetModule, soneModule, webInterfaceModule);
216 protected Injector createInjector(Module... modules) {
217 return injectorCreator.invoke(modules);
224 public void terminate() {
226 /* stop the web interface. */
232 /* stop the web of trust connector. */
233 webOfTrustConnector.stop();
234 } catch (Throwable t1) {
235 logger.log(Level.SEVERE, "Error while shutting down!", t1);
237 deregisterLoggerHandlers();
241 private void deregisterLoggerHandlers() {
242 for (Handler handler : soneLogger.getHandlers()) {
243 soneLogger.removeHandler(handler);
248 // INTERFACE FredPluginFCP
255 public void handle(PluginReplySender pluginReplySender, SimpleFieldSet parameters, Bucket data, int accessType) {
256 fcpInterface.handle(pluginReplySender, parameters, data, accessType);
260 // INTERFACE FredPluginL10n
267 public String getString(String key) {
268 return l10n.getBase().getString(key);
275 public void setLanguage(LANGUAGE newLanguage) {
276 l10n = new PluginL10n(this, newLanguage);
280 // INTERFACE FredPluginBaseL10n
287 public String getL10nFilesBasePath() {
295 public String getL10nFilesMask() {
296 return "sone.${lang}.properties";
303 public String getL10nOverrideFilesMask() {
304 return "sone.${lang}.override.properties";
311 public ClassLoader getPluginClassLoader() {
312 return SonePlugin.class.getClassLoader();
316 // INTERFACE FredPluginVersioned
323 public String getVersion() {
324 return getPluginVersion();