<file_sep>package test.com.ctrip.platform.dal.dao.configure.file;

import com.ctrip.platform.dal.dao.configure.DataSourceConfigureConstants;
import com.ctrip.platform.dal.dao.configure.file.PropertyFilePoolPropertiesProvider;
import org.junit.Assert;
import org.junit.Test;

import java.util.Map;

public class PropertyFilePoolPropertiesProviderTest implements DataSourceConfigureConstants {
    @Test
    public void testPropertyFilePoolPropertiesProvider() throws Exception {
        PropertyFilePoolPropertiesProvider provider = new PropertyFilePoolPropertiesProvider();
        Map<String, String> map = provider.getPoolProperties();
        // Custom value
        Assert.assertEquals(map.get(MINIDLE), "1");

        // Default value
        Assert.assertEquals(map.get(MAXACTIVE), String.valueOf(DEFAULT_MAXACTIVE));
    }
}
<file_sep>package com.ctrip.platform.dal.dao.configure.file;

import com.ctrip.platform.dal.dao.configure.DataSourceConfigure;
import com.ctrip.platform.dal.dao.datasource.ConnectionStringChanged;
import com.ctrip.platform.dal.dao.datasource.ConnectionStringProvider;

import java.io.File;
import java.io.FileReader;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class PropertyFileConnectionStringProvider implements ConnectionStringProvider {
    private static final String PROPERTY_FILE_NAME = "database.properties";
    private static final String USER_NAME = ".userName";
    private static final String PASSWORD = <PASSWORD>";
    private static final String CONNECTION_URL = ".connectionUrl";
    private static final String DRIVER_CLASS_NAME = ".driverClassName";

    private Properties properties = new Properties();

    public PropertyFileConnectionStringProvider() {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        if (classLoader == null) {
            classLoader = PropertyFileConnectionStringProvider.class.getClassLoader();
        }

        URL url = classLoader.getResource(PROPERTY_FILE_NAME);
        if (url == null)
            return;

        try {
            properties.load(new FileReader(new File(url.toURI())));
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Map<String, DataSourceConfigure> getConnectionStrings(Set<String> dbNames) throws Exception {
        if (dbNames == null || dbNames.isEmpty())
            return null;

        Map<String, DataSourceConfigure> map = new HashMap<>();
        for (String name : dbNames) {
            DataSourceConfigure configure = new DataSourceConfigure();
            configure.setUserName(properties.getProperty(name + USER_NAME));
            configure.setPassword(properties.getProperty(name + PASSWORD));
            configure.setConnectionUrl(properties.getProperty(name + CONNECTION_URL));
            configure.setDriverClass(properties.getProperty(name + DRIVER_CLASS_NAME));

            map.put(name, configure);
        }

        return map;
    }

    @Override
    public void addConnectionStringChangedListener(String name, ConnectionStringChanged callback) {}

}
<file_sep>package com.ctrip.platform.dal.dao.datasource;

import java.util.Map;

public interface PoolPropertiesProvider {
    Map<String, String> getPoolProperties();

    void addPoolPropertiesChangedListener(final PoolPropertiesChanged callback);
}
<file_sep>package com.ctrip.platform.dal.dao.datasource;

import java.sql.Connection;
import java.util.Map;
import java.util.Set;

import com.ctrip.platform.dal.dao.client.DalConnectionLocator;
import com.ctrip.platform.dal.dao.configure.DataSourceConfigureProvider;
import com.ctrip.platform.dal.dao.configure.DefaultDataSourceConfigureProvider;
import com.ctrip.platform.dal.dao.helper.ConnectionStringKeyHelper;

import javax.sql.DataSource;

public class DefaultDalConnectionLocator implements DalConnectionLocator {
    private static final String DATASOURCE_CONFIG_PROVIDER = "dataSourceConfigureProvider";

    private DataSourceConfigureProvider provider;
    private DataSourceLocator locator;

    @Override
    public void initialize(Map<String, String> settings) throws Exception {
        provider = new DefaultDataSourceConfigureProvider();
        if (settings.containsKey(DATASOURCE_CONFIG_PROVIDER)) {
            provider =
                    (DataSourceConfigureProvider) Class.forName(settings.get(DATASOURCE_CONFIG_PROVIDER)).newInstance();
        }

        provider.initialize(settings);
        locator = new DataSourceLocator(provider);
    }

    @Override
    public void setup(Set<String> dbNames) {
        provider.setup(dbNames);
    }

    @Override
    public Connection getConnection(String name) throws Exception {
        String keyName = ConnectionStringKeyHelper.getKeyName(name);
        DataSource dataSource = locator.getDataSource(keyName);
        return dataSource.getConnection();
    }

}
<file_sep>package com.ctrip.platform.dal.dao.configure;

import com.ctrip.platform.dal.exceptions.DalException;

/**
 * Setup at global level to simplify 
 * 
 * @author jhhe
 *
 */
public interface DatabaseSelector {
    String select(SelectionContext context) throws DalException;
}
<file_sep>package test.com.ctrip.platform.dal.dao.configure.file;

import com.ctrip.platform.dal.dao.configure.DataSourceConfigure;
import com.ctrip.platform.dal.dao.configure.DataSourceConfigureConstants;
import com.ctrip.platform.dal.dao.configure.file.PropertyFileConnectionStringProvider;
import org.junit.Assert;
import org.junit.Test;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class PropertyFileConnectionStringProviderTest implements DataSourceConfigureConstants {
    private static final String NAME = "test";

    @Test
    public void testPropertyFileConnectionStringProvider() throws Exception {
        PropertyFileConnectionStringProvider provider = new PropertyFileConnectionStringProvider();
        Set<String> names = new HashSet<>();
        names.add(NAME);
        Map<String, DataSourceConfigure> map = provider.getConnectionStrings(names);
        DataSourceConfigure configure = map.get(NAME);

        Assert.assertEquals(configure.getUserName(), "testUser");
        Assert.assertEquals(configure.getPassword(), "<PASSWORD>");
        Assert.assertEquals(configure.getConnectionUrl(), "jdbc:mysql://testIP:3306/testDb");
        Assert.assertEquals(configure.getDriverClass(), "com.mysql.jdbc.Driver");
    }
}
