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