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 net.pterodactylus.sone.core.*;
26 import net.pterodactylus.sone.core.event.*;
27 import net.pterodactylus.sone.fcp.*;
28 import net.pterodactylus.sone.freenet.wot.*;
29 import net.pterodactylus.sone.web.*;
30 import net.pterodactylus.sone.web.notification.NotificationHandler;
31 import net.pterodactylus.sone.web.notification.NotificationHandlerModule;
33 import freenet.l10n.BaseL10n.*;
34 import freenet.l10n.*;
35 import freenet.pluginmanager.*;
36 import freenet.support.*;
37 import freenet.support.api.*;
39 import com.google.common.annotations.*;
40 import com.google.common.eventbus.*;
41 import com.google.common.cache.*;
42 import com.google.inject.*;
43 import com.google.inject.Module;
44 import com.google.inject.name.*;
45 import kotlin.jvm.functions.*;
48 * This class interfaces with Freenet. It is the class that is loaded by the
49 * node and starts up the whole Sone system.
51 public class SonePlugin implements FredPlugin, FredPluginFCP, FredPluginL10n, FredPluginBaseL10n, FredPluginThreadless, FredPluginVersioned {
53 private static final Logger soneLogger = getLogger("net.pterodactylus.sone");
56 /* initialize logging. */
57 soneLogger.setUseParentHandlers(false);
58 soneLogger.setLevel(Level.ALL);
59 soneLogger.addHandler(new Handler() {
60 private final LoadingCache<String, Class<?>> classCache = CacheBuilder.newBuilder()
61 .build(new CacheLoader<String, Class<?>>() {
63 public Class<?> load(String key) throws Exception {
64 return SonePlugin.class.getClassLoader().loadClass(key);
69 public void publish(LogRecord logRecord) {
70 int recordLevel = logRecord.getLevel().intValue();
71 Class<?> loggingClass = classCache.getUnchecked(logRecord.getLoggerName());
72 if (recordLevel < Level.FINE.intValue()) {
73 freenet.support.Logger.debug(loggingClass, logRecord.getMessage(), logRecord.getThrown());
74 } else if (recordLevel < Level.INFO.intValue()) {
75 freenet.support.Logger.minor(loggingClass, logRecord.getMessage(), logRecord.getThrown());
76 } else if (recordLevel < Level.WARNING.intValue()) {
77 freenet.support.Logger.normal(loggingClass, logRecord.getMessage(), logRecord.getThrown());
78 } else if (recordLevel < Level.SEVERE.intValue()) {
79 freenet.support.Logger.warning(loggingClass, logRecord.getMessage(), logRecord.getThrown());
81 freenet.support.Logger.error(loggingClass, logRecord.getMessage(), logRecord.getThrown());
95 /** The current year at time of release. */
96 private static final int YEAR = 2019;
97 private static final String SONE_HOMEPAGE = "USK@nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI,DuQSUZiI~agF8c-6tjsFFGuZ8eICrzWCILB60nT8KKo,AQACAAE/sone/";
98 private static final int LATEST_EDITION = 79;
101 private static final Logger logger = getLogger(SonePlugin.class.getName());
103 private final Function1<Module[], Injector> injectorCreator;
105 /** The plugin respirator. */
106 private PluginRespirator pluginRespirator;
111 /** The web interface. */
112 private WebInterface webInterface;
114 /** The FCP interface. */
115 private FcpInterface fcpInterface;
117 /** The l10n helper. */
118 private PluginL10n l10n;
120 /** The web of trust connector. */
121 private WebOfTrustConnector webOfTrustConnector;
123 public SonePlugin() {
124 this(Guice::createInjector);
128 public SonePlugin(Function1<Module[], Injector> injectorCreator) {
129 this.injectorCreator = injectorCreator;
137 * Returns the plugin respirator for this plugin.
139 * @return The plugin respirator
141 public PluginRespirator pluginRespirator() {
142 return pluginRespirator;
146 * Returns the core started by this plugin.
155 * Returns the plugin’s l10n helper.
157 * @return The plugin’s l10n helper
159 public PluginL10n l10n() {
163 public static String getPluginVersion() {
164 net.pterodactylus.sone.main.Version version = VersionParserKt.getParsedVersion();
165 return (version == null) ? "unknown" : version.getNice();
168 public int getYear() {
172 public String getHomepage() {
173 return SONE_HOMEPAGE + LATEST_EDITION;
176 public static long getLatestEdition() {
177 return LATEST_EDITION;
181 // FREDPLUGIN METHODS
188 public void runPlugin(PluginRespirator pluginRespirator) {
189 this.pluginRespirator = pluginRespirator;
191 Injector injector = createInjector();
192 core = injector.getInstance(Core.class);
194 /* create web of trust connector. */
195 webOfTrustConnector = injector.getInstance(WebOfTrustConnector.class);
197 /* create FCP interface. */
198 fcpInterface = injector.getInstance(FcpInterface.class);
200 /* create the web interface. */
201 webInterface = injector.getInstance(WebInterface.class);
203 /* we need to request this to install all notification handlers. */
204 injector.getInstance(NotificationHandler.class);
209 /* start the web interface! */
210 webInterface.start();
213 if (injector.getInstance(Key.get(Boolean.class, Names.named("FirstStart")))) {
214 injector.getInstance(EventBus.class).post(new FirstStart());
217 if (injector.getInstance(Key.get(Boolean.class, Names.named("NewConfig")))) {
218 injector.getInstance(EventBus.class).post(new ConfigNotRead());
224 protected Injector createInjector() {
225 FreenetModule freenetModule = new FreenetModule(pluginRespirator);
226 AbstractModule soneModule = new SoneModule(this, new EventBus());
227 Module webInterfaceModule = new WebInterfaceModule();
228 Module notificationHandlerModule = new NotificationHandlerModule();
230 return createInjector(freenetModule, soneModule, webInterfaceModule, notificationHandlerModule);
234 protected Injector createInjector(Module... modules) {
235 return injectorCreator.invoke(modules);
242 public void terminate() {
244 /* stop the web interface. */
250 /* stop the web of trust connector. */
251 webOfTrustConnector.stop();
252 } catch (Throwable t1) {
253 logger.log(Level.SEVERE, "Error while shutting down!", t1);
255 deregisterLoggerHandlers();
259 private void deregisterLoggerHandlers() {
260 for (Handler handler : soneLogger.getHandlers()) {
261 soneLogger.removeHandler(handler);
266 // INTERFACE FredPluginFCP
273 public void handle(PluginReplySender pluginReplySender, SimpleFieldSet parameters, Bucket data, int accessType) {
274 fcpInterface.handle(pluginReplySender, parameters, data, accessType);
278 // INTERFACE FredPluginL10n
285 public String getString(String key) {
286 return l10n.getBase().getString(key);
293 public void setLanguage(LANGUAGE newLanguage) {
294 l10n = new PluginL10n(this, newLanguage);
298 // INTERFACE FredPluginBaseL10n
305 public String getL10nFilesBasePath() {
313 public String getL10nFilesMask() {
314 return "sone.${lang}.properties";
321 public String getL10nOverrideFilesMask() {
322 return "sone.${lang}.override.properties";
329 public ClassLoader getPluginClassLoader() {
330 return SonePlugin.class.getClassLoader();
334 // INTERFACE FredPluginVersioned
341 public String getVersion() {
342 return getPluginVersion();