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