All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.javaclub.sso.common.SSOProperties Maven / Gradle / Ivy

/**
 * Copyright (c) 2011-2018.
 *
 * 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.github.javaclub.sso.common;

import java.util.Map.Entry;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.logging.Logger;

import com.github.javaclub.sso.common.util.Utils;

/**
 * 

* Properties 辅助工具类 *

*/ public class SSOProperties { private static final Logger logger = Logger.getLogger("SSOProperties"); private final Properties properties; public SSOProperties(Properties properties) { this.properties = properties; } public SSOProperties(Properties mergeProperties, String runMode) { this.properties = extractRunMode(mergeProperties, runMode); } public SSOProperties(Properties mergeProperties, String runMode, String currentMode) { this.properties = extractRunMode(mergeProperties, runMode, currentMode); } public String get(String key) { String val = properties.getProperty(key); return null == val ? null : val.trim(); } /** * properties 提取当前模式配置 * -------------------- * dev_mode 开发模式 * test_mode 测试模式 * online_mode 生产模式 * -------------------- * eclipse 开发模式配置,启动参数 Arguments 属性 VM * arguments 设置 -Dsso.run.mode=dev_mode */ public static Properties extractRunMode( Properties prop, String runMode ) { return extractRunMode(prop, runMode, "online_mode"); } /** *

* properties 提取当前模式配置 *

* * @param prop * 配置文件 Properties * @param runMode * 当前配置模式 * @param defaultMode * 默认模式 * @return */ public static Properties extractRunMode( Properties prop, String runMode, String defaultMode ) { if ( prop == null || runMode == null || defaultMode == null ) { return null; } /** * 获取路由规则, 系统属性设置mode优先 */ Properties properties = new Properties(); String mode = System.getProperty(runMode); if ( mode == null ) { String str = prop.getProperty(runMode); mode = (str != null) ? str : defaultMode; } logger.info("sso.run.mode=" + mode); properties.put(runMode, mode); Set> es = prop.entrySet(); for ( Entry entry : es ) { String key = (String) entry.getKey(); int idx = key.lastIndexOf("_mode"); String realKey = key; if ( idx > 0 ) { if ( key.contains(mode) ) { realKey = key.substring(0, key.lastIndexOf("_" + mode)); } else { realKey = null; } } if ( realKey != null && !properties.containsKey(realKey) ) { Object value = null; if ( idx > 0 ) { value = prop.get(realKey + "_" + mode); } else { value = prop.get(realKey); } if ( value != null ) { properties.put(realKey, value); } else { throw new RuntimeException("impossible empty property for " + realKey); } } } return properties; } public void overrideMerge(Properties props) { Set> es = props.entrySet(); for ( Entry entry : es ) { properties.put(entry.getKey(), entry.getValue()); } } public String get( String key, String defaultVal ) { String val = this.get(key); return val == null ? defaultVal : val.trim(); } public String findValue( String... keys ) { for ( String key : keys ) { String value = this.get(key); if ( value != null ) { return value.trim(); } } return null; } public boolean getBoolean( String key, boolean defaultVal ) { String val = this.get(key); return val == null ? defaultVal : Boolean.parseBoolean(val.trim()); } public long getLong( String key, long defaultVal ) { String val = this.get(key); return val == null ? defaultVal : Long.parseLong(val.trim()); } public int getInt( String key, int defaultVal ) { return (int) getLong(key, defaultVal); } public double getDouble( String key, double defaultVal ) { String val = this.get(key); return val == null ? defaultVal : Double.parseDouble(val.trim()); } public > T getEnum( String key, Class type, T defaultValue ) { String val = this.get(key); return val == null ? defaultValue : Enum.valueOf(type, val.trim()); } public Properties getProperties() { return properties; } @Override public String toString() { StringBuilder sbf = new StringBuilder(); if(null != this.properties) { Map map = Utils.toTreeMap(this.properties); for (Map.Entry entry : map.entrySet()) { sbf.append(entry.getKey()).append(" = ").append(entry.getValue()).append("\n"); } } return "SSOProperties [properties=" + sbf.toString() + "]"; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy