package com.custardsource.parfait;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;

import com.google.common.base.Preconditions;

public class MonitorableRegistry {
	public static MonitorableRegistry DEFAULT_REGISTRY = new MonitorableRegistry();
	
    /**
     * This is a TreeMap so that the Monitorables are maintained in alphabetical order.
     */
    private final Map<String, Monitorable<?>> monitorables = new TreeMap<String, Monitorable<?>>();

    private boolean stateFrozen = false;

    public synchronized <T> void register(Monitorable<T> monitorable) {
        if (stateFrozen) {
            throw new IllegalStateException("Cannot register monitorable " + monitorable.getName()
                    + " after MonitorableRegistry has been frozen");
        }
        if (monitorables.containsKey(monitorable.getName())) {
            throw new UnsupportedOperationException(
                    "There is already an instance of the Monitorable [" + monitorable.getName()
                            + "] registered.");
        }
        monitorables.put(monitorable.getName(), monitorable);
    }

    public synchronized void freeze() {
    	stateFrozen = true;
    }
    
    public synchronized Collection<Monitorable<?>> getMonitorables() {
    	Preconditions.checkState(stateFrozen, "MonitorableRegistry must be frozen before retrieving monitorable list");
        stateFrozen = true;
        return Collections.unmodifiableCollection(monitorables.values());
    }
    
    /*
     * Testing only -- should be eliminated once the default registry is gone
     */
    public static void clearDefaultRegistry() {
        DEFAULT_REGISTRY = new MonitorableRegistry();
    }
}
@@ -146,92 +146,322 @@
 p;%0D%0A
-%0D%0Aimport com.google.common.base.Preconditions;%0D%0A%0D%0Apublic class MonitorableRegistry %7B
+import java.util.concurrent.ConcurrentHashMap;%0D%0Aimport java.util.concurrent.ConcurrentMap;%0D%0A%0D%0Aimport com.google.common.base.Preconditions;%0D%0A%0D%0Apublic class MonitorableRegistry %7B%0D%0A%09private static final ConcurrentMap%3CString, MonitorableRegistry%3E NAMED_INSTANCES = new ConcurrentHashMap%3CString, MonitorableRegistry%3E();
 %0D%0A%09p
@@ -540,17 +540,16 @@
 try();%0D%0A
-%09
 %0D%0A    /*
@@ -794,16 +794,20 @@
 false;%0D%0A
+    
 %0D%0A    pu
@@ -1807,27 +1807,24 @@
 );%0D%0A    %7D%0D%0A 
-   
 %0D%0A    /*%0D%0A  
@@ -2019,10 +2019,408 @@
 %0D%0A    %7D%0D
+%0A%0D%0A    public static MonitorableRegistry getNamedInstance(String name) %7B%0D%0A    %09MonitorableRegistry instance = NAMED_INSTANCES.get(name);%0D%0A    %09if (instance == null) %7B%0D%0A    %09%09instance = new MonitorableRegistry();%0D%0A    %09%09MonitorableRegistry existing = NAMED_INSTANCES.putIfAbsent(name, instance);%0D%0A    %09%09if (existing != null) %7B%0D%0A    %09%09%09return existing;%0D%0A    %09%09%7D%0D%0A    %09%7D%0D%0A    %09return instance;%0D%0A    %7D%0D
 %0A%7D

Add concept of 'named' registries to allow JDBC driver lookups