package org.liveontologies.protege.explanation.justification;



import java.util.ArrayList;
import java.util.Collection;

import org.liveontologies.protege.explanation.justification.service.JustificationComputationPlugin;
import org.liveontologies.protege.explanation.justification.service.JustificationComputationPluginLoader;
import org.liveontologies.protege.explanation.justification.service.JustificationComputationService;
import org.protege.editor.core.Disposable;
import org.protege.editor.owl.OWLEditorKit;

/**
 * Keeps track of the available {@link JustificationComputationService} plugins.
 * 
 * @author Pavel Klinov pavel.klinov@uni-ulm.de
 * 
 * @author Yevgeny Kazakov
 */
public class JustificationComputationServiceManager implements Disposable {

	private static final String KEY_ = "org.liveontologies.protege.explanation.justification.services";

	private final OWLEditorKit kit_;

	private final Collection<JustificationComputationService> services_;

	private JustificationComputationService defaultService_;

	private JustificationComputationServiceManager(OWLEditorKit kit)
			throws Exception {
		kit_ = kit;
		services_ = new ArrayList<JustificationComputationService>();
		JustificationComputationPluginLoader loader = new JustificationComputationPluginLoader(
				kit_);
		for (JustificationComputationPlugin plugin : loader.getPlugins()) {
			JustificationComputationService service = plugin.newInstance();
			service.initialise();
			services_.add(service);
		}
	}

	public static synchronized JustificationComputationServiceManager get(
			OWLEditorKit kit) throws Exception {
		JustificationComputationServiceManager manager = kit.getModelManager()
				.get(KEY_);
		if (manager == null) {
			manager = new JustificationComputationServiceManager(kit);
			kit.getModelManager().put(KEY_, manager);
		}
		return manager;
	}

	@Override
	public void dispose() throws Exception {
		for (JustificationComputationService service : services_) {
			service.dispose();
		}
	}

	public OWLEditorKit getOwlEditorKit() {
		return kit_;
	}

	public Collection<JustificationComputationService> getServices() {
		return services_;
	}

	public JustificationComputationService getDefaultService() {
		return defaultService_;
	}

	public void setDefaultService(JustificationComputationService service) {
		this.defaultService_ = service;
	}

}