ws-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jimisola Laursen <li...@jimisola.com>
Subject Re: An example using HandlerMapping
Date Tue, 18 Jul 2006 21:54:51 GMT

"Read the source Luke"...

Jochen, great work! Truly. Once I read the code and figured it out it worked
on the first run :)

One question though, reading the source there doesn't seem to be a default
handler as there is with the 2.0.x versions. Is this correct or have I
missed out on something?

I've attached some code that I think would be very useful to other as
"Getting started" examples.

Regards,
Jimisola

(the code below uses Calculator, Adder and AdderImpl which are available on
the site for Apache XML-RPC)

Server.java:

package org.apache.xmlrpc.demo.webserver;

import java.net.InetAddress;

import org.apache.xmlrpc.common.TypeConverterFactoryImpl;
import org.apache.xmlrpc.demo.Calculator;
import org.apache.xmlrpc.demo.proxy.Adder;
import org.apache.xmlrpc.demo.webserver.proxy.impls.AdderImpl;
import org.apache.xmlrpc.server.DynamicHandlerMapping;
import org.apache.xmlrpc.server.XmlRpcServer;
import org.apache.xmlrpc.server.XmlRpcServerConfigImpl;
import org.apache.xmlrpc.webserver.WebServer;

public class Server
{
    public static void main(String[] args) throws Exception
    {
        WebServer webServer = new WebServer(8080,
InetAddress.getByName("127.0.0.1"));
        
        XmlRpcServer xmlRpcServer = webServer.getXmlRpcServer();
        
        // use reflection for (dynamic) mapping
        DynamicHandlerMapping dhm = new DynamicHandlerMapping(new
TypeConverterFactoryImpl(), true);
    
        // add "Calculator" handler - used by regular agent
        dhm.addHandler("Calculator", Calculator.class);
        // add Adder handler - using full name for use by dynamic proxy
        dhm.addHandler(Adder.class.getName(), AdderImpl.class);
        xmlRpcServer.setHandlerMapping(dhm);
        
        XmlRpcServerConfigImpl serverConfig = (XmlRpcServerConfigImpl)
xmlRpcServer.getConfig();
        serverConfig.setEnabledForExtensions(true);
        serverConfig.setContentLengthOptional(false);

        webServer.start();
    }
}


Client.java:

package org.apache.xmlrpc.demo.client;

import java.net.MalformedURLException;
import java.net.URL;

import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
import org.apache.xmlrpc.client.XmlRpcCommonsTransportFactory;
import org.apache.xmlrpc.client.util.ClientFactory;
import org.apache.xmlrpc.demo.proxy.Adder;

public class Client
{
    public static void main(String[] args) throws XmlRpcException,
MalformedURLException
    {
        // create configuration
        XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
        config.setServerURL(new URL("http://127.0.0.1:8080/xmlrpc"));
        config.setEnabledForExtensions(true);  
        config.setConnectionTimeout(60 * 1000);
        config.setReplyTimeout(60 * 1000);

        XmlRpcClient client = new XmlRpcClient();
        
        // use Commons HttpClient as transport
        client.setTransportFactory(new
XmlRpcCommonsTransportFactory(client));
        // set configuration
        client.setConfig(config);

        // make the a regular call
        Object[] params = new Object[]
        { new Integer(2), new Integer(3) };
        Integer result = (Integer) client.execute("Calculator.add", params);
        System.out.println("2 + 3 = " + result);
        
        // make a call using dynamic proxy
        ClientFactory factory = new ClientFactory(client);
        Adder adder = (Adder) factory.newInstance(Adder.class);
        int sum = adder.add(2, 4);
        System.out.println("2 + 4 = " + sum);
    }
}
-- 
View this message in context: http://www.nabble.com/An-example-using-tf1963053.html#a5387175
Sent from the Apache Xml-RPC - User forum at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: xmlrpc-user-unsubscribe@ws.apache.org
For additional commands, e-mail: xmlrpc-user-help@ws.apache.org


Mime
View raw message