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