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