Stop the web interface when the plugin is stopped.
[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 web interface. */
66         private WebInterface webInterface;
67
68         /** The l10n helper. */
69         private PluginL10n l10n;
70
71         //
72         // ACCESSORS
73         //
74
75         /**
76          * Returns the plugin respirator for this plugin.
77          *
78          * @return The plugin respirator
79          */
80         public PluginRespirator pluginRespirator() {
81                 return pluginRespirator;
82         }
83
84         /**
85          * Returns the core started by this plugin.
86          *
87          * @return The core
88          */
89         public Core core() {
90                 return core;
91         }
92
93         /**
94          * Returns the plugin’s l10n helper.
95          *
96          * @return The plugin’s l10n helper
97          */
98         public PluginL10n l10n() {
99                 return l10n;
100         }
101
102         //
103         // FREDPLUGIN METHODS
104         //
105
106         /**
107          * {@inheritDoc}
108          */
109         @Override
110         public void runPlugin(PluginRespirator pluginRespirator) {
111                 this.pluginRespirator = pluginRespirator;
112
113                 /* create a configuration. */
114                 Configuration configuration;
115                 try {
116                         configuration = new Configuration(new PluginStoreConfigurationBackend(pluginRespirator.getStore()));
117                 } catch (DatabaseDisabledException dde1) {
118                         logger.log(Level.WARNING, "Could not load plugin store, using XML files.");
119                         try {
120                                 configuration = new Configuration(new XMLConfigurationBackend(new File("sone.xml"), true));
121                         } catch (ConfigurationException ce1) {
122                                 logger.log(Level.SEVERE, "Could not load or create the “sone.xml” configuration file!");
123                                 configuration = new Configuration(new MapConfigurationBackend(Collections.<String, String> emptyMap()));
124                         }
125                 }
126
127                 /* create freenet interface. */
128                 FreenetInterface freenetInterface = new FreenetInterface(pluginRespirator.getNode(), pluginRespirator.getHLSimpleClient());
129
130                 /* create the web interface. */
131                 webInterface = new WebInterface(this);
132
133                 /* create core. */
134                 core = new Core();
135                 core.configuration(configuration);
136                 core.freenetInterface(freenetInterface);
137
138                 /* start core! */
139                 core.start();
140                 webInterface.start();
141         }
142
143         /**
144          * {@inheritDoc}
145          */
146         @Override
147         public void terminate() {
148                 /* stop the web interface. */
149                 webInterface.stop();
150
151                 /* stop the core. */
152                 core.stop();
153
154                 /* TODO wait for core to stop? */
155         }
156
157         //
158         // INTERFACE FredPluginL10n
159         //
160
161         /**
162          * {@inheritDoc}
163          */
164         @Override
165         public String getString(String key) {
166                 return l10n.getBase().getString(key);
167         }
168
169         /**
170          * {@inheritDoc}
171          */
172         @Override
173         public void setLanguage(LANGUAGE newLanguage) {
174                 l10n = new PluginL10n(this, newLanguage);
175         }
176
177         //
178         // INTERFACE FredPluginBaseL10n
179         //
180
181         /**
182          * {@inheritDoc}
183          */
184         @Override
185         public String getL10nFilesBasePath() {
186                 return "i18n";
187         }
188
189         /**
190          * {@inheritDoc}
191          */
192         @Override
193         public String getL10nFilesMask() {
194                 return "sone.${lang}.properties";
195         }
196
197         /**
198          * {@inheritDoc}
199          */
200         @Override
201         public String getL10nOverrideFilesMask() {
202                 return "sone.${lang}.override.properties";
203         }
204
205         /**
206          * {@inheritDoc}
207          */
208         @Override
209         public ClassLoader getPluginClassLoader() {
210                 return SonePlugin.class.getClassLoader();
211         }
212
213 }