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

jadex.micro.tutorial.SpamInterceptorD4 Maven / Gradle / Ivy

Go to download

The Jadex micro applications package contains several example applications, benchmarks and testcases using micro agents.

There is a newer version: 4.0.267
Show newest version
package jadex.micro.tutorial;

import jadex.bridge.service.component.IServiceInvocationInterceptor;
import jadex.bridge.service.component.ServiceInvocationContext;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;

/**
 *  Simple interceptor that refuses messages of spammers.
 */
public class SpamInterceptorD4 implements IServiceInvocationInterceptor
{
	/**
	 *  Test if the interceptor is applicable.
	 *  @return True, if applicable.
	 */
	public boolean isApplicable(ServiceInvocationContext context)
	{
		try
		{
			return context.getMethod().getName().equals("message");
		}
		catch(Exception e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	
	/**
	 *  Execute the interceptor.
	 *  @param context The invocation context.
	 */
	public IFuture execute(ServiceInvocationContext context)
	{
		String sender = (String)context.getArgumentArray()[0];
		String text = (String)context.getArgumentArray()[1];
		if(sender.indexOf("Bot")!=-1)
		{
			System.out.println("Blocked spam message: "+sender+" "+text);
			return new Future((new RuntimeException("No spammers allowed.")));
		}
		else
		{
			return context.invoke();
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy