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) {
        Class[] result = new Class[allParams.length];
        for (int i = 0; i < result.length; i++) result[i] = allParams[i].getClass();
        return result;
    }
}

@@ -1193,16 +1193,59 @@
 ams(
-storage,
+%0D%0A                    storage,%0D%0A                   
  con
@@ -1275,32 +1275,46 @@
 ctorMoreParams()
+%0D%0A            
 );%0D%0A            
@@ -2454,24 +2454,41 @@
 %5D allParams)
+ throws Exception
  %7B%0D%0A        
@@ -2546,22 +2546,368 @@
     
-for (int i = 0
+Class storageClass = Class.forName(%22net.java.dev.moskito.central.StatStorage%22);%0D%0A        boolean firstStorage = (allParams.length %3E 0 && allParams%5B0%5D != null && storageClass.isInstance(allParams%5B0%5D));%0D%0A        if (firstStorage) %7B%0D%0A            result%5B0%5D = storageClass;%0D%0A        %7D%0D%0A        int startIndex = firstStorage ? 1 : 0;%0D%0A        for (int i = startIndex
 ; i 

tested on ubuntu. fixed code errors