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