mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
extract some Properties related method into Utils
This commit is contained in:
@@ -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<String> nameList = new ArrayList<String>();
|
||||
private Properties properties;
|
||||
private List<String> 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<String, DataSource> createMap(HighAvailableDataSource haDataSource) throws SQLException {
|
||||
public Map<String, DataSource> createMap(HighAvailableDataSource haDataSource) {
|
||||
Map<String, DataSource> map = new ConcurrentHashMap<String, DataSource>();
|
||||
|
||||
if (nameList == null || nameList.isEmpty()) {
|
||||
@@ -136,59 +130,6 @@ public class DataSourceCreator {
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
private void loadNameList(String propertyPrefix) {
|
||||
Set<String> names = new HashSet<String>();
|
||||
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<String> getNameList() {
|
||||
return nameList;
|
||||
}
|
||||
|
||||
@@ -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<String> loadNameList(Properties properties, String propertyPrefix) {
|
||||
List<String> nameList = new ArrayList<String>();
|
||||
|
||||
Set<String> names = new HashSet<String>();
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user