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