Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Part of sone inplimentation of https://github.com/freenet/fred/blob/next/src/freenet/pluginmanager/FredPlugin.java
- //Line 172 of SonePlugin.java https://github.com/Bombe/Sone/blob/next/src/main/java/net/pterodactylus/sone/main/SonePlugin.java
- public void runPlugin(PluginRespirator pluginRespirator) {
- this.pluginRespirator = pluginRespirator;
- /* create a configuration. */
- Configuration oldConfiguration;
- Configuration newConfiguration = null;
- boolean firstStart = !new File("sone.properties").exists();
- boolean newConfig = false;
- try {
- oldConfiguration = new Configuration(new MapConfigurationBackend(new File("sone.properties"), false));
- newConfiguration = oldConfiguration;
- } catch (ConfigurationException ce1) {
- newConfig = true;
- logger.log(Level.INFO, "Could not load configuration file, trying plugin store…", ce1);
- try {
- newConfiguration = new Configuration(new MapConfigurationBackend(new File("sone.properties"), true));
- logger.log(Level.INFO, "Created new configuration file.");
- } catch (ConfigurationException ce2) {
- logger.log(Level.SEVERE, "Could not create configuration file, using Plugin Store!", ce2);
- }
- try {
- oldConfiguration = new Configuration(new PluginStoreConfigurationBackend(pluginRespirator));
- logger.log(Level.INFO, "Plugin store loaded.");
- } catch (DatabaseDisabledException dde1) {
- logger.log(Level.SEVERE, "Could not load any configuration, using empty configuration!");
- oldConfiguration = new Configuration(new MapConfigurationBackend());
- }
- }
- final Configuration startConfiguration;
- if ((newConfiguration != null) && (oldConfiguration != newConfiguration)) {
- logger.log(Level.INFO, "Setting configuration to file-based configuration.");
- startConfiguration = newConfiguration;
- } else {
- startConfiguration = oldConfiguration;
- }
- final EventBus eventBus = new EventBus();
- /* Freenet injector configuration. */
- AbstractModule freenetModule = new AbstractModule() {
- @Override
- @SuppressWarnings("synthetic-access")
- protected void configure() {
- bind(PluginRespirator.class).toInstance(SonePlugin.this.pluginRespirator);
- bind(Node.class).toInstance(SonePlugin.this.pluginRespirator.getNode());
- }
- };
- /* Sone injector configuration. */
- AbstractModule soneModule = new AbstractModule() {
- @Override
- protected void configure() {
- bind(Core.class).in(Singleton.class);
- bind(MemoryDatabase.class).in(Singleton.class);
- bind(EventBus.class).toInstance(eventBus);
- bind(Configuration.class).toInstance(startConfiguration);
- bind(FreenetInterface.class).in(Singleton.class);
- bind(PluginConnector.class).in(Singleton.class);
- Context context = new Context("Sone");
- bind(Context.class).toInstance(context);
- bind(getOptionalContextTypeLiteral()).toInstance(of(context));
- bind(WebOfTrustConnector.class).in(Singleton.class);
- bind(WebOfTrustUpdater.class).in(Singleton.class);
- bind(IdentityManager.class).in(Singleton.class);
- bind(SonePlugin.class).toInstance(SonePlugin.this);
- bind(FcpInterface.class).in(Singleton.class);
- bind(Database.class).to(MemoryDatabase.class);
- bind(PostBuilderFactory.class).to(MemoryDatabase.class);
- bind(PostReplyBuilderFactory.class).to(MemoryDatabase.class);
- bind(SoneProvider.class).to(Core.class).in(Singleton.class);
- bind(PostProvider.class).to(MemoryDatabase.class);
- bindListener(Matchers.any(), new TypeListener() {
- @Override
- public <I> void hear(TypeLiteral<I> typeLiteral, TypeEncounter<I> typeEncounter) {
- typeEncounter.register(new InjectionListener<I>() {
- @Override
- public void afterInjection(I injectee) {
- eventBus.register(injectee);
- }
- });
- }
- });
- }
- private TypeLiteral<Optional<Context>> getOptionalContextTypeLiteral() {
- return new TypeLiteral<Optional<Context>>() {
- };
- }
- };
- Injector injector = Guice.createInjector(freenetModule, soneModule);
- core = injector.getInstance(Core.class);
- /* create web of trust connector. */
- webOfTrustConnector = injector.getInstance(WebOfTrustConnector.class);
- /* create FCP interface. */
- fcpInterface = injector.getInstance(FcpInterface.class);
- core.setFcpInterface(fcpInterface);
- /* create the web interface. */
- webInterface = injector.getInstance(WebInterface.class);
- boolean startupFailed = true;
- try {
- /* start core! */
- core.start();
- webInterface.start();
- webInterface.setFirstStart(firstStart);
- webInterface.setNewConfig(newConfig);
- 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();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement