net.ymate.platform.configuration.provider.impl.JConfigProvider.java Source code

Java tutorial

Introduction

Here is the source code for net.ymate.platform.configuration.provider.impl.JConfigProvider.java

Source

/*
 * Copyright 2007-2107 the original author or authors.
 *
 * 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 net.ymate.platform.configuration.provider.impl;

import java.io.File;
import java.net.URL;
import java.util.*;

import net.ymate.platform.base.YMP;
import net.ymate.platform.commons.i18n.I18N;
import net.ymate.platform.commons.util.FileUtils;
import net.ymate.platform.commons.util.RuntimeUtils;
import net.ymate.platform.configuration.ConfigurationLoadException;
import net.ymate.platform.configuration.IConfiguration;
import net.ymate.platform.configuration.provider.IConfigurationProvider;

import org.apache.commons.lang.StringUtils;
import org.jconfig.Configuration;
import org.jconfig.ConfigurationManager;
import org.jconfig.ConfigurationManagerException;
import org.jconfig.handler.XMLFileHandler;

/**
 * <p>
 * JConfigProvider
 * </p>
 * <p>
 * JConfig?????
 * </p>
 * 
 * @author (suninformation@163.com)
 * @version 0.0.0
 *          <table style="border:1px solid gray;">
 *          <tr>
 *          <th width="100px">?</th><th width="100px"></th><th
 *          width="100px"></th><th width="100px"></th>
 *          </tr>
 *          <!--  Table ?? -->
 *          <tr>
 *          <td>0.0.0</td>
 *          <td></td>
 *          <td></td>
 *          <td>2010-4-17?02:34:44</td>
 *          </tr>
 *          </table>
 */
public class JConfigProvider implements IConfigurationProvider {

    /**
     * ????
     */
    private static final Map<String, Configuration> __CONFIG_MAPS = new HashMap<String, Configuration>();

    /**
     * JConfig?
     */
    private Configuration config;

    /**
     * ??
     */
    private String cfgFileName;

    /**
     * 
     */
    public JConfigProvider() {
    }

    /* (non-Javadoc)
     * @see net.ymate.platform.configuration.IConfigurationProvider#getString(java.lang.String)
     */
    public String getString(String key) {
        return getString(key, "");
    }

    /* (non-Javadoc)
     * @see net.ymate.platform.configuration.IConfigurationProvider#getString(java.lang.String, java.lang.String)
     */
    public String getString(String key, String defaultValue) {
        String[] keys = StringUtils.split(key, IConfiguration.CFG_KEY_SEPERATE);
        int keysSize = keys.length;
        if (keysSize == 1) {
            return config.getProperty(keys[0], defaultValue);
        } else if (keysSize == 2) {
            return config.getProperty(keys[1], defaultValue, keys[0]);
        }
        return defaultValue;
    }

    /* (non-Javadoc)
     * @see net.ymate.platform.configuration.IConfigurationProvider#getBoolean(java.lang.String)
     */
    public boolean getBoolean(String key) {
        return getBoolean(key, false);
    }

    /* (non-Javadoc)
     * @see net.ymate.platform.configuration.IConfigurationProvider#getBoolean(java.lang.String, boolean)
     */
    public boolean getBoolean(String key, boolean defaultValue) {
        String[] keys = StringUtils.split(key, IConfiguration.CFG_KEY_SEPERATE);
        int keysSize = keys.length;
        if (keysSize == 1) {
            return config.getBooleanProperty(keys[0], defaultValue);
        } else if (keysSize == 2) {
            return config.getBooleanProperty(keys[1], defaultValue, keys[0]);
        }
        return defaultValue;
    }

    /* (non-Javadoc)
     * @see net.ymate.platform.configuration.IConfigurationProvider#getCfgsMap()
     */
    public Map<String, String> getCfgsMap() {
        Map<String, String> cfgsMap = new HashMap<String, String>();
        String[] categoryNames = config.getCategoryNames();
        if (categoryNames != null && categoryNames.length > 0) {
            for (String categoryName : categoryNames) {
                String[] propertyNames = config.getPropertyNames(categoryName);
                if (propertyNames != null && propertyNames.length > 0) {
                    for (String propertyName : propertyNames) {
                        cfgsMap.put(categoryName + IConfiguration.CFG_KEY_SEPERATE + propertyName,
                                config.getProperty(propertyName, "", categoryName));
                    }
                }
            }
        }
        return cfgsMap;
    }

    public double getDouble(String key) {
        return getDouble(key, Double.MIN_VALUE);
    }

    public double getDouble(String key, double defaultValue) {
        String[] keys = StringUtils.split(key, IConfiguration.CFG_KEY_SEPERATE);
        int keysSize = keys.length;
        if (keysSize == 1) {
            return config.getDoubleProperty(keys[0], defaultValue);
        } else if (keysSize == 2) {
            return config.getDoubleProperty(keys[1], defaultValue, keys[0]);
        }
        return defaultValue;
    }

    public float getFloat(String key) {
        return getFloat(key, Float.MIN_VALUE);
    }

    public float getFloat(String key, float defaultValue) {
        return new Double(getDouble(key, defaultValue)).floatValue();
    }

    public int getInt(String key) {
        return getInt(key, Integer.MIN_VALUE);
    }

