Advertisement
s243a

net.pterodactylus.sone.main.SonePlugin.SonePlugin

May 25th, 2015
445
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.86 KB | None | 0 0
  1. //Part of sone inplimentation of https://github.com/freenet/fred/blob/next/src/freenet/pluginmanager/FredPlugin.java
  2. //Line 172 of SonePlugin.java https://github.com/Bombe/Sone/blob/next/src/main/java/net/pterodactylus/sone/main/SonePlugin.java
  3. public void runPlugin(PluginRespirator pluginRespirator) {
  4.         this.pluginRespirator = pluginRespirator;
  5.  
  6.         /* create a configuration. */
  7.         Configuration oldConfiguration;
  8.         Configuration newConfiguration = null;
  9.         boolean firstStart = !new File("sone.properties").exists();
  10.         boolean newConfig = false;
  11.         try {
  12.             oldConfiguration = new Configuration(new MapConfigurationBackend(new File("sone.properties"), false));
  13.             newConfiguration = oldConfiguration;
  14.         } catch (ConfigurationException ce1) {
  15.             newConfig = true;
  16.             logger.log(Level.INFO, "Could not load configuration file, trying plugin store…", ce1);
  17.             try {
  18.                 newConfiguration = new Configuration(new MapConfigurationBackend(new File("sone.properties"), true));
  19.                 logger.log(Level.INFO, "Created new configuration file.");
  20.             } catch (ConfigurationException ce2) {
  21.                 logger.log(Level.SEVERE, "Could not create configuration file, using Plugin Store!", ce2);
  22.             }
  23.             try {
  24.                 oldConfiguration = new Configuration(new PluginStoreConfigurationBackend(pluginRespirator));
  25.                 logger.log(Level.INFO, "Plugin store loaded.");
  26.             } catch (DatabaseDisabledException dde1) {
  27.                 logger.log(Level.SEVERE, "Could not load any configuration, using empty configuration!");
  28.                 oldConfiguration = new Configuration(new MapConfigurationBackend());
  29.             }
  30.         }
  31.  
  32.         final Configuration startConfiguration;
  33.         if ((newConfiguration != null) && (oldConfiguration != newConfiguration)) {
  34.             logger.log(Level.INFO, "Setting configuration to file-based configuration.");
  35.             startConfiguration = newConfiguration;
  36.         } else {
  37.             startConfiguration = oldConfiguration;
  38.         }
  39.         final EventBus eventBus = new EventBus();
  40.  
  41.         /* Freenet injector configuration. */
  42.         AbstractModule freenetModule = new AbstractModule() {
  43.  
  44.             @Override
  45.             @SuppressWarnings("synthetic-access")
  46.             protected void configure() {
  47.                 bind(PluginRespirator.class).toInstance(SonePlugin.this.pluginRespirator);
  48.                 bind(Node.class).toInstance(SonePlugin.this.pluginRespirator.getNode());
  49.             }
  50.         };
  51.         /* Sone injector configuration. */
  52.         AbstractModule soneModule = new AbstractModule() {
  53.  
  54.             @Override
  55.             protected void configure() {
  56.                 bind(Core.class).in(Singleton.class);
  57.                 bind(MemoryDatabase.class).in(Singleton.class);
  58.                 bind(EventBus.class).toInstance(eventBus);
  59.                 bind(Configuration.class).toInstance(startConfiguration);
  60.                 bind(FreenetInterface.class).in(Singleton.class);
  61.                 bind(PluginConnector.class).in(Singleton.class);
  62.                 Context context = new Context("Sone");
  63.                 bind(Context.class).toInstance(context);
  64.                 bind(getOptionalContextTypeLiteral()).toInstance(of(context));
  65.                 bind(WebOfTrustConnector.class).in(Singleton.class);
  66.                 bind(WebOfTrustUpdater.class).in(Singleton.class);
  67.                 bind(IdentityManager.class).in(Singleton.class);
  68.                 bind(SonePlugin.class).toInstance(SonePlugin.this);
  69.                 bind(FcpInterface.class).in(Singleton.class);
  70.                 bind(Database.class).to(MemoryDatabase.class);
  71.                 bind(PostBuilderFactory.class).to(MemoryDatabase.class);
  72.                 bind(PostReplyBuilderFactory.class).to(MemoryDatabase.class);
  73.                 bind(SoneProvider.class).to(Core.class).in(Singleton.class);
  74.                 bind(PostProvider.class).to(MemoryDatabase.class);
  75.                 bindListener(Matchers.any(), new TypeListener() {
  76.  
  77.                     @Override
  78.                     public <I> void hear(TypeLiteral<I> typeLiteral, TypeEncounter<I> typeEncounter) {
  79.                         typeEncounter.register(new InjectionListener<I>() {
  80.  
  81.                             @Override
  82.                             public void afterInjection(I injectee) {
  83.                                 eventBus.register(injectee);
  84.                             }
  85.                         });
  86.                     }
  87.                 });
  88.             }
  89.  
  90.             private TypeLiteral<Optional<Context>> getOptionalContextTypeLiteral() {
  91.                 return new TypeLiteral<Optional<Context>>() {
  92.                 };
  93.             }
  94.  
  95.         };
  96.         Injector injector = Guice.createInjector(freenetModule, soneModule);
  97.         core = injector.getInstance(Core.class);
  98.  
  99.         /* create web of trust connector. */
  100.         webOfTrustConnector = injector.getInstance(WebOfTrustConnector.class);
  101.  
  102.         /* create FCP interface. */
  103.         fcpInterface = injector.getInstance(FcpInterface.class);
  104.         core.setFcpInterface(fcpInterface);
  105.  
  106.         /* create the web interface. */
  107.         webInterface = injector.getInstance(WebInterface.class);
  108.  
  109.         boolean startupFailed = true;
  110.         try {
  111.  
  112.             /* start core! */
  113.             core.start();
  114.             webInterface.start();
  115.             webInterface.setFirstStart(firstStart);
  116.             webInterface.setNewConfig(newConfig);
  117.             startupFailed = false;
  118.         } finally {
  119.             if (startupFailed) {
  120.                 /*
  121.                  * we let the exception bubble up but shut the logging down so
  122.                  * that the logfile is not swamped by the installed logging
  123.                  * handlers of the failed instances.
  124.                  */
  125.                 Logging.shutdown();
  126.             }
  127.         }
  128.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement