jadex.bridge.service.component.multiinvoke.MultiServiceInvocationHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jadex-platform-bridge Show documentation
Show all versions of jadex-platform-bridge Show documentation
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.
package jadex.bridge.service.component.multiinvoke;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Iterator;
import jadex.bridge.IInternalAccess;
import jadex.bridge.service.IService;
import jadex.bridge.service.RequiredServiceInfo;
import jadex.bridge.service.annotation.MultiplexCollector;
import jadex.bridge.service.annotation.MultiplexDistributor;
import jadex.bridge.service.annotation.TargetMethod;
import jadex.bridge.service.annotation.Value;
import jadex.bridge.service.component.IInternalRequiredServicesFeature;
import jadex.bridge.service.component.IInternalServiceMonitoringFeature;
import jadex.bridge.service.component.IRequiredServicesFeature;
import jadex.commons.IFilter;
import jadex.commons.IValueFetcher;
import jadex.commons.SReflect;
import jadex.commons.Tuple2;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;
import jadex.commons.future.IIntermediateFuture;
import jadex.commons.future.IIntermediateResultListener;
import jadex.commons.future.IntermediateFuture;
import jadex.javaparser.SJavaParser;
/**
* Invocation handler for multiplexing service calls.
*/
public class MultiServiceInvocationHandler implements InvocationHandler
{
//-------- attributes --------
/** The agent. */
protected IInternalAccess agent;
/** The required service name. */
protected String reqname;
/** The service type. */
protected Class> servicetype;
/** The multiplex service type. */
protected Class> muxservicetype;
//-------- constructors --------
/**
* Create a new invocation handler.
*/
public MultiServiceInvocationHandler(IInternalAccess agent, String reqname, Class> muxservicetype)
{
this.agent = agent;
this.reqname = reqname;
RequiredServiceInfo reqs = ((IInternalRequiredServicesFeature)agent.getFeature(IRequiredServicesFeature.class)).getServiceInfo(reqname);
if(reqs==null)
throw new RuntimeException("Required service not found: "+reqname);
this.servicetype = reqs.getType().getType(agent.getClassLoader(), agent.getModel().getAllImports());
this.muxservicetype = muxservicetype;
}
//-------- methods --------
/**
* Called when a method is invoked.
*/
public Object invoke(Object proxy, Method method, final Object[] args) throws Throwable
{
// System.out.println("invoke: "+Thread.currentThread());
Object gret;
Class> rettype = method.getReturnType();
// Get the method on original interface
String methodname = method.getName();
Class>[] params = method.getParameterTypes();
Method muxmethod = muxservicetype.getMethod(methodname, method.getParameterTypes());
if(muxmethod.isAnnotationPresent(TargetMethod.class))
{
TargetMethod tm = muxmethod.getAnnotation(TargetMethod.class);
methodname = tm.value();
if(tm.parameters().length>0)
params = tm.parameters();
}
final Method sermethod = servicetype.getMethod(methodname, params);
IIntermediateFuture