diff --git a/src/main/java/com/alibaba/druid/pool/ha/DataSourceCreator.java b/src/main/java/com/alibaba/druid/pool/ha/DataSourceCreator.java index aad52ed234..bfcaa1f73a 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/DataSourceCreator.java +++ b/src/main/java/com/alibaba/druid/pool/ha/DataSourceCreator.java @@ -15,16 +15,10 @@ */ package com.alibaba.druid.pool.ha; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.InputStream; import java.sql.SQLException; -import java.util.ArrayList; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Properties; -import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import javax.sql.DataSource; @@ -41,19 +35,19 @@ import com.alibaba.druid.support.logging.LogFactory; public class DataSourceCreator { private final static Log LOG = LogFactory.getLog(DataSourceCreator.class); - private Properties properties = new Properties(); - private List nameList = new ArrayList(); + private Properties properties; + private List nameList; public DataSourceCreator(String file) { this(file, ""); } public DataSourceCreator(String file, String propertyPrefix) { - loadProperties(file); - loadNameList(propertyPrefix); + this.properties = PropertiesUtils.loadProperties(file); + this.nameList = PropertiesUtils.loadNameList(this.properties, propertyPrefix); } - public Map createMap(HighAvailableDataSource haDataSource) throws SQLException { + public Map createMap(HighAvailableDataSource haDataSource) { Map map = new ConcurrentHashMap(); if (nameList == null || nameList.isEmpty()) { @@ -136,59 +130,6 @@ public class DataSourceCreator { return dataSource; } - private void loadNameList(String propertyPrefix) { - Set names = new HashSet(); - for (String n : properties.stringPropertyNames()) { - if (propertyPrefix != null && !propertyPrefix.isEmpty() - && !n.startsWith(propertyPrefix)) { - continue; - } - if (n.endsWith(".url")) { - names.add(n.split("\\.url")[0]); - } - } - if (!names.isEmpty()) { - nameList.addAll(names); - } - } - - private void loadProperties(String file) { - Properties properties = new Properties(); - if (file == null) { - return; - } - InputStream is = null; - try { - LOG.debug("Trying to load " + file + " from FileSystem."); - is = new FileInputStream(file); - } catch(FileNotFoundException e) { - LOG.debug("Trying to load " + file + " from Classpath."); - try { - is = DataSourceCreator.class.getResourceAsStream(file); - } catch (Exception ex) { - LOG.warn("Can not load resource " + file, ex); - } - } - if (is != null) { - try { - properties.load(is); - this.properties = properties; - } catch(Exception e) { - LOG.error("Exception occurred while loading " + file, e); - } finally { - if (is != null) { - try { - is.close(); - } catch (Exception e) { - LOG.debug("Can not close Inputstream.", e); - } - } - } - } else { - LOG.warn("File " + file + " can't be loaded!"); - } - } - public List getNameList() { return nameList; } diff --git a/src/main/java/com/alibaba/druid/pool/ha/PropertiesUtils.java b/src/main/java/com/alibaba/druid/pool/ha/PropertiesUtils.java new file mode 100644 index 0000000000..337a589427 --- /dev/null +++ b/src/main/java/com/alibaba/druid/pool/ha/PropertiesUtils.java @@ -0,0 +1,101 @@ +/* + * Copyright 1999-2020 Alibaba Group Holding Ltd. + * + * 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.alibaba.druid.pool.ha; + +import com.alibaba.druid.support.logging.Log; +import com.alibaba.druid.support.logging.LogFactory; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Properties; +import java.util.Set; + +/** + * Utilities for Properties. + * + * @author DigitalSonic + */ +public class PropertiesUtils { + private final static Log LOG = LogFactory.getLog(PropertiesUtils.class); + + /** + * Load properties from the given file into Properties. + */ + public static Properties loadProperties(String file) { + Properties properties = new Properties(); + if (file == null) { + return properties; + } + + InputStream is = null; + try { + LOG.debug("Trying to load " + file + " from FileSystem."); + is = new FileInputStream(file); + } catch(FileNotFoundException e) { + LOG.debug("Trying to load " + file + " from Classpath."); + try { + is = PropertiesUtils.class.getResourceAsStream(file); + } catch (Exception ex) { + LOG.warn("Can not load resource " + file, ex); + } + } + if (is != null) { + try { + properties.load(is); + } catch(Exception e) { + LOG.error("Exception occurred while loading " + file, e); + } finally { + if (is != null) { + try { + is.close(); + } catch (Exception e) { + LOG.debug("Can not close Inputstream.", e); + } + } + } + } else { + LOG.warn("File " + file + " can't be loaded!"); + } + + return properties; + } + + /** + * Pick the name of a JDBC url. Such as xxx.url, xxx is the name. + */ + public static List loadNameList(Properties properties, String propertyPrefix) { + List nameList = new ArrayList(); + + Set names = new HashSet(); + for (String n : properties.stringPropertyNames()) { + if (propertyPrefix != null && !propertyPrefix.isEmpty() + && !n.startsWith(propertyPrefix)) { + continue; + } + if (n.endsWith(".url")) { + names.add(n.split("\\.url")[0]); + } + } + if (!names.isEmpty()) { + nameList.addAll(names); + } + return nameList; + } +}