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

Java tutorial

Introduction

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

Source

/**
 * Copyright (c) 2011-2014, jiazhang (676002187@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.File;
import java.io.FilenameFilter;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;

/**
 * java.exe?-Dsw.home=D:/development???*.properties. ?properties?:
 * <ol>
 * <li>?---D:/development/jdbc.properties</li>
 * <li>---D:/test/jdbc.properties</li>
 * <li>---D:/online/jdbc.properties</li>
 * </ol>
 * 
 * <p>
 * --------------------   ?       -------------------------
 * <bean class="com.baomidou.framework.spring.SWPropertyPlaceholderConfigurer">
 *     <property name="swDeployConfigPathKey" value="sw.home" />
 * </bean>
 * -------------------------------------------------------
 * </p>
 * 
 * "war+properties?"??.
 * 
 * @author jiazhang
 * @Date 2016-05-16
 */
public class SWPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
    private static final String PROPERTIES_SUFFX = ".properties";

    /**
     * Java.exe-Dsw.home=D:/development?.
     * 
     * spring? <property name="swDeployConfigPathKey" value="sw.home"/>.
     * 
     * @param propertiesPath
     *            JVM?-Dsw.home=D:/developmentkey, ?sw.home
     */
    public void setSwDeployConfigPathKey(String propertiesPath) {
        String configFilePath = System.getProperty(propertiesPath);
        if (configFilePath == null)
            throw new IllegalArgumentException(propertiesPath
                    + "null, ?java.exe?, ?-D"
                    + propertiesPath + "=D:/xxx");
        try {
            // 
            File file = new File(configFilePath);
            if (!file.exists() || !file.isDirectory())
                throw new IllegalArgumentException(propertiesPath + "" + configFilePath
                        + ", ???");
            // 
            File[] listPropertiesFiles = file.listFiles(new FilenameFilter() {
                @Override
                public boolean accept(File dir, String name) {
                    if (name.endsWith(PROPERTIES_SUFFX)) {
                        return true;
                    }
                    return false;
                }
            });
            // UrlResource?
            List<UrlResource> listResource = null;
            if (listPropertiesFiles != null && listPropertiesFiles.length > 0) {
                listResource = new ArrayList<UrlResource>();
                for (File propertiesFile : listPropertiesFiles) {
                    URI uri = propertiesFile.toURI();
                    UrlResource urlResource = new UrlResource(uri);
                    listResource.add(urlResource);
                }
            }
            // 
            if (listResource != null) {
                this.setLocations((Resource[]) listResource.toArray(new UrlResource[listResource.size()]));
            }
        } catch (Exception e) {
            throw new IllegalArgumentException(e);
        }
    }
}