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.datasource;
018
019import java.io.PrintWriter;
020import java.sql.SQLException;
021import java.util.logging.Logger;
022
023import javax.sql.DataSource;
024
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027
028/**
029 * Abstract base class for Spring's {@link javax.sql.DataSource}
030 * implementations, taking care of the padding.
031 *
032 * <p>'Padding' in the context of this class means default implementations
033 * for certain methods from the {@code DataSource} interface, such as
034 * {@link #getLoginTimeout()}, {@link #setLoginTimeout(int)}, and so forth.
035 *
036 * @author Juergen Hoeller
037 * @since 07.05.2003
038 * @see DriverManagerDataSource
039 */
040public abstract class AbstractDataSource implements DataSource {
041
042        /** Logger available to subclasses. */
043        protected final Log logger = LogFactory.getLog(getClass());
044
045
046        /**
047         * Returns 0, indicating the default system timeout is to be used.
048         */
049        @Override
050        public int getLoginTimeout() throws SQLException {
051                return 0;
052        }
053
054        /**
055         * Setting a login timeout is not supported.
056         */
057        @Override
058        public void setLoginTimeout(int timeout) throws SQLException {
059                throw new UnsupportedOperationException("setLoginTimeout");
060        }
061
062        /**
063         * LogWriter methods are not supported.
064         */
065        @Override
066        public PrintWriter getLogWriter() {
067                throw new UnsupportedOperationException("getLogWriter");
068        }
069
070        /**
071         * LogWriter methods are not supported.
072         */
073        @Override
074        public void setLogWriter(PrintWriter pw) throws SQLException {
075                throw new UnsupportedOperationException("setLogWriter");
076        }
077
078
079        //---------------------------------------------------------------------
080        // Implementation of JDBC 4.0's Wrapper interface
081        //---------------------------------------------------------------------
082
083        @Override
084        @SuppressWarnings("unchecked")
085        public <T> T unwrap(Class<T> iface) throws SQLException {
086                if (iface.isInstance(this)) {
087                        return (T) this;
088                }
089                throw new SQLException("DataSource of type [" + getClass().getName() +
090                                "] cannot be unwrapped as [" + iface.getName() + "]");
091        }
092
093        @Override
094        public boolean isWrapperFor(Class<?> iface) throws SQLException {
095                return iface.isInstance(this);
096        }
097
098
099        //---------------------------------------------------------------------
100        // Implementation of JDBC 4.1's getParentLogger method
101        //---------------------------------------------------------------------
102
103        @Override
104        public Logger getParentLogger() {
105                return Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
106        }
107
108}