001    /**
002     * Copyright (c) 2000-present 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            @Override
036            public Connection getConnection() throws SQLException {
037                    return _dataSource.getConnection();
038            }
039    
040            @Override
041            public Connection getConnection(String username, String password)
042                    throws SQLException {
043    
044                    return _dataSource.getConnection(username, password);
045            }
046    
047            @Override
048            public int getLoginTimeout() throws SQLException {
049                    return _dataSource.getLoginTimeout();
050            }
051    
052            @Override
053            public PrintWriter getLogWriter() throws SQLException {
054                    return _dataSource.getLogWriter();
055            }
056    
057            @Override
058            public Logger getParentLogger() {
059                    throw new UnsupportedOperationException();
060            }
061    
062            public DataSource getWrappedDataSource() {
063                    return _dataSource;
064            }
065    
066            @Override
067            public boolean isWrapperFor(Class<?> clazz) {
068    
069                    // JDK 6
070    
071                    return DataSource.class.equals(clazz);
072            }
073    
074            @Override
075            public void setLoginTimeout(int seconds) throws SQLException {
076                    _dataSource.setLoginTimeout(seconds);
077            }
078    
079            @Override
080            public void setLogWriter(PrintWriter out) throws SQLException {
081                    _dataSource.setLogWriter(out);
082            }
083    
084            public void setWrappedDataSource(DataSource wrappedDataSource) {
085                    _dataSource = wrappedDataSource;
086            }
087    
088            @Override
089            public <T> T unwrap(Class<T> clazz) throws SQLException {
090    
091                    // JDK 6
092    
093                    if (!DataSource.class.equals(clazz)) {
094                            throw new SQLException("Invalid class " + clazz);
095                    }
096    
097                    return (T)this;
098            }
099    
100            private volatile DataSource _dataSource;
101    
102    }