com.baomidou.framework.spring.MutilPropertyPlaceholderConfigurer.java Source code

Java tutorial

Introduction

Here is the source code for com.baomidou.framework.spring.MutilPropertyPlaceholderConfigurer.java

Source

/**
 * Copyright (c) 2011-2014, hubin (jobob@qq.com).
 *
 * 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.baomidou.framework.spring;

import java.io.IOException;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import com.baomidou.framework.exception.SpringWindException;
import com.baomidou.kisso.common.util.EnvUtil;

/**
 * <p>
 * spring ???????
 * </p>
 * <p>
 * ??? key ? configEnv  spring_runmode<br>
 * online   dev ?  test <br>
 * ???????spring_runmode ??dev <br>
 * ? JVM -D ??-Dspring_runmode=dev <br>
 * </p>
 * 
 * ?????
 * <p>
 * jdbc.password_dev_mode=1230600<br>
 * jdbc.password_test_mode=2001006<br>
 * jdbc.password_online_mode=#!Esd30210<br>
 * </p>
 * 
 * <p>
 * --------------------   ?       -------------------------
 * <bean id="placeholder" class="com.baomidou.mybatisplus.spring.MutilPropertyPlaceholderConfigurer">
 *      <property name="locations">
 *        <list>
 *          <value>classpath:jdbc.properties</value>
 *          <value>classpath*:*-placeholder.properties</value>
 *        </list>
 *      </property>
 * </bean>
 * -------------------------------------------------------
 * </p>
 * <p>
 * --------------------          -------------------------
 *  //1? Controller ? prop.getProperty("xxx") ? properties ?
 *    //   @Autowired
 *   //   protected MutilPropertyPlaceholderConfigurer prop;
 *   <br>
 *  //2? Service  Value ?  properties ?
 *  //   @Value(${'xxxxx'})
 *  //   private String xxx;
 * -------------------------------------------------------
 * </p>
 * 
 * @author hubin
 * @Date 2016-01-27
 */
public class MutilPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {

    /**
     * <p>
     * ?<br>
     * online   dev ?  test 
     * </p>
     */
    private static final String ONLINE = "online";

    private static final String DEV = "dev";

    private static final String TEST = "test";

    /**
     * ??????
     */
    private String configEnv = "spring_runmode";

    private Properties properties;

    /**
     * ???? DEV ??
     * <p>
     * ???????spring_runmode ??dev <br>
     * ? JVM -D ??-Dspring_runmode=dev <br>
     * </p>
     */
    public String getRunMode() {
        String mode = System.getenv(getConfigEnv());
        if (mode == null || "".equals(mode)) {
            mode = System.getProperty(getConfigEnv());
        }
        if (mode != null) {
            if (ONLINE.equals(mode)) {
                mode = ONLINE;
            } else if (DEV.equals(mode)) {
                mode = DEV;
            } else if (TEST.equals(mode)) {
                mode = TEST;
            }
            /**
             *  mode  IP  mode = 30
             * ?  jdb.url_30_mode = xxxxx 
             */
        } else {
            /*  Windows ? */
            if (EnvUtil.isLinux()) {
                mode = ONLINE;
            } else {
                mode = DEV;
            }
        }
        System.err.println("-Dspring_runmode=" + mode + "_mode");
        return mode;
    }

    /**
     * 
     * <p>
     * spring ???  PropertiesLoaderSupport  mergeProperties() ?<br>
     * ????
     * </p>
     * 
     */
    @Override
    protected Properties mergeProperties() throws IOException {
        this.properties = convertMergeProperties(super.mergeProperties());
        return this.properties;
    }

    /**
     * <p>
     * ? prop 
     * </p>
     * @param mergeProperties
     *                spring  Properties 
     * @return
     */
    protected Properties convertMergeProperties(Properties mergeProperties) {
        Properties prop = new Properties();
        String runMode = "_" + getRunMode() + "_mode";
        Set<Entry<Object, Object>> es = mergeProperties.entrySet();
        for (Entry<Object, Object> entry : es) {
            String key = (String) entry.getKey();
            String realKey = key;
            int idx = key.lastIndexOf("_mode");
            if (idx > 0) {
                if (key.contains(runMode)) {
                    realKey = key.substring(0, key.lastIndexOf(runMode));
                } else {
                    /** ?? */
                    realKey = null;
                }
            }
            /**
             * ??<br>
             * ??
             */
            if (realKey != null && !prop.containsKey(realKey)) {
                Object value = null;
                if (idx > 0) {
                    value = mergeProperties.get(realKey + runMode);
                } else {
                    value = mergeProperties.get(realKey);
                }
                if (value != null) {
                    prop.put(realKey, value);
                } else {
                    throw new SpringWindException("impossible empty property for " + realKey);
                }
            }
        }
        return prop;
    }

    /** 
     * 
     * <p>
     * ? 
     * </p>
     *  
     * @param key
     *             
     * @return 
     */
    public String getProperty(String key) {
        return this.resolvePlaceholder(key, this.properties);
    }

    public String getConfigEnv() {
        return configEnv;
    }

    public void setConfigEnv(String configEnv) {
        if (configEnv != null && !"".equals(configEnv)) {
            this.configEnv = configEnv;
        }
    }

}