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

jadex.platform.service.security.DecentralizedAcquisitionMechanism Maven / Gradle / Ivy

Go to download

The Jadex platform package contains implementations of platform services as well as the platform component itself.

There is a newer version: 3.0.117
Show newest version
package jadex.platform.service.security;

import java.security.cert.Certificate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import jadex.bridge.BasicComponentIdentifier;
import jadex.bridge.IComponentIdentifier;
import jadex.bridge.service.IService;
import jadex.bridge.service.RequiredServiceInfo;
import jadex.bridge.service.search.SServiceProvider;
import jadex.bridge.service.types.security.ISecurityService;
import jadex.bridge.service.types.security.MechanismInfo;
import jadex.bridge.service.types.security.ParameterInfo;
import jadex.commons.ChangeEvent;
import jadex.commons.Properties;
import jadex.commons.Property;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;
import jadex.commons.future.IIntermediateResultListener;
import jadex.commons.future.IResultListener;

/**
 *  The decentralized acquisition mechanism is based on a peer to peer scheme. It
 *  asks all available platforms for the certificate of a specific caller.
 *  It then waits for the first n (acquirecnt) answers and checks if it received
 *  consistent results (all are equal). If this is the case it will deliver the
 *  certificate, otherwise an exception is raised. 
 */
public class DecentralizedAcquisitionMechanism extends AAcquisitionMechanism
{
	//-------- attributes --------
	
	/** The number of responses (<1=disabled). */
	protected int responses;
	
	//-------- constructors --------

	/**
	 *  Create a new mechanism.
	 */
	public DecentralizedAcquisitionMechanism()
	{
		this(1);
	}
	
	/**
	 *  Create a new mechanism.
	 */
	public DecentralizedAcquisitionMechanism(int responses)
	{
		this.responses = responses;
//		setResponses(responses);
	}
	
	//-------- methods --------
	
	/**
	 * 
	 */
	public IFuture acquireCertificate(final String name)
	{
		final Future ret = new Future();

		if(responses<1)
		{
			ret.setException(new SecurityException("Certificate not available and aquisition disabled: "+name));
			return ret;
		}
		
		final IComponentIdentifier cid = new BasicComponentIdentifier(name);
		
		// Try to fetch certificate from other platforms
		SServiceProvider.getServices(secser.getComponent(), ISecurityService.class, RequiredServiceInfo.SCOPE_GLOBAL)
			.addResultListener(new IIntermediateResultListener()
		{
			protected int ongoing;
			
			protected boolean finished;
			
			protected List certs = new ArrayList();
			
			public void intermediateResultAvailable(ISecurityService ss)
			{
				ongoing++;
				
				if(!((IService)ss).getServiceIdentifier().equals(secser.getServiceIdentifier()))
				{
					ss.getPlatformCertificate(cid).addResultListener(new IResultListener()
					{
						public void resultAvailable(Certificate cert)
						{
							certs.add(cert);
							if(certs.size()>=responses && !ret.isDone())
							{
								try
								{
									byte[] enc = certs.get(0).getEncoded();
									boolean ok = true;
									for(int i=1; i result)
			{
				for(ISecurityService ss: result)
				{
					intermediateResultAvailable(ss);
				}
				finished();
			}
			
			public void exceptionOccurred(Exception exception)
			{
				finished();
			}
			
			protected void checkFinish()
			{
				if(ongoing==0 && finished && !ret.isDone())
				{
					ret.setExceptionIfUndone(new SecurityException("Unable to retrieve certificate: "+name));
				}
			}
		});
		
		return ret;
	}
	
	/**
	 *  Get the mechanism info for the gui.
	 */
	public MechanismInfo getMechanismInfo()
	{
		List params = new ArrayList();
		params.add(new ParameterInfo("responses", "Number of evaluated certificate responses " +
			"(must all be equal, use 1 for bootstrapping, use <1 to disable)", int.class, Integer.valueOf(responses)));
		MechanismInfo ret = new MechanismInfo("Decentralized", getClass(), params);
		return ret;
	}
	
	/**
	 *  Set a mechanism parameter value.
	 */
	public void setParameterValue(String name, Object value)
	{
//		System.out.println("set param val: "+name+" "+value);
		
		if("responses".equals(name))
		{
			setResponses(((Integer)value).intValue());
		}
		else
		{
			throw new RuntimeException("Unknown parameter: "+name);
		}
	}
	
	/**
	 *  Set the responses.
	 *  @param responses The responses to set.
	 */
	public void setResponses(int responses)
	{
		this.responses = responses;
		
		getSecurityService().publishEvent(new ChangeEvent(getClass(), ISecurityService.PROPERTY_MECHANISMPARAMETER, 
			new Object[]{"responses", Integer.valueOf(responses)}));
	}

	/**
	 *  Get the properties of the mechanism.
	 */
	public Properties getProperties()
	{
		Properties props = new Properties();
		props.addProperty(new Property("responses", ""+responses));
		return props;
	}

	/**
	 *  Set the properties of the mechanism.
	 */
	public void setProperties(Properties props)
	{
		if(props.getProperty("responses")!=null)
		{
			setResponses(props.getIntProperty("responses"));
		}
	}
}