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.fcp.*;
27 import net.pterodactylus.sone.freenet.wot.*;
28 import net.pterodactylus.sone.web.*;
30 import freenet.l10n.BaseL10n.*;
31 import freenet.l10n.*;
32 import freenet.pluginmanager.*;
33 import freenet.support.*;
34 import freenet.support.api.*;
36 import com.google.common.annotations.*;
37 import com.google.common.cache.*;
38 import com.google.inject.*;
39 import com.google.inject.name.*;
40 import kotlin.jvm.functions.*;
43 * This class interfaces with Freenet. It is the class that is loaded by the
44 * node and starts up the whole Sone system.
46 public class SonePlugin implements FredPlugin, FredPluginFCP, FredPluginL10n, FredPluginBaseL10n, FredPluginThreadless, FredPluginVersioned {
48 private static final Logger soneLogger = getLogger("net.pterodactylus.sone");
51 /* initialize logging. */
52 soneLogger.setUseParentHandlers(false);
53 soneLogger.addHandler(new Handler() {
54 private final LoadingCache<String, Class<?>> classCache = CacheBuilder.newBuilder()
55 .build(new CacheLoader<String, Class<?>>() {
57 public Class<?> load(String key) throws Exception {
58 return SonePlugin.class.getClassLoader().loadClass(key);
63 public void publish(LogRecord logRecord) {
64 int recordLevel = logRecord.getLevel().intValue();
65 Class<?> loggingClass = classCache.getUnchecked(logRecord.getLoggerName());
66 if (recordLevel < Level.FINE.intValue()) {
67 freenet.support.Logger.debug(loggingClass, logRecord.getMessage(), logRecord.getThrown());
68 } else if (recordLevel < Level.INFO.intValue()) {
69 freenet.support.Logger.minor(loggingClass, logRecord.getMessage(), logRecord.getThrown());
70 } else if (recordLevel < Level.WARNING.intValue()) {
71 freenet.support.Logger.normal(loggingClass, logRecord.getMessage(), logRecord.getThrown());
72 } else if (recordLevel < Level.SEVERE.intValue()) {
73 freenet.support.Logger.warning(loggingClass, logRecord.getMessage(), logRecord.getThrown());
75 freenet.support.Logger.error(loggingClass, logRecord.getMessage(), logRecord.getThrown());
89 /** The current year at time of release. */
90 private static final int YEAR = 2019;
91 private static final String SONE_HOMEPAGE = "USK@nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI,DuQSUZiI~agF8c-6tjsFFGuZ8eICrzWCILB60nT8KKo,AQACAAE/sone/";
92 private static final int LATEST_EDITION = 79;
95 private static final Logger logger = getLogger(SonePlugin.class.getName());
97 private final Function1<Module[], Injector> injectorCreator;
99 /** The plugin respirator. */
100 private PluginRespirator pluginRespirator;
105 /** The web interface. */
106 private WebInterface webInterface;
108 /** The FCP interface. */
109 private FcpInterface fcpInterface;
111 /** The l10n helper. */
112 private PluginL10n l10n;
114 /** The web of trust connector. */
115 private WebOfTrustConnector webOfTrustConnector;
117 public SonePlugin() {
118 this(Guice::createInjector);
122 public SonePlugin(Function1<Module[], Injector> injectorCreator) {
123 this.injectorCreator = injectorCreator;
131 * Returns the plugin respirator for this plugin.
133 * @return The plugin respirator
135 public PluginRespirator pluginRespirator() {
136 return pluginRespirator;
140 * Returns the core started by this plugin.
149 * Returns the plugin’s l10n helper.
151 * @return The plugin’s l10n helper
153 public PluginL10n l10n() {
157 public static String getPluginVersion() {
158 net.pterodactylus.sone.main.Version version = VersionParserKt.getParsedVersion();
159 return (version == null) ? "unknown" : version.getNice();
162 public int getYear() {
166 public String getHomepage() {
167 return SONE_HOMEPAGE + LATEST_EDITION;
170 public static long getLatestEdition() {
171 return LATEST_EDITION;
175 // FREDPLUGIN METHODS
182 public void runPlugin(PluginRespirator pluginRespirator) {
183 this.pluginRespirator = pluginRespirator;
185 Injector injector = createInjector();
186 core = injector.getInstance(Core.class);
188 /* create web of trust connector. */
189 webOfTrustConnector = injector.getInstance(WebOfTrustConnector.class);
191 /* create FCP interface. */
192 fcpInterface = injector.getInstance(FcpInterface.class);
194 /* create the web interface. */
195 webInterface = injector.getInstance(WebInterface.class);
199 webInterface.start();
200 webInterface.setFirstStart(injector.getInstance(Key.get(Boolean.class, Names.named("FirstStart"))));
201 webInterface.setNewConfig(injector.getInstance(Key.get(Boolean.class, Names.named("NewConfig"))));
205 protected Injector createInjector() {
206 FreenetModule freenetModule = new FreenetModule(pluginRespirator);
207 AbstractModule soneModule = new SoneModule(this);
208 Module webInterfaceModule = new WebInterfaceModule();
210 return createInjector(freenetModule, soneModule, webInterfaceModule);
214 protected Injector createInjector(Module... modules) {
215 return injectorCreator.invoke(modules);
222 public void terminate() {
224 /* stop the web interface. */
230 /* stop the web of trust connector. */
231 webOfTrustConnector.stop();
232 } catch (Throwable t1) {
233 logger.log(Level.SEVERE, "Error while shutting down!", t1);
235 deregisterLoggerHandlers();
239 private void deregisterLoggerHandlers() {
240 for (Handler handler : soneLogger.getHandlers()) {
241 soneLogger.removeHandler(handler);
246 // INTERFACE FredPluginFCP
253 public void handle(PluginReplySender pluginReplySender, SimpleFieldSet parameters, Bucket data, int accessType) {
254 fcpInterface.handle(pluginReplySender, parameters, data, accessType);
258 // INTERFACE FredPluginL10n
265 public String getString(String key) {
266 return l10n.getBase().getString(key);
273 public void setLanguage(LANGUAGE newLanguage) {
274 l10n = new PluginL10n(this, newLanguage);
278 // INTERFACE FredPluginBaseL10n
285 public String getL10nFilesBasePath() {
293 public String getL10nFilesMask() {
294 return "sone.${lang}.properties";
301 public String getL10nOverrideFilesMask() {
302 return "sone.${lang}.override.properties";
309 public ClassLoader getPluginClassLoader() {
310 return SonePlugin.class.getClassLoader();
314 // INTERFACE FredPluginVersioned
321 public String getVersion() {
322 return getPluginVersion();