/*
* Created on 21.07.2003
*/
package de.dfki.util.xmlrpc.common;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Vector;
import org.apache.xmlrpc.XmlRpcClient;
import org.apache.xmlrpc.XmlRpcException;
import de.dfki.util.xmlrpc.client.MethodCall;
import de.dfki.util.xmlrpc.client.MethodCallParameterException;
/**
* Virtual connection to a XML-RPC server. Actually the connection will be opened when a method is called.
*
* @author lauer
*/
public class XmlRpcConnection
{
/**
* Identifies XML-RPC connections. This class is <b>valid</b> as a key
* class in maps (TreeMap, HashMap) and and can be used in <code>HashSet</code>s.
*/
public static class Id
implements Comparable<Id>
{
public Id( String host, int port )
{
this( host, port, null );
}
public Id( String host, int port, String path )
{
if (host == null)
throw( new IllegalArgumentException( "host must not be <null>" ) );
mHost = host;
mPort = port;
mPath = ( path == null ) ? "RPC2" : path;
}
public String getHost()
{
return mHost;
}
public int getPort()
{
return mPort;
}
public String getPath()
{
return mPath;
}
public String asString()
{
return ( getHost() + ":" + getPort() + "/" + getPath() );
}
/**
* @return Connection id turned into the corresponding URL
*/
public String asStringUrl()
{
return( "http://" + asString() );
}
public int compareTo( Id o )
{
return ( asString().compareTo( o.asString() ) );
}
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + ( ( mHost == null ) ? 0 : mHost.hashCode() );
result = prime * result + ( ( mPath == null ) ? 0 : mPath.hashCode() );
result = prime * result + mPort;
return result;
}
@Override
public boolean equals( Object obj )
{
if( this == obj )
return true;
if( obj == null )
return false;
if( getClass() != obj.getClass() )
return false;
final Id other = (Id)obj;
if( mHost == null )
{
if( other.mHost != null )
return false;
}
else if( !mHost.equals( other.mHost ) )
return false;
if( mPath == null )
{
if( other.mPath != null )
return false;
}
else if( !mPath.equals( other.mPath ) )
return false;
if( mPort != other.mPort )
return false;
return true;
}
final private String mHost;
final private int mPort;
final String mPath;
}
public static XmlRpcConnection connect( Id connId )
{
XmlRpcConnection conn = null;
try
{
URL url = new URL( connId.asStringUrl() );
conn = connect( url );
}
catch( MalformedURLException shouldNotHappen )
{
shouldNotHappen.printStackTrace( System.err );
throw( new RuntimeException( shouldNotHappen ) );
}
return( conn );
}
/**
* Connects to <code>http://[host]:[port]/RPC2</code>
*/
public static XmlRpcConnection connect( String host, int port )
{
return( connect( host, port, null ) );
}
/**
* Connects to <code>http://[host]:[port]/[path]</code>
*/
public static XmlRpcConnection connect( String host, int port, String path )
{
Id id = new Id( host, port, path );
return ( connect( id ) );
}
public static XmlRpcConnection connect( String urlAsString )
throws MalformedURLException
{
return( connect( new URL( urlAsString ) ) );
}
public static XmlRpcConnection connect( URL url )
{
XmlRpcClient connClient = new XmlRpcClient( url );
return ( new XmlRpcConnection( connClient ) );
}
protected XmlRpcConnection( XmlRpcClient conn )
{
mConnection = conn;
}
public Object invoke( MethodCall methodCall )
throws Exception,
IOException,
MethodCallParameterException
{
final String name = methodCall.getName();
final Vector<?> params = methodCall.getParameters();
//first check if call is ok
methodCall.validateCall();
Object result = mConnection.execute( name, params );
return ( result );
}
private final XmlRpcClient mConnection;
}