91f21eb7dca45e0ee3d9b5b53b41adb278b348cf
[Sone.git] / src / main / java / net / pterodactylus / sone / main / SonePlugin.java
1 /*
2  * Sone - SonePlugin.java - Copyright © 2010–2019 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.main;
19
20 import static java.util.logging.Logger.*;
21
22 import java.util.logging.Logger;
23 import java.util.logging.*;
24
25 import javax.annotation.Nonnull;
26
27 import net.pterodactylus.sone.core.*;
28 import net.pterodactylus.sone.core.event.*;
29 import net.pterodactylus.sone.fcp.*;
30 import net.pterodactylus.sone.freenet.wot.*;
31 import net.pterodactylus.sone.web.*;
32 import net.pterodactylus.sone.web.notification.NotificationHandler;
33 import net.pterodactylus.sone.web.notification.NotificationHandlerModule;
34
35 import freenet.l10n.BaseL10n.*;
36 import freenet.l10n.*;
37 import freenet.pluginmanager.*;
38 import freenet.support.*;
39 import freenet.support.api.*;
40
41 import com.google.common.annotations.*;
42 import com.google.common.eventbus.*;
43 import com.google.common.cache.*;
44 import com.google.inject.*;
45 import com.google.inject.Module;
46 import com.google.inject.name.*;
47 import kotlin.jvm.functions.*;
48
49 /**
50  * This class interfaces with Freenet. It is the class that is loaded by the
51  * node and starts up the whole Sone system.
52  */
53 public class SonePlugin implements FredPlugin, FredPluginFCP, FredPluginL10n, FredPluginBaseL10n, FredPluginThreadless, FredPluginVersioned {
54
55         private static final Logger soneLogger = getLogger("net.pterodactylus.sone");
56
57         static {
58                 /* initialize logging. */
59                 soneLogger.setUseParentHandlers(false);
60                 soneLogger.setLevel(Level.ALL);
61                 soneLogger.addHandler(new Handler() {
62                         private final LoadingCache<String, Class<?>> classCache = CacheBuilder.newBuilder()
63                                         .build(new CacheLoader<String, Class<?>>() {
64                                                 @Override
65                                                 public Class<?> load(@Nonnull String key) throws Exception {
66                                                         return SonePlugin.class.getClassLoader().loadClass(key);
67                                                 }
68                                         });
69
70                         @Override
71                         public void publish(LogRecord logRecord) {
72                                 int recordLevel = logRecord.getLevel().intValue();
73                                 Class<?> loggingClass = classCache.getUnchecked(logRecord.getLoggerName());
74                                 if (recordLevel < Level.FINE.intValue()) {
75                                         freenet.support.Logger.debug(loggingClass, logRecord.getMessage(), logRecord.getThrown());
76                                 } else if (recordLevel < Level.INFO.intValue()) {
77                                         freenet.support.Logger.minor(loggingClass, logRecord.getMessage(), logRecord.getThrown());
78                                 } else if (recordLevel < Level.WARNING.intValue()) {
79                                         freenet.support.Logger.normal(loggingClass, logRecord.getMessage(), logRecord.getThrown());
80                                 } else if (recordLevel < Level.SEVERE.intValue()) {
81                                         freenet.support.Logger.warning(loggingClass, logRecord.getMessage(), logRecord.getThrown());
82                                 } else {
83                                         freenet.support.Logger.error(loggingClass, logRecord.getMessage(), logRecord.getThrown());
84                                 }
85                         }
86
87                         @Override
88                         public void flush() {
89                         }
90
91                         @Override
92                         public void close() {
93                         }
94                 });
95         }
96
97         /** The current year at time of release. */
98         private static final int YEAR = 2019;
99         private static final String SONE_HOMEPAGE = "USK@nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI,DuQSUZiI~agF8c-6tjsFFGuZ8eICrzWCILB60nT8KKo,AQACAAE/sone/";
100         private static final int LATEST_EDITION = 79;
101
102         /** The logger. */
103         private static final Logger logger = getLogger(SonePlugin.class.getName());
104
105         private final Function1<Module[], Injector> injectorCreator;
106
107         /** The plugin respirator. */
108         private PluginRespirator pluginRespirator;
109
110         /** The core. */
111         private Core core;
112
113         /** The web interface. */
114         private WebInterface webInterface;
115
116         /** The FCP interface. */
117         private FcpInterface fcpInterface;
118
119         /** The l10n helper. */
120         private PluginL10n l10n;
121
122         /** The web of trust connector. */
123         private WebOfTrustConnector webOfTrustConnector;
124
125         public SonePlugin() {
126                 this(Guice::createInjector);
127         }
128
129         @VisibleForTesting
130         public SonePlugin(Function1<Module[], Injector> injectorCreator) {
131                 this.injectorCreator = injectorCreator;
132         }
133
134         //
135         // ACCESSORS
136         //
137
138         /**
139          * Returns the plugin respirator for this plugin.
140          *
141          * @return The plugin respirator
142          */
143         public PluginRespirator pluginRespirator() {
144                 return pluginRespirator;
145         }
146
147         /**
148          * Returns the core started by this plugin.
149          *
150          * @return The core
151          */
152         public Core core() {
153                 return core;
154         }
155
156         /**
157          * Returns the plugin’s l10n helper.
158          *
159          * @return The plugin’s l10n helper
160          */
161         public PluginL10n l10n() {
162                 return l10n;
163         }
164
165         public static String getPluginVersion() {
166                 net.pterodactylus.sone.main.Version version = VersionParserKt.getParsedVersion();
167                 return (version == null) ? "unknown" : version.getNice();
168         }
169
170         public int getYear() {
171                 return YEAR;
172         }
173
174         public String getHomepage() {
175                 return SONE_HOMEPAGE + LATEST_EDITION;
176         }
177
178         public static long getLatestEdition() {
179                 return LATEST_EDITION;
180         }
181
182         //
183         // FREDPLUGIN METHODS
184         //
185
186         /**
187          * {@inheritDoc}
188          */
189         @Override
190         public void runPlugin(PluginRespirator pluginRespirator) {
191                 this.pluginRespirator = pluginRespirator;
192
193                 Injector injector = createInjector();
194                 core = injector.getInstance(Core.class);
195
196                 /* create web of trust connector. */
197                 webOfTrustConnector = injector.getInstance(WebOfTrustConnector.class);
198
199                 /* create FCP interface. */
200                 fcpInterface = injector.getInstance(FcpInterface.class);
201
202                 /* create the web interface. */
203                 webInterface = injector.getInstance(WebInterface.class);
204
205                 /* we need to request this to install all notification handlers. */
206                 injector.getInstance(NotificationHandler.class);
207
208                 /* start core! */
209                 core.start();
210
211                 /* start the web interface! */
212                 webInterface.start();
213
214                 /* send some events on startup */
215                 EventBus eventBus = injector.getInstance(EventBus.class);
216
217                 /* first start? */
218                 if (injector.getInstance(Key.get(Boolean.class, Names.named("FirstStart")))) {
219                         eventBus.post(new FirstStart());
220                 } else {
221                         /* new config? */
222                         if (injector.getInstance(Key.get(Boolean.class, Names.named("NewConfig")))) {
223                                 eventBus.post(new ConfigNotRead());
224                         }
225                 }
226
227                 eventBus.post(new Startup());
228         }
229
230         @VisibleForTesting
231         protected Injector createInjector() {
232                 FreenetModule freenetModule = new FreenetModule(pluginRespirator);
233                 AbstractModule soneModule = new SoneModule(this, new EventBus());
234                 Module webInterfaceModule = new WebInterfaceModule();
235                 Module notificationHandlerModule = new NotificationHandlerModule();
236
237                 return createInjector(freenetModule, soneModule, webInterfaceModule, notificationHandlerModule);
238         }
239
240         @VisibleForTesting
241         protected Injector createInjector(Module... modules) {
242                 return injectorCreator.invoke(modules);
243         }
244
245         /**
246          * {@inheritDoc}
247          */
248         @Override
249         public void terminate() {
250                 try {
251                         /* stop the web interface. */
252                         webInterface.stop();
253
254                         /* stop the core. */
255                         core.stop();
256
257                         /* stop the web of trust connector. */
258                         webOfTrustConnector.stop();
259                 } catch (Throwable t1) {
260                         logger.log(Level.SEVERE, "Error while shutting down!", t1);
261                 } finally {
262                         deregisterLoggerHandlers();
263                 }
264         }
265
266         private void deregisterLoggerHandlers() {
267                 for (Handler handler : soneLogger.getHandlers()) {
268                         soneLogger.removeHandler(handler);
269                 }
270         }
271
272         //
273         // INTERFACE FredPluginFCP
274         //
275
276         /**
277          * {@inheritDoc}
278          */
279         @Override
280         public void handle(PluginReplySender pluginReplySender, SimpleFieldSet parameters, Bucket data, int accessType) {
281                 fcpInterface.handle(pluginReplySender, parameters, data, accessType);
282         }
283
284         //
285         // INTERFACE FredPluginL10n
286         //
287
288         /**
289          * {@inheritDoc}
290          */
291         @Override
292         public String getString(String key) {
293                 return l10n.getBase().getString(key);
294         }
295
296         /**
297          * {@inheritDoc}
298          */
299         @Override
300         public void setLanguage(LANGUAGE newLanguage) {
301                 l10n = new PluginL10n(this, newLanguage);
302         }
303
304         //
305         // INTERFACE FredPluginBaseL10n
306         //
307
308         /**
309          * {@inheritDoc}
310          */
311         @Override
312         public String getL10nFilesBasePath() {
313                 return "i18n";
314         }
315
316         /**
317          * {@inheritDoc}
318          */
319         @Override
320         public String getL10nFilesMask() {
321                 return "sone.${lang}.properties";
322         }
323
324         /**
325          * {@inheritDoc}
326          */
327         @Override
328         public String getL10nOverrideFilesMask() {
329                 return "sone.${lang}.override.properties";
330         }
331
332         /**
333          * {@inheritDoc}
334          */
335         @Override
336         public ClassLoader getPluginClassLoader() {
337                 return SonePlugin.class.getClassLoader();
338         }
339
340         //
341         // INTERFACE FredPluginVersioned
342         //
343
344         /**
345          * {@inheritDoc}
346          */
347         @Override
348         public String getVersion() {
349                 return getPluginVersion();
350         }
351
352 }