package net.sf.taverna.t2.servicedescriptions.impl;

import java.util.HashSet;
import java.util.Set;

import net.sf.taverna.t2.servicedescriptions.ServiceDescription;
import net.sf.taverna.t2.servicedescriptions.ServiceDescriptionProvider;
import net.sf.taverna.t2.servicedescriptions.ServiceDescriptionRegistry;
import net.sf.taverna.t2.spi.SPIRegistry;

public class ServiceDescriptionRegistryImpl implements
		ServiceDescriptionRegistry {

	private static class Singleton {
		private static final ServiceDescriptionRegistryImpl instance = 
			new ServiceDescriptionRegistryImpl();
	}

	public static ServiceDescriptionRegistryImpl getInstance() {
		return Singleton.instance;
	}

	public SPIRegistry<ServiceDescriptionProvider> providerRegistry = new SPIRegistry<ServiceDescriptionProvider>(
			ServiceDescriptionProvider.class);

	private Set<ServiceDescriptionProvider> localDescriptions = new HashSet<ServiceDescriptionProvider>();

	public void addServiceDescriptionProvider(
			ServiceDescriptionProvider provider) {
		localDescriptions.add(provider);
	}

	@SuppressWarnings("unchecked")
	public Set<ServiceDescription> getServiceDescriptions() {
		Set<ServiceDescription> serviceDescriptions = new HashSet<ServiceDescription>();
		for (ServiceDescriptionProvider provider : getServiceDescriptionProviders()) {
			serviceDescriptions.addAll(provider.getServiceDescriptions());
		}
		return serviceDescriptions;
	}

	public Set<ServiceDescriptionProvider> getServiceDescriptionProviders() {
		HashSet<ServiceDescriptionProvider> providers = new HashSet<ServiceDescriptionProvider>(
				providerRegistry.getInstances());
		providers.addAll(localDescriptions);
		return providers;
	}

}

@@ -89,21 +89,129 @@
 va.util.
-Set;%0A
+List;%0Aimport java.util.Set;%0A%0Aimport net.sf.taverna.t2.servicedescriptions.ConfigurableServiceDescriptionProvider;
 %0Aimport 
@@ -455,16 +455,112 @@
 egistry;
+%0Aimport net.sf.taverna.t2.workflowmodel.ConfigurationException;%0A%0Aimport org.apache.log4j.Logger;
 %0A%0Apublic
@@ -739,20 +739,16 @@
 tance = 
-%0A%09%09%09
 new Serv
@@ -872,24 +872,116 @@
 stance;%0A%09%7D%0A%0A
+%09public static Logger logger = Logger%0A%09%09%09.getLogger(ServiceDescriptionRegistryImpl.class);%0A%0A
 %09public SPIR
@@ -1704,24 +1704,232 @@
 ptions;%0A%09%7D%0A%0A
+%09public %3CConfigBean%3E List%3CConfigBean%3E getConfigurationsFor(%0A%09%09%09ConfigurableServiceDescriptionProvider%3CConfigBean%3E provider) %7B%0A%09%09return provider.getDefaultConfigurations();%0A%09%7D%0A%0A%09@SuppressWarnings(%22unchecked%22)%0A
 %09public Set%3C
@@ -2085,13 +2085,56 @@
 er%3E(
+);
 %0A%09%09
-%09%09
+for (ServiceDescriptionProvider provider : 
 prov
@@ -2145,16 +2145,21 @@
 Registry
+%0A%09%09%09%09
 .getInst
@@ -2166,17 +2166,755 @@
 ances())
-;
+ %7B%0A%09%09%09if (provider instanceof ConfigurableServiceDescriptionProvider) %7B%0A%09%09%09%09ConfigurableServiceDescriptionProvider template = ((ConfigurableServiceDescriptionProvider) provider);%0A%09%09%09%09List%3CObject%3E configurables = getConfigurationsFor(template);%0A%09%09%09%09for (Object config : configurables) %7B%0A%09%09%09%09%09// Make a copy that we can configure%0A%09%09%09%09%09ConfigurableServiceDescriptionProvider configurableProvider = template%0A%09%09%09%09%09%09%09.clone();%0A%09%09%09%09%09try %7B%0A%09%09%09%09%09%09configurableProvider.configure(config);%0A%09%09%09%09%09%7D catch (ConfigurationException e) %7B%0A%09%09%09%09%09%09logger.warn(%22Can't configure provider %22%0A%09%09%09%09%09%09%09%09+ configurableProvider + %22 with %22 + config);%0A%09%09%09%09%09%09continue;%0A%09%09%09%09%09%7D%0A%09%09%09%09%09providers.add(configurableProvider);%0A%09%09%09%09%7D%0A%09%09%09%7D else %7B%0A%09%09%09%09providers.add(provider);%0A%09%09%09%7D%0A%09%09%7D
 %0A%09%09provi

support configurable providers