/*
 * ImplementationRegistry.java
 *
 * Generated by {productTitle}  {versionName} - {utilityTitle}
 *
 * {copywrite}
 */
package {packageName}.common;

import java.util.HashMap;

import javax.wbem.WBEMException;

public class ImplementationRegistry {

	// Stores the implementation files
	private static HashMap<String, ProviderImplIF> map = new HashMap<String, ProviderImplIF>(
			300);

	protected ImplementationRegistry() {
		// Exists to defeat instantiation
	}

	/**
	 * Gets an implementation based on the Java class
	 * 
	 * @param clz
	 *            The class for the implementation being retrieved.
	 * @return A ProviderImpl to the requested class
	 * @throws WBEMException
	 */
	public static <T extends ProviderImplIF> ProviderImplIF getImplementation(
			Class<T> clz) throws WBEMException {
		ProviderImplIF singleton = map.get(clz.getCanonicalName());
		if (null == singleton) {
			synchronized (ImplementationRegistry.class) {				
				// make sure no one created while waiting for lock
				singleton = map.get(clz.getCanonicalName());
				if (null == singleton) {
					try {
						singleton = clz.newInstance();
						map.put(clz.getCanonicalName(), singleton);
					} catch (InstantiationException | IllegalAccessException e) {
						throw new WBEMException(WBEMException.CIM_ERR_FAILED,
								"Exception instantiating "
										+ clz.getSimpleName(), null, e);
					}
				}
			}
		}
		return singleton;
	}
}