001/* 002 * Copyright 2002-2019 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.jdbc.support; 018 019import java.util.HashMap; 020import java.util.Map; 021 022import org.apache.commons.logging.Log; 023import org.apache.commons.logging.LogFactory; 024 025/** 026 * Registry for custom {@link org.springframework.jdbc.support.SQLExceptionTranslator} instances associated with 027 * specific databases allowing for overriding translation based on values contained in the configuration file 028 * named "sql-error-codes.xml". 029 * 030 * @author Thomas Risberg 031 * @since 3.1.1 032 * @see SQLErrorCodesFactory 033 */ 034public class CustomSQLExceptionTranslatorRegistry { 035 036 private static final Log logger = LogFactory.getLog(CustomSQLExceptionTranslatorRegistry.class); 037 038 /** 039 * Keep track of a single instance so we can return it to classes that request it. 040 */ 041 private static final CustomSQLExceptionTranslatorRegistry instance = new CustomSQLExceptionTranslatorRegistry(); 042 043 044 /** 045 * Return the singleton instance. 046 */ 047 public static CustomSQLExceptionTranslatorRegistry getInstance() { 048 return instance; 049 } 050 051 052 /** 053 * Map registry to hold custom translators specific databases. 054 * Key is the database product name as defined in the 055 * {@link org.springframework.jdbc.support.SQLErrorCodesFactory}. 056 */ 057 private final Map<String, SQLExceptionTranslator> translatorMap = new HashMap<String, SQLExceptionTranslator>(); 058 059 060 /** 061 * Create a new instance of the {@link CustomSQLExceptionTranslatorRegistry} class. 062 * <p>Not public to enforce Singleton design pattern. 063 */ 064 private CustomSQLExceptionTranslatorRegistry() { 065 } 066 067 /** 068 * Register a new custom translator for the specified database name. 069 * @param dbName the database name 070 * @param translator the custom translator 071 */ 072 public void registerTranslator(String dbName, SQLExceptionTranslator translator) { 073 SQLExceptionTranslator replaced = this.translatorMap.put(dbName, translator); 074 if (logger.isInfoEnabled()) { 075 if (replaced != null) { 076 logger.info("Replacing custom translator [" + replaced + "] for database '" + dbName + 077 "' with [" + translator + "]"); 078 } 079 else { 080 logger.info("Adding custom translator of type [" + translator.getClass().getName() + 081 "] for database '" + dbName + "'"); 082 } 083 } 084 } 085 086 /** 087 * Find a custom translator for the specified database. 088 * @param dbName the database name 089 * @return the custom translator, or {@code null} if none found 090 */ 091 public SQLExceptionTranslator findTranslatorForDatabase(String dbName) { 092 return this.translatorMap.get(dbName); 093 } 094 095}