cn.leancloud.diamond.sdkapi.impl.DiamondSDKManagerImpl.java Source code

Java tutorial

Introduction

Here is the source code for cn.leancloud.diamond.sdkapi.impl.DiamondSDKManagerImpl.java

Source

/*
 * (C) 2007-2012 Alibaba Group Holding Limited.
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 * Authors:
 *   leiwen <chrisredfield1985@126.com> , boyan <killme2008@gmail.com>
 */
package cn.leancloud.diamond.sdkapi.impl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;

import cn.leancloud.diamond.common.Constants;
import cn.leancloud.diamond.domain.DiamondSDKConf;
import cn.leancloud.diamond.domain.Page;
import cn.leancloud.diamond.domain.PageContextResult;
import cn.leancloud.diamond.util.PatternUtils;
import cn.leancloud.diamond.utils.JSONUtils;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.jackson.type.TypeReference;

import cn.leancloud.diamond.domain.BatchContextResult;
import cn.leancloud.diamond.domain.ConfigInfo;
import cn.leancloud.diamond.domain.ConfigInfoEx;
import cn.leancloud.diamond.domain.ContextResult;
import cn.leancloud.diamond.domain.DiamondConf;
import cn.leancloud.diamond.sdkapi.DiamondSDKManager;
import cn.leancloud.diamond.util.RandomDiamondUtils;

/**
 * SDK??
 * 
 * @filename DiamondSDKManagerImpl.java
 * @author libinbin.pt
 * @datetime 2010-7-16 ?04:00:19
 */
public class DiamondSDKManagerImpl implements DiamondSDKManager {

    private static final Log log = LogFactory.getLog("diamondSdkLog");

    // DiamondSDKConf?map
    private Map<String, DiamondSDKConf> diamondSDKConfMaps;

    // 
    private final int connection_timeout;
    // 
    private final int require_timeout;

    // ?
    public DiamondSDKManagerImpl(int connection_timeout, int require_timeout) throws IllegalArgumentException {
        if (connection_timeout < 0)
            throw new IllegalArgumentException("0[??()]!");
        if (require_timeout < 0)
            throw new IllegalArgumentException("0[??()]!");
        this.connection_timeout = connection_timeout;
        this.require_timeout = require_timeout;
        int maxHostConnections = 50;
        MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();

        connectionManager.getParams().setDefaultMaxConnectionsPerHost(maxHostConnections);
        connectionManager.getParams().setStaleCheckingEnabled(true);
        this.client = new HttpClient(connectionManager);
        // 
        client.getHttpConnectionManager().getParams().setConnectionTimeout(this.connection_timeout);
        // 1
        client.getHttpConnectionManager().getParams().setSoTimeout(60 * 1000);
        client.getParams().setContentCharset("UTF-8");
        log.info(": " + this.connection_timeout + "");
    }

    /**
     * diamond???
     * 
     * @param dataId
     * @param groupName
     * @param context
     * @param serverId
     * @return ContextResult ?
     */
    public synchronized ContextResult pulish(String dataId, String groupName, String context, String serverId) {
        ContextResult response = null;
        // dataId,groupName,context,serverId?
        if (validate(dataId, groupName, context)) {
            response = this.processPulishByDefinedServerId(dataId, groupName, context, serverId);
            return response;
        }

        // ?
        response = new ContextResult();
        response.setSuccess(false);
        response.setStatusMsg("?dataId,group,content?");
        return response;
    }

    /**
     * diamond????
     * 
     * @param dataId
     * @param groupName
     * @param context
     * @param serverId
     * @return ContextResult ?
     */
    public synchronized ContextResult pulishAfterModified(String dataId, String groupName, String context,
            String serverId) {

        ContextResult response = null;
        // dataId,groupName,context,serverId?
        if (validate(dataId, groupName, context)) {
            // ?diamondserver??
            response = this.processPulishAfterModifiedByDefinedServerId(dataId, groupName, context, serverId);
            return response;
        } else {
            response = new ContextResult();
            // ?
            response.setSuccess(false);
            response.setStatusMsg("?dataId,group,content?");
            return response;
        }

    }

