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