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