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

jadex.bridge.nonfunctional.NFPropertyProvider 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.

The newest version!
package jadex.bridge.nonfunctional;

import jadex.bridge.IInternalAccess;
import jadex.bridge.service.types.monitoring.MonitoringEvent;
import jadex.bridge.service.types.monitoring.IMonitoringService.PublishEventLevel;
import jadex.bridge.service.types.monitoring.IMonitoringService.PublishTarget;
import jadex.commons.future.CounterResultListener;
import jadex.commons.future.DelegationResultListener;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;

import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

/**
 *  Base impl for nf property property provider.
 */
public abstract class NFPropertyProvider implements INFPropertyProvider
{
	/** The parent. */
	protected INFPropertyProvider nfparent;
	
	/** Non-functional properties. */
	protected Map> nfproperties;
	
	/**
	 *  Create a new provider.
	 */
	public NFPropertyProvider()
	{
	}
	
	/**
	 *  Create a new provider.
	 */
	public NFPropertyProvider(INFPropertyProvider parent)
	{
		this.nfparent = parent;
	}
	
	/**
	 *  Returns the names of all non-functional properties of this service.
	 *  @return The names of the non-functional properties of this service.
	 */
	public IFuture getNFPropertyNames()
	{
		return new Future(nfproperties != null? nfproperties.keySet().toArray(new String[nfproperties.size()]) : new String[0]);
	}
	
	/**
	 *  Returns the names of all non-functional properties of this service.
	 *  @return The names of the non-functional properties of this service.
	 */
	public IFuture getNFAllPropertyNames()
	{
		final Future ret = new Future();
		final String[] myprops = nfproperties != null? nfproperties.keySet().toArray(new String[nfproperties.size()]) : new String[0];
		if(getParent()!=null)
		{
			getParent().getNFAllPropertyNames().addResultListener(new DelegationResultListener(ret)
			{
				public void customResultAvailable(String[] result)
				{
					Set tmp = new LinkedHashSet();
					for(String p: result)
					{
						tmp.add(p);
					}
					for(String p: myprops)
					{
						tmp.add(p);
					}
					ret.setResult((String[])tmp.toArray(new String[tmp.size()]));
				}
			});
		}
		else
		{
			ret.setResult(myprops);
		}
		return ret;
	}
	
	/**
	 *  Returns the meta information about a non-functional property of this service.
	 *  @param name Name of the property.
	 *  @return The meta information about a non-functional property of this service.
	 */
	public IFuture> getNFPropertyMetaInfos()
	{
		Future> ret = new Future>();
		
		Map res = new HashMap();
		if(nfproperties!=null)
		{
			for(String key: nfproperties.keySet())
			{
				res.put(key, nfproperties.get(key).getMetaInfo());
			}
		}
		ret.setResult(res);
		
		return ret;
	}

	
	/**
	 *  Returns the meta information about a non-functional property of this service.
	 *  @param name Name of the property.
	 *  @return The meta information about a non-functional property of this service.
	 */
	public IFuture getNFPropertyMetaInfo(String name)
	{
		Future ret = new Future();
		
		INFPropertyMetaInfo mi = nfproperties != null? nfproperties.get(name) != null? nfproperties.get(name).getMetaInfo() : null : null;
		
		if(mi != null)
		{
			ret.setResult(mi);
		}
		else if(getParent()!=null)
		{
			getParent().getNFPropertyMetaInfo(name).addResultListener(new DelegationResultListener(ret));
		}
		else
		{
			ret.setException(new RuntimeException("Property not found: "+name));
		}
		
		return ret;
	}
	
	/**
	 *  Returns the current value of a non-functional property of this service, performs unit conversion.
	 *  @param name Name of the property.
	 *  @param type Type of the property value.
	 *  @return The current value of a non-functional property of this service.
	 */
	public  IFuture getNFPropertyValue(String name)
	{
		Future ret = new Future();
		
		INFProperty prop = (INFProperty) (nfproperties != null? nfproperties.get(name) : null);
		
		if(prop != null)
		{
			try
			{
				prop.getValue().addResultListener(new DelegationResultListener(ret));
			}
			catch (Exception e)
			{
				ret.setException(e);
			}
		}
		else if(getParent()!=null)
		{
			IFuture fut = getParent().getNFPropertyValue(name);
			fut.addResultListener(new DelegationResultListener(ret));
		}
		else
		{
			ret.setException(new RuntimeException("Property not found: "+name));
		}
		
		return ret;
	}
	
