/* JFox, the OpenSource J2EE Application Server
 *
 * Distributable under GNU LGPL license by gun.org
 * more details please visit http://www.huihoo.org/jfox
 */
package org.jfox.ejb.connector.soap;

import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.xmlrpc.XmlRpcServer;

/**
 * 将调用转发给 JRMPContainerService，完成真正的调用
 *
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public class XMLRPCServlet extends HttpServlet {
    private static XmlRpcServer xmlrpc;

    public void init() throws ServletException {
        xmlrpc = new XmlRpcServer();
        try {
            xmlrpc.addHandler("$default", new JRMPXmlRpcHandler());
        }
        catch(Exception e) {
            throw new ServletException(e);
        }
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        res.setContentType("text/plain");
        OutputStream out = res.getOutputStream();
        out.write("xml rpc proxy servlet\n".getBytes());
        out.close();
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        byte[] result = xmlrpc.execute(req.getInputStream());
        res.setContentType("text/xml");
        res.setContentLength(result.length);
        OutputStream output = res.getOutputStream();
        output.write(result);
        output.flush();
        output.close();
    }

    public static void main(String[] args) {

    }
}