Change loading order of configuration backends.
[Sone.git] / src / main / java / net / pterodactylus / sone / main / SonePlugin.java
index ecf7f5b..6d9e3a0 100644 (file)
@@ -26,6 +26,9 @@ import java.util.logging.Logger;
 import net.pterodactylus.sone.core.Core;
 import net.pterodactylus.sone.core.FreenetInterface;
 import net.pterodactylus.sone.freenet.PluginStoreConfigurationBackend;
+import net.pterodactylus.sone.freenet.wot.IdentityManager;
+import net.pterodactylus.sone.freenet.wot.PluginConnector;
+import net.pterodactylus.sone.freenet.wot.WebOfTrustConnector;
 import net.pterodactylus.sone.web.WebInterface;
 import net.pterodactylus.util.config.Configuration;
 import net.pterodactylus.util.config.ConfigurationException;
@@ -74,12 +77,11 @@ public class SonePlugin implements FredPlugin, FredPluginL10n, FredPluginBaseL10
                                        freenet.support.Logger.error(loggerClass, String.format(logRecord.getMessage(), logRecord.getParameters()), logRecord.getThrown());
                                }
                        }
-
                });
        }
 
        /** The version. */
-       public static final Version VERSION = new Version("RC2", 0, 1);
+       public static final Version VERSION = new Version(0, 3);
 
        /** The logger. */
        private static final Logger logger = Logging.getLogger(SonePlugin.class);
@@ -99,6 +101,9 @@ public class SonePlugin implements FredPlugin, FredPluginL10n, FredPluginBaseL10
        /** The plugin store. */
        private PluginStore pluginStore;
 
+       /** The identity manager. */
+       private IdentityManager identityManager;
+
        //
        // ACCESSORS
        //
@@ -143,14 +148,20 @@ public class SonePlugin implements FredPlugin, FredPluginL10n, FredPluginBaseL10
 
                /* create a configuration. */
                Configuration configuration;
+               Configuration xmlConfiguration = null;
                try {
-                       configuration = new Configuration(new PluginStoreConfigurationBackend(pluginStore = pluginRespirator.getStore()));
-               } catch (DatabaseDisabledException dde1) {
-                       logger.log(Level.WARNING, "Could not load plugin store, using XML files.");
+                       configuration = new Configuration(new XMLConfigurationBackend(new File("sone.xml"), false));
+                       xmlConfiguration = configuration;
+               } catch (ConfigurationException ce1) {
+                       try {
+                               xmlConfiguration = new Configuration(new XMLConfigurationBackend(new File("sone.xml"), true));
+                       } catch (ConfigurationException ce2) {
+                               logger.log(Level.SEVERE, "Could not create XML file, using Plugin Store!");
+                       }
                        try {
-                               configuration = new Configuration(new XMLConfigurationBackend(new File("sone.xml"), true));
-                       } catch (ConfigurationException ce1) {
-                               logger.log(Level.SEVERE, "Could not load or create the “sone.xml” configuration file!");
+                               configuration = new Configuration(new PluginStoreConfigurationBackend(pluginRespirator));
+                       } catch (DatabaseDisabledException dde1) {
+                               logger.log(Level.SEVERE, "Could not load any configuration, using in-memory configuration!");
                                configuration = new Configuration(new MapConfigurationBackend(Collections.<String, String> emptyMap()));
                        }
                }
@@ -158,19 +169,31 @@ public class SonePlugin implements FredPlugin, FredPluginL10n, FredPluginBaseL10
                /* create freenet interface. */
                FreenetInterface freenetInterface = new FreenetInterface(pluginRespirator.getNode(), pluginRespirator.getHLSimpleClient());
 
+               /* create web of trust connector. */
+               PluginConnector pluginConnector = new PluginConnector(pluginRespirator);
+               WebOfTrustConnector webOfTrustConnector = new WebOfTrustConnector(pluginConnector);
+               identityManager = new IdentityManager(webOfTrustConnector);
+               identityManager.setContext("Sone");
+
+               /* create core. */
+               core = new Core(configuration, freenetInterface, identityManager);
+
                /* create the web interface. */
                webInterface = new WebInterface(this);
+               core.addCoreListener(webInterface);
 
-               /* create core. */
-               core = new Core();
-               core.configuration(configuration);
-               core.freenetInterface(freenetInterface);
+               /* create the identity manager. */
+               identityManager.addIdentityListener(core);
 
                /* start core! */
                boolean startupFailed = true;
                try {
                        core.start();
+                       if ((xmlConfiguration != null) && (configuration != xmlConfiguration)) {
+                               core.setConfiguration(xmlConfiguration);
+                       }
                        webInterface.start();
+                       identityManager.start();
                        startupFailed = false;
                } finally {
                        if (startupFailed) {
@@ -196,6 +219,9 @@ public class SonePlugin implements FredPlugin, FredPluginL10n, FredPluginBaseL10
                        /* stop the core. */
                        core.stop();
 
+                       /* stop the identity manager. */
+                       identityManager.stop();
+
                        /* TODO wait for core to stop? */
                        try {
                                pluginRespirator.putStore(pluginStore);