Re-enable Freenet logger.
[Sone.git] / src / main / java / net / pterodactylus / sone / main / SonePlugin.java
1 /*
2  * FreenetSone - SonePlugin.java - Copyright © 2010 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 java.io.File;
21 import java.util.Collections;
22 import java.util.logging.Level;
23 import java.util.logging.LogRecord;
24 import java.util.logging.Logger;
25
26 import net.pterodactylus.sone.core.Core;
27 import net.pterodactylus.sone.core.FreenetInterface;
28 import net.pterodactylus.sone.freenet.PluginStoreConfigurationBackend;
29 import net.pterodactylus.sone.freenet.wot.IdentityManager;
30 import net.pterodactylus.sone.freenet.wot.PluginConnector;
31 import net.pterodactylus.sone.freenet.wot.WebOfTrustConnector;
32 import net.pterodactylus.sone.web.WebInterface;
33 import net.pterodactylus.util.config.Configuration;
34 import net.pterodactylus.util.config.ConfigurationException;
35 import net.pterodactylus.util.config.MapConfigurationBackend;
36 import net.pterodactylus.util.config.XMLConfigurationBackend;
37 import net.pterodactylus.util.logging.Logging;
38 import net.pterodactylus.util.logging.LoggingListener;
39 import net.pterodactylus.util.version.Version;
40 import freenet.client.async.DatabaseDisabledException;
41 import freenet.l10n.BaseL10n.LANGUAGE;
42 import freenet.l10n.PluginL10n;
43 import freenet.pluginmanager.FredPlugin;
44 import freenet.pluginmanager.FredPluginBaseL10n;
45 import freenet.pluginmanager.FredPluginL10n;
46 import freenet.pluginmanager.FredPluginThreadless;
47 import freenet.pluginmanager.FredPluginVersioned;
48 import freenet.pluginmanager.PluginRespirator;
49 import freenet.pluginmanager.PluginStore;
50
51 /**
52  * This class interfaces with Freenet. It is the class that is loaded by the
53  * node and starts up the whole Sone system.
54  *
55  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
56  */
57 public class SonePlugin implements FredPlugin, FredPluginL10n, FredPluginBaseL10n, FredPluginThreadless, FredPluginVersioned {
58
59         static {
60                 /* initialize logging. */
61                 Logging.setup("sone");
62                 Logging.setupConsoleLogging();
63                 Logging.addLoggingListener(new LoggingListener() {
64
65                         @Override
66                         public void logged(LogRecord logRecord) {
67                                 Class<?> loggerClass = Logging.getLoggerClass(logRecord.getLoggerName());
68                                 int recordLevel = logRecord.getLevel().intValue();
69                                 if (recordLevel < Level.FINE.intValue()) {
70                                         freenet.support.Logger.debug(loggerClass, String.format(logRecord.getMessage(), logRecord.getParameters()), logRecord.getThrown());
71                                 } else if (recordLevel < Level.INFO.intValue()) {
72                                         freenet.support.Logger.minor(loggerClass, String.format(logRecord.getMessage(), logRecord.getParameters()), logRecord.getThrown());
73                                 } else if (recordLevel < Level.WARNING.intValue()) {
74                                         freenet.support.Logger.normal(loggerClass, String.format(logRecord.getMessage(), logRecord.getParameters()), logRecord.getThrown());
75                                 } else if (recordLevel < Level.SEVERE.intValue()) {
76                                         freenet.support.Logger.warning(loggerClass, String.format(logRecord.getMessage(), logRecord.getParameters()), logRecord.getThrown());
77                                 } else {
78                                         freenet.support.Logger.error(loggerClass, String.format(logRecord.getMessage(), logRecord.getParameters()), logRecord.getThrown());
79                                 }
80                         }
81                 });
82         }
83
84         /** The version. */
85         public static final Version VERSION = new Version("RC1", 0, 2);
86
87         /** The logger. */
88         private static final Logger logger = Logging.getLogger(SonePlugin.class);
89
90         /** The plugin respirator. */
91         private PluginRespirator pluginRespirator;
92
93         /** The core. */
94         private Core core;
95
96         /** The web interface. */
97         private WebInterface webInterface;
98
99         /** The l10n helper. */
100         private PluginL10n l10n;
101
102         /** The plugin store. */
103         private PluginStore pluginStore;
104
105         /** The identity manager. */
106         private IdentityManager identityManager;
107
108         //
109         // ACCESSORS
110         //
111
112         /**
113          * Returns the plugin respirator for this plugin.
114          *
115          * @return The plugin respirator
116          */
117         public PluginRespirator pluginRespirator() {
118                 return pluginRespirator;
119         }
120
121         /**
122          * Returns the core started by this plugin.
123          *
124          * @return The core
125          */
126         public Core core() {
127                 return core;
128         }
129
130         /**
131          * Returns the plugin’s l10n helper.
132          *
133          * @return The plugin’s l10n helper
134          */
135         public PluginL10n l10n() {
136                 return l10n;
137         }
138
139         //
140         // FREDPLUGIN METHODS
141         //
142
143         /**
144          * {@inheritDoc}
145          */
146         @Override
147         public void runPlugin(PluginRespirator pluginRespirator) {
148                 this.pluginRespirator = pluginRespirator;
149
150                 /* create a configuration. */
151                 Configuration configuration;
152                 try {
153                         configuration = new Configuration(new PluginStoreConfigurationBackend(pluginStore = pluginRespirator.getStore()));
154                 } catch (DatabaseDisabledException dde1) {
155                         logger.log(Level.WARNING, "Could not load plugin store, using XML files.");
156                         try {
157                                 configuration = new Configuration(new XMLConfigurationBackend(new File("sone.xml"), true));
158                         } catch (ConfigurationException ce1) {
159                                 logger.log(Level.SEVERE, "Could not load or create the “sone.xml” configuration file!");
160                                 configuration = new Configuration(new MapConfigurationBackend(Collections.<String, String> emptyMap()));
161                         }
162                 }
163
164                 /* create freenet interface. */
165                 FreenetInterface freenetInterface = new FreenetInterface(pluginRespirator.getNode(), pluginRespirator.getHLSimpleClient());
166
167                 /* create web of trust connector. */
168                 PluginConnector pluginConnector = new PluginConnector(pluginRespirator);
169                 WebOfTrustConnector webOfTrustConnector = new WebOfTrustConnector(pluginConnector);
170                 identityManager = new IdentityManager(webOfTrustConnector);
171                 identityManager.setContext("Sone");
172
173                 /* create the web interface. */
174                 webInterface = new WebInterface(this);
175
176                 /* create core. */
177                 core = new Core(configuration, freenetInterface, identityManager);
178
179                 /* create the identity manager. */
180                 identityManager.addIdentityListener(core);
181
182                 /* start core! */
183                 boolean startupFailed = true;
184                 try {
185                         core.start();
186                         webInterface.start();
187                         identityManager.start();
188                         startupFailed = false;
189                 } finally {
190                         if (startupFailed) {
191                                 /*
192                                  * we let the exception bubble up but shut the logging down so
193                                  * that the logfile is not swamped by the installed logging
194                                  * handlers of the failed instances.
195                                  */
196                                 Logging.shutdown();
197                         }
198                 }
199         }
200
201         /**
202          * {@inheritDoc}
203          */
204         @Override
205         public void terminate() {
206                 try {
207                         /* stop the web interface. */
208                         webInterface.stop();
209
210                         /* stop the core. */
211                         core.stop();
212
213                         /* stop the identity manager. */
214                         identityManager.stop();
215
216                         /* TODO wait for core to stop? */
217                         try {
218                                 pluginRespirator.putStore(pluginStore);
219                         } catch (DatabaseDisabledException dde1) {
220                                 logger.log(Level.WARNING, "Could not store plugin store, database is disabled.", dde1);
221                         }
222
223                 } finally {
224                         /* shutdown logger. */
225                         Logging.shutdown();
226                 }
227         }
228
229         //
230         // INTERFACE FredPluginL10n
231         //
232
233         /**
234          * {@inheritDoc}
235          */
236         @Override
237         public String getString(String key) {
238                 return l10n.getBase().getString(key);
239         }
240
241         /**
242          * {@inheritDoc}
243          */
244         @Override
245         public void setLanguage(LANGUAGE newLanguage) {
246                 l10n = new PluginL10n(this, newLanguage);
247         }
248
249         //
250         // INTERFACE FredPluginBaseL10n
251         //
252
253         /**
254          * {@inheritDoc}
255          */
256         @Override
257         public String getL10nFilesBasePath() {
258                 return "i18n";
259         }
260
261         /**
262          * {@inheritDoc}
263          */
264         @Override
265         public String getL10nFilesMask() {
266                 return "sone.${lang}.properties";
267         }
268
269         /**
270          * {@inheritDoc}
271          */
272         @Override
273         public String getL10nOverrideFilesMask() {
274                 return "sone.${lang}.override.properties";
275         }
276
277         /**
278          * {@inheritDoc}
279          */
280         @Override
281         public ClassLoader getPluginClassLoader() {
282                 return SonePlugin.class.getClassLoader();
283         }
284
285         //
286         // INTERFACE FredPluginVersioned
287         //
288
289         /**
290          * {@inheritDoc}
291          */
292         @Override
293         public String getVersion() {
294                 return VERSION.toString();
295         }
296
297 }