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