CHESSIoT_Dev_old-master/org.polarsys.chess.core/src/org/polarsys/chess/core/transformationExecutor/TransformationExecutorManager.java
/*
-----------------------------------------------------------------------
--          			CHESS core plugin							 --
--                                                                   --
--                    Copyright (C) 2011-2012                        --
--                 University of Padova, ITALY                       --
--                                                                   --
-- Author: Alessandro Zovi         azovi@math.unipd.it 		         --
--                                                                   --
-- All rights reserved. This program and the accompanying materials  --
-- are made available under the terms of the Eclipse Public License  --
-- v1.0 which accompanies this distribution, and is available at     --
-- http://www.eclipse.org/legal/epl-v20.html                         --
-----------------------------------------------------------------------
*/

package org.polarsys.chess.core.transformationExecutor;

import java.util.Hashtable;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;
import org.osgi.framework.Bundle;

/**
 * Manages the extension-point 'org.polarsys.chess.transformationExecutor'
 *
 */
public class TransformationExecutorManager {

	/** The executor registry. */
	// list of of listener
	private Hashtable<String, ITransformationExecutor> executorRegistry;

	/** The listenerid extension id. */
	// extension point ID
	private String LISTENERID_EXTENSION_ID = "org.polarsys.chess.transformationExecutor";

	/** The name id. */
	private String NAME_ID = "name";

	/** The realization id. */
	private String REALIZATION_ID = "realization";
	
	/** The instance. */
	private static TransformationExecutorManager instance; 

	
	/**
	 * Creates the singleton instance of the manager.
	 *
	 * @return the transformation executor manager
	 */
	public static TransformationExecutorManager instance(){
		if (instance == null)
			instance = new TransformationExecutorManager();
		return instance;
	}
	
	/**
	 * Instantiates a new transformation executor manager.
	 */
	private TransformationExecutorManager() {
		super();
		executorRegistry = new Hashtable<String, ITransformationExecutor>();
		initializeExecutorList();
	}

	/**
	 * Loads all listeners of the model.
	 */
	private void initializeExecutorList() {
		// Reading data from plugins
				IExtensionRegistry reg = Platform.getExtensionRegistry();
		
		IConfigurationElement[] configElements = Platform.getExtensionRegistry().getConfigurationElementsFor(LISTENERID_EXTENSION_ID);
		
		for(int i = 0; i < configElements.length; i++) {
			inializeOneRule(configElements[i]);
		}
	}

	/**
	 * Loads one listener.
	 *
	 * @param element        the extension point
	 */
	private void inializeOneRule(IConfigurationElement element) {
		String listenerName = element.getAttribute(NAME_ID);
		try {
			ITransformationExecutor listener = (ITransformationExecutor)createExtension(element, element.getAttribute(REALIZATION_ID));
			executorRegistry.put(listenerName, listener);
		} catch (Exception e) {
			//Activator.log.error("- " + listenerName + " can not be loaded: "+e.getLocalizedMessage(), e);
		}

	}

	/**
	 * Creates the extension.
	 *
	 * @param element the element
	 * @param classAttribute the class attribute
	 * @return the object
	 * @throws Exception the exception
	 */
	private static Object createExtension(final IConfigurationElement element, final String classAttribute) throws Exception {
		try {
			Bundle extensionBundle = Platform.getBundle(element.getDeclaringExtension().getNamespaceIdentifier());
			Class clazz = extensionBundle.loadClass(classAttribute);
			Object obj = clazz.newInstance();
			return obj;
			// return element.createExecutableExtension(classAttribute);
		} catch (Exception e) {
			throw new Exception("unable to create Extension " + e);
		}
	}
	
	/**
	 * Retrieves the transformation to execute given its name.
	 * 
	 * @param name  the name of the transformation
	 * @return  the executor
	 */
	public ITransformationExecutor getExecutor(final String name){
		return executorRegistry.get(name);
	}
	
	/**
	 * Initializes the manager. 
	 */
	public static void init() {
		instance();
	}
}
