DatabaseRecognizer.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. *
  3. */
  4. package com.primeton.dams.utils.template;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.apache.commons.lang.StringUtils;
  7. import org.springframework.beans.factory.InitializingBean;
  8. import javax.sql.DataSource;
  9. import java.sql.Connection;
  10. import java.sql.DatabaseMetaData;
  11. import java.sql.SQLException;
  12. import java.util.HashMap;
  13. import java.util.Iterator;
  14. import java.util.Map;
  15. /**
  16. * 数据库辨认,如将全称的“Microsoft SQL Server”辨认为简称“SQLServer”
  17. * @author user
  18. * @version 2.2 2010-10-21
  19. */
  20. @Slf4j
  21. public class DatabaseRecognizer implements InitializingBean {
  22. /**
  23. * 需要辨认的数据库名称,KEY为全称,VALUE为简称
  24. */
  25. private Map<String,String> databaseNames = new HashMap<String, String>();
  26. /**
  27. * 当前数据库类型
  28. */
  29. String dbType;
  30. /**
  31. * 当前元数据系统所使用的数据库的产品名称,如“Microsoft SQL Server”、“DB2/NT”、“DB2/LINUX”、“DB2/SUN”
  32. */
  33. String currentDatabaseProductName;
  34. /**
  35. * 当前元数据系统所使用的数据库的产品版本
  36. */
  37. String currentDatabaseProductVersion;
  38. @Override
  39. public void afterPropertiesSet() throws Exception {
  40. }
  41. /**
  42. * 获取数据库连接并取得数据库名称,然后选择合适的与数据库相关的Spring配置文件
  43. * @param dataSource dataSource
  44. * @return Spring配置文件名
  45. */
  46. public String determineDatabase(DataSource dataSource) {
  47. String databaseName = null;
  48. try (Connection conn = dataSource.getConnection()){
  49. DatabaseMetaData meta = conn.getMetaData();
  50. databaseName = meta.getDatabaseProductName();
  51. if(databaseName.equals("DM DBMS")){
  52. databaseName = "DM";
  53. }
  54. String productVersion = meta.getDatabaseProductVersion();
  55. if (log.isInfoEnabled()) {
  56. log.info("Current Database is " + databaseName + ", Version is " + productVersion);
  57. }
  58. setCurrentDatabaseProductName(databaseName);
  59. setCurrentDatabaseProductVersion(productVersion);
  60. databaseName = StringUtils.lowerCase(getDatabaseName(databaseName));
  61. this.dbType = databaseName;
  62. } catch (SQLException e) {
  63. String s = "Cannot determine the database brand name: " + databaseName;
  64. log.error(s, e);
  65. throw new IllegalArgumentException(s, e);
  66. }
  67. return databaseName;
  68. }
  69. /**
  70. * 返回数据库的简称
  71. * @param dbFullName 数据库全称
  72. * @return 简称,若没有找到则返回dbFullName
  73. */
  74. public String getDatabaseName(String dbFullName) {
  75. if (databaseNames.containsKey(dbFullName)) {
  76. // 名称全匹配的情况
  77. return databaseNames.get(dbFullName);
  78. }
  79. for (Iterator<String> it = databaseNames.keySet().iterator(); it.hasNext();) {
  80. String key = it.next();
  81. // 忽略大小写,匹配一次
  82. if(StringUtils.equalsIgnoreCase(dbFullName, key)) {
  83. return databaseNames.get(key);
  84. }
  85. // 正则表达式匹配,如DB2的产品名称可能返回不一样,如:DB2/NT, DB2/LINUX, DB2/SUN
  86. if (dbFullName.matches(key)) {
  87. return databaseNames.get(key);
  88. }
  89. }
  90. return dbFullName;
  91. }
  92. /**
  93. * 返回当前元数据系统所使用的数据库的产品名称,如“Microsoft SQL Server”、“DB2/NT”、“DB2/LINUX”
  94. * @return
  95. */
  96. public String getCurrentDatabaseProductName() {
  97. return this.currentDatabaseProductName;
  98. }
  99. /**
  100. * 设置当前元数据系统所使用的数据库的产品名称
  101. * @param currentDatabaseProductName
  102. */
  103. public void setCurrentDatabaseProductName(String currentDatabaseProductName) {
  104. this.currentDatabaseProductName = currentDatabaseProductName;
  105. }
  106. public String getCurrentDatabaseProductVersion() {
  107. return currentDatabaseProductVersion;
  108. }
  109. public void setCurrentDatabaseProductVersion(
  110. String currentDatabaseProductVersion) {
  111. this.currentDatabaseProductVersion = currentDatabaseProductVersion;
  112. }
  113. public Map<String, String> getDatabaseNames() {
  114. return databaseNames;
  115. }
  116. public void setDatabaseNames(Map<String, String> databaseNames) {
  117. this.databaseNames = databaseNames;
  118. }
  119. public String getDbType() {
  120. return dbType;
  121. }
  122. public void setDbType(String dbType) {
  123. this.dbType = dbType;
  124. }
  125. }