    public int getInt(String key, int defaultValue) {
        String[] keys = StringUtils.split(key, IConfiguration.CFG_KEY_SEPERATE);
        int keysSize = keys.length;
        if (keysSize == 1) {
            return config.getIntProperty(keys[0], defaultValue);
        } else if (keysSize == 2) {
            return config.getIntProperty(keys[1], defaultValue, keys[0]);
        }
        return defaultValue;
    }

    public List<String> getList(String key) {
        List<String> valueList = new ArrayList<String>();
        String[] keys = StringUtils.split(key, IConfiguration.CFG_KEY_SEPERATE);
        int keysSize = keys.length;
        Properties properties = null;
        if (keysSize == 1) {
            properties = config.getProperties();
        } else if (keysSize == 2) {
            properties = config.getProperties(keys[0]);
        }
        if (properties != null && !properties.isEmpty()) {
            for (Object name : properties.keySet()) {
                if (name != null && name.toString().startsWith(keysSize == 1 ? keys[0] : keys[1])) { // key
                    Object value = properties.get(name);
                    valueList.add(value == null ? "" : value.toString());
                }
            }
        }
        return valueList;
    }

    public Map<String, String> getMap(String keyHead) {
        Map<String, String> map = new HashMap<String, String>();
        if (StringUtils.isBlank(keyHead)) {
            return map;
        }
        String[] keys = StringUtils.split(keyHead, IConfiguration.CFG_KEY_SEPERATE);
        int keysSize = keys.length;
        Properties properties = null;
        // "|"0??
        if (keysSize == 1) {
            properties = config.getProperties();
        } else if (keysSize == 2) {
            properties = config.getProperties(keys[0]);
        }
        int headLength = keysSize == 1 ? keys[0].length() : keys[1].length();
        if (properties != null && !properties.isEmpty()) {
            for (Object name : properties.keySet()) {
                if (name != null && name.toString().startsWith(keysSize == 1 ? keys[0] : keys[1])) {
                    String key = name.toString();
                    Object value = properties.get(name);
                    map.put(key.substring(headLength), value.toString());
                }
            }
        }
        return map;
    }

    public String getCfgFileName() {
        return cfgFileName;
    }

    public long getLong(String key) {
        return getLong(key, Long.MIN_VALUE);
    }

    public long getLong(String key, long defaultValue) {
        String[] keys = StringUtils.split(key, IConfiguration.CFG_KEY_SEPERATE);
        int keysSize = keys.length;
        if (keysSize == 1) {
            return config.getLongProperty(keys[0], defaultValue);
        } else if (keysSize == 2) {
            return config.getLongProperty(keys[1], defaultValue, keys[0]);
        }
        return defaultValue;
    }

    public boolean contains(String key) {
        String[] keys = StringUtils.split(key, IConfiguration.CFG_KEY_SEPERATE);
        int keysSize = keys.length;
        if (keysSize == 1) {
            return config.getProperty(keys[0], null) != null;
        } else if (keysSize == 2) {
            return config.getProperty(keys[1], null, keys[0]) != null;
        }
        return false;
    }

    public void load(String cfgFileName) throws ConfigurationLoadException {
        if (StringUtils.isBlank(cfgFileName)) {
            throw new ConfigurationLoadException(
                    I18N.formatMessage(YMP.__LSTRING_FILE, null, null, "ymp.configuration.load_config_error", ""));
        }
        config = __CONFIG_MAPS.get(cfgFileName);
        if (config == null) {
            this.cfgFileName = cfgFileName;
            // JConfig???????jar?
            URL _url = FileUtils.toURL(cfgFileName);
            File file = FileUtils.toFile(_url);
            if (file == null) {
                throw new ConfigurationLoadException(I18N.formatMessage(YMP.__LSTRING_FILE, null, null,
                        "ymp.configuration.load_config_error", cfgFileName));
            }
            // ??JConfig?
            System.setProperty("jconfig.filewatcher", "false");
            try {
                ConfigurationManager.getInstance().load(new XMLFileHandler(file.getAbsolutePath()),
                        IConfiguration.DEFAULT_CFG_CATEGORY_NAME);
                config = ConfigurationManager.getConfiguration(IConfiguration.DEFAULT_CFG_CATEGORY_NAME);
                // INFO: "???"
                __CONFIG_MAPS.put(cfgFileName, config);
            } catch (ConfigurationManagerException e) {
                throw new ConfigurationLoadException(RuntimeUtils.unwrapThrow(e));
            }
        }
    }

    public void reload() throws ConfigurationLoadException {
        if (StringUtils.isNotBlank(this.cfgFileName)) {
            // 
            __CONFIG_MAPS.remove(this.cfgFileName);
            // ?
            load(this.cfgFileName);
        }
    }

    public String[] getArray(String key) {
        return getArray(key, true);
    }

    public String[] getArray(String key, boolean zeroSize) {
        String[] keys = StringUtils.split(key, IConfiguration.CFG_KEY_SEPERATE);
        int keysSize = keys.length;
        if (keysSize == 1) {
            return config.getArray(keys[0], zeroSize ? new String[] {} : null);
        } else if (keysSize == 2) {
            return config.getArray(keys[1], zeroSize ? new String[] {} : null, keys[0]);
        }
        return zeroSize ? new String[] {} : null;
    }

    public List<String> getCategoryNames() {
        return Arrays.asList(config.getCategoryNames());
    }

}