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

Java tutorial

Introduction

Here is the source code for com.asakusafw.cleaner.log.LogMessageLoader.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.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.math.NumberUtils;

import com.asakusafw.cleaner.common.Constants;

/**
 * ?????
 * @author yuta.shirai
 *
 */
final class LogMessageLoader {
    /** ??????ID?? */
    private static final String LEVEL_KEY_END = ".level";
    /** ????ID?? */
    private static final String TEMPLATE_KEY_END = ".message";
    /** ?????ID?? */
    private static final String SIZE_KEY_END = ".size";

    /**
     * ????static????????
     */
    private LogMessageLoader() {
        return;
    }

    /**
     * ??
     * ????????
     * <p>
     * ????.properties?????
     * ???????????
     * </p>
     * @param manager ?
     * @throws IOException ?????
     */
    static void loadFile(LogMessageManager manager) throws IOException {
        // ?
        InputStream in = null;
        Properties props = new Properties();
        try {
            in = LogMessageLoader.class.getClassLoader().getResourceAsStream(Constants.LOG_MESSAGE_FILE);
            props.load(in);
            Enumeration<?> keys = props.propertyNames();
            while (keys.hasMoreElements()) {
                String key = (String) keys.nextElement();
                if (key.endsWith(LEVEL_KEY_END)) {
                    String messageId = LogMessageLoader.getMessageId(key, LEVEL_KEY_END);
                    manager.putLevel(messageId, props.getProperty(key));
                } else if (key.endsWith(TEMPLATE_KEY_END)) {
                    String messageId = LogMessageLoader.getMessageId(key, TEMPLATE_KEY_END);
                    manager.putTemplate(messageId, props.getProperty(key));
                } else if (key.endsWith(SIZE_KEY_END)) {
                    String messageId = LogMessageLoader.getMessageId(key, SIZE_KEY_END);
                    String sizeStr = props.getProperty(key);
                    if (NumberUtils.isNumber(sizeStr)) {
                        manager.putSize(messageId, Integer.valueOf(sizeStr));
                    }
                }
            }
        } catch (IOException ex) {
            throw new IOException(
                    "??????????"
                            + Constants.LOG_MESSAGE_FILE,
                    ex);
        } finally {
            IOUtils.closeQuietly(in);
        }
    }

    /**
     * ??ID????
     * @param key 
     * @param endStr ?ID?
     * @return ID
     */
    private static String getMessageId(String key, String endStr) {
        String[] splits = key.split("\\.");
        return splits[0];
    }
}