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