	/**
	 *  Returns the current value of a non-functional property of this service, performs unit conversion.
	 *  @param name Name of the property.
	 *  @param unit Unit of the property value.
	 *  @return The current value of a non-functional property of this service.
	 */
//	public IFuture getNFPropertyValue(String name, Class unit)
	public IFuture getNFPropertyValue(String name, U unit)
	{
		Future ret = new Future();
		
		INFProperty prop = (INFProperty) (nfproperties != null? nfproperties.get(name) : null);
		
		if(prop!=null)
		{
			try
			{
				prop.getValue(unit).addResultListener(new DelegationResultListener(ret));
			}
			catch (Exception e)
			{
				ret.setException(e);
		//			ret.setException(new ClassCastException("Requested value type (" + String.valueOf(type) + ") does not match value type (" + String.valueOf(reto.getClass()) + ") for this non-functional property: " + name));
			}
		}
		else if(getParent()!=null)
		{
//			internalaccess.getExternalAccess().getNFPropertyValue(name, unit).addResultListener(new DelegationResultListener(ret));
			IFuture fut = getParent().getNFPropertyValue(name, unit);
			fut.addResultListener(new DelegationResultListener(ret));
		}
		else
		{
			ret.setException(new RuntimeException("Property not found: "+name));
		}
		
		return ret;
	}
	
	/**
	 *  Add a non-functional property.
	 *  @param metainfo The metainfo.
	 */
	public IFuture addNFProperty(INFProperty nfprop)
	{
		final Future ret = new Future();
		if(nfproperties==null)
			nfproperties = new HashMap>();
		nfproperties.put(nfprop.getName(), nfprop);
		
		if(getInternalAccess().hasEventTargets(PublishTarget.TOALL, PublishEventLevel.COARSE))
		{
			MonitoringEvent me = new MonitoringEvent(getInternalAccess().getComponentIdentifier(), getInternalAccess().getComponentDescription().getCreationTime(), 
				MonitoringEvent.TYPE_PROPERTY_REMOVED, System.currentTimeMillis(), PublishEventLevel.COARSE);
			me.setProperty("propname", nfprop.getName());
			getInternalAccess().publishEvent(me, PublishTarget.TOALL).addResultListener(new DelegationResultListener(ret));
		}
		else
		{
			ret.setResult(null);
		}
		return ret;
	}
	
	/**
	 *  Remove a non-functional property.
	 *  @param The name.
	 */
	public IFuture removeNFProperty(final String name)
	{
		final Future ret = new Future();
		if(nfproperties!=null)
		{
			INFProperty prop = nfproperties.remove(name);
			if(prop!=null)
			{
				prop.dispose().addResultListener(new DelegationResultListener(ret)
				{
					public void customResultAvailable(Void result)
					{
						if(getInternalAccess().hasEventTargets(PublishTarget.TOALL, PublishEventLevel.COARSE))
						{
							MonitoringEvent me = new MonitoringEvent(getInternalAccess().getComponentIdentifier(), getInternalAccess().getComponentDescription().getCreationTime(), 
								MonitoringEvent.TYPE_PROPERTY_REMOVED, System.currentTimeMillis(), PublishEventLevel.COARSE);
							me.setProperty("propname", name);
							getInternalAccess().publishEvent(me, PublishTarget.TOALL).addResultListener(new DelegationResultListener(ret));
						}
						else
						{
							ret.setResult(null);
						}
					}
				});
			}
			else
			{
				ret.setResult(null);
			}
		}
		else
		{
			ret.setResult(null);
		}
		return ret;
	}
	
	/**
	 *  Get the parent.
	 *  return The parent.
	 */
	public INFPropertyProvider getParent()
	{
		return nfparent;
	}

	/**
	 *  Set the parent. 
	 *  @param parent The parent to set.
	 */
	public void setParent(INFPropertyProvider parent)
	{
		this.nfparent = parent;
	}
	
	/**
	 *  Shutdown the provider.
	 */
	public IFuture shutdownNFPropertyProvider()
	{
		Future ret = new Future();
		if(nfproperties!=null)
		{
			CounterResultListener lis = new CounterResultListener(nfproperties.size(), true, new DelegationResultListener(ret));
			for(INFProperty prop: nfproperties.values())
			{
				prop.dispose().addResultListener(lis);
			}
		}
		else
		{
			ret.setResult(null);
		}
		return ret;
	}
	
	/**
	 *  Get the internal access.
	 */
	public abstract IInternalAccess getInternalAccess();
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy