package net.java.dev.moskito.core.producers;

import java.util.List;
import java.util.ArrayList;
import java.util.StringTokenizer;


/**
 * Registers archiver w/ archivers config
 *
 * @author imercuriev
 *         Date: Mar 9, 2010
 *         Time: 1:52:28 PM
 */
public enum SnapshotArchiverRegistry {

    INSTANCE;

    private List<ISnapshotArchiver> registeredArchivers = new ArrayList<ISnapshotArchiver>();

    public List<ISnapshotArchiver> getRegisteredArchivers() {
        return registeredArchivers;
    }

    public void setRegisteredArchivers(List<ISnapshotArchiver> registeredArchivers) {
        this.registeredArchivers = registeredArchivers;
    }

    public ISnapshotArchiver registerArchiver(SnapshotArchiverConfig config) {
        try {
            Class storageClass = Class.forName(config.getStorageClassName());
            Object[] storageParams = parseParams(null, config.getStorageParams());
            Object storage = storageClass.getConstructor(getClassesArray(storageParams)).newInstance(storageParams);
            Class archiverClass = Class.forName(config.getClassName());
            Object[] archiverParams = parseParams(
                    storage,
                    config.getArchiversConstructorMoreParams()
            );
            ISnapshotArchiver archiver = (ISnapshotArchiver) archiverClass.getConstructor(getClassesArray(archiverParams)).newInstance(archiverParams);
            registeredArchivers.add(archiver);
            return archiver;
        } catch (Exception e) {
            throw new RuntimeException("Failed to create stats archiver from configureme config file", e);
        }
    }

    private static Object[] parseParams(Object firstParam, String params) {
        boolean firstPresent = firstParam != null;
        int minParams = firstPresent ? 1 : 0;
        Object[] result;
        if (params == null || params.length() == 0) {
            result = new Object[minParams];
        } else {
            StringTokenizer st = new StringTokenizer(params, ",");
            result = new Object[minParams + st.countTokens()];
            int i = minParams;
            while (st.hasMoreTokens()) {
                result[i] = st.nextToken();
                i++;
            }
        }
        if (firstPresent) result[0] = firstParam;
        return result;
    }

    private static Class[] getClassesArray(Object[] allParams) throws Exception {
        Class[] result = new Class[allParams.length];
        Class storageClass = Class.forName("net.java.dev.moskito.central.StatStorage");
        boolean firstStorage = (allParams.length > 0 && allParams[0] != null && storageClass.isInstance(allParams[0]));
        if (firstStorage) {
            result[0] = storageClass;
        }
        int startIndex = firstStorage ? 1 : 0;
        for (int i = startIndex; i < result.length; i++) result[i] = allParams[i].getClass();
        return result;
    }
}

@@ -793,37 +793,38 @@
             
-Class
+Object
  storage
 Class = Clas
@@ -811,36 +811,31 @@
  storage
-Class = Class.forNam
+ = createStorag
 e(config
@@ -860,210 +860,35 @@
 me()
-);%0D%0A            Object%5B%5D storageParams = parseParams(null, config.getStorageParams());%0D%0A            Object storage = storageClass.getConstructor(getClassesArray(storageParams)).newInstance(storageParams
+, config.getStorageParams()
 );%0D%0A
@@ -1506,32 +1506,387 @@
     %7D%0D%0A    %7D%0D%0A%0D%0A
+    public Object createStorage(String storageClassName, String stringStorageParams) throws Exception %7B%0D%0A        Class storageClass = Class.forName(storageClassName);%0D%0A        Object%5B%5D storageParams = parseParams(null, stringStorageParams);%0D%0A        return storageClass.getConstructor(getClassesArray(storageParams)).newInstance(storageParams);%0D%0A    %7D%0D%0A%0D%0A
     private stat

refactor to extract create storage method