2 * FreenetSone - SonePlugin.java - Copyright © 2010 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.main;
21 import java.util.Collections;
22 import java.util.logging.Level;
23 import java.util.logging.LogRecord;
24 import java.util.logging.Logger;
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;
49 * This class interfaces with Freenet. It is the class that is loaded by the
50 * node and starts up the whole Sone system.
52 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
54 public class SonePlugin implements FredPlugin, FredPluginL10n, FredPluginBaseL10n, FredPluginThreadless, FredPluginVersioned {
57 /* initialize logging. */
58 Logging.setup("sone");
59 Logging.addLoggingListener(new LoggingListener() {
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());
74 freenet.support.Logger.error(loggerClass, String.format(logRecord.getMessage(), logRecord.getParameters()), logRecord.getThrown());
82 public static final Version VERSION = new Version("RC1", 0, 1);
85 private static final Logger logger = Logging.getLogger(SonePlugin.class);
87 /** The plugin respirator. */
88 private PluginRespirator pluginRespirator;
93 /** The web interface. */
94 private WebInterface webInterface;
96 /** The l10n helper. */
97 private PluginL10n l10n;
99 /** The plugin store. */
100 private PluginStore pluginStore;
107 * Returns the plugin respirator for this plugin.
109 * @return The plugin respirator
111 public PluginRespirator pluginRespirator() {
112 return pluginRespirator;
116 * Returns the core started by this plugin.
125 * Returns the plugin’s l10n helper.
127 * @return The plugin’s l10n helper
129 public PluginL10n l10n() {
134 // FREDPLUGIN METHODS
141 public void runPlugin(PluginRespirator pluginRespirator) {
142 this.pluginRespirator = pluginRespirator;
144 /* create a configuration. */
145 Configuration configuration;
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.");
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()));
158 /* create freenet interface. */
159 FreenetInterface freenetInterface = new FreenetInterface(pluginRespirator.getNode(), pluginRespirator.getHLSimpleClient());
161 /* create the web interface. */
162 webInterface = new WebInterface(this);
166 core.configuration(configuration);
167 core.freenetInterface(freenetInterface);
170 boolean startupFailed = true;
173 webInterface.start();
174 startupFailed = false;
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.
191 public void terminate() {
193 /* stop the web interface. */
199 /* TODO wait for core to stop? */
201 pluginRespirator.putStore(pluginStore);
202 } catch (DatabaseDisabledException dde1) {
203 logger.log(Level.WARNING, "Could not store plugin store, database is disabled.", dde1);
207 /* shutdown logger. */
213 // INTERFACE FredPluginL10n
220 public String getString(String key) {
221 return l10n.getBase().getString(key);
228 public void setLanguage(LANGUAGE newLanguage) {
229 l10n = new PluginL10n(this, newLanguage);
233 // INTERFACE FredPluginBaseL10n
240 public String getL10nFilesBasePath() {
248 public String getL10nFilesMask() {
249 return "sone.${lang}.properties";
256 public String getL10nOverrideFilesMask() {
257 return "sone.${lang}.override.properties";
264 public ClassLoader getPluginClassLoader() {
265 return SonePlugin.class.getClassLoader();
269 // INTERFACE FredPluginVersioned
276 public String getVersion() {
277 return VERSION.toString();