001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.dao.jdbc.util;
016    
017    import java.io.PrintWriter;
018    
019    import java.sql.Connection;
020    import java.sql.SQLException;
021    
022    import java.util.logging.Logger;
023    
024    import javax.sql.DataSource;
025    
026    /**
027     * @author Shuyang Zhou
028     */
029    public class DataSourceWrapper implements DataSource {
030    
031            public DataSourceWrapper(DataSource dataSource) {
032                    _dataSource = dataSource;
033            }
034    
035            public Connection getConnection() throws SQLException {
036                    return _dataSource.getConnection();
037            }
038    
039            public Connection getConnection(String username, String password)
040                    throws SQLException {
041    
042                    return _dataSource.getConnection(username, password);
043            }
044    
045            public int getLoginTimeout() throws SQLException {
046                    return _dataSource.getLoginTimeout();
047            }
048    
049            public PrintWriter getLogWriter() throws SQLException {
050                    return _dataSource.getLogWriter();
051            }
052    
053            public Logger getParentLogger() {
054    
055                    // JDK 7
056    
057                    throw new UnsupportedOperationException();
058            }
059    
060            public DataSource getWrappedDataSource() {
061                    return _dataSource;
062            }
063    
064            public boolean isWrapperFor(Class<?> clazz) {
065    
066                    // JDK 6
067    
068                    return DataSource.class.equals(clazz);
069            }
070    
071            public void setLoginTimeout(int seconds) throws SQLException {
072                    _dataSource.setLoginTimeout(seconds);
073            }
074    
075            public void setLogWriter(PrintWriter out) throws SQLException {
076                    _dataSource.setLogWriter(out);
077            }
078    
079            public void setWrappedDataSource(DataSource wrappedDataSource) {
080                    _dataSource = wrappedDataSource;
081            }
082    
083            public <T> T unwrap(Class<T> clazz) throws SQLException {
084    
085                    // JDK 6
086    
087                    if (!DataSource.class.equals(clazz)) {
088                            throw new SQLException("Invalid class " + clazz);
089                    }
090    
091                    return (T)this;
092            }
093    
094            private volatile DataSource _dataSource;
095    
096    }