    // --------------------------------------------------------//
    /**
     * diamond??
     * 
     * @param dataIdPattern
     * @param groupNamePattern
     * @param serverId
     * @param currentPage
     * @param sizeOfPerPage
     * @return PageContextResult<ConfigInfo> ?
     * @throws SQLException
     */
    public synchronized PageContextResult<ConfigInfo> queryBy(String dataIdPattern, String groupNamePattern,
            String serverId, long currentPage, long sizeOfPerPage) {
        return processQuery(dataIdPattern, groupNamePattern, null, serverId, currentPage, sizeOfPerPage);
    }

    /**
     * ? dataId,??content?diamond?? ???'*',?'%'[
     * like ]? ????'*'?" ",[ = ]?
     * 
     * @param dataIdPattern
     * @param groupNamePattern
     * @param contentPattern
     * @param serverId
     * @param currentPage
     * @param sizeOfPerPage
     * @return PageContextResult<ConfigInfo> ?
     * @throws SQLException
     */

    public synchronized PageContextResult<ConfigInfo> queryBy(String dataIdPattern, String groupNamePattern,
            String contentPattern, String serverId, long currentPage, long sizeOfPerPage) {
        return processQuery(dataIdPattern, groupNamePattern, contentPattern, serverId, currentPage, sizeOfPerPage);
    }

    // ===================== ==================================

    /**
     * diamonddataId,groupName??
     * 
     * @param dataId
     * @param groupName
     * @param serverId
     * @return ContextResult ?
     * @throws SQLException
     */
    public synchronized ContextResult queryByDataIdAndGroupName(String dataId, String groupName, String serverId) {
        ContextResult result = new ContextResult();
        PageContextResult<ConfigInfo> pageContextResult = processQuery(dataId, groupName, null, serverId, 1, 1);
        result.setStatusMsg(pageContextResult.getStatusMsg());
        result.setSuccess(pageContextResult.isSuccess());
        result.setStatusCode(pageContextResult.getStatusCode());
        if (pageContextResult.isSuccess()) {
            List<ConfigInfo> list = pageContextResult.getDiamondData();
            if (list != null && !list.isEmpty()) {
                ConfigInfo info = list.iterator().next();
                result.setConfigInfo(info);
                result.setReceiveResult(info.getContent());
                result.setStatusCode(pageContextResult.getStatusCode());

            }
        }
        return result;
    }

    // ========================?==================================

    // /////////////////////////?////////////////////////////////////////

    private final HttpClient client;

    // =========================== ? ===============================

    private ContextResult processPulishByDefinedServerId(String dataId, String groupName, String context,
            String serverId) {
        ContextResult response = new ContextResult();
        // 
        if (!login(serverId)) {
            response.setSuccess(false);
            response.setStatusMsg(",??serverId?");
            return response;
        }
        if (log.isDebugEnabled())
            log.debug("processPulishByDefinedServerId(" + dataId + "," + groupName + "," + context + ","
                    + serverId + ")?");

        String postUrl = "/diamond-server/admin.do?method=postConfig";
        PostMethod post = new PostMethod(postUrl);
        // 
        post.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, require_timeout);
        try {
            NameValuePair dataId_value = new NameValuePair("dataId", dataId);
            NameValuePair group_value = new NameValuePair("group", groupName);
            NameValuePair content_value = new NameValuePair("content", context);

            // ?
            post.setRequestBody(new NameValuePair[] { dataId_value, group_value, content_value });
            // ?
            ConfigInfo configInfo = new ConfigInfo();
            configInfo.setDataId(dataId);
            configInfo.setGroup(groupName);
            configInfo.setContent(context);
            if (log.isDebugEnabled())
                log.debug("?ConfigInfo: " + configInfo);
            // ??
            response.setConfigInfo(configInfo);
            // http??
            int status = client.executeMethod(post);
            response.setReceiveResult(post.getResponseBodyAsString());
            response.setStatusCode(status);
            log.info("??" + status + ",?" + post.getResponseBodyAsString());
            if (status == HttpStatus.SC_OK) {
                response.setSuccess(true);
                response.setStatusMsg("???");
                log.info("???, dataId=" + dataId + ",group=" + groupName + ",content=" + context
                        + ",serverId=" + serverId);
            } else if (status == HttpStatus.SC_REQUEST_TIMEOUT) {
                response.setSuccess(false);
                response.setStatusMsg("??, :" + require_timeout + "");
                log.error("??:" + require_timeout + ", dataId="
                        + dataId + ",group=" + groupName + ",content=" + context + ",serverId=" + serverId);
            } else {
                response.setSuccess(false);
                response.setStatusMsg("??, ??:" + status);
                log.error("??:" + response.getReceiveResult() + ",dataId=" + dataId + ",group="
                        + groupName + ",content=" + context + ",serverId=" + serverId);
            }
        } catch (HttpException e) {
            response.setStatusMsg("???HttpException" + e.getMessage());
            log.error("???HttpException: dataId=" + dataId + ",group=" + groupName + ",content="
                    + context + ",serverId=" + serverId, e);
        } catch (IOException e) {
            response.setStatusMsg("???IOException" + e.getMessage());
            log.error("???IOException: dataId=" + dataId + ",group=" + groupName + ",content="
                    + context + ",serverId=" + serverId, e);
        } finally {
            // ?
            post.releaseConnection();
        }

