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