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