Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
jadex.platform.service.remote.commands.RemoteMethodInvocationCommand Maven / Gradle / Ivy
Go to download
The Jadex platform package contains implementations of platform services as well as the platform component itself.
package jadex.platform.service.remote.commands;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import jadex.bridge.IComponentIdentifier;
import jadex.bridge.IExternalAccess;
import jadex.bridge.IInternalAccess;
import jadex.bridge.ServiceCall;
import jadex.bridge.component.IExecutionFeature;
import jadex.bridge.component.impl.IInternalExecutionFeature;
import jadex.bridge.service.IServiceIdentifier;
import jadex.bridge.service.annotation.Security;
import jadex.bridge.service.component.interceptors.CallAccess;
import jadex.bridge.service.search.SServiceProvider;
import jadex.commons.SReflect;
import jadex.commons.future.DelegationResultListener;
import jadex.commons.future.ExceptionDelegationResultListener;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;
import jadex.commons.future.IFutureCommandResultListener;
import jadex.commons.future.IIntermediateFuture;
import jadex.commons.future.IIntermediateFutureCommandResultListener;
import jadex.commons.future.ITerminableFuture;
import jadex.commons.future.IntermediateFuture;
import jadex.commons.transformation.annotations.Alias;
import jadex.platform.service.remote.IRemoteCommand;
import jadex.platform.service.remote.RemoteReference;
import jadex.platform.service.remote.RemoteReferenceModule;
import jadex.platform.service.remote.RemoteServiceManagementService;
@Alias ("jadex.base.service.remote.commands.RemoteMethodInvocationCommand" )
public class RemoteMethodInvocationCommand extends AbstractRemoteCommand
{
protected RemoteReference rr;
protected Method method;
protected String methodname;
protected Class[] parametertypes;
protected Object[] parametervalues;
protected boolean returnisref;
protected String callid;
protected String securitylevel;
protected Object target;
protected IComponentIdentifier caller;
public RemoteMethodInvocationCommand ()
{
}
public RemoteMethodInvocationCommand (RemoteReference rr, Method method,
Object[] parametervalues, String callid, IComponentIdentifier caller, Map nonfunc)
{
super (nonfunc);
this .rr = rr;
this .method = method;
this .methodname = method.getName();
this .parametertypes = method.getParameterTypes();
this .parametervalues = parametervalues!=null ? parametervalues.clone(): null ;
this .callid = callid;
this .caller = caller;
}
public String getSecurityLevel ()
{
return securitylevel;
}
public IFuture preprocessCommand (IInternalAccess component, final RemoteReferenceModule rrm, final IComponentIdentifier target)
{
Future ret = new Future();
super .preprocessCommand(component, rrm, target)
.addResultListener(new DelegationResultListener(ret)
{
public void customResultAvailable (Void result)
{
{
final Future ret = new Future();
rrm.getTargetObject(getRemoteReference())
.addResultListener(component.getComponentFeature(IExecutionFeature.class).createResultListener(new ExceptionDelegationResultListener(ret)
{
public void customResultAvailable (Object result)
{
try
{
RemoteMethodInvocationCommand.this .target = result;
method = result.getClass().getMethod(methodname, parametertypes);
Security sec = null ;
List> classes = new ArrayList>();
classes.add(result.getClass());
for (int i=0 ; sec==null && i clazz = classes.get(i);
try
{
Method m = clazz.getMethod(methodname, parametertypes);
sec = m.getAnnotation(Security.class);
}
catch (Exception e)
{
}
if (sec==null )
{
sec = clazz.getAnnotation(Security.class);
if (sec==null )
{
classes.addAll(Arrays.asList((Class[])clazz.getInterfaces()));
if (clazz.getSuperclass()!=null )
{
classes.add(clazz.getSuperclass());
}
}
}
}
securitylevel = sec!=null ? sec.value() : Security.PASSWORD;
ret.setResult(null );
}
catch (Exception e)
{
super .exceptionOccurred(e);
}
}
}));
return ret;
}
public IIntermediateFuture execute (IExternalAccess component, RemoteServiceManagementService rsms)
{
final IntermediateFuture ret = new IntermediateFuture();
IInternalAccess ada = IInternalExecutionFeature.LOCAL.get();
IComponentIdentifier.LOCAL.set(caller);
IInternalExecutionFeature.LOCAL.set(null );
Map props = getNonFunctionalProperties();
ServiceCall.getOrCreateNextInvocation(props);
invokeMethod(ret, rsms);
CallAccess.resetNextInvocation();
IComponentIdentifier.LOCAL.set(component.getComponentIdentifier());
IInternalExecutionFeature.LOCAL.set(ada);
return ret;
}
public void invokeMethod (final IntermediateFuture ret, final RemoteServiceManagementService rsms)
{
final IComponentIdentifier ridcom = getRealReceiver();
try
{
final boolean terminable = SReflect.isSupertype(ITerminableFuture.class, method.getReturnType());
if (target.getClass().isAnonymousClass())
method.setAccessible(true );
final Object res = method.invoke(target, parametervalues);
handleResultFuture(terminable, rsms, callid, res, terminable, methodname, ridcom, ret);
}
catch (Exception exception)
{
if (exception instanceof InvocationTargetException
&& ((InvocationTargetException)exception).getTargetException() instanceof Exception)
{
exception = (Exception)((InvocationTargetException)exception).getTargetException();
}
ret.addIntermediateResult(new RemoteResultCommand(ridcom, null , exception, callid,
false , methodname, getNonFunctionalProperties()));
ret.setFinished();
rsms.removeProcessingCall(callid);
}
}
public String getMethodName ()
{
return methodname;
}
public void setMethodName (String methodname)
{
this .methodname = methodname;
}
public Class[] getParameterTypes()
{
return parametertypes;
}
public void setParameterTypes (Class[] parametertypes)
{
this .parametertypes = parametertypes.clone();
}
public Object[] getParameterValues()
{
return parametervalues;
}
public void setParameterValues (Object[] parametervalues)
{
this .parametervalues = parametervalues.clone();
}
public String getCallId ()
{
return callid;
}
public void setCallId (String callid)
{
this .callid = callid;
}
public IComponentIdentifier getCaller ()
{
return caller;
}
public void setCaller (IComponentIdentifier caller)
{
this .caller = caller;
}
public RemoteReference getRemoteReference ()
{
return rr;
}
public void setRemoteReference (RemoteReference rr)
{
this .rr = rr;
}
public boolean isReturnValueReference ()
{
return returnisref;
}
public void setReturnValueReference (boolean returnisref)
{
this .returnisref = returnisref;
}
public IComponentIdentifier getSender ()
{
return caller;
}
public IComponentIdentifier getRealReceiver ()
{
IComponentIdentifier ret = null ;
Object ti = getRemoteReference().getTargetIdentifier();
if (ti instanceof IComponentIdentifier)
ret = (IComponentIdentifier)ti;
else if (ti instanceof IServiceIdentifier)
ret = ((IServiceIdentifier)ti).getProviderId();
return ret;
}
public String toString ()
{
return "RemoteMethodInvocationCommand(remote reference=" + rr + ", methodname="
+ methodname + ", callid=" + callid + ")" ;
}
public void handleResultFuture (boolean terminable, final RemoteServiceManagementService rsms, final String callid, final Object res,
final boolean returnisref, final String methodname, final IComponentIdentifier rec, final IntermediateFuture ret)
{
if (terminable)
{
rsms.putProcessingCall(callid, res);
List cmds = rsms.removeFutureCommands(callid);
if (cmds!=null )
{
for (Runnable cmd: cmds)
{
cmd.run();
}
}
}
if (res instanceof IIntermediateFuture)
{
((IIntermediateFuture)res).addResultListener(new IIntermediateFutureCommandResultListener()
{
int cnt = 0 ;
public void intermediateResultAvailable (Object result)
{
ret.addIntermediateResult(new RemoteIntermediateResultCommand(rec, result, callid,
returnisref, methodname, false , getNFProps(true ), (IFuture)res, cnt++));
}
public void finished ()
{
ret.addIntermediateResult(new RemoteIntermediateResultCommand(rec, null , callid,
returnisref, methodname, true , getNFProps(false ), (IFuture)res, cnt));
ret.setFinished();
rsms.removeProcessingCall(callid);
}
public void resultAvailable (Object result)
{
ret.addIntermediateResult(new RemoteResultCommand(rec, result, null , callid,
returnisref, methodname, getNFProps(false )));
ret.setFinished();
rsms.removeProcessingCall(callid);
}
public void resultAvailable (Collection result)
{
ret.addIntermediateResult(new RemoteResultCommand(rec, result, null , callid,
returnisref, methodname, getNFProps(false )));
ret.setFinished();
rsms.removeProcessingCall(callid);
}
public void exceptionOccurred (Exception exception)
{
ret.addIntermediateResult(new RemoteResultCommand(rec, null , exception, callid,
false , methodname, getNFProps(false )));
ret.setFinished();
rsms.removeProcessingCall(callid);
}
public void commandAvailable (Object command)
{
ret.addIntermediateResult(new RemoteFutureSourceCommand(rec, command, callid,
returnisref, methodname, getNFProps(true )));
}
});
}
else if (res instanceof IFuture)
{
((IFuture)res).addResultListener(new IFutureCommandResultListener()
{
public void resultAvailable (Object result)
{
ret.addIntermediateResult(new RemoteResultCommand(rec, result, null , callid,
returnisref, methodname, getNFProps(false )));
ret.setFinished();
rsms.removeProcessingCall(callid);
}
public void exceptionOccurred (Exception exception)
{
ret.addIntermediateResult(new RemoteResultCommand(rec, null , exception, callid,
false , methodname, getNFProps(false )));
ret.setFinished();
rsms.removeProcessingCall(callid);
}
public void commandAvailable (Object command)
{
ret.addIntermediateResult(new RemoteFutureSourceCommand(rec, command, callid,
returnisref, methodname, getNFProps(true )));
}
});
}
else
{
ret.addIntermediateResult(new RemoteResultCommand(rec, res, null , callid,
returnisref, methodname, getNFProps(false )));
ret.setFinished();
rsms.removeProcessingCall(callid);
}
}
protected Map getNFProps (boolean intermediate)
{
Map ret = nonfunc;
ServiceCall sc = intermediate? ServiceCall.getCurrentInvocation(): ServiceCall.getLastInvocation();
if (sc!=null )
ret = sc.getProperties();
return ret;
}
}