View Javadoc

1   /*
2    * Created on 20.04.2005 from Linke
3    *
4    */
5   package net.sf.crispy.impl.xmlrpc;
6   
7   import java.util.Hashtable;
8   import java.util.Map;
9   
10  import net.sf.crispy.impl.MiniServer;
11  import net.sf.crispy.util.Util;
12  
13  import org.apache.commons.logging.Log;
14  import org.apache.commons.logging.LogFactory;
15  import org.apache.xmlrpc.WebServer;
16  import org.apache.xmlrpc.XmlRpc;
17  
18  
19  /**
20   * Mini web server for XML-RPC to answer to XML-RPC client requests.
21   *  
22   * @author Linke
23   *  Mini_WebServer
24   */
25  public class MiniXmlRpcServer extends WebServer implements MiniServer {
26  
27  	public static final int DEFAULT_PORT = 9090;
28  	
29  	protected static final Log log = LogFactory.getLog (MiniXmlRpcServer.class);
30  	protected Integer port = Integer.valueOf(Integer.toString(DEFAULT_PORT));
31      private String nullValue = null;
32      
33      public final void setNullValue (String pvNullValue) {
34      	nullValue = pvNullValue;
35      }
36      public final String getNullValue() {
37  		return nullValue;
38  	}
39  	
40  	private final void init() {
41  		
42          try { addDefaultHandlers(); } catch (Exception e) { e.printStackTrace(); }
43          Map lvMapService = new Hashtable();
44          addHandler(XmlRpcAdminService.class.getName(), new XmlRpcConvertHandler(new XmlRpcAdminServiceImpl(this.xmlrpc, lvMapService, getNullValue()), getNullValue()));
45  	}
46  
47  	public MiniXmlRpcServer() {
48  		super(DEFAULT_PORT);
49  		init();
50  	}
51  
52  	public MiniXmlRpcServer(String pvNullValue) {
53  		super(DEFAULT_PORT);
54  		setNullValue(pvNullValue);
55  		init();
56  	}
57  
58  	public MiniXmlRpcServer(int pvPort) {
59  		super(pvPort);
60  		port = Integer.valueOf(Integer.toString(pvPort));
61  		init();
62  	}
63  
64  	public MiniXmlRpcServer(int pvPort, String pvNullValue) {
65  		super(pvPort);
66  		port = Integer.valueOf(Integer.toString(pvPort));
67  		setNullValue(pvNullValue);
68  		init();
69  	}
70  
71  	public Integer getPort() { return port; }
72  	
73  	public void addService (String pvServiceInterface, String pvServiceObject) {
74  		try {
75  			Object lvServiceObject = Util.createObject(pvServiceObject);
76  			addService(pvServiceInterface, lvServiceObject);
77  		} catch (Exception e) {
78  			if (log.isDebugEnabled()) { log.debug("Can'nt addService: " + pvServiceInterface
79  					+ " with implementation: " + pvServiceObject, e); }
80  		}
81  	}
82  
83  	public void addService(String pvServiceInterface, Object pvServiceImpl) {
84  		addHandler(pvServiceInterface, new XmlRpcConvertHandler(pvServiceImpl, getNullValue()));
85  	}
86  	
87  	/**
88  	 * @see org.apache.xmlrpc.WebServer#start()
89  	 */
90  	public void start() {
91  		try {
92  			boolean isPortFree = Util.isPortFree(port.intValue());
93  			if (isPortFree == true) {
94  				super.start();
95  			}		
96  			log.debug("MiniXmlRpcServer is started!");
97  		} catch (Exception e) {
98  			log.warn("Excption by start MiniXmlRpcServer.", e); 
99  		}
100 	}
101 
102 	/**
103 	 * @see org.apache.xmlrpc.WebServer#shutdown()
104 	 */	
105 	public void stop() {
106 		shutdown();
107 	}	
108 	
109     public static void main(String[] argv)
110     {
111         int p = determinePort(argv, 8080);
112         // XmlRpc.setDebug (true);
113         XmlRpc.setKeepAlive(true);
114         MiniXmlRpcServer webserver = new MiniXmlRpcServer(p);
115 
116         try
117         {            
118             webserver.start();
119         }
120         catch (Exception e)
121         {
122             System.err.println("Error running web server");
123             e.printStackTrace();
124             System.exit(1);
125         }
126     }
127 
128 
129 }