001/*
002 * Copyright 2002-2012 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.springframework.beans.factory.InitializingBean;
023
024/**
025 * Registry for registering custom {@link org.springframework.jdbc.support.SQLExceptionTranslator}
026 * instances for specific databases.
027 *
028 * @author Thomas Risberg
029 * @since 3.1.1
030 */
031public class CustomSQLExceptionTranslatorRegistrar implements InitializingBean {
032
033        /**
034         * Map registry to hold custom translators specific databases.
035         * Key is the database product name as defined in the
036         * {@link org.springframework.jdbc.support.SQLErrorCodesFactory}.
037         */
038        private final Map<String, SQLExceptionTranslator> translators = new HashMap<String, SQLExceptionTranslator>();
039
040
041        /**
042         * Setter for a Map of {@link SQLExceptionTranslator} references where the key must
043         * be the database name as defined in the {@code sql-error-codes.xml} file.
044         * <p>Note that any existing translators will remain unless there is a match in the
045         * database name, at which point the new translator will replace the existing one.
046         */
047        public void setTranslators(Map<String, SQLExceptionTranslator> translators) {
048                this.translators.putAll(translators);
049        }
050
051        @Override
052        public void afterPropertiesSet() {
053                for (String dbName : this.translators.keySet()) {
054                        CustomSQLExceptionTranslatorRegistry.getInstance().registerTranslator(dbName, this.translators.get(dbName));
055                }
056        }
057
058}