001: /*
002: * Created on 20.04.2005 from Linke
003: *
004: */
005: package net.sf.crispy.impl.xmlrpc;
006:
007: import java.util.Hashtable;
008: import java.util.Map;
009:
010: import net.sf.crispy.impl.MiniServer;
011: import net.sf.crispy.util.Util;
012:
013: import org.apache.commons.logging.Log;
014: import org.apache.commons.logging.LogFactory;
015: import org.apache.xmlrpc.WebServer;
016: import org.apache.xmlrpc.XmlRpc;
017:
018: /**
019: * Mini web server for XML-RPC to answer to XML-RPC client requests.
020: *
021: * @author Linke
022: * Mini_WebServer
023: */
024: public class MiniXmlRpcServer extends WebServer implements MiniServer {
025:
026: public static final int DEFAULT_PORT = 9090;
027:
028: protected static final Log log = LogFactory
029: .getLog(MiniXmlRpcServer.class);
030: protected Integer port = Integer.valueOf(Integer
031: .toString(DEFAULT_PORT));
032: private String nullValue = null;
033:
034: public final void setNullValue(String pvNullValue) {
035: nullValue = pvNullValue;
036: }
037:
038: public final String getNullValue() {
039: return nullValue;
040: }
041:
042: private final void init() {
043:
044: try {
045: addDefaultHandlers();
046: } catch (Exception e) {
047: e.printStackTrace();
048: }
049: Map lvMapService = new Hashtable();
050: addHandler(XmlRpcAdminService.class.getName(),
051: new XmlRpcConvertHandler(new XmlRpcAdminServiceImpl(
052: this .xmlrpc, lvMapService, getNullValue()),
053: getNullValue()));
054: }
055:
056: public MiniXmlRpcServer() {
057: super (DEFAULT_PORT);
058: init();
059: }
060:
061: public MiniXmlRpcServer(String pvNullValue) {
062: super (DEFAULT_PORT);
063: setNullValue(pvNullValue);
064: init();
065: }
066:
067: public MiniXmlRpcServer(int pvPort) {
068: super (pvPort);
069: port = Integer.valueOf(Integer.toString(pvPort));
070: init();
071: }
072:
073: public MiniXmlRpcServer(int pvPort, String pvNullValue) {
074: super (pvPort);
075: port = Integer.valueOf(Integer.toString(pvPort));
076: setNullValue(pvNullValue);
077: init();
078: }
079:
080: public Integer getPort() {
081: return port;
082: }
083:
084: public void addService(String pvServiceInterface,
085: String pvServiceObject) {
086: try {
087: Object lvServiceObject = Util.createObject(pvServiceObject);
088: addService(pvServiceInterface, lvServiceObject);
089: } catch (Exception e) {
090: if (log.isDebugEnabled()) {
091: log
092: .debug("Can'nt addService: "
093: + pvServiceInterface
094: + " with implementation: "
095: + pvServiceObject, e);
096: }
097: }
098: }
099:
100: public void addService(String pvServiceInterface,
101: Object pvServiceImpl) {
102: addHandler(pvServiceInterface, new XmlRpcConvertHandler(
103: pvServiceImpl, getNullValue()));
104: }
105:
106: /**
107: * @see org.apache.xmlrpc.WebServer#start()
108: */
109: public void start() {
110: try {
111: boolean isPortFree = Util.isPortFree(port.intValue());
112: if (isPortFree == true) {
113: super .start();
114: }
115: log.debug("MiniXmlRpcServer is started!");
116: } catch (Exception e) {
117: log.warn("Excption by start MiniXmlRpcServer.", e);
118: }
119: }
120:
121: /**
122: * @see org.apache.xmlrpc.WebServer#shutdown()
123: */
124: public void stop() {
125: shutdown();
126: }
127:
128: public static void main(String[] argv) {
129: int p = determinePort(argv, 8080);
130: // XmlRpc.setDebug (true);
131: XmlRpc.setKeepAlive(true);
132: MiniXmlRpcServer webserver = new MiniXmlRpcServer(p);
133:
134: try {
135: webserver.start();
136: } catch (Exception e) {
137: System.err.println("Error running web server");
138: e.printStackTrace();
139: System.exit(1);
140: }
141: }
142:
143: }
|