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