View Javadoc

1   package net.sf.crispy.impl.xmlrpc;
2   
3   import java.io.IOException;
4   import java.io.OutputStream;
5   import java.util.Hashtable;
6   import java.util.List;
7   import java.util.Map;
8   
9   import javax.servlet.ServletConfig;
10  import javax.servlet.ServletException;
11  import javax.servlet.http.HttpServlet;
12  import javax.servlet.http.HttpServletRequest;
13  import javax.servlet.http.HttpServletResponse;
14  
15  import net.sf.crispy.util.Util;
16  
17  import org.apache.commons.logging.Log;
18  import org.apache.commons.logging.LogFactory;
19  import org.apache.xmlrpc.XmlRpcServer;
20  
21  /**
22   * This servlet can you use, to make service for XML-RPC availeable.
23   * Or you can overwrite this servlet. 
24   * 
25   * @author Linke
26   *
27   */
28  public class XmlRpcHttpAdminServlet extends HttpServlet {
29  	
30  	protected static final Log log = LogFactory.getLog (XmlRpcAdminServiceImpl.class);
31  
32  	private static final long serialVersionUID = 5325412481289471271L;
33  	protected XmlRpcAdminServiceImpl adminService = null;
34  
35  	
36  	public void init(ServletConfig config) throws ServletException {
37          Map lvMapService = new Hashtable();
38          adminService = new XmlRpcAdminServiceImpl(new XmlRpcServer(), lvMapService, null);
39          initTest();
40  	}
41  	
42  	private void initTest() {
43  		try {
44  			Object lvEchoImpl = Util.createObject("test.crispy.example.service.EchoImpl");
45  			adminService.addService("test.crispy.example.service.Echo", lvEchoImpl);	
46  		} catch (Exception e) {
47  			if (log.isDebugEnabled()) { log.debug("Can't load Test-Class test.crispy.example.service.Echo."); }
48  		}
49  	}
50  	
51  	
52  	public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
53  		synchronized (adminService) {
54  			XmlRpcServer lvXmlRpcServer = adminService.getXmlRpcServer();
55  	        byte[] result = lvXmlRpcServer.execute(req.getInputStream ());
56  	        res.setContentType("text/xml");
57  	        res.setContentLength(result.length);
58  	        OutputStream output = res.getOutputStream();
59  	        output.write(result);
60  	        output.flush();
61  		}
62  	}
63  
64  	
65  	public String addService(String pvServiceInterface, String pvImplClass) {
66  		return adminService.addService(pvServiceInterface, pvImplClass);
67  	}
68  
69  	public String addService(String pvServiceInterface, Object pvServiceImpl) {
70  		return adminService.addService(pvServiceInterface, pvServiceImpl);
71  	}
72  	
73  	
74  	public String removeService(String pvServiceInterface) {
75  		return adminService.removeService(pvServiceInterface);
76  	}
77  
78  	public Boolean existService(String pvServiceInterface) {
79  		return adminService.existService(pvServiceInterface);
80  	}
81  
82  	public List getAllService() {
83  		return adminService.getAllService();
84  	}
85  
86  	
87  }