package net.kencochrane.raven;

import net.kencochrane.raven.dsn.Dsn;

import java.util.ServiceLoader;

/**
 * Factory in charge of creating {@link Raven} instances.
 * <p>
 * The factories register themselves through the {@link ServiceLoader} system.
 * </p>
 */
public abstract class RavenFactory {
    private static final ServiceLoader<RavenFactory> RAVEN_FACTORIES = ServiceLoader.load(RavenFactory.class);

    public static Raven ravenInstance() {
        return ravenInstance(Dsn.dsnLookup());
    }

    public static Raven ravenInstance(String dsn) {
        return ravenInstance(new Dsn(dsn));
    }

    public static Raven ravenInstance(Dsn dsn) {
        for (RavenFactory ravenFactory : RAVEN_FACTORIES) {
            Raven raven = ravenFactory.createRavenInstance(dsn);
            if (raven != null) {
                return raven;
            }
        }

        throw new IllegalStateException("Couldn't create a raven instance for '" + dsn + "'");
    }

    public static Raven ravenInstance(Dsn dsn, String ravenFactoryName) {
        if (ravenFactoryName == null)
            return ravenInstance(dsn);

        for (RavenFactory ravenFactory : RAVEN_FACTORIES) {
            if (!ravenFactoryName.equals(ravenFactory.getClass().getName()))
                continue;

            Raven raven = ravenFactory.createRavenInstance(dsn);
            if (raven != null) {
                return raven;
            }
        }

        throw new IllegalStateException("Couldn't create a raven instance for '" + dsn + "'");
    }

    protected abstract Raven createRavenInstance(Dsn dsn);
}

@@ -60,24 +60,82 @@
 n.dsn.Dsn;%0A%0A
+import java.util.Collection;%0Aimport java.util.LinkedList;%0A
 import java.
@@ -405,21 +405,31 @@
 actory%3E 
-RAVEN
+AUTO_REGISTERED
 _FACTORI
@@ -472,16 +472,262 @@
 .class);
+%0A    private static final Collection%3CRavenFactory%3E MANUALLY_REGISTERED_FACTORIES = new LinkedList%3CRavenFactory%3E();%0A%0A    public static void registerFactory(RavenFactory ravenFactory) %7B%0A        MANUALLY_REGISTERED_FACTORIES.add(ravenFactory);%0A    %7D
 %0A%0A    pu
@@ -1001,37 +1001,274 @@
  ravenFactory : 
-RAVEN
+MANUALLY_REGISTERED_FACTORIES) %7B%0A            Raven raven = ravenFactory.createRavenInstance(dsn);%0A            if (raven != null) %7B%0A                return raven;%0A            %7D%0A        %7D%0A%0A        for (RavenFactory ravenFactory : AUTO_REGISTERED
 _FACTORIES) %7B%0A  
@@ -1717,13 +1717,354 @@
 y : 
-RAVEN
+MANUALLY_REGISTERED_FACTORIES) %7B%0A            if (!ravenFactoryName.equals(ravenFactory.getClass().getName()))%0A                continue;%0A%0A            Raven raven = ravenFactory.createRavenInstance(dsn);%0A            if (raven != null) %7B%0A                return raven;%0A            %7D%0A        %7D%0A%0A        for (RavenFactory ravenFactory : AUTO_REGISTERED
 _FAC

Add the possibility to register factories manually