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