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