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