rpc

package server;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.xmlrpc.server.PropertyHandlerMapping;
import org.apache.xmlrpc.server.XmlRpcServerConfigImpl;
import org.apache.xmlrpc.webserver.XmlRpcServletServer;

@SuppressWarnings("serial")
public class Server extends HttpServlet
{
    private XmlRpcServletServer server;

    @SuppressWarnings("rawtypes")
    public void init(ServletConfig config) throws ServletException
    {
        super.init(config);
        try
        {
            server = new XmlRpcServletServer();
            XmlRpcServerConfigImpl serverConfig = (XmlRpcServerConfigImpl) server.getConfig();
            serverConfig.setEnabledForExtensions(true);
            serverConfig.setEnabledForExceptions(true);
            serverConfig.setContentLengthOptional(false);
            PropertyHandlerMapping phm = new PropertyHandlerMapping();
            String servicedir = config.getInitParameter("servicedir");
            servicedir = (servicedir != null && servicedir.length() > 0) ? servicedir : "service";
            List<String> services = getServices(servicedir);
            for (String service : services)
            {
                Class cls = Class.forName(service);
                phm.addHandler(service, cls);
            }
            server.setHandlerMapping(phm);
        }
        catch (Exception e)
        {
            log("Failed to create Server: " + e.getMessage(), e);
            e.printStackTrace();
        }

    }

    public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
        String ip = request.getRemoteAddr();
        System.out.println(ip);
        server.execute(request, response);
    }

    private List<String> getServices(String packageName)
    {
        URL url = getClass().getClassLoader().getResource(packageName.replace(".", "\\"));
        String filePath = url.getFile();
        List<String> fileNames = getClassName(filePath, null);
        return fileNames;
    }

    private List<String> getClassName(String filePath, List<String> className)
    {
        List<String> myClassName = new ArrayList<String>();
        File file = new File(filePath);
        File[] childFiles = file.listFiles();
        for (File childFile : childFiles)
        {
            if (childFile.isDirectory())
            {
                myClassName.addAll(getClassName(childFile.getPath(), myClassName));
            }
            else
            {
                String childFilePath = childFile.getPath();
                childFilePath = childFilePath.substring(childFilePath.indexOf("\\classes") + 9, childFilePath
                        .lastIndexOf("."));
                childFilePath = childFilePath.replace("\\", ".");
                myClassName.add(childFilePath);
            }
        }

        return myClassName;
    }

}
 

你可能感兴趣的:(rpc)