be4f1fb298dbce90a890efe5ae2ad42da1955246
[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.freenet.PluginStoreConfigurationBackend;
27 import net.pterodactylus.util.config.Configuration;
28 import net.pterodactylus.util.config.ConfigurationException;
29 import net.pterodactylus.util.config.MapConfigurationBackend;
30 import net.pterodactylus.util.config.XMLConfigurationBackend;
31 import net.pterodactylus.util.logging.Logging;
32 import freenet.client.async.DatabaseDisabledException;
33 import freenet.pluginmanager.FredPlugin;
34 import freenet.pluginmanager.PluginRespirator;
35
36 /**
37  * This class interfaces with Freenet. It is the class that is loaded by the
38  * node and starts up the whole Sone system.
39  *
40  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41  */
42 public class SonePlugin implements FredPlugin {
43
44         static {
45                 /* initialize logging. */
46                 Logging.setup("sone");
47         }
48
49         /** The logger. */
50         private static final Logger logger = Logging.getLogger(SonePlugin.class);
51
52         /** The core. */
53         private Core core;
54
55         //
56         // FREDPLUGIN METHODS
57         //
58
59         /**
60          * {@inheritDoc}
61          */
62         @Override
63         public void runPlugin(PluginRespirator pluginRespirator) {
64
65                 /* create a configuration. */
66                 Configuration configuration;
67                 try {
68                         configuration = new Configuration(new PluginStoreConfigurationBackend(pluginRespirator.getStore()));
69                 } catch (DatabaseDisabledException dde1) {
70                         logger.log(Level.WARNING, "Could not load plugin store, using XML files.");
71                         try {
72                                 configuration = new Configuration(new XMLConfigurationBackend(new File("sone.xml"), true));
73                         } catch (ConfigurationException ce1) {
74                                 logger.log(Level.SEVERE, "Could not load or create the “sone.xml” configuration file!");
75                                 configuration = new Configuration(new MapConfigurationBackend(Collections.<String, String> emptyMap()));
76                         }
77                 }
78
79                 /* create core. */
80                 core = new Core();
81                 core.configuration(configuration);
82
83                 /* start core! */
84                 core.start();
85         }
86
87         /**
88          * {@inheritDoc}
89          */
90         @Override
91         public void terminate() {
92                 /* stop the core. */
93                 core.stop();
94
95                 /* TODO wait for core to stop? */
96         }
97
98 }