1. /*
  2. * Copyright 2002-2004 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.jdbc.support;
  17. import java.sql.DatabaseMetaData;
  18. import java.sql.SQLException;
  19. import java.util.Arrays;
  20. import java.util.HashMap;
  21. import java.util.Map;
  22. import javax.sql.DataSource;
  23. import org.apache.commons.logging.Log;
  24. import org.apache.commons.logging.LogFactory;
  25. import org.springframework.beans.factory.BeanDefinitionStoreException;
  26. import org.springframework.beans.factory.ListableBeanFactory;
  27. import org.springframework.beans.factory.xml.XmlBeanFactory;
  28. import org.springframework.core.io.ClassPathResource;
  29. import org.springframework.core.io.Resource;
  30. /**
  31. * Factory for creating SQLErrorCodes based on the
  32. * "databaseProductName" taken from the DatabaseMetaData.
  33. *
  34. * <p>Returns SQLErrorCodes populated with vendor codes
  35. * defined in a configuration file named "sql-error-codes.xml".
  36. * Reads the default file in this package if not overridden by a file in
  37. * the root of the class path (e.g. in the "/WEB-INF/classes" directory).
  38. *
  39. * @author Thomas Risberg
  40. * @author Rod Johnson
  41. * @author Juergen Hoeller
  42. * @version $Id: SQLErrorCodesFactory.java,v 1.15 2004/05/30 15:14:26 jhoeller Exp $
  43. * @see java.sql.DatabaseMetaData#getDatabaseProductName
  44. */
  45. public class SQLErrorCodesFactory {
  46. protected static final Log logger = LogFactory.getLog(SQLErrorCodesFactory.class);
  47. /**
  48. * Name of custom SQL error codes file, loading from the root
  49. * of the class path (e.g. in the WEB-INF/classes directory).
  50. */
  51. public static final String SQL_ERROR_CODE_OVERRIDE_PATH = "sql-error-codes.xml";
  52. /**
  53. * Name of default SQL error code files, loading from the class path.
  54. */
  55. public static final String SQL_ERROR_CODE_DEFAULT_PATH = "org/springframework/jdbc/support/sql-error-codes.xml";
  56. /**
  57. * Keep track of a single instance so we can return it to classes that request it.
  58. */
  59. private static final SQLErrorCodesFactory instance = new SQLErrorCodesFactory();
  60. /**
  61. * Return singleton instance.
  62. */
  63. public static SQLErrorCodesFactory getInstance() {
  64. return instance;
  65. }
  66. /**
  67. * Map to hold database product name retrieved from database metadata.
  68. * Key is the DataSource, value is the database product name.
  69. */
  70. private final Map dataSourceProductName = new HashMap(10);
  71. /**
  72. * Map to hold error codes for all databases defined in the config file.
  73. * Key is the database product name, value is the SQLErrorCodes instance.
  74. */
  75. private final Map rdbmsErrorCodes;
  76. /**
  77. * Not public to enforce Singleton design pattern.
  78. * Would be private except to allow testing via overriding
  79. * the loadResource method.
  80. * <b>Do not subclass in application code.</b>
  81. * @see #loadResource
  82. */
  83. protected SQLErrorCodesFactory() {
  84. Map errorCodes = null;
  85. try {
  86. String path = SQL_ERROR_CODE_OVERRIDE_PATH;
  87. Resource resource = loadResource(path);
  88. if (resource == null || !resource.exists()) {
  89. path = SQL_ERROR_CODE_DEFAULT_PATH;
  90. resource = loadResource(path);
  91. if (resource == null || !resource.exists()) {
  92. throw new BeanDefinitionStoreException("Unable to locate file [" + SQL_ERROR_CODE_DEFAULT_PATH + "]");
  93. }
  94. }
  95. ListableBeanFactory bf = new XmlBeanFactory(resource);
  96. String[] rdbmsNames = bf.getBeanDefinitionNames(SQLErrorCodes.class);
  97. errorCodes = new HashMap(rdbmsNames.length);
  98. for (int i = 0; i < rdbmsNames.length; i++) {
  99. SQLErrorCodes ec = (SQLErrorCodes) bf.getBean(rdbmsNames[i]);
  100. if (ec.getBadSqlGrammarCodes() == null) {
  101. ec.setBadSqlGrammarCodes(new String[0]);
  102. }
  103. else {
  104. Arrays.sort(ec.getBadSqlGrammarCodes());
  105. }
  106. if (ec.getDataIntegrityViolationCodes() == null) {
  107. ec.setDataIntegrityViolationCodes(new String[0]);
  108. }
  109. else {
  110. Arrays.sort(ec.getDataIntegrityViolationCodes());
  111. }
  112. if (ec.getDatabaseProductName() == null) {
  113. errorCodes.put(rdbmsNames[i], ec);
  114. }
  115. else {
  116. errorCodes.put(ec.getDatabaseProductName(), ec);
  117. }
  118. }
  119. logger.info("SQLErrorCodes loaded: " + errorCodes.keySet());
  120. }
  121. catch (BeanDefinitionStoreException ex) {
  122. logger.warn("Error loading error codes from config file. Message: " + ex.getMessage());
  123. errorCodes = new HashMap(0);
  124. }
  125. this.rdbmsErrorCodes = errorCodes;
  126. }
  127. /**
  128. * Protected for testability. Load the given resource from the class path.
  129. * @param path resource path. SQL_ERROR_CODE_DEFAULT_PATH or
  130. * SQL_ERROR_CODE_OVERRIDE_PATH.
  131. * <b>Not to be overriden by application developers, who should obtain instances
  132. * of this class from the static getInstance() method.</b>
  133. * @return the input stream or null if the resource wasn't found
  134. * @see #getInstance
  135. */
  136. protected Resource loadResource(String path) {
  137. return new ClassPathResource(path);
  138. }
  139. /**
  140. * Return SQLErrorCodes for the given DataSource,
  141. * evaluating databaseProductName from DatabaseMetaData,
  142. * or an empty error codes instance if no SQLErrorCodes were found.
  143. * @see java.sql.DatabaseMetaData#getDatabaseProductName
  144. */
  145. public SQLErrorCodes getErrorCodes(DataSource ds) {
  146. logger.info("Looking up default SQLErrorCodes for DataSource");
  147. // Let's avoid looking up database product info if we can.
  148. String dataSourceDbName = (String) this.dataSourceProductName.get(ds);
  149. if (dataSourceDbName != null) {
  150. logger.info("Database product name found in cache for DataSource [" +
  151. ds + "]. Name is '" + dataSourceDbName + "'.");
  152. return getErrorCodes(dataSourceDbName);
  153. }
  154. // We could not find it - got to look it up.
  155. try {
  156. Map dbmdInfo = (Map) JdbcUtils.extractDatabaseMetaData(ds, new DatabaseMetaDataCallback() {
  157. public Object processMetaData(DatabaseMetaData dbmd) throws SQLException {
  158. Map info = new HashMap(2);
  159. if (dbmd != null) {
  160. info.put("DatabaseProductName", dbmd.getDatabaseProductName());
  161. info.put("DriverVersion", dbmd.getDriverVersion());
  162. }
  163. return info;
  164. }
  165. });
  166. if (dbmdInfo != null) {
  167. // should always be the case outside of test environments
  168. String dbName = (String) dbmdInfo.get("DatabaseProductName");
  169. String driverVersion = (String) dbmdInfo.get("DriverVersion");
  170. // special check for DB2
  171. if (dbName != null && dbName.startsWith("DB2/")) {
  172. dbName = "DB2";
  173. }
  174. if (dbName != null) {
  175. this.dataSourceProductName.put(ds, dbName);
  176. logger.info("Database Product Name is " + dbName);
  177. logger.info("Driver Version is " + driverVersion);
  178. SQLErrorCodes sec = (SQLErrorCodes) this.rdbmsErrorCodes.get(dbName);
  179. if (sec != null) {
  180. return sec;
  181. }
  182. logger.info("Error Codes for " + dbName + " not found");
  183. }
  184. }
  185. }
  186. catch (MetaDataAccessException ex) {
  187. logger.warn("Error while getting database metadata", ex);
  188. }
  189. // fallback is to return an empty ErrorCodes instance
  190. return new SQLErrorCodes();
  191. }
  192. /**
  193. * Return SQLErrorCodes instance for the given database.
  194. * No need for a database metadata lookup.
  195. */
  196. public SQLErrorCodes getErrorCodes(String dbName) {
  197. SQLErrorCodes sec = (SQLErrorCodes) this.rdbmsErrorCodes.get(dbName);
  198. if (sec == null) {
  199. // could not find the database among the defined ones
  200. sec = new SQLErrorCodes();
  201. }
  202. return sec;
  203. }
  204. }