ff8f57adf2d9dd1932bbda0ba149471ef8407b0c
[WoTNS.git] / src / main / java / net / pterodactylus / wotns / main / WoTNSPlugin.java
1 /*
2  * WoTNS - WoTNSPlugin.java - Copyright © 2011 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.wotns.main;
19
20 import java.util.logging.Level;
21 import java.util.logging.LogRecord;
22
23 import net.pterodactylus.util.logging.Logging;
24 import net.pterodactylus.util.logging.LoggingListener;
25 import net.pterodactylus.util.version.Version;
26 import net.pterodactylus.wotns.freenet.plugin.PluginConnector;
27 import net.pterodactylus.wotns.freenet.wot.IdentityManager;
28 import net.pterodactylus.wotns.freenet.wot.WebOfTrustConnector;
29 import net.pterodactylus.wotns.ui.web.WebInterface;
30 import freenet.client.HighLevelSimpleClient;
31 import freenet.clients.http.ToadletContainer;
32 import freenet.l10n.PluginL10n;
33 import freenet.l10n.BaseL10n.LANGUAGE;
34 import freenet.pluginmanager.FredPlugin;
35 import freenet.pluginmanager.FredPluginBaseL10n;
36 import freenet.pluginmanager.FredPluginL10n;
37 import freenet.pluginmanager.FredPluginThreadless;
38 import freenet.pluginmanager.FredPluginVersioned;
39 import freenet.pluginmanager.PluginRespirator;
40
41 /**
42  * Main plugin class for Web of Trust Name Service.
43  *
44  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
45  */
46 public class WoTNSPlugin implements FredPlugin, FredPluginL10n, FredPluginBaseL10n, FredPluginVersioned, FredPluginThreadless {
47
48         static {
49                 Logging.setup("WoTNS");
50                 Logging.addLoggingListener(new LoggingListener() {
51
52                         @Override
53                         public void logged(LogRecord logRecord) {
54                                 Class<?> loggerClass = Logging.getLoggerClass(logRecord.getLoggerName());
55                                 int recordLevel = logRecord.getLevel().intValue();
56                                 if (recordLevel < Level.FINE.intValue()) {
57                                         freenet.support.Logger.debug(loggerClass, String.format(logRecord.getMessage(), logRecord.getParameters()), logRecord.getThrown());
58                                 } else if (recordLevel < Level.INFO.intValue()) {
59                                         freenet.support.Logger.minor(loggerClass, String.format(logRecord.getMessage(), logRecord.getParameters()), logRecord.getThrown());
60                                 } else if (recordLevel < Level.WARNING.intValue()) {
61                                         freenet.support.Logger.normal(loggerClass, String.format(logRecord.getMessage(), logRecord.getParameters()), logRecord.getThrown());
62                                 } else if (recordLevel < Level.SEVERE.intValue()) {
63                                         freenet.support.Logger.warning(loggerClass, String.format(logRecord.getMessage(), logRecord.getParameters()), logRecord.getThrown());
64                                 } else {
65                                         freenet.support.Logger.error(loggerClass, String.format(logRecord.getMessage(), logRecord.getParameters()), logRecord.getThrown());
66                                 }
67                         }
68                 });
69         }
70
71         /** The current version of the plugin. */
72         private static final Version VERSION = new Version(0, 0, 7);
73
74         /** The plugin respirator. */
75         private PluginRespirator pluginRespirator;
76
77         /** The l10n handler. */
78         private PluginL10n l10n;
79
80         /** The web interface. */
81         private WebInterface webInterface;
82
83         /** The resolver. */
84         private Resolver resolver;
85
86         /** The web of trust connector. */
87         private WebOfTrustConnector webOfTrustConnector;
88
89         /** The identity manager. */
90         private IdentityManager identityManager;
91
92         //
93         // ACCESSORS
94         //
95
96         /**
97          * Returns the high-level simple client for the node.
98          *
99          * @return The high-level simple client
100          */
101         public HighLevelSimpleClient getHighLevelSimpleClient() {
102                 return pluginRespirator.getHLSimpleClient();
103         }
104
105         /**
106          * Returns the toadlet container of the node.
107          *
108          * @return The toadlet container of the node
109          */
110         public ToadletContainer getToadletContainer() {
111                 return pluginRespirator.getToadletContainer();
112         }
113
114         /**
115          * Returns the identity manager.
116          *
117          * @return The identity manager
118          */
119         public IdentityManager getIdentityManager() {
120                 return identityManager;
121         }
122
123         /**
124          * Returns the resolver.
125          *
126          * @return The resolver
127          */
128         public Resolver getResolver() {
129                 return resolver;
130         }
131
132         //
133         // FREDPLUGIN METHODS
134         //
135
136         /**
137          * {@inheritDoc}
138          */
139         @Override
140         public void runPlugin(PluginRespirator pluginRespirator) {
141                 this.pluginRespirator = pluginRespirator;
142
143                 PluginConnector pluginConnector = new PluginConnector(pluginRespirator);
144                 webOfTrustConnector = new WebOfTrustConnector(pluginConnector);
145                 identityManager = new IdentityManager(webOfTrustConnector);
146                 identityManager.setContext("WoTNS");
147                 identityManager.start();
148
149                 resolver = new Resolver(identityManager);
150
151                 webInterface = new WebInterface(this);
152
153                 webInterface.start();
154         }
155
156         /**
157          * {@inheritDoc}
158          */
159         @Override
160         public void terminate() {
161                 identityManager.stop();
162                 webOfTrustConnector.stop();
163                 webInterface.stop();
164                 Logging.shutdown();
165         }
166
167         //
168         // FREDPLUGINL10N METHODS
169         //
170
171         /**
172          * {@inheritDoc}
173          */
174         @Override
175         public String getString(String key) {
176                 return l10n.getBase().getString(key);
177         }
178
179         /**
180          * {@inheritDoc}
181          */
182         @Override
183         public void setLanguage(LANGUAGE newLanguage) {
184                 l10n = new PluginL10n(this, newLanguage);
185         }
186
187         //
188         // FREDPLUGINBASEL10N METHODS
189         //
190
191         /**
192          * {@inheritDoc}
193          */
194         @Override
195         public String getL10nFilesBasePath() {
196                 return "i18n";
197         }
198
199         /**
200          * {@inheritDoc}
201          */
202         @Override
203         public String getL10nFilesMask() {
204                 return "WoTNS.${lang}.properties";
205         }
206
207         /**
208          * {@inheritDoc}
209          */
210         @Override
211         public String getL10nOverrideFilesMask() {
212                 return "WoTNS.${lang}.override.properties";
213         }
214
215         /**
216          * {@inheritDoc}
217          */
218         @Override
219         public ClassLoader getPluginClassLoader() {
220                 return WoTNSPlugin.class.getClassLoader();
221         }
222
223         //
224         // FREDPLUGINVERSIONED METHODS
225         //
226
227         /**
228          * {@inheritDoc}
229          */
230         @Override
231         public String getVersion() {
232                 return VERSION.toString();
233         }
234
235 }