From 65fafdb67aeb546e55c7f1f158a450e714de97b8 Mon Sep 17 00:00:00 2001 From: DigitalSonic Date: Thu, 13 Dec 2018 00:15:47 +0800 Subject: [PATCH] add a special prefix into ha-datasource properties file In order to put HA Datasource properties and other properties altogether into one properties file, HighAvailableDataSource add a new field named propertyPrefix. --- .../druid/pool/ha/DataSourceCreator.java | 18 ++++++++-- .../pool/ha/HighAvailableDataSource.java | 11 +++++- .../druid/pool/ha/DataSourceCreatorTest.java | 35 +++++++++++++++++++ .../ha/ha-with-prefix-datasource.properties | 4 +++ 4 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 src/test/resources/com/alibaba/druid/pool/ha/ha-with-prefix-datasource.properties 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 be1e9b69f1..72eed4c98f 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/DataSourceCreator.java +++ b/src/main/java/com/alibaba/druid/pool/ha/DataSourceCreator.java @@ -44,8 +44,12 @@ public class DataSourceCreator { private List nameList = new ArrayList(); public DataSourceCreator(String file) { + this(file, ""); + } + + public DataSourceCreator(String file, String propertyPrefix) { loadProperties(file); - loadNameList(); + loadNameList(propertyPrefix); } public Map createMap(HighAvailableDataSource haDataSource) throws SQLException { @@ -119,10 +123,14 @@ public class DataSourceCreator { return dataSource; } - private void loadNameList() { + private void loadNameList(String propertyPrefix) { Set names = new HashSet(); for (String n : properties.stringPropertyNames()) { - if (n.contains(".url")) { + if (propertyPrefix != null && !propertyPrefix.isEmpty() + && !n.startsWith(propertyPrefix)) { + continue; + } + if (n.endsWith(".url")) { names.add(n.split("\\.url")[0]); } } @@ -167,4 +175,8 @@ public class DataSourceCreator { LOG.warn("File " + file + " can't be loaded!"); } } + + public List getNameList() { + return nameList; + } } diff --git a/src/main/java/com/alibaba/druid/pool/ha/HighAvailableDataSource.java b/src/main/java/com/alibaba/druid/pool/ha/HighAvailableDataSource.java index b347944293..fc6fa583e7 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/HighAvailableDataSource.java +++ b/src/main/java/com/alibaba/druid/pool/ha/HighAvailableDataSource.java @@ -88,6 +88,7 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc private Map dataSourceMap = new ConcurrentHashMap(); private DataSourceSelector selector = new RandomDataSourceSelector(this); private String dataSourceFile = DEFAULT_DATA_SOURCE_FILE; + private String propertyPrefix = ""; private boolean inited = false; @@ -100,7 +101,7 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc return; } if (dataSourceMap == null || dataSourceMap.isEmpty()) { - dataSourceMap = new DataSourceCreator(dataSourceFile).createMap(this); + dataSourceMap = new DataSourceCreator(dataSourceFile, propertyPrefix).createMap(this); } if (selector == null) { selector = new RandomDataSourceSelector(this); @@ -146,6 +147,14 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc this.dataSourceFile = dataSourceFile; } + public String getPropertyPrefix() { + return propertyPrefix; + } + + public void setPropertyPrefix(String propertyPrefix) { + this.propertyPrefix = propertyPrefix; + } + public void setDataSourceMap(Map dataSourceMap) { if (dataSourceMap != null) { this.dataSourceMap = dataSourceMap; diff --git a/src/test/java/com/alibaba/druid/pool/ha/DataSourceCreatorTest.java b/src/test/java/com/alibaba/druid/pool/ha/DataSourceCreatorTest.java index f3052702b6..76a4dfc08d 100644 --- a/src/test/java/com/alibaba/druid/pool/ha/DataSourceCreatorTest.java +++ b/src/test/java/com/alibaba/druid/pool/ha/DataSourceCreatorTest.java @@ -3,11 +3,29 @@ package com.alibaba.druid.pool.ha; import org.junit.Test; import javax.sql.DataSource; +import java.util.List; import java.util.Map; import static org.junit.Assert.*; public class DataSourceCreatorTest { + @Test + public void testNameList() { + String file = "/com/alibaba/druid/pool/ha/ha-with-prefix-datasource.properties"; + DataSourceCreator creator = new DataSourceCreator(file, "prefix1"); + List list = creator.getNameList(); + assertFalse(list.isEmpty()); + assertEquals(1, list.size()); + assertEquals("prefix1.foo", list.get(0)); + + creator = new DataSourceCreator(file); + list = creator.getNameList(); + assertEquals(3, list.size()); + for (String k : list) { + assertNotEquals("bar", k); + } + } + @Test public void createMap() throws Exception { HighAvailableDataSource highAvailableDataSource = new HighAvailableDataSource(); @@ -19,4 +37,21 @@ public class DataSourceCreatorTest { assertNotNull(map.get("bar")); } + @Test + public void createMapWithPrefix() throws Exception { + HighAvailableDataSource highAvailableDataSource = new HighAvailableDataSource(); + String file = "/com/alibaba/druid/pool/ha/ha-with-prefix-datasource.properties"; + DataSourceCreator creator = new DataSourceCreator(file, "prefix1"); + Map map = creator.createMap(highAvailableDataSource); + assertEquals(1, map.size()); + assertNotNull(map.get("prefix1.foo")); + assertNull(map.get("prefix2.foo")); + + creator = new DataSourceCreator(file, "prefix2"); + map = creator.createMap(highAvailableDataSource); + assertEquals(1, map.size()); + assertNull(map.get("prefix1.foo")); + assertNotNull(map.get("prefix2.foo")); + } + } \ No newline at end of file diff --git a/src/test/resources/com/alibaba/druid/pool/ha/ha-with-prefix-datasource.properties b/src/test/resources/com/alibaba/druid/pool/ha/ha-with-prefix-datasource.properties new file mode 100644 index 0000000000..b008c63d9f --- /dev/null +++ b/src/test/resources/com/alibaba/druid/pool/ha/ha-with-prefix-datasource.properties @@ -0,0 +1,4 @@ +prefix1.foo.url=jdbc:derby:memory:foo1;create=true +prefix2.foo.url=jdbc:derby:memory:foo2;create=true +foo.url=jdbc:derby:memory:foo3;create=true +bar=jdbc:derby:memory:bar;create=true