2010年5月17日 星期一

XMLRPC-server

///////////////////////////////////////////////////////////////
0. Include the jar file
///////////////////////////////////////////////////////////////
commons-httpclient-3.0.1.jar
ws-commons-java5-1.0.1.jar
xmlrpc-common-3.0.jar
commons-codec-1.3.jar
commons-logging.jar
ws-commons-util-1.0.1.jar
xmlrpc-server-3.0.jar
commons-collections-3.1.jar
commons-pool-1.2.jar
commons-dbcp-1.2.1.jar
servlet-api.jar
xmlrpc-client-3.0.jar



///////////////////////////////////////////////////////////////
1. write the xmlRPC Server
///////////////////////////////////////////////////////////////

XMLRPCTest.java
{{{
import java.io.IOException;

import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.server.PropertyHandlerMapping;
import org.apache.xmlrpc.server.XmlRpcServer;
import org.apache.xmlrpc.server.XmlRpcServerConfigImpl;
import org.apache.xmlrpc.webserver.WebServer;

public class XMLRPCTest {


static final int port = 9999;

public static void initXMLRpcServer() {
WebServer webServer = new WebServer(port);
XmlRpcServer xmlRpcServer = webServer.getXmlRpcServer();
PropertyHandlerMapping phm = new PropertyHandlerMapping();

try{
phm.addHandler("userInfo",HelloHandlerImpl.class);
xmlRpcServer.setHandlerMapping(phm);
XmlRpcServerConfigImpl serverConfig=(XmlRpcServerConfigImpl)xmlRpcServer
.getConfig();
serverConfig.setEnabledForExtensions(true);
serverConfig.setContentLengthOptional(false);
System.out.println("Starting the server....");
webServer.start();
}catch(XmlRpcException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException,XmlRpcException{
initXMLRpcServer();
}

}

}}}



////////////////////////////////////////////////////////////////////
2. Write the remote call handle and Compile the HelloHandler
////////////////////////////////////////////////////////////////////
vim HelloHandlerImpl.java
{{{

import java.util.*;

public class HelloHandlerImpl {

public Object[] sayHello(int x,int y){
Object[] sg=new Object[]{20+x,20+y};
return sg;

}

}

}}}




//////////////////////////////////
3. compiler the Server code
//////////////////////////////////
.
javac -classpath ./lib/xmlrpc-server-3.0.jar:./lib/xmlrpc-client-3.0.jar:./lib/xmlrpc-common-3.0.jar:./lib/commons-httpclient-3.0.1.jar:./lib/commons-codec-1.3.jar:./:./lib/commons-logging.jar:./lib/ws-commons-util-1.0.1.jar:./lib/ws-commons-java5-1.0.1.jar XMLRPCTest.java

.
java -classpath ./lib/xmlrpc-server-3.0.jar:./lib/xmlrpc-client-3.0.jar:./lib/xmlrpc-common-3.0.jar:./lib/commons-httpclient-3.0.1.jar:./lib/commons-codec-1.3.jar:./:./lib/commons-logging.jar:./lib/ws-commons-util-1.0.1.jar:./lib/ws-commons-java5-1.0.1.jar XMLRPCTest

沒有留言:

張貼留言