mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
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.
This commit is contained in:
@@ -44,8 +44,12 @@ public class DataSourceCreator {
|
||||
private List<String> nameList = new ArrayList<String>();
|
||||
|
||||
public DataSourceCreator(String file) {
|
||||
this(file, "");
|
||||
}
|
||||
|
||||
public DataSourceCreator(String file, String propertyPrefix) {
|
||||
loadProperties(file);
|
||||
loadNameList();
|
||||
loadNameList(propertyPrefix);
|
||||
}
|
||||
|
||||
public Map<String, DataSource> createMap(HighAvailableDataSource haDataSource) throws SQLException {
|
||||
@@ -119,10 +123,14 @@ public class DataSourceCreator {
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
private void loadNameList() {
|
||||
private void loadNameList(String propertyPrefix) {
|
||||
Set<String> names = new HashSet<String>();
|
||||
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<String> getNameList() {
|
||||
return nameList;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,6 +88,7 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc
|
||||
private Map<String, DataSource> dataSourceMap = new ConcurrentHashMap<String, DataSource>();
|
||||
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<String, DataSource> dataSourceMap) {
|
||||
if (dataSourceMap != null) {
|
||||
this.dataSourceMap = dataSourceMap;
|
||||
|
||||
Reference in New Issue
Block a user