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