com.greenline.hrs.admin.cache.redis.util.RedisInfoParser.java Source code

Java tutorial

Introduction

Here is the source code for com.greenline.hrs.admin.cache.redis.util.RedisInfoParser.java

Source

/*
 * Project: admin-parent
 * 
 * File Created at 2014-04-02
 * 
 * Copyright 2012 Greenline.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Greenline Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Greenline.com.
 */
package com.greenline.hrs.admin.cache.redis.util;

import com.greenline.hrs.admin.base.annotation.ParserField;
import com.greenline.hrs.admin.cache.redis.cons.RedisCommonConst;
import com.greenline.hrs.admin.cache.redis.po.RedisKeyspaceInfo;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.joda.time.DateTime;

import java.lang.reflect.Field;
import java.util.*;

/**
 * @author July
 * @Type RedisInfoParser
 * @Desc Redis info?
 * @date 2014-04-02
 * @Version V1.0
 */
public class RedisInfoParser {

    private static final Log LOG = LogFactory.getLog(RedisInfoParser.class);

    /**
     * Redis Info 
     */
    private static final String INFO_PROPERTY_DELIMITER = "\r\n";

    /**
     * Redis Info key?value
     */
    private static final char INFO_PROPERTY_KY_DELIMITER = ':';

    /**
     * KeySpace Info
     */
    private static final String KS_PROPERTY_DELIMITER = "[,=]";

    /**
     * KeySpace Info?
     */
    private static final int KS_PROPERTY_MIN_SIZE = 2;

    /**
     * 
     */
    private static final int PROPERTY_TOKENS_LEN = 2;

    /**
     * key
     */
    private static final int PROP_KEY_IDX = 0;

    /**
     * value
     */
    private static final int PROP_VALUE_IDX = 1;

    /**
     *  ?
     */
    private RedisInfoParser() {
        super();
    }

    /**
     * infoString?map
     *
     * @param redisInfoStr
     * @return infoStrnullMap
     */
    public static Map<String, String> infoMapParser(String redisInfoStr) {
        if (redisInfoStr == null || redisInfoStr.isEmpty()) {
            return Collections.emptyMap();
        }
        String[] infoTokens = redisInfoStr.split(INFO_PROPERTY_DELIMITER);
        Map<String, String> infoMap = new HashMap<String, String>(infoTokens.length * 2);
        String[] propTokens = null;
        for (String infoToken : infoTokens) {
            propTokens = StringUtils.split(infoToken, INFO_PROPERTY_KY_DELIMITER);
            if (propTokens.length == PROPERTY_TOKENS_LEN) {
                infoMap.put(propTokens[PROP_KEY_IDX], propTokens[PROP_VALUE_IDX]);
            }
        }
        return infoMap;
    }

    /**
     * RedisInfoMapT?RedisInfoMapRedis?
     * RedisServerInfoPO
     *
     * @param instanceType class
     * @param redisInfoMap Map
     * @param <T>          
     * @return instanceTypeinfomapnullinfoMap?instancenullRedisServerInfoPO
     */
    public static <T> T parserRedisInfo(Class<T> instanceType, Map<String, String> redisInfoMap) {
        if (instanceType == null || redisInfoMap == null || redisInfoMap.isEmpty()) {
            return null;
        }
        T instance = null;
        try {
            instance = instanceType.newInstance();
        } catch (Exception e) {
            LOG.error("Instance:" + instanceType.getName(), e);
            return null;
        }
        Field[] fields = instanceType.getDeclaredFields();
        String inputName = null;
        for (Field field : fields) {
            ParserField parserField = field.getAnnotation(ParserField.class);
            if (parserField != null) {
                inputName = parserField.inputName();
            } else {
                inputName = field.getName();
            }
            if (redisInfoMap.containsKey(inputName)) {
                field.setAccessible(true);
                try {
                    field.set(instance, ConvertUtils.convert(redisInfoMap.get(inputName), field.getType()));
                } catch (Exception e) {
                    LOG.error("Field:" + field.getName() + "\t|\t value:" + redisInfoMap.get(inputName), e);
                }
            }
        }
        return instance;
    }

    /**
     * RedisInfoMapT?RedisInfoMapRedis?
     *
     * @param redisInfoMap Map
     * @return instanceTypeinfomapnullinfoMap?instancenullRedisServerInfoPO
     */
    public static List<RedisKeyspaceInfo> parserToKeySpaceInfo(Map<String, String> redisInfoMap) {
        if (redisInfoMap == null || redisInfoMap.isEmpty()) {
            return Collections.EMPTY_LIST;
        }
        int dbSize = 16;
        String dbSizeStr = redisInfoMap.get(RedisCommonConst.DB_SIZE);
        if (StringUtils.isNumeric(dbSizeStr)) {
            dbSize = Integer.valueOf(dbSizeStr);
        }
        List<RedisKeyspaceInfo> result = new ArrayList<RedisKeyspaceInfo>(dbSize * 2);
        for (int i = 0; i < dbSize; i++) {
            RedisKeyspaceInfo keyspaceInfo = new RedisKeyspaceInfo();
            // TODO createTime??
            keyspaceInfo.setCreateTime(DateTime.now().toDate());
            keyspaceInfo.setDbIndex(i);
            keyspaceInfo.setKeys(0L);
            keyspaceInfo.setExpires(0L);
            String keyspaceStr = redisInfoMap.get("db" + i);
            if (keyspaceStr != null) {
                String[] properties = StringUtils.split(keyspaceStr, KS_PROPERTY_DELIMITER);
                if (properties.length >= KS_PROPERTY_MIN_SIZE) {
                    keyspaceInfo.setKeys(Long.valueOf(properties[1]));
                    keyspaceInfo.setExpires(Long.valueOf(properties[3]));
                    result.add(keyspaceInfo);
                }
            }
        }
        return result;
    }
}