001    /**
002     * Copyright (c) 2000-2013 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 com.liferay.portal.tools.javadocformatter.SinceJava;
018    
019    import java.io.PrintWriter;
020    
021    import java.sql.Connection;
022    import java.sql.SQLException;
023    
024    import java.util.logging.Logger;
025    
026    import javax.sql.DataSource;
027    
028    /**
029     * @author Shuyang Zhou
030     */
031    public class DataSourceWrapper implements DataSource {
032    
033            public DataSourceWrapper(DataSource dataSource) {
034                    _dataSource = dataSource;
035            }
036    
037            @Override
038            public Connection getConnection() throws SQLException {
039                    return _dataSource.getConnection();
040            }
041    
042            @Override
043            public Connection getConnection(String username, String password)
044                    throws SQLException {
045    
046                    return _dataSource.getConnection(username, password);
047            }
048    
049            @Override
050            public int getLoginTimeout() throws SQLException {
051                    return _dataSource.getLoginTimeout();
052            }
053    
054            @Override
055            public PrintWriter getLogWriter() throws SQLException {
056                    return _dataSource.getLogWriter();
057            }
058    
059            @SinceJava(1.7)
060            public Logger getParentLogger() {
061                    throw new UnsupportedOperationException();
062            }
063    
064            public DataSource getWrappedDataSource() {
065                    return _dataSource;
066            }
067    
068            @Override
069            public boolean isWrapperFor(Class<?> clazz) {
070    
071                    // JDK 6
072    
073                    return DataSource.class.equals(clazz);
074            }
075    
076            @Override
077            public void setLoginTimeout(int seconds) throws SQLException {
078                    _dataSource.setLoginTimeout(seconds);
079            }
080    
081            @Override
082            public void setLogWriter(PrintWriter out) throws SQLException {
083                    _dataSource.setLogWriter(out);
084            }
085    
086            public void setWrappedDataSource(DataSource wrappedDataSource) {
087                    _dataSource = wrappedDataSource;
088            }
089    
090            @Override
091            public <T> T unwrap(Class<T> clazz) throws SQLException {
092    
093                    // JDK 6
094    
095                    if (!DataSource.class.equals(clazz)) {
096                            throw new SQLException("Invalid class " + clazz);
097                    }
098    
099                    return (T)this;
100            }
101    
102            private volatile DataSource _dataSource;
103    
104    }