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