UsingSensors: SimpleXmlRpcServer.java

File SimpleXmlRpcServer.java, 4.2 KB (added by yanyan, 7 years ago)
Line 
1package seattle.sensortest;
2
3import java.util.HashMap;
4import java.util.Map;
5import org.apache.xmlrpc.XmlRpcException;
6import org.apache.xmlrpc.XmlRpcRequest;
7import org.apache.xmlrpc.server.PropertyHandlerMapping;
8import org.apache.xmlrpc.server.RequestProcessorFactoryFactory;
9import org.apache.xmlrpc.server.XmlRpcServer;
10import org.apache.xmlrpc.server.XmlRpcServerConfigImpl;
11import org.apache.xmlrpc.server.RequestProcessorFactoryFactory.RequestProcessorFactory;
12import org.apache.xmlrpc.webserver.WebServer;
13 
14/** A simple implementation of XML-RPC server for apache ws-xmlrpc
15 * Version 1.2 - support single instance handler object, but not yet multi-threaded  */
16public class SimpleXmlRpcServer
17{
18        /* ======================================== runtime attr ========================================== */
19        private int port;
20        private WebServer webServer = null;
21        private PropertyHandlerMapping phm = null;
22        private SimpleXmlRpcRequestHandlerFactory handler = null;
23        private XmlRpcServer xmlRpcServer = null;
24 
25        /* ======================================== init/destroy ========================================== */ 
26        /** Creates an instance of xml rpc server, using :handler as the class that  will handle all request.
27         * Note that a new instance of :handler will be created at every xml-rpc request.
28         * @param name
29         * @param port
30         * @param handler
31         * @throws Exception */
32        public SimpleXmlRpcServer(int port) throws Exception
33        {
34                this.port = port;
35                this.webServer = new WebServer(this.port);      // bind
36                this.xmlRpcServer = this.webServer.getXmlRpcServer();
37                this.handler = new SimpleXmlRpcRequestHandlerFactory(); 
38                this.phm = new PropertyHandlerMapping();
39                this.phm.setRequestProcessorFactoryFactory(this.handler);
40        }
41 
42        /* ======================================== services ========================================== */ 
43        /** Adds a handler instance (which CAN be stateful) for each request. Note that
44         * every public method in this object will be callable via xml-rpc client
45         * @param name - handler name
46         * @param requestHandler - handler obj instance
47         * @throws Exception */
48        public void addHandler(String name, Object requestHandler) throws Exception
49        {
50                this.handler.setHandler(name, requestHandler);
51                this.phm.addHandler(name, requestHandler.getClass());
52        }
53 
54        /** Start the xml-rpc server forever. In the rare case of fatal exception, the web server will be
55         * restarted automatically. This is a blocking call (not thread-based). */
56        public void serve_forever() throws Exception
57        {
58                this.xmlRpcServer.setHandlerMapping(phm);
59                XmlRpcServerConfigImpl serverConfig = (XmlRpcServerConfigImpl) xmlRpcServer.getConfig();
60                serverConfig.setEnabledForExtensions(true);
61                this.webServer.start();
62        }
63 
64        /* ====================================== getter/setter ======================================== */ 
65        public int getPort() { return port; }
66        public void setPort(int port) { this.port = port; }
67        public WebServer getWebServer() { return webServer; }
68        public void setWebServer(WebServer webServer) { this.webServer = webServer; }
69        public PropertyHandlerMapping getPhm() { return phm; }
70        public void setPhm(PropertyHandlerMapping phm) { this.phm = phm; }
71        public SimpleXmlRpcRequestHandlerFactory getHandler() { return handler; }
72        public void setHandler(SimpleXmlRpcRequestHandlerFactory handler) { this.handler = handler; }
73        public XmlRpcServer getXmlRpcServer() { return xmlRpcServer; }
74        public void setXmlRpcServer(XmlRpcServer xmlRpcServer) { this.xmlRpcServer = xmlRpcServer; } 
75 
76}
77
78class SimpleXmlRpcRequestHandlerFactory implements RequestProcessorFactoryFactory, RequestProcessorFactory
79{
80        private Map handlerMap = new HashMap();
81       
82        public void setHandler(String name, Object handler) { this.handlerMap.put(name, handler); }
83        public Object getHandler(String name) { return this.handlerMap.get(name); }
84 
85        public RequestProcessorFactory getRequestProcessorFactory(Class arg0) throws XmlRpcException { return (RequestProcessorFactory) this; }
86       
87        public Object getRequestProcessor(XmlRpcRequest request) throws XmlRpcException
88        {
89                String handlerName = request.getMethodName().substring(0,request.getMethodName().lastIndexOf("."));
90                if( !handlerMap.containsKey(handlerName)) throw new XmlRpcException("Unknown handler: "+handlerName);
91                return handlerMap.get(handlerName);
92        }
93}