package edu.teco.dnd.blocks;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.apache.bcel.util.ClassPath;
import org.apache.bcel.util.Repository;
import org.apache.bcel.util.SyntheticRepository;

import edu.teco.dnd.temperature.TemperatureLogicBlock;
import edu.teco.dnd.util.SynchronizedBCELRepository;

public class FunctionBlockFactory {
	private final Repository repository;

	private final ConcurrentMap<String, FunctionBlockClass> blocks =
			new ConcurrentHashMap<String, FunctionBlockClass>();

	public FunctionBlockFactory(final Repository repository) throws ClassNotFoundException {
		this.repository = new SynchronizedBCELRepository(repository);
	}

	public FunctionBlockFactory(final ClassPath classPath) throws ClassNotFoundException {
		this(SyntheticRepository.getInstance(classPath));
	}

	public FunctionBlockFactory(final String classPath) throws ClassNotFoundException {
		this(new ClassPath(classPath));
	}

	public FunctionBlockClass getFunctionBlockClass(final Class<?> cls) throws ClassNotFoundException {
		return getFunctionBlockClass(cls.getCanonicalName());
	}

	public FunctionBlockClass getFunctionBlockClass(final String className) throws ClassNotFoundException {
		if (!blocks.containsKey(className)) {
			blocks.putIfAbsent(className, new FunctionBlockClass(repository, className));
		}
		return blocks.get(className);
	}

	// FIXME: remove once no longer needed for testing
	public static void main(final String[] args) throws ClassNotFoundException {
		final FunctionBlockFactory factory = new FunctionBlockFactory(SyntheticRepository.getInstance());
		final FunctionBlockClass blockClass = factory.getFunctionBlockClass(TemperatureLogicBlock.class);
		System.out.println(blockClass.getOptions());
	}
}

@@ -1404,388 +1404,7 @@
 ;%0A%09%7D
-%0A%0A%09// FIXME: remove once no longer needed for testing%0A%09public static void main(final String%5B%5D args) throws ClassNotFoundException %7B%0A%09%09final FunctionBlockFactory factory = new FunctionBlockFactory(SyntheticRepository.getInstance());%0A%09%09final FunctionBlockClass blockClass = factory.getFunctionBlockClass(TemperatureLogicBlock.class);%0A%09%09System.out.println(blockClass.getOptions());%0A%09%7D
 %0A%7D%0A

Remove main method used for testing