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

jadex.bpmn.runtime.task.CreateComponentTask Maven / Gradle / Ivy

package jadex.bpmn.runtime.task;

import jadex.bpmn.model.MParameter;
import jadex.bpmn.runtime.BpmnInterpreter;
import jadex.bpmn.runtime.ITask;
import jadex.bpmn.runtime.ITaskContext;
import jadex.bridge.CreationInfo;
import jadex.bridge.IComponentIdentifier;
import jadex.bridge.IComponentManagementService;
import jadex.bridge.service.RequiredServiceBinding;
import jadex.bridge.service.RequiredServiceInfo;
import jadex.bridge.service.SServiceProvider;
import jadex.commons.future.DelegationResultListener;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;
import jadex.commons.future.IResultListener;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 *  Task for creating a component.
 */
public class CreateComponentTask implements ITask
{
	/** Future called when the component is created to allow compensation (i.e. killing the component). */
	protected Future creationFuture = new Future();
	
	static Set reserved;
	static
	{
		reserved = new HashSet();
		reserved.add("name");
		reserved.add("model");
		reserved.add("configuration");
		reserved.add("suspend");
		reserved.add("subcomponent");
		reserved.add("killlistener");
		reserved.add("resultmapping");
		reserved.add("wait");
		reserved.add("master");
		reserved.add("arguments");
	}
	
	/**
	 *  Execute the task.
	 */
	public IFuture execute(final ITaskContext context, final BpmnInterpreter instance)
	{
		final Future ret = new Future();
		
		SServiceProvider.getService(instance.getServiceContainer(), IComponentManagementService.class, RequiredServiceInfo.SCOPE_PLATFORM).addResultListener(instance.createResultListener(new IResultListener()
		{
			public void resultAvailable(Object result)
			{
				IComponentManagementService	cms	= (IComponentManagementService)result;
				String name = (String)context.getParameterValue("name");
				String model = (String)context.getParameterValue("model");
				String config = (String)context.getParameterValue("configuration");
				boolean suspend = context.getParameterValue("suspend")!=null? ((Boolean)context.getParameterValue("suspend")).booleanValue(): false;
				boolean sub = context.getParameterValue("subcomponent")!=null? ((Boolean)context.getParameterValue("subcomponent")).booleanValue(): false;
				final IResultListener killlistener = (IResultListener)context.getParameterValue("killlistener");
				final String[] resultmapping = (String[])context.getParameterValue("resultmapping");
				boolean wait = context.getParameterValue("wait")!=null? ((Boolean)context.getParameterValue("wait")).booleanValue(): resultmapping!=null;
				Boolean master = context.getParameterValue("master")!=null? (Boolean)context.getParameterValue("master"): null;
				Boolean daemon = context.getParameterValue("daemon")!=null? (Boolean)context.getParameterValue("daemon"): null;
				Boolean autoshutdown = context.getParameterValue("autoshutdown")!=null? (Boolean)context.getParameterValue("autoshutdown"): null;
				RequiredServiceBinding[] bindings = context.getParameterValue("bindings")!=null? (RequiredServiceBinding[])context.getParameterValue("bindings"): null;
				
				Map args = (Map)context.getParameterValue("arguments");
				if(args==null)
				{
					args = new HashMap();
					Map params = context.getActivity().getParameters();
					if(params!=null)
					{
						for(Iterator it=params.values().iterator(); it.hasNext(); )
						{
							MParameter param = (MParameter)it.next();
							if(!reserved.contains(param.getName()))
								args.put(param.getName(), context.getParameterValue(param.getName()));
						}
					}
				}
//				System.out.println("args: "+args);
				IResultListener lis = killlistener;
				if(wait)
				{
					lis = new IResultListener()
					{
						public void resultAvailable(Object result)
						{
							if(result!=null)
							{
								Map results = (Map)result;
								for(int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy