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