Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
net.ymate.platform.configuration.impl.PropertyConfigurationProvider Maven / Gradle / Ivy
/*
* Copyright 2007-2017 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.impl;
import net.ymate.platform.configuration.IConfigurationProvider;
import net.ymate.platform.configuration.support.PropertyConfigFileHandler;
import net.ymate.platform.configuration.support.XMLConfigFileHandler;
import net.ymate.platform.core.lang.BlurObject;
import net.ymate.platform.core.util.FileUtils;
import org.apache.commons.lang.NullArgumentException;
import org.apache.commons.lang.StringUtils;
import java.util.*;
/**
* 基于properties文件的配置提供者接口实现
*
* @author 刘镇 ([email protected] ) on 15/10/26 下午5:21
* @version 1.0
*/
public class PropertyConfigurationProvider implements IConfigurationProvider {
/**
* 配置对象缓存,对于重复的文件加载会使用缓存,减少文件读写频率
*/
private static final Map __CONFIG_CACHE_MAPS = new HashMap();
/**
* 配置对象
*/
private PropertyConfigFileHandler __config;
/**
* 装载配置文件参数
*/
private String __cfgFileName;
public void load(String cfgFileName) throws Exception {
if (StringUtils.isBlank(cfgFileName)) {
throw new NullArgumentException("cfgFileName");
}
if ((__config = __CONFIG_CACHE_MAPS.get(cfgFileName)) == null) {
this.__cfgFileName = cfgFileName;
__config = new PropertyConfigFileHandler(FileUtils.toURL(cfgFileName)).load(true);
__CONFIG_CACHE_MAPS.put(cfgFileName, __config);
}
}
public void reload() throws Exception {
// 移除缓存项
__CONFIG_CACHE_MAPS.remove(this.__cfgFileName);
// 加载配置
load(this.__cfgFileName);
}
public String getCfgFileName() {
return __cfgFileName;
}
public String getString(String key) {
XMLConfigFileHandler.XMLProperty _prop = __config.getDefaultCategory().getProperty(key);
return _prop == null ? null : _prop.getContent();
}
public String getString(String key, String defaultValue) {
return StringUtils.defaultIfEmpty(getString(key), defaultValue);
}
public String getString(String category, String key, String defaultValue) {
XMLConfigFileHandler.XMLCategory _category = __config.getCategory(category);
if (_category == null) {
return null;
}
XMLConfigFileHandler.XMLProperty _prop = _category.getProperty(key);
return StringUtils.defaultIfEmpty(_prop == null ? null : _prop.getContent(), defaultValue);
}
public List getList(String key) {
return getList(XMLConfigFileHandler.DEFAULT_CATEGORY_NAME, key);
}
public List getList(String category, String key) {
List _returnValue = new ArrayList();
XMLConfigFileHandler.XMLProperty _prop = __config.getCategory(category).getProperty(key);
if (_prop != null && StringUtils.isNotBlank(_prop.getContent())) {
_returnValue.addAll(Arrays.asList(StringUtils.split(_prop.getContent(), "|")));
}
return _returnValue;
}
public Map getMap(String keyHead) {
return getMap(XMLConfigFileHandler.DEFAULT_CATEGORY_NAME, keyHead);
}
public Map getMap(String category, String keyHead) {
Map _returnValue = new LinkedHashMap();
XMLConfigFileHandler.XMLProperty _prop = __config.getCategory(category).getProperty(keyHead);
if (_prop != null) {
for (String name : _prop.getAttributeMap().keySet()) {
String value = _prop.getAttribute(name).getValue();
if (StringUtils.isNotBlank(value)) {
_returnValue.put(name, value);
}
}
}
return _returnValue;
}
public String[] getArray(String key) {
List _resultValue = getList(key);
return _resultValue.toArray(new String[_resultValue.size()]);
}
public String[] getArray(String key, boolean zeroSize) {
return getArray(XMLConfigFileHandler.DEFAULT_CATEGORY_NAME, key, zeroSize);
}
public String[] getArray(String category, String key, boolean zeroSize) {
List _values = getList(category, key);
if (_values.isEmpty() && !zeroSize) {
return null;
}
return _values.toArray(new String[_values.size()]);
}
public int getInt(String key) {
return getInt(XMLConfigFileHandler.DEFAULT_CATEGORY_NAME, key, 0);
}
public int getInt(String key, int defaultValue) {
return getInt(XMLConfigFileHandler.DEFAULT_CATEGORY_NAME, key, defaultValue);
}
public int getInt(String category, String key, int defaultValue) {
XMLConfigFileHandler.XMLCategory _category = __config.getCategory(category);
if (_category != null) {
XMLConfigFileHandler.XMLProperty _prop = _category.getProperty(key);
if (_prop != null) {
return new BlurObject(_prop.getContent()).toIntValue();
}
}
return defaultValue;
}
public boolean getBoolean(String key) {
return getBoolean(XMLConfigFileHandler.DEFAULT_CATEGORY_NAME, key, false);
}
public boolean getBoolean(String key, boolean defaultValue) {
return getBoolean(XMLConfigFileHandler.DEFAULT_CATEGORY_NAME, key, defaultValue);
}
public boolean getBoolean(String category, String key, boolean defaultValue) {
XMLConfigFileHandler.XMLCategory _category = __config.getCategory(category);
if (_category != null) {
XMLConfigFileHandler.XMLProperty _prop = _category.getProperty(key);
if (_prop != null) {
return new BlurObject(_prop.getContent()).toBooleanValue();
}
}
return defaultValue;
}
public long getLong(String key) {
return getLong(XMLConfigFileHandler.DEFAULT_CATEGORY_NAME, key, 0l);
}
public long getLong(String key, long defaultValue) {
return getLong(XMLConfigFileHandler.DEFAULT_CATEGORY_NAME, key, defaultValue);
}
public long getLong(String category, String key, long defaultValue) {
XMLConfigFileHandler.XMLCategory _category = __config.getCategory(category);
if (_category != null) {
XMLConfigFileHandler.XMLProperty _prop = _category.getProperty(key);
if (_prop != null) {
return new BlurObject(_prop.getContent()).toLongValue();
}
}
return defaultValue;
}
public float getFloat(String key) {
return getFloat(XMLConfigFileHandler.DEFAULT_CATEGORY_NAME, key, 0f);
}
public float getFloat(String key, float defaultValue) {
return getFloat(XMLConfigFileHandler.DEFAULT_CATEGORY_NAME, key, defaultValue);
}
public float getFloat(String category, String key, float defaultValue) {
XMLConfigFileHandler.XMLCategory _category = __config.getCategory(category);
if (_category != null) {
XMLConfigFileHandler.XMLProperty _prop = _category.getProperty(key);
if (_prop != null) {
return new BlurObject(_prop.getContent()).toFloatValue();
}
}
return defaultValue;
}
public double getDouble(String key) {
return getDouble(XMLConfigFileHandler.DEFAULT_CATEGORY_NAME, key, 0d);
}
public double getDouble(String key, double defaultValue) {
return getDouble(XMLConfigFileHandler.DEFAULT_CATEGORY_NAME, key, defaultValue);
}
public double getDouble(String category, String key, double defaultValue) {
XMLConfigFileHandler.XMLCategory _category = __config.getCategory(category);
if (_category != null) {
XMLConfigFileHandler.XMLProperty _prop = _category.getProperty(key);
if (_prop != null) {
return new BlurObject(_prop.getContent()).toDoubleValue();
}
}
return defaultValue;
}
public Map toMap() {
return toMap(XMLConfigFileHandler.DEFAULT_CATEGORY_NAME);
}
public Map toMap(String category) {
XMLConfigFileHandler.XMLCategory _category = __config.getCategory(category);
if (_category == null) {
return Collections.emptyMap();
}
Collection _properties = _category.getPropertyMap().values();
Map _returnValue = new LinkedHashMap(_properties.size());
for (XMLConfigFileHandler.XMLProperty _prop : _properties) {
_returnValue.put(_prop.getName(), _prop.getContent());
for (XMLConfigFileHandler.XMLAttribute _attr : _prop.getAttributeMap().values()) {
_returnValue.put(_prop.getName().concat(".").concat(_attr.getKey()), _attr.getValue());
}
}
return _returnValue;
}
public List getCategoryNames() {
return new ArrayList(__config.getCategories().keySet());
}
public boolean contains(String key) {
return __config.getDefaultCategory().getProperty(key) != null;
}
public boolean contains(String category, String key) {
XMLConfigFileHandler.XMLCategory _category = __config.getCategory(category);
return _category != null && _category.getProperty(key) != null;
}
}