com.asakusafw.cleaner.log.LogMessageManager.java Source code

Java tutorial

Introduction

Here is the source code for com.asakusafw.cleaner.log.LogMessageManager.java

Source

/**
 * Copyright 2011-2015 Asakusa Framework Team.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.asakusafw.cleaner.log;

import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Level;

/**
 * ?????
 * @author yuta.shirai
 *
 */
public class LogMessageManager {

    /**
     * ??ID??
     * ????????
     */
    private static final String MESSAGE_ID_NOT_FOUND = "??ID?????ID={0}?={1}";

    /**
     * ????
     * ?/???????
     */
    private static final String ILLEGAL_SIZE = "{0, date} {0, time} ??????ID={1}?={2}";

    /**
     * ID????
     */
    private Map<String, Level> levelMap = new HashMap<String, Level>();

    /**
     * ID??
     */
    private Map<String, String> templateMap = new HashMap<String, String>();

    /**
     * ID???
     */
    private Map<String, Integer> sizeMap = new HashMap<String, Integer>();

    /**
     * ?
     */
    private static LogMessageManager instance = new LogMessageManager();

    /**
     * ????getInstance()????????
     * <p>
     * ????Singleton??private?????????
     * ?????????protected??????
     * </p>
     */
    protected LogMessageManager() {
        return;
    }

    /**
     * ??????
     * @return ??
     */
    public static LogMessageManager getInstance() {
        return LogMessageManager.instance;
    }

    /**
     * ID?????????
     * @param messageId ID
     * @param level ??
     */
    public void putLevel(String messageId, String level) {
        levelMap.put(messageId, Level.toLevel(level));
    }

    /**
     * ID???????
     * @param messageId ID
     * @param templates 
     */
    public void putTemplate(String messageId, String templates) {
        templateMap.put(messageId, templates);
    }

    /**
     * ID???????
     * @param messageId ID
     * @param index ?
     */
    public void putSize(String messageId, Integer index) {
        sizeMap.put(messageId, index);
    }

    /**
     * ID?????
     * <p>
     * ??ID?????????
     * ????
     * ??ID?????ID=xxx?=xxx, xxx, xxx?
     * </p>
     * <p>
     * ???????????
     * ????
     * </p>
     * @param messageId ID
     * @param messageArgs 
     * @return 
     */
    public String createLogMessage(String messageId, Object... messageArgs) {

        Object[] messageArgsConverted = toStringMessageArgs(messageArgs);

        String templateStr = templateMap.get(messageId);
        if (templateStr == null) {
            String message = MessageFormat.format(MESSAGE_ID_NOT_FOUND, messageId,
                    StringUtils.join(messageArgsConverted, ", "));
            return message;
        }
        Integer index = sizeMap.get(messageId);
        if (index != null) {
            if (messageArgsConverted.length != index.intValue()) {
                String message = MessageFormat.format(ILLEGAL_SIZE, new Date(), messageId,
                        StringUtils.join(messageArgsConverted, ", "));
                System.err.println(message);
            }
        }

        return MessageFormat.format(templateStr, messageArgsConverted);
    }

    /**
     * ????
     * <p>
     * ??ID?????????
     * ERROR???
     * </p>
     * @param messageId ID
     * @return 
     */
    public Level getLogLevel(String messageId) {
        Level level = levelMap.get(messageId);
        if (level == null) {
            return Level.ERROR;
        }

        return level;
    }

    /**
     * ???
     *
     * @param messageArgs 
     * @return ??
     */
    private Object[] toStringMessageArgs(Object[] messageArgs) {
        if (messageArgs == null) {
            return messageArgs;
        }
        Object[] messageArgsConverted = new Object[messageArgs.length];
        for (int i = 0; i < messageArgs.length; i++) {
            Object obj = messageArgs[i];
            if (obj == null) {
                messageArgsConverted[i] = null;
            } else if (obj.getClass().isArray()) {
                messageArgsConverted[i] = ArrayUtils.toString(obj);
            } else if (obj instanceof Long || obj instanceof Integer || obj instanceof BigDecimal) {
                messageArgsConverted[i] = ObjectUtils.toString(obj, "null");
            } else {
                messageArgsConverted[i] = messageArgs[i];
            }
        }
        return messageArgsConverted;
    }

}