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