/*
* Created on 21.07.2003
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package de.dfki.util.xmlrpc.client;
import java.util.Collection;
import java.util.Date;
import java.util.Map;
import java.util.Vector;
import de.dfki.util.xmlrpc.XmlRpc;
import de.dfki.util.xmlrpc.common.ApiParameter;
import de.dfki.util.xmlrpc.common.MethodSignature;
import de.dfki.util.xmlrpc.conversion.TypeConversionException;
/**
* Class to control parameter population for a XML-RPC call. Does type conversions of parameter and
* return values. Checks validity of supplied parameters on basis of the given {@link MethodSignature}.
*
* @author lauer
*/
public class MethodCall
{
MethodCall( final MethodSignature sig )
{
mMethodSignature = sig;
resetParameterCount();
mParameters = new Vector<Object>( sig.getParameterCount() );
}
public MethodSignature getMethodSignature()
{
return ( mMethodSignature );
}
@Deprecated
public void addParameter( String p )
throws MethodCallParameterException
{
add( p );
}
@Deprecated
public void addParameter( Boolean p )
throws MethodCallParameterException
{
add( p );
}
@Deprecated
public void addParameter( boolean p )
throws MethodCallParameterException
{
add( p );
}
@Deprecated
public void addParameter( int p )
throws MethodCallParameterException
{
add( p );
}
@Deprecated
public void addParameter( Integer p )
throws MethodCallParameterException
{
add( p );
}
@Deprecated
public void addParameter( double p )
throws MethodCallParameterException
{
add( p );
}
@Deprecated
public void addParameter( Double p )
throws MethodCallParameterException
{
add( p );
}
@Deprecated
public void addParameter( Date p )
throws MethodCallParameterException
{
add( p );
}
@Deprecated
public <T,K> void addParameter( Map<T,K> p )
throws MethodCallParameterException
{
add( p );
}
@Deprecated
public <T> void addParameter( Collection<T> p )
throws MethodCallParameterException
{
add( p );
}
@Deprecated
public void addParameter( byte[] p )
throws MethodCallParameterException
{
add( p );
}
public void add( Object param )
throws MethodCallParameterException
{
// if (param == null)
// {
// throw( new MethodCallParameterException( "Parameter must not be null" ) );
// }
try
{
ApiParameter parameter = getMethodSignature().getParameterAt( getParameterCount() );
Object toAdd = XmlRpc.getTypeConverter().convertToXmlRpcRepresentation( parameter, param );
getParameters().add( getParameterCount(), toAdd );
incParameterCount();
}
catch( TypeConversionException e )
{
throw( new MethodCallParameterException( "Error for parameter '" + param + "'", e ) );
}
}
/**
* Add a bunch of parameters.
*/
public void addParameters( Object ... param )
throws MethodCallParameterException
{
for( Object arg: param )
{
add( arg );
}
}
// public XmlRpc.Type getTypeForCurrentParameter()
// throws MethodCallParameterException
// {
// XmlRpc.Type type = null;
// try
// {
// type = getMethodSignature().getParameterAt( getParameterCount() ).getXmlRpcType();
// }
// catch( ArrayIndexOutOfBoundsException e )
// {
// throw ( new MethodCallParameterException( "Too many paramters! Only "
// + getParameterCount() + " parameter(s) allowed for method '"
// + getMethodSignature().getName() + "'" ) );
// }
// return ( type );
// }
private void resetParameterCount()
{
mParameterCount = 0;
}
private void incParameterCount()
{
mParameterCount++;
}
public int getParameterCount()
{
return ( mParameterCount );
}
public Vector<Object> getParameters()
{
return ( mParameters );
}
public String getName()
{
return ( getMethodSignature().getName() );
}
/**
* Checks, if the current call (with all added parameters) is valid.
* @throws MethodCallParameterException Thrown, iff call is not valid for some reason.
*/
public void validateCall()
throws MethodCallParameterException
{
if( getParameterCount() != getMethodSignature().getParameterCount() )
{
throw ( new MethodCallParameterException( "Wrong number of arguments ("
+ getParameterCount() + ") needed: "
+ getMethodSignature().getParameterCount() ) );
}
}
public Object convertReturnValueToUserRepresentation( Object xmlReturnValue )
throws TypeConversionException
{
final ApiParameter returnParam = getMethodSignature().getReturnParameter();
Object result = XmlRpc.getTypeConverter().convertToUserRepresentation( returnParam, xmlReturnValue );
return( result );
}
final private MethodSignature mMethodSignature;
private int mParameterCount;
private Vector<Object> mParameters;
}