Too many changes to list them all.
[Sone.git] / src / main / java / net / pterodactylus / sone / main / SonePlugin.java
index 7ac2c83..f1b9ff3 100644 (file)
@@ -25,6 +25,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;
@@ -41,6 +44,7 @@ import freenet.pluginmanager.FredPluginL10n;
 import freenet.pluginmanager.FredPluginThreadless;
 import freenet.pluginmanager.FredPluginVersioned;
 import freenet.pluginmanager.PluginRespirator;
+import freenet.pluginmanager.PluginStore;
 
 /**
  * This class interfaces with Freenet. It is the class that is loaded by the
@@ -53,10 +57,34 @@ public class SonePlugin implements FredPlugin, FredPluginL10n, FredPluginBaseL10
        static {
                /* initialize logging. */
                Logging.setup("sone");
+               Logging.setupConsoleLogging();
+               /*
+                * Logging.addLoggingListener(new LoggingListener() {
+                * @Override public void logged(LogRecord logRecord) { Class<?>
+                * loggerClass = Logging.getLoggerClass(logRecord.getLoggerName()); int
+                * recordLevel = logRecord.getLevel().intValue(); if (recordLevel <
+                * Level.FINE.intValue()) { freenet.support.Logger.debug(loggerClass,
+                * String.format(logRecord.getMessage(), logRecord.getParameters()),
+                * logRecord.getThrown()); } else if (recordLevel <
+                * Level.INFO.intValue()) { freenet.support.Logger.minor(loggerClass,
+                * String.format(logRecord.getMessage(), logRecord.getParameters()),
+                * logRecord.getThrown()); } else if (recordLevel <
+                * Level.WARNING.intValue()) {
+                * freenet.support.Logger.normal(loggerClass,
+                * String.format(logRecord.getMessage(), logRecord.getParameters()),
+                * logRecord.getThrown()); } else if (recordLevel <
+                * Level.SEVERE.intValue()) {
+                * freenet.support.Logger.warning(loggerClass,
+                * String.format(logRecord.getMessage(), logRecord.getParameters()),
+                * logRecord.getThrown()); } else {
+                * freenet.support.Logger.error(loggerClass,
+                * String.format(logRecord.getMessage(), logRecord.getParameters()),
+                * logRecord.getThrown()); } } });
+                */
        }
 
        /** The version. */
-       private static final Version VERSION = new Version("SNAPSHOT", 0, 1);
+       public static final Version VERSION = new Version("RC1", 0, 2);
 
        /** The logger. */
        private static final Logger logger = Logging.getLogger(SonePlugin.class);
@@ -73,6 +101,12 @@ public class SonePlugin implements FredPlugin, FredPluginL10n, FredPluginBaseL10
        /** The l10n helper. */
        private PluginL10n l10n;
 
+       /** The plugin store. */
+       private PluginStore pluginStore;
+
+       /** The identity manager. */
+       private IdentityManager identityManager;
+
        //
        // ACCESSORS
        //
@@ -118,7 +152,7 @@ public class SonePlugin implements FredPlugin, FredPluginL10n, FredPluginBaseL10
                /* create a configuration. */
                Configuration configuration;
                try {
-                       configuration = new Configuration(new PluginStoreConfigurationBackend(pluginRespirator.getStore()));
+                       configuration = new Configuration(new PluginStoreConfigurationBackend(pluginStore = pluginRespirator.getStore()));
                } catch (DatabaseDisabledException dde1) {
                        logger.log(Level.WARNING, "Could not load plugin store, using XML files.");
                        try {
@@ -132,17 +166,38 @@ 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 the web interface. */
                webInterface = new WebInterface(this);
 
                /* create core. */
-               core = new Core();
-               core.configuration(configuration);
-               core.freenetInterface(freenetInterface);
+               core = new Core(configuration, freenetInterface, identityManager);
+
+               /* create the identity manager. */
+               identityManager.addIdentityListener(core);
 
                /* start core! */
-               core.start();
-               webInterface.start();
+               boolean startupFailed = true;
+               try {
+                       core.start();
+                       webInterface.start();
+                       identityManager.start();
+                       startupFailed = false;
+               } finally {
+                       if (startupFailed) {
+                               /*
+                                * we let the exception bubble up but shut the logging down so
+                                * that the logfile is not swamped by the installed logging
+                                * handlers of the failed instances.
+                                */
+                               Logging.shutdown();
+                       }
+               }
        }
 
        /**
@@ -150,16 +205,27 @@ public class SonePlugin implements FredPlugin, FredPluginL10n, FredPluginBaseL10
         */
        @Override
        public void terminate() {
-               /* stop the web interface. */
-               webInterface.stop();
+               try {
+                       /* stop the web interface. */
+                       webInterface.stop();
+
+                       /* stop the core. */
+                       core.stop();
 
-               /* stop the core. */
-               core.stop();
+                       /* stop the identity manager. */
+                       identityManager.stop();
 
-               /* TODO wait for core to stop? */
+                       /* TODO wait for core to stop? */
+                       try {
+                               pluginRespirator.putStore(pluginStore);
+                       } catch (DatabaseDisabledException dde1) {
+                               logger.log(Level.WARNING, "Could not store plugin store, database is disabled.", dde1);
+                       }
 
-               /* shutdown logger. */
-               Logging.shutdown();
+               } finally {
+                       /* shutdown logger. */
+                       Logging.shutdown();
+               }
        }
 
        //