001/*
002 * Copyright 2002-2017 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 org.springframework.dao.DataAccessException;
020import org.springframework.lang.Nullable;
021import org.springframework.util.StringUtils;
022
023/**
024 * JavaBean for holding custom JDBC error codes translation for a particular
025 * database. The "exceptionClass" property defines which exception will be
026 * thrown for the list of error codes specified in the errorCodes property.
027 *
028 * @author Thomas Risberg
029 * @since 1.1
030 * @see SQLErrorCodeSQLExceptionTranslator
031 */
032public class CustomSQLErrorCodesTranslation {
033
034        private String[] errorCodes = new String[0];
035
036        @Nullable
037        private Class<?> exceptionClass;
038
039
040        /**
041         * Set the SQL error codes to match.
042         */
043        public void setErrorCodes(String... errorCodes) {
044                this.errorCodes = StringUtils.sortStringArray(errorCodes);
045        }
046
047        /**
048         * Return the SQL error codes to match.
049         */
050        public String[] getErrorCodes() {
051                return this.errorCodes;
052        }
053
054        /**
055         * Set the exception class for the specified error codes.
056         */
057        public void setExceptionClass(@Nullable Class<?> exceptionClass) {
058                if (exceptionClass != null && !DataAccessException.class.isAssignableFrom(exceptionClass)) {
059                        throw new IllegalArgumentException("Invalid exception class [" + exceptionClass +
060                                        "]: needs to be a subclass of [org.springframework.dao.DataAccessException]");
061                }
062                this.exceptionClass = exceptionClass;
063        }
064
065        /**
066         * Return the exception class for the specified error codes.
067         */
068        @Nullable
069        public Class<?> getExceptionClass() {
070                return this.exceptionClass;
071        }
072
073}