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

jadex.bridge.component.impl.PropertiesComponentFeature Maven / Gradle / Ivy

Go to download

Jadex bridge is a base package for kernels and platforms, i.e., it is used by both and provides commonly used interfaces and classes for active components and their management.

There is a newer version: 4.0.267
Show newest version
package jadex.bridge.component.impl;

import java.util.LinkedHashMap;
import java.util.Map;

import jadex.bridge.IInternalAccess;
import jadex.bridge.component.ComponentCreationInfo;
import jadex.bridge.component.IComponentFeatureFactory;
import jadex.bridge.component.IPropertiesFeature;
import jadex.bridge.modelinfo.UnparsedExpression;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;
import jadex.javaparser.SJavaParser;

/**
 *  This feature provides arguments.
 */
// Todo: remove!!! only used for logging level.
public class PropertiesComponentFeature	extends	AbstractComponentFeature implements IPropertiesFeature
{
	//-------- constants ---------
	
	/** The factory. */
	public static final IComponentFeatureFactory FACTORY = new ComponentFeatureFactory(
		IPropertiesFeature.class, PropertiesComponentFeature.class);
	
	//-------- attributes --------
	
	/** The properties. */
	protected Map	properties;
	
	//-------- constructors --------
	
	/**
	 *  Create the feature.
	 */
	public PropertiesComponentFeature(IInternalAccess component, ComponentCreationInfo cinfo)
	{
		super(component, cinfo);
	}
	
	/**
	 *  Initialize the feature.
	 */
	public IFuture init()
	{
		try
		{
		
			// Todo: runtime-supplied properties?
	//		if(cinfo.getArguments()!=null)
	//		{
	//			for(Iterator> it=cinfo.getArguments().entrySet().iterator(); it.hasNext(); )
	//			{
	//				Map.Entry entry = it.next();
	//				if(arguments==null)
	//				{
	//					this.arguments	= new LinkedHashMap();
	//				}
	//				arguments.put(entry.getKey(), entry.getValue());
	//			}
	//		}
			
			// Todo: configuration properties?
	//		ConfigurationInfo	ci	= cinfo.getConfiguration()!=null ? component.getModel().getConfiguration(cinfo.getConfiguration()) : null;
	//		if(ci!=null)
	//		{
	//			UnparsedExpression[]	upes	= ci.getArguments();
	//			for(int i=0; i();
	//					}
	//					arguments.put(upes[i].getName(), SJavaParser.getParsedValue(upes[i], component.getModel().getAllImports(), component.getFetcher(), component.getClassLoader()));
	//				}
	//			}
	//		}
			
			
			for(Map.Entry prop: getComponent().getModel().getProperties().entrySet())
			{
				if((properties==null || !properties.containsKey(prop.getKey())))
				{
					if(properties==null)
					{
						this.properties	= new LinkedHashMap();
					}
					
					Object tmp	= prop.getValue();
					if(tmp instanceof UnparsedExpression)
					{
						final UnparsedExpression unexp = (UnparsedExpression)tmp;
						Class clazz = unexp.getClazz()!=null? unexp.getClazz().getType(getComponent().getClassLoader(), getComponent().getModel().getAllImports()): null;
						if(unexp.getValue()==null || unexp.getValue().length()==0 && clazz!=null)
						{
							tmp = clazz.getDeclaredConstructor().newInstance(); //clazz.newInstance();
						}
						else
						{
							tmp = SJavaParser.evaluateExpression(unexp.getValue(), getComponent().getModel().getAllImports(), getComponent().getFetcher(), getComponent().getClassLoader());
						}
					}
					properties.put(prop.getKey(), tmp);
				}
			}
			
			//System.out.println("init of prop base done: "+getComponent().getId());
			
			return IFuture.DONE;
		}
		catch(Exception e)
		{
			return new Future(e);
		}
	}
	
	/**
	 *  Check if the feature potentially executed user code in body.
	 *  Allows blocking operations in user bodies by using separate steps for each feature.
	 *  Non-user-body-features are directly executed for speed.
	 *  If unsure just return true. ;-)
	 */
	public boolean	hasUserBody()
	{
		return false;
	}
	
	//-------- IArgumentsFeature interface --------
	
	/**
	 *  Get a property value.
	 *  @param name	The property name.
	 *  @return The property value (if any).
	 */
	public Object	getProperty(String name)
	{
		return properties!=null ? properties.get(name) : null;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy