package de.dfki.util.xmlrpc.client;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import de.dfki.util.xmlrpc.XmlRpc;
import de.dfki.util.xmlrpc.common.MethodSignature;
/**
* Handles calls to the {@link Proxy} generated by calling {@link XmlRpc#createClient(Class, de.dfki.util.xmlrpc.common.XmlRpcConnection)}.
* This proxy implements the {@link XmlRpcClient} interface as well as the api it is supposed to redirect.
*
* @author lauer
*/
public class ClientInvocationHandler implements InvocationHandler
{
public ClientInvocationHandler( XmlRpcClient client )
{
mClient = client;
}
public Object invoke( Object proxy, Method method, Object[] args )
throws Throwable
{
//forward XmlRpcClient method calls to getClient()
final Class<?> cls = method.getDeclaringClass();
if (XmlRpcClient.class.isAssignableFrom( cls ))
{
return( method.invoke( getClient(), args ) );
}
//we have a call on the api class: convert it into a XML-RPC call
MethodSignature sig = MethodSignature.createFromMethod( method );
MethodCall call = getClient().prepareCall( sig );
if (args == null)
{
args = new Object[0];
}
call.addParameters( args );
Object result = getClient().invoke( call );
return( result );
}
protected XmlRpcClient getClient()
{
return( mClient );
}
final XmlRpcClient mClient;
}