package org.lodder.subtools.multisubdownloader.framework;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.prefs.Preferences;

import org.lodder.subtools.multisubdownloader.framework.service.providers.ServiceProvider;
import org.lodder.subtools.multisubdownloader.framework.service.providers.ServiceProviderComparator;
import org.lodder.subtools.multisubdownloader.settings.model.Settings;
import org.lodder.subtools.sublibrary.Manager;
import org.lodder.subtools.sublibrary.logging.Logger;
import org.reflections.Reflections;

public class Bootstrapper {

  private final Container app;
  private final Settings settings;
  private final Preferences preferences;
  private final Manager manager;

  public Bootstrapper(Container app, Settings settings, Preferences preferences, Manager manager) {
    this.app = app;
    this.settings = settings;
    this.preferences = preferences;
    this.manager = manager;
  }

  public void initialize() {
    /* Bind Settings to IoC Container */
    this.app.bind("Settings", new Resolver() {
      @Override
      public Object resolve() {
        return settings;
      }
    });
    
    /* Bind Preferences to IoC Container */
    this.app.bind("Preferences", new Resolver() {
      @Override
      public Object resolve() {
        return preferences;
      }
    });
    
    /* Bind Manager to IoC Container */
    this.app.bind("Manager", new Resolver() {
      @Override
      public Object resolve() {
        return manager;
      }
    });

    // Collect ServiceProviders
    List<ServiceProvider> providers = this.getProviders();

    // Sort according to priority
    Collections.sort(providers, new ServiceProviderComparator());

    // Register ServiceProviders
    this.registerProviders(providers);
  }

  @SuppressWarnings({"rawtypes", "unchecked"})
  public List<ServiceProvider> getProviders() {

    Reflections reflections = new Reflections("org.lodder.subtools.multisubdownloader");
    Set<Class<? extends ServiceProvider>> providerClasses = reflections.getSubTypesOf(ServiceProvider.class);

    List<ServiceProvider> providers = new ArrayList<>();

    // Intantieer alle serviceproviders
    for (Class serviceProviderClass : providerClasses) {
      ServiceProvider serviceProvider = null;

      try {
        Constructor constructor = serviceProviderClass.getConstructor();
        serviceProvider = (ServiceProvider) constructor.newInstance();
      } catch (Exception e) {
        Logger.instance.error(
          "ServiceProvider: '" + serviceProviderClass.getClass().getName() + "' failed to create instance.");
      }

      if (serviceProvider == null)
        continue;

      providers.add(serviceProvider);
    }
    return providers;
  }

  public void registerProviders(List<ServiceProvider> providers) {
    // Register serviceproviders
    for (ServiceProvider provider : providers) {
      provider.register(this.app);

      Logger.instance.debug("ServiceProvider: '" + provider.getClass().getName() + "' registered.");
    }
  }
}

@@ -555,115 +555,203 @@
 org.
-lodder.subtools.sublibrary.logging.Logger;%0Aimport org.reflections.Reflections;%0A%0Apublic class Bootstrapper %7B
+reflections.Reflections;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0Apublic class Bootstrapper %7B%0A%0A  private static final Logger LOGGER = LoggerFactory.getLogger(Bootstrapper.class);
 %0A%0A  
@@ -1306,36 +1306,32 @@
       %7D%0A    %7D);%0A
-    
 %0A    /* Bind Pre
@@ -1501,20 +1501,16 @@
     %7D);%0A
-    
 %0A    /* 
@@ -2191,16 +2191,24 @@
 lasses =
+%0A       
  reflect
@@ -2652,62 +2652,71 @@
    L
-ogger.instance.error(%0A          %22ServiceProvider: '%22 +
+OGGER.error(%22ServiceProvider: '%7B%7D' failed to create instance.%22,
  ser
@@ -2728,24 +2728,37 @@
 roviderClass
+%0A            
 .getClass().
@@ -2770,41 +2770,8 @@
 me()
- + %22' failed to create instance.%22
 );%0A 
@@ -2812,24 +2812,16 @@
 == null)
-%0A       
  continu
@@ -3083,30 +3083,20 @@
 p);%0A
-%0A
       L
-ogger.instance
+OGGER
 .deb
@@ -3121,11 +3121,25 @@
 r: '
-%22 +
+%7B%7D' registered.%22,
  pro
@@ -3168,26 +3168,8 @@
 me()
- + %22' registered.%22
 );%0A 

Convert Bootstrapper from old logger to new LOGGER slf4j