|
|
@@ -1,10 +1,18 @@
|
|
|
package com.uas.report.util;
|
|
|
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileReader;
|
|
|
+import java.io.IOException;
|
|
|
import java.sql.Connection;
|
|
|
import java.sql.PreparedStatement;
|
|
|
import java.sql.ResultSet;
|
|
|
import java.sql.SQLException;
|
|
|
-import java.util.Properties;
|
|
|
+import java.sql.SQLSyntaxErrorException;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Map.Entry;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
|
import javax.sql.DataSource;
|
|
|
|
|
|
@@ -12,7 +20,7 @@ import org.apache.log4j.Logger;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
import com.alibaba.druid.pool.DruidDataSource;
|
|
|
-import com.alibaba.druid.pool.DruidDataSourceFactory;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.uas.report.core.exception.ReportException;
|
|
|
|
|
|
/**
|
|
|
@@ -23,107 +31,185 @@ import com.uas.report.core.exception.ReportException;
|
|
|
*/
|
|
|
public class DataSourceUtils {
|
|
|
|
|
|
+ /**
|
|
|
+ * 数据源
|
|
|
+ */
|
|
|
+ private static Map<String, DruidDataSource> dataSources = new ConcurrentHashMap<>();
|
|
|
+
|
|
|
private static Logger logger = Logger.getLogger(DataSourceUtils.class);
|
|
|
|
|
|
+ public static DataSource getDataSource(String userName, String profile) {
|
|
|
+ if (StringUtils.isEmpty(userName)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ // 需要考虑B2B打印时账套名为B2B/10045740等类似的情况
|
|
|
+ if (!StringUtils.isEmpty(profile)) {
|
|
|
+ userName = userName.split("/")[0] + "." + profile;
|
|
|
+ }
|
|
|
+ DruidDataSource dataSource = dataSources.get(userName);
|
|
|
+ // 如果Map里未存放该数据源,从jdbc配置文件获取数据源
|
|
|
+ if (dataSource == null) {
|
|
|
+ dataSource = getDataSource(ReportConstants.JDBC_FILES_ABSOLUTE_PATH + userName + ".json");
|
|
|
+ // jdbc文件并未配置,则可能是子帐套,需要从主账套表中获取
|
|
|
+ if (dataSource != null) {
|
|
|
+ dataSources.put(userName, dataSource);
|
|
|
+ } else {
|
|
|
+ initAllDataSources();
|
|
|
+ Set<Entry<String, DruidDataSource>> entrySet = dataSources.entrySet();
|
|
|
+ for (Entry<String, DruidDataSource> entry : entrySet) {
|
|
|
+ dataSource = getDataSourceFromMainMaster(entry.getValue(), userName);
|
|
|
+ if (dataSource != null) {
|
|
|
+ dataSources.put(userName, dataSource);
|
|
|
+ return dataSource;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return dataSource;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 初始化所有数据源
|
|
|
+ */
|
|
|
+ private static void initAllDataSources() {
|
|
|
+ File file = new File(ReportConstants.JDBC_FILES_ABSOLUTE_PATH);
|
|
|
+ // 获取所有jdbc配置文件
|
|
|
+ if (file.exists() && file.listFiles().length > 0) {
|
|
|
+ File[] files = file.listFiles();
|
|
|
+ for (File f : files) {
|
|
|
+ String userName = f.getName().replaceAll(".json", "");
|
|
|
+ if (dataSources.get(userName) == null) {
|
|
|
+ DruidDataSource dataSource = getDataSource(f.getPath());
|
|
|
+ if (dataSource != null) {
|
|
|
+ dataSources.put(userName, dataSource);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
- * 根据当前账套用户名,从主数据库master表获取(UAS系统)账套数据库配置信息,作为报表模板的数据源
|
|
|
+ * 从文件中读取jdbc配置,获取数据源
|
|
|
*
|
|
|
- * @param userName
|
|
|
- * 当前账套用户名
|
|
|
+ * @param jdbcFileAbsolutePath
|
|
|
+ * jdbc配置文件
|
|
|
* @return 数据源
|
|
|
*/
|
|
|
- public static DataSource getUASDataSource(String userName) {
|
|
|
- if (StringUtils.isEmpty(userName)) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- // 默认数据源(主数据库)
|
|
|
- DruidDataSource defaultDataSource = ContextUtils.getApplicationContext().getBean("defaultSob",
|
|
|
- DruidDataSource.class);
|
|
|
- if (defaultDataSource == null) {
|
|
|
+ public static DruidDataSource getDataSource(String jdbcFileAbsolutePath) {
|
|
|
+ if (StringUtils.isEmpty(jdbcFileAbsolutePath)) {
|
|
|
return null;
|
|
|
}
|
|
|
- // 若当前账套用户名为默认数据库用户名
|
|
|
- if (userName.equals(defaultDataSource.getUsername())) {
|
|
|
- return defaultDataSource;
|
|
|
+ File file = new File(jdbcFileAbsolutePath);
|
|
|
+ if (file.exists()) {
|
|
|
+ BufferedReader bufferedReader = null;
|
|
|
+ try {
|
|
|
+ bufferedReader = new BufferedReader(new FileReader(file));
|
|
|
+ StringBuilder stringBuilder = new StringBuilder();
|
|
|
+ String line = "";
|
|
|
+ while (!StringUtils.isEmpty(line = bufferedReader.readLine())) {
|
|
|
+ stringBuilder.append(line);
|
|
|
+ }
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(stringBuilder.toString());
|
|
|
+ DruidDataSource dataSource = new DruidDataSource();
|
|
|
+ dataSource.setDriverClassName(jsonObject.getString("driverClassName"));
|
|
|
+ dataSource.setUrl(jsonObject.getString("url"));
|
|
|
+ dataSource.setUsername(jsonObject.getString("username"));
|
|
|
+ dataSource.setPassword(jsonObject.getString("password"));
|
|
|
+ dataSource.setInitialSize(jsonObject.getIntValue("initialSize"));
|
|
|
+ dataSource.setMinIdle(jsonObject.getIntValue("minIdle"));
|
|
|
+ dataSource.setMaxActive(jsonObject.getIntValue("maxActive"));
|
|
|
+ dataSource.setMaxWait(jsonObject.getLongValue("maxWait"));
|
|
|
+ dataSource.setTimeBetweenEvictionRunsMillis(jsonObject.getLongValue("timeBetweenEvictionRunsMillis"));
|
|
|
+ dataSource.setMinEvictableIdleTimeMillis(jsonObject.getLongValue("minEvictableIdleTimeMillis"));
|
|
|
+ dataSource.setValidationQuery(jsonObject.getString("validationQuery"));
|
|
|
+ dataSource.setTestWhileIdle(jsonObject.getBooleanValue("testWhileIdle"));
|
|
|
+ dataSource.setTestOnBorrow(jsonObject.getBooleanValue("testOnBorrow"));
|
|
|
+ dataSource.setTestOnReturn(jsonObject.getBooleanValue("testOnReturn"));
|
|
|
+ dataSource.setRemoveAbandoned(jsonObject.getBooleanValue("removeAbandoned"));
|
|
|
+ dataSource.setRemoveAbandoned(jsonObject.getBooleanValue("removeAbandonedTimeout"));
|
|
|
+ dataSource.setLogAbandoned(jsonObject.getBooleanValue("logAbandoned"));
|
|
|
+ dataSource.setPoolPreparedStatements(jsonObject.getBooleanValue("poolPreparedStatements"));
|
|
|
+ dataSource.setMaxPoolPreparedStatementPerConnectionSize(
|
|
|
+ jsonObject.getIntValue("maxPoolPreparedStatementPerConnectionSize"));
|
|
|
+ dataSource.setFilters(jsonObject.getString("filters"));
|
|
|
+ return dataSource;
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ logger.error("druid configuration initialization filter");
|
|
|
+ } finally {
|
|
|
+ if (bufferedReader != null) {
|
|
|
+ try {
|
|
|
+ bufferedReader.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
+ return null;
|
|
|
+ }
|
|
|
|
|
|
+ /**
|
|
|
+ * 从主账套的MA_USER表中获取子账套数据源
|
|
|
+ *
|
|
|
+ * @param mainMaster
|
|
|
+ * @param userName
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private static DruidDataSource getDataSourceFromMainMaster(DruidDataSource mainMaster, String userName) {
|
|
|
Connection connection = null;
|
|
|
PreparedStatement preparedStatement = null;
|
|
|
ResultSet resultSet = null;
|
|
|
try {
|
|
|
- logger.info("defaultDataSource.getConnection...");
|
|
|
- connection = defaultDataSource.getConnection();
|
|
|
- logger.info("defaultDataSource.getConnection done...");
|
|
|
+ logger.info("mainMaster.getConnection...");
|
|
|
+ connection = mainMaster.getConnection();
|
|
|
+ logger.info("mainMaster.getConnection done...");
|
|
|
// 根据当前账套用户名获取其数据库配置信息
|
|
|
String sql = "select * from master where MA_USER = ?";
|
|
|
preparedStatement = connection.prepareStatement(sql);
|
|
|
preparedStatement.setString(1, userName);
|
|
|
- resultSet = preparedStatement.executeQuery();
|
|
|
+ try {
|
|
|
+ resultSet = preparedStatement.executeQuery();
|
|
|
+ } catch (SQLSyntaxErrorException e) {
|
|
|
+ // 并不是每个数据源都有MA_USER表,因此,出现异常,返回空,紧接着遍历下一个数据源
|
|
|
+ return null;
|
|
|
+ }
|
|
|
if (resultSet.next()) {
|
|
|
String password = resultSet.getString("MS_PWD");
|
|
|
if (!StringUtils.isEmpty(password)) {
|
|
|
- Properties properties = new Properties();
|
|
|
- properties.put("driverClassName", defaultDataSource.getDriverClassName());
|
|
|
- properties.put("url", defaultDataSource.getUrl());
|
|
|
- properties.put("username", userName);
|
|
|
- properties.put("password", password);
|
|
|
- properties.put("testWhileIdle", "true");
|
|
|
- properties.put("validationQuery", "select 1 from SYS.DUAL");
|
|
|
- logger.info("DruidDataSourceFactory.createDataSource...");
|
|
|
- return DruidDataSourceFactory.createDataSource(properties);
|
|
|
+ // 除了用户名、密码,其他属性一样
|
|
|
+ DruidDataSource result = mainMaster.cloneDruidDataSource();
|
|
|
+ result.setUsername(userName);
|
|
|
+ result.setPassword(password);
|
|
|
+ return result;
|
|
|
}
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
throw new ReportException(e).setDetailedMessage(e);
|
|
|
} finally {
|
|
|
- try {
|
|
|
- if (resultSet != null) {
|
|
|
+ if (resultSet != null) {
|
|
|
+ try {
|
|
|
resultSet.close();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
- } catch (SQLException e) {
|
|
|
- e.printStackTrace();
|
|
|
}
|
|
|
- try {
|
|
|
- if (preparedStatement != null) {
|
|
|
+ if (preparedStatement != null) {
|
|
|
+ try {
|
|
|
preparedStatement.close();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
- } catch (SQLException e) {
|
|
|
- e.printStackTrace();
|
|
|
}
|
|
|
- try {
|
|
|
- if (connection != null) {
|
|
|
+ if (connection != null) {
|
|
|
+ try {
|
|
|
connection.close();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
- } catch (SQLException e) {
|
|
|
- throw new ReportException(e).setDetailedMessage(e);
|
|
|
}
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
-
|
|
|
- /**
|
|
|
- * 根据当前账套用户名和profile配置,获取相应B2C、B2B数据源
|
|
|
- *
|
|
|
- * @param userName
|
|
|
- * 当前账套用户名
|
|
|
- * @param profile
|
|
|
- * 用于标识请求源(B2C、B2B)是正式、测试还是开发版本:prod、test、dev
|
|
|
- * @return 数据源
|
|
|
- */
|
|
|
- public static DataSource getPlatformDataSource(String userName, String profile) {
|
|
|
- // 如果userName是B2C或B2B,直接获取配置好的B2C数据源(B2B数据源与B2C相同)
|
|
|
- if (userName.toUpperCase().startsWith("B2C") || userName.toUpperCase().startsWith("B2B")) {
|
|
|
- if (profile.equalsIgnoreCase("dev")) {
|
|
|
- return ContextUtils.getApplicationContext().getBean("platformDevDataSource", DruidDataSource.class);
|
|
|
- } else if (profile.equalsIgnoreCase("test")) {
|
|
|
- return ContextUtils.getApplicationContext().getBean("platformTestDataSource", DruidDataSource.class);
|
|
|
- } else if (profile.equalsIgnoreCase("prod")) {
|
|
|
- return ContextUtils.getApplicationContext().getBean("platformProdDataSource", DruidDataSource.class);
|
|
|
- } else {
|
|
|
- throw new ReportException("profile只能为:dev、test或prod,不可为" + profile);
|
|
|
- }
|
|
|
- } else {
|
|
|
- throw new ReportException("暂时不支持" + userName);
|
|
|
- }
|
|
|
- }
|
|
|
}
|