        return response;
    }

    // =========================== ?? ===============================

    // ===========================  ===============================

    private ContextResult processPulishAfterModifiedByDefinedServerId(String dataId, String groupName,
            String context, String serverId) {
        ContextResult response = new ContextResult();
        // 
        if (!login(serverId)) {
            response.setSuccess(false);
            response.setStatusMsg(",??serverId");
            return response;
        }
        if (log.isDebugEnabled())
            log.debug("processPulishAfterModifiedByDefinedServerId(" + dataId + "," + groupName + ","
                    + context + "," + serverId + ")?");
        // ?dataId,groupName?
        ContextResult result = null;
        result = queryByDataIdAndGroupName(dataId, groupName, serverId);
        if (null == result || !result.isSuccess()) {
            response.setSuccess(false);
            response.setStatusMsg("????!");
            log.warn("????! dataId=" + dataId + ",group="
                    + groupName + ",serverId=" + serverId);
            return response;
        }
        // ?
        else {
            String postUrl = "/diamond-server/admin.do?method=updateConfig";
            PostMethod post = new PostMethod(postUrl);
            // 
            post.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, require_timeout);
            try {
                NameValuePair dataId_value = new NameValuePair("dataId", dataId);
                NameValuePair group_value = new NameValuePair("group", groupName);
                NameValuePair content_value = new NameValuePair("content", context);
                // ?
                post.setRequestBody(new NameValuePair[] { dataId_value, group_value, content_value });
                // ?
                ConfigInfo configInfo = new ConfigInfo();
                configInfo.setDataId(dataId);
                configInfo.setGroup(groupName);
                configInfo.setContent(context);
                if (log.isDebugEnabled())
                    log.debug("?ConfigInfo: " + configInfo);
                // ??
                response.setConfigInfo(configInfo);
                // http??
                int status = client.executeMethod(post);
                response.setReceiveResult(post.getResponseBodyAsString());
                response.setStatusCode(status);
                log.info("??" + status + ",?" + post.getResponseBodyAsString());
                if (status == HttpStatus.SC_OK) {
                    response.setSuccess(true);
                    response.setStatusMsg("???");
                    log.info("???");
                } else if (status == HttpStatus.SC_REQUEST_TIMEOUT) {
                    response.setSuccess(false);
                    response.setStatusMsg(
                            "??:" + require_timeout + "");
                    log.error("??:" + require_timeout
                            + ", dataId=" + dataId + ",group=" + groupName + ",content=" + context
                            + ",serverId=" + serverId);
                } else {
                    response.setSuccess(false);
                    response.setStatusMsg(
                            "??,ContextResultgetReceiveResult()");
                    log.error("??:" + response.getReceiveResult() + ",dataId=" + dataId
                            + ",group=" + groupName + ",content=" + context + ",serverId=" + serverId);
                }

            } catch (HttpException e) {
                response.setSuccess(false);
                response.setStatusMsg("??HttpException" + e.getMessage());
                log.error(
                        "?processPulishAfterModifiedByDefinedServerId(String dataId, String groupName, String context,String serverId)?HttpExceptiondataId="
                                + dataId + ",group=" + groupName + ",content=" + context + ",serverId=" + serverId,
                        e);
                return response;
            } catch (IOException e) {
                response.setSuccess(false);
                response.setStatusMsg("??IOException" + e.getMessage());
                log.error(
                        "?processPulishAfterModifiedByDefinedServerId(String dataId, String groupName, String context,String serverId)?IOExceptiondataId="
                                + dataId + ",group=" + groupName + ",content=" + context + ",serverId=" + serverId,
                        e);
                return response;
            } finally {
                // ?
                post.releaseConnection();
            }

            return response;
        }
    }

    // =========================== ? ===============================

    /**
     *  httpclient?
     * 
     * @return  true:?,false:
     */

    private boolean login(String serverId) {
        // serverId 
        if (StringUtils.isEmpty(serverId) || StringUtils.isBlank(serverId))
            return false;
        DiamondSDKConf defaultConf = diamondSDKConfMaps.get(serverId);
        log.info("[login] serverId:" + serverId + "," + defaultConf);
        if (null == defaultConf)
            return false;
        RandomDiamondUtils util = new RandomDiamondUtils();
        // ???
        util.init(defaultConf.getDiamondConfs());
        if (defaultConf.getDiamondConfs().size() == 0)
            return false;
        boolean flag = false;
        log.info("[randomSequence] ?: " + util.getSequenceToString());
        // ???diamondConf
        while (util.getRetry_times() < util.getMax_times()) {

            // ??diamondConf
            DiamondConf diamondConf = util.generatorOneDiamondConf();
            log.info("" + util.getRetry_times() + "?:" + diamondConf);
            if (diamondConf == null)
                break;
            client.getHostConfiguration().setHost(diamondConf.getDiamondIp(),
                    Integer.parseInt(diamondConf.getDiamondPort()), "http");
            PostMethod post = new PostMethod("/diamond-server/login.do?method=login");
            // 
            post.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, require_timeout);
            // ???
            NameValuePair username_value = new NameValuePair("username", diamondConf.getDiamondUsername());
            NameValuePair password_value = new NameValuePair("password", diamondConf.getDiamondPassword());
            // 
            post.setRequestBody(new NameValuePair[] { username_value, password_value });
            log.info("diamondIp: " + diamondConf.getDiamondIp() + ",diamondPort: "
                    + diamondConf.getDiamondPort() + ",diamondUsername: " + diamondConf.getDiamondUsername()
                    + ",diamondPassword: " + diamondConf.getDiamondPassword() + "diamondServerUrl: ["
                    + diamondConf.getDiamondConUrl() + "]");

            try {
                int state = client.executeMethod(post);
                log.info("??" + state);
                // ??200?,true
                if (state == HttpStatus.SC_OK) {
                    log.info("" + util.getRetry_times() + "??");
                    flag = true;
                    break;
                }

            } catch (HttpException e) {
                log.error("?HttpException", e);
            } catch (IOException e) {
                log.error("?IOException", e);
            } finally {
                post.releaseConnection();
            }
        }
        if (flag == false) {
            log.error(
                    "?login?diamondServer?????serverId="
                            + serverId);
        }
        return flag;
    }

    static final String LIST_FORMAT_URL = "/diamond-server/admin.do?method=listConfig&group=%s&dataId=%s&pageNo=%d&pageSize=%d";
    static final String LIST_LIKE_FORMAT_URL = "/diamond-server/admin.do?method=listConfigLike&group=%s&dataId=%s&pageNo=%d&pageSize=%d";

    /**
     * ?
     * 
     * @param dataIdPattern
     * @param groupNamePattern
     * @param contentPattern
     * @param serverId
     * @param currentPage
     * @param sizeOfPerPage
     * @return
     */
    @SuppressWarnings("unchecked")
    private PageContextResult<ConfigInfo> processQuery(String dataIdPattern, String groupNamePattern,
            String contentPattern, String serverId, long currentPage, long sizeOfPerPage) {
        PageContextResult<ConfigInfo> response = new PageContextResult<ConfigInfo>();
        // 
        if (!login(serverId)) {
            response.setSuccess(false);
            response.setStatusMsg(",??serverId?");
            return response;
        }
        if (log.isDebugEnabled())
            log.debug("processQuery(" + dataIdPattern + "," + groupNamePattern + "," + contentPattern + ","
                    + serverId + ")");
        boolean hasPattern = PatternUtils.hasCharPattern(dataIdPattern)
                || PatternUtils.hasCharPattern(groupNamePattern) || PatternUtils.hasCharPattern(contentPattern);
        String url = null;
        if (hasPattern) {
            if (!StringUtils.isBlank(contentPattern)) {
                log.warn("?, ??, dataIdPattern=" + dataIdPattern
                        + ",groupNamePattern=" + groupNamePattern + ",contentPattern=" + contentPattern);
                // ?
                url = String.format(LIST_LIKE_FORMAT_URL, groupNamePattern, dataIdPattern, 1, Integer.MAX_VALUE);
            } else
                url = String.format(LIST_LIKE_FORMAT_URL, groupNamePattern, dataIdPattern, currentPage,
                        sizeOfPerPage);
        } else {
            url = String.format(LIST_FORMAT_URL, groupNamePattern, dataIdPattern, currentPage, sizeOfPerPage);
        }

        GetMethod method = new GetMethod(url);
        configureGetMethod(method);
        try {

            int status = client.executeMethod(method);
            response.setStatusCode(status);
            switch (status) {
            case HttpStatus.SC_OK:
                String json = "";
                try {
                    json = getContent(method).trim();

                    Page<ConfigInfo> page = null;

                    if (!json.equals("null")) {
                        page = (Page<ConfigInfo>) JSONUtils.deserializeObject(json,
                                new TypeReference<Page<ConfigInfo>>() {
                                });
                    }
                    if (page != null) {
                        List<ConfigInfo> diamondData = page.getPageItems();
                        if (!StringUtils.isBlank(contentPattern)) {
                            Pattern pattern = Pattern.compile(contentPattern.replaceAll("\\*", ".*"));
                            List<ConfigInfo> newList = new ArrayList<ConfigInfo>();
                            // ?
                            Collections.sort(diamondData);
                            int totalCount = 0;
                            long begin = sizeOfPerPage * (currentPage - 1);
                            long end = sizeOfPerPage * currentPage;
                            for (ConfigInfo configInfo : diamondData) {
                                if (configInfo.getContent() != null) {
                                    Matcher m = pattern.matcher(configInfo.getContent());
                                    if (m.find()) {
                                        // ?sizeOfPerPage
                                        if (totalCount >= begin && totalCount < end) {
                                            newList.add(configInfo);
                                        }
                                        totalCount++;
                                    }
                                }
                            }
                            page.setPageItems(newList);
                            page.setTotalCount(totalCount);
                        }
                        response.setOriginalDataSize(diamondData.size());
                        response.setTotalCounts(page.getTotalCount());
                        response.setCurrentPage(currentPage);
                        response.setSizeOfPerPage(sizeOfPerPage);
                    } else {
                        response.setOriginalDataSize(0);
                        response.setTotalCounts(0);
                        response.setCurrentPage(currentPage);
                        response.setSizeOfPerPage(sizeOfPerPage);
                    }
                    response.operation();
                    List<ConfigInfo> pageItems = new ArrayList<ConfigInfo>();
                    if (page != null) {
                        pageItems = page.getPageItems();
                    }
                    response.setDiamondData(pageItems);
                    response.setSuccess(true);
                    response.setStatusMsg("diamond?");
                    log.info("diamond?, url=" + url);
                } catch (Exception e) {
                    response.setSuccess(false);
                    response.setStatusMsg("???,?" + e.getLocalizedMessage());
                    log.error("???page, dataId=" + dataIdPattern + ",group=" + groupNamePattern
                            + ",serverId=" + serverId + ",json=" + json, e);
                }
                break;
            case HttpStatus.SC_REQUEST_TIMEOUT:
                response.setSuccess(false);
                response.setStatusMsg("?" + require_timeout + "");
                log.error("?:" + require_timeout + ", dataId="
                        + dataIdPattern + ",group=" + groupNamePattern + ",serverId=" + serverId);
                break;
            default:
                response.setSuccess(false);
                response.setStatusMsg("????" + status);
                log.error("???" + status + ",dataId=" + dataIdPattern + ",group="
                        + groupNamePattern + ",serverId=" + serverId);
                break;
            }

        } catch (HttpException e) {
            response.setSuccess(false);
            response.setStatusMsg("?,?" + e.getMessage());
            log.error("?, dataId=" + dataIdPattern + ",group=" + groupNamePattern + ",serverId="
                    + serverId, e);
        } catch (IOException e) {
            response.setSuccess(false);
            response.setStatusMsg("?,?" + e.getMessage());
            log.error("?, dataId=" + dataIdPattern + ",group=" + groupNamePattern + ",serverId="
                    + serverId, e);
        } finally {
            // ?
            method.releaseConnection();
        }

        return response;
    }

    /**
     * ?
     * 
     * @param httpMethod
     * @return
     */
    boolean isZipContent(HttpMethod httpMethod) {
        if (null != httpMethod.getResponseHeader(Constants.CONTENT_ENCODING)) {
            String acceptEncoding = httpMethod.getResponseHeader(Constants.CONTENT_ENCODING).getValue();
            if (acceptEncoding.toLowerCase().indexOf("gzip") > -1) {
                return true;
            }
        }
        return false;
    }

    /**
     * ?Response??
     * 
     * @param httpMethod
     * @return
     */
    String getContent(HttpMethod httpMethod) throws UnsupportedEncodingException {
        StringBuilder contentBuilder = new StringBuilder();
        if (isZipContent(httpMethod)) {
            // ???
            InputStream is = null;
            GZIPInputStream gzin = null;
            InputStreamReader isr = null;
            BufferedReader br = null;
            try {
                is = httpMethod.getResponseBodyAsStream();
                gzin = new GZIPInputStream(is);
                isr = new InputStreamReader(gzin, ((HttpMethodBase) httpMethod).getResponseCharSet()); // ?????
                br = new BufferedReader(isr);
                char[] buffer = new char[4096];
                int readlen = -1;
                while ((readlen = br.read(buffer, 0, 4096)) != -1) {
                    contentBuilder.append(buffer, 0, readlen);
                }
            } catch (Exception e) {
                log.error("", e);
            } finally {
                try {
                    br.close();
                } catch (Exception e1) {
                    // ignore
                }
                try {
                    isr.close();
                } catch (Exception e1) {
                    // ignore
                }
                try {
                    gzin.close();
                } catch (Exception e1) {
                    // ignore
                }
                try {
                    is.close();
                } catch (Exception e1) {
                    // ignore
                }
            }
        } else {
            // ???
            String content = null;
            try {
                content = httpMethod.getResponseBodyAsString();
            } catch (Exception e) {
                log.error("???", e);
            }
            if (null == content) {
                return null;
            }
            contentBuilder.append(content);
        }
        return StringEscapeUtils.unescapeHtml(contentBuilder.toString());
    }

    private void configureGetMethod(GetMethod method) {
        method.addRequestHeader(Constants.ACCEPT_ENCODING, "gzip,deflate");
        method.addRequestHeader("Accept", "application/json");
        // 
        method.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, require_timeout);
    }

    /**
     * dataId,groupName,context?,?false
     * 
     * @param dataId
     * @param groupName
     * @param context
     * @return
     */
    private boolean validate(String dataId, String groupName, String context) {
        if (StringUtils.isEmpty(dataId) || StringUtils.isEmpty(groupName) || StringUtils.isEmpty(context)
                || StringUtils.isBlank(dataId) || StringUtils.isBlank(groupName) || StringUtils.isBlank(context))
            return false;
        return true;
    }

    public synchronized ContextResult unpublish(String serverId, long id) {
        return processDelete(serverId, id);
    }

    /**
     * ?
     * 
     * @param serverId
     * @param id
     * @return
     */
    private ContextResult processDelete(String serverId, long id) {
        ContextResult response = new ContextResult();
        // 
        if (!login(serverId)) {
            response.setSuccess(false);
            response.setStatusMsg(",??serverId?");
            return response;
        }
        log.info("processDelete(" + serverId + "," + id);
        String url = "/diamond-server/admin.do?method=deleteConfig&id=" + id;
        GetMethod method = new GetMethod(url);
        configureGetMethod(method);
        try {

            int status = client.executeMethod(method);
            response.setStatusCode(status);
            switch (status) {
            case HttpStatus.SC_OK:
                response.setSuccess(true);
                response.setReceiveResult(getContent(method));
                response.setStatusMsg("?, url=" + url);
                log.warn("???, url=" + url);
                break;
            case HttpStatus.SC_REQUEST_TIMEOUT:
                response.setSuccess(false);
                response.setStatusMsg("?" + require_timeout + "");
                log.error("?:" + require_timeout + ", id=" + id
                        + ",serverId=" + serverId);
                break;
            default:
                response.setSuccess(false);
                response.setStatusMsg("????" + status);
                log.error("???" + status + ", id=" + id + ",serverId=" + serverId);
                break;
            }

        } catch (HttpException e) {
            response.setSuccess(false);
            response.setStatusMsg("?,?" + e.getMessage());
            log.error("?, id=" + id + ",serverId=" + serverId, e);
        } catch (IOException e) {
            response.setSuccess(false);
            response.setStatusMsg("?,?" + e.getMessage());
            log.error("?, id=" + id + ",serverId=" + serverId, e);
        } finally {
            // ?
            method.releaseConnection();
        }

        return response;
    }

    @Override
    public Map<String, DiamondSDKConf> getDiamondSDKConfMaps() {
        return this.diamondSDKConfMaps;
    }

    @Override
    public BatchContextResult<ConfigInfoEx> batchQuery(String serverId, String groupName, List<String> dataIds) {
        // 
        BatchContextResult<ConfigInfoEx> response = new BatchContextResult<ConfigInfoEx>();

        // list?null
        if (dataIds == null) {
            log.error("dataId list cannot be null, serverId=" + serverId + ",group=" + groupName);
            response.setSuccess(false);
            response.setStatusMsg("dataId list cannot be null");
            return response;
        }

        // dataIdlist????
        StringBuilder dataIdBuilder = new StringBuilder();
        for (String dataId : dataIds) {
            dataIdBuilder.append(dataId).append(Constants.LINE_SEPARATOR);
        }
        String dataIdStr = dataIdBuilder.toString();
        // 
        if (!login(serverId)) {
            response.setSuccess(false);
            response.setStatusMsg("login fail, serverId=" + serverId);
            return response;
        }

        // HTTP method
        PostMethod post = new PostMethod("/diamond-server/admin.do?method=batchQuery");
        // 
        post.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, require_timeout);
        try {
            // ?
            NameValuePair dataId_value = new NameValuePair("dataIds", dataIdStr);
            NameValuePair group_value = new NameValuePair("group", groupName);

            post.setRequestBody(new NameValuePair[] { dataId_value, group_value });

            // http??
            int status = client.executeMethod(post);
            response.setStatusCode(status);
            String responseMsg = post.getResponseBodyAsString();
            response.setResponseMsg(responseMsg);

            if (status == HttpStatus.SC_OK) {
                String json = null;
                try {
                    json = responseMsg;

                    // ???json, ??BatchContextResult
                    List<ConfigInfoEx> configInfoExList = new LinkedList<ConfigInfoEx>();
                    Object resultObj = JSONUtils.deserializeObject(json, new TypeReference<List<ConfigInfoEx>>() {
                    });
                    if (!(resultObj instanceof List<?>)) {
                        throw new RuntimeException("batch query deserialize type error, not list, json=" + json);
                    }
                    List<ConfigInfoEx> resultList = (List<ConfigInfoEx>) resultObj;
                    for (ConfigInfoEx configInfoEx : resultList) {
                        configInfoExList.add(configInfoEx);
                    }
                    response.getResult().addAll(configInfoExList);

                    // ????, ??
                    response.setSuccess(true);
                    response.setStatusMsg("batch query success");
                    log.info("batch query success, serverId=" + serverId + ",dataIds=" + dataIdStr + ",group="
                            + groupName + ",json=" + json);
                } catch (Exception e) {
                    response.setSuccess(false);
                    response.setStatusMsg("batch query deserialize error");
                    log.error("batch query deserialize error, serverId=" + serverId + ",dataIdStr=" + dataIdStr
                            + ",group=" + groupName + ",json=" + json, e);
                }

            } else if (status == HttpStatus.SC_REQUEST_TIMEOUT) {
                response.setSuccess(false);
                response.setStatusMsg("batch query timeout, socket timeout(ms):" + require_timeout);
                log.error("batch query timeout, socket timeout(ms):" + require_timeout + ", serverId=" + serverId
                        + ",dataIds=" + dataIdStr + ",group=" + groupName);
            } else {
                response.setSuccess(false);
                response.setStatusMsg("batch query fail, status:" + status);
                log.error("batch query fail, status:" + status + ", response:" + responseMsg + ",serverId="
                        + serverId + ",dataIds=" + dataIdStr + ",group=" + groupName);
            }
        } catch (HttpException e) {
            response.setSuccess(false);
            response.setStatusMsg("batch query http exception" + e.getMessage());
            log.error("batch query http exception, serverId=" + serverId + ",dataIds=" + dataIdStr + ",group="
                    + groupName, e);
        } catch (IOException e) {
            response.setSuccess(false);
            response.setStatusMsg("batch query io exception" + e.getMessage());
            log.error("batch query io exception, serverId=" + serverId + ",dataIds=" + dataIdStr + ",group="
                    + groupName, e);
        } finally {
            // ?
            post.releaseConnection();
        }

        return response;
    }

    @Override
    public BatchContextResult<ConfigInfoEx> batchAddOrUpdate(String serverId, String groupName,
            Map<String, String> dataId2ContentMap) {
        // 
        BatchContextResult<ConfigInfoEx> response = new BatchContextResult<ConfigInfoEx>();

        // map?null
        if (dataId2ContentMap == null) {
            log.error("dataId2ContentMap cannot be null, serverId=" + serverId + " ,group=" + groupName);
            response.setSuccess(false);
            response.setStatusMsg("dataId2ContentMap cannot be null");
            return response;
        }

        // dataIdcontentmap????
        StringBuilder allDataIdAndContentBuilder = new StringBuilder();
        for (String dataId : dataId2ContentMap.keySet()) {
            String content = dataId2ContentMap.get(dataId);
            allDataIdAndContentBuilder.append(dataId + Constants.WORD_SEPARATOR + content)
                    .append(Constants.LINE_SEPARATOR);
        }
        String allDataIdAndContent = allDataIdAndContentBuilder.toString();

        // 
        if (!login(serverId)) {
            response.setSuccess(false);
            response.setStatusMsg("login fail, serverId=" + serverId);
            return response;
        }

        // HTTP method
        PostMethod post = new PostMethod("/diamond-server/admin.do?method=batchAddOrUpdate");
        // 
        post.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, require_timeout);
        try {
            // ?
            NameValuePair dataId_value = new NameValuePair("allDataIdAndContent", allDataIdAndContent);
            NameValuePair group_value = new NameValuePair("group", groupName);

            post.setRequestBody(new NameValuePair[] { dataId_value, group_value });

            // http??
            int status = client.executeMethod(post);
            response.setStatusCode(status);
            String responseMsg = post.getResponseBodyAsString();
            response.setResponseMsg(responseMsg);

            if (status == HttpStatus.SC_OK) {
                String json = null;
                try {
                    json = responseMsg;

                    // ???json, ??BatchContextResult
                    List<ConfigInfoEx> configInfoExList = new LinkedList<ConfigInfoEx>();
                    Object resultObj = JSONUtils.deserializeObject(json, new TypeReference<List<ConfigInfoEx>>() {
                    });
                    if (!(resultObj instanceof List<?>)) {
                        throw new RuntimeException("batch write deserialize type error, not list, json=" + json);
                    }
                    List<ConfigInfoEx> resultList = (List<ConfigInfoEx>) resultObj;
                    for (ConfigInfoEx configInfoEx : resultList) {
                        configInfoExList.add(configInfoEx);
                    }
                    response.getResult().addAll(configInfoExList);
                    // ????, ???
                    response.setStatusMsg("batch write success");
                    log.info("batch write success,serverId=" + serverId + ",allDataIdAndContent="
                            + allDataIdAndContent + ",group=" + groupName + ",json=" + json);
                } catch (Exception e) {
                    response.setSuccess(false);
                    response.setStatusMsg("batch write deserialize error");
                    log.error("batch write deserialize error, serverId=" + serverId + ",allDataIdAndContent="
                            + allDataIdAndContent + ",group=" + groupName + ",json=" + json, e);
                }
            } else if (status == HttpStatus.SC_REQUEST_TIMEOUT) {
                response.setSuccess(false);
                response.setStatusMsg("batch write timeout, socket timeout(ms):" + require_timeout);
                log.error("batch write timeout, socket timeout(ms):" + require_timeout + ", serverId=" + serverId
                        + ",allDataIdAndContent=" + allDataIdAndContent + ",group=" + groupName);
            } else {
                response.setSuccess(false);
                response.setStatusMsg("batch write fail, status:" + status);
                log.error("batch write fail, status:" + status + ", response:" + responseMsg + ",serverId="
                        + serverId + ",allDataIdAndContent=" + allDataIdAndContent + ",group=" + groupName);
            }
        } catch (HttpException e) {
            response.setSuccess(false);
            response.setStatusMsg("batch write http exception" + e.getMessage());
            log.error("batch write http exception, serverId=" + serverId + ",allDataIdAndContent="
                    + allDataIdAndContent + ",group=" + groupName, e);
        } catch (IOException e) {
            response.setSuccess(false);
            response.setStatusMsg("batch write io exception" + e.getMessage());
            log.error("batch write io exception, serverId=" + serverId + ",allDataIdAndContent="
                    + allDataIdAndContent + ",group=" + groupName, e);
        } finally {
            // ?
            post.releaseConnection();
        }

        return response;
    }

}