All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.objectweb.fractal.bf.adl.binder.BinderCompiler Maven / Gradle / Ivy

There is a newer version: 0.9
Show newest version
package org.objectweb.fractal.bf.adl.binder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.logging.Logger;

import org.objectweb.fractal.adl.ADLException;
import org.objectweb.fractal.adl.components.Component;
import org.objectweb.fractal.adl.components.ComponentContainer;
import org.objectweb.fractal.adl.components.ComponentPair;
import org.objectweb.fractal.adl.components.PrimitiveCompiler;
import org.objectweb.fractal.api.NoSuchInterfaceException;
import org.objectweb.fractal.api.control.BindingController;
import org.objectweb.fractal.api.control.IllegalBindingException;
import org.objectweb.fractal.api.control.IllegalLifeCycleException;
import org.objectweb.fractal.bf.adl.common.Parameter;
import org.objectweb.fractal.bf.adl.common.ParameterContainer;
import org.objectweb.fractal.bf.adl.exporter.ExporterBuilder;
import org.objectweb.fractal.task.core.Task;
import org.objectweb.fractal.task.core.TaskMap;
import org.objectweb.fractal.task.deployment.api.InstanceProviderTask;
import org.objectweb.fractal.task.deployment.lib.AbstractRequireInstanceProviderTask;

public class BinderCompiler implements PrimitiveCompiler, BindingController {

	Logger log = Logger.getLogger(BinderCompiler.class.getCanonicalName());

	/**
	 * Name of the mandatory interface bound to the {@link ExporterBuilder} used
	 * by this compiler.
	 */

	public final static String BUILDER_BINDING = "builder";

	private BinderBuilder builder;

	/**
	 * @see org.objectweb.fractal.adl.components.PrimitiveCompiler#compile(java.util.List,
	 *      org.objectweb.fractal.adl.components.ComponentContainer,
	 *      org.objectweb.fractal.task.core.TaskMap, java.util.Map)
	 */
	public void compile(List path,
			ComponentContainer container, TaskMap tasks,
			Map context) throws ADLException {

		log.finest("Entering Binder Compiler");

		// this code is here purely to obtain a reference to serverComp via the
		// subComponents map
		Map subComponents = new HashMap();
		subComponents.put("this", container);
		Component[] comps = container.getComponents();
		for (int i = 0; i < comps.length; i++) {
			subComponents.put(comps[i].getName(), comps[i]);
		}

		if (container instanceof BinderContainer) {

			Binder[] binders = ((BinderContainer) container).getBinders();

			for (Binder binder : binders) {

				Parameter[] parameters = null;

				if (binder instanceof ParameterContainer) {

					ParameterContainer pc = (ParameterContainer) binder;
					parameters = pc.getParameters();
				}

				final String componentDotInterface = binder.getInterface();
				// compName.itfName
				int dot = componentDotInterface.lastIndexOf(".");
				final String componentName = componentDotInterface.substring(0,
						dot);
				final String itfName = componentDotInterface.substring(dot + 1);

				Object serverComp = subComponents.get(componentName);
				try {
					// the task may already exist, in case of a shared component
					tasks.getTask("binder" + componentName, serverComp);
				} catch (NoSuchElementException e) {

					// createServerTask TaskHole
					TaskMap.TaskHole createServerTaskHole = tasks.getTaskHole(
							"create", serverComp);

					BinderTask exportTask = new BinderTask(builder, binder
							.getType(), itfName, parameters);

					exportTask
							.setServerInstanceProviderTask(createServerTaskHole);

					tasks.addTask("binder" + componentName, serverComp,
							exportTask);

					if (serverComp != container) {

						TaskMap.TaskHole addTaskHole = tasks.getTaskHole("add",
								new ComponentPair(container,
										(Component) serverComp));
						exportTask.addDependency(addTaskHole,
								Task.PREVIOUS_TASK_ROLE, context);
					}

				} // end catch block

			}

		}

		log.finest("Exiting Binder Compiler");
	}

	static class BinderTask extends AbstractRequireInstanceProviderTask {

		private TaskMap.TaskHole serverInstanceProviderTask;

		Logger log = Logger.getLogger(BinderTask.class.getCanonicalName());

		private final BinderBuilder builder;

		String type;

		String itfName;

		Parameter[] parameters;

		/**
		 * @param builder
		 * @param type
		 * @param itfName
		 * @param parameters
		 */
		public BinderTask(BinderBuilder builder, String type, String itfName,
				Parameter[] parameters) {
			this.builder = builder;
			this.type = type;
			this.itfName = itfName;
			this.parameters = parameters;
		}

		/**
		 * @see org.objectweb.fractal.task.core.Task#execute(java.util.Map)
		 */
		public void execute(Map context) throws Exception {

			List prms = new ArrayList();

			if (parameters != null) {
				for (Parameter p : parameters)
					prms.add(p);
			} else {
				log
						.warning("Couldn't find any parameters to configure the binder");
			}

			Object server = getServerInstanceProviderTask().getInstance();

			builder.bind(itfName, server, prms, type, context);

		}

		/*
		 * the following 2 methods are copied from BindingCompiler
		 */
		public InstanceProviderTask getServerInstanceProviderTask() {
			return (serverInstanceProviderTask) == null ? null
					: (InstanceProviderTask) serverInstanceProviderTask
							.getTask();
		}

		public void setServerInstanceProviderTask(final TaskMap.TaskHole task) {
			if (serverInstanceProviderTask != null) {
				removePreviousTask(serverInstanceProviderTask);
			}
			serverInstanceProviderTask = task;
			if (serverInstanceProviderTask != null) {
				addPreviousTask(serverInstanceProviderTask);
			}
		}

		/**
		 * @see org.objectweb.fractal.task.core.Task#getResult()
		 */
		public Object getResult() {
			return null;
		}

		@Override
		public String toString() {
			return "T" + System.identityHashCode(this) + "[BinderTask()]";
		}

	}

	// ~ BindingController methods
	public void bindFc(String itf, Object value)
			throws NoSuchInterfaceException, IllegalBindingException,
			IllegalLifeCycleException {

		if (itf.equals(BUILDER_BINDING)) {
			builder = (BinderBuilder) value;
			return;
		}
		throw new NoSuchInterfaceException("Cannot find interface " + itf);
	}

	public String[] listFc() {
		return new String[] { BUILDER_BINDING };
	}

	public Object lookupFc(String itf) throws NoSuchInterfaceException {

		if (itf.equals(BUILDER_BINDING)) {
			return builder;
		}
		throw new NoSuchInterfaceException("Cannot find interface " + itf);
	}

	public void unbindFc(String itf) throws NoSuchInterfaceException,
			IllegalBindingException, IllegalLifeCycleException {

		if (itf.equals(BUILDER_BINDING)) {
			builder = null;
			return;
		}
		throw new NoSuchInterfaceException("Cannot find interface " + itf);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy