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.shard;
016    
017    import com.liferay.portal.kernel.dao.shard.ShardUtil;
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 Alexander Chow
030     */
031    public class ShardDataSource implements DataSource {
032    
033            public static DataSource getInstance() {
034                    return _instance;
035            }
036    
037            public Connection getConnection() throws SQLException {
038                    return getDataSource().getConnection();
039            }
040    
041            public Connection getConnection(String username, String password)
042                    throws SQLException {
043    
044                    return getDataSource().getConnection(username, password);
045            }
046    
047            public int getLoginTimeout() throws SQLException {
048                    return getDataSource().getLoginTimeout();
049            }
050    
051            public PrintWriter getLogWriter() throws SQLException {
052                    return getDataSource().getLogWriter();
053            }
054    
055            public Logger getParentLogger() {
056    
057                    // JDK 7
058    
059                    throw new UnsupportedOperationException();
060            }
061    
062            public boolean isWrapperFor(Class<?> clazz) {
063    
064                    // Directly implement this method for JDK 5 compatibility. Logic is
065                    // copied from org.springframework.jdbc.datasource.AbstractDataSource.
066    
067                    return DataSource.class.equals(clazz);
068            }
069    
070            public void setLoginTimeout(int seconds) throws SQLException {
071                    getDataSource().setLoginTimeout(seconds);
072            }
073    
074            public void setLogWriter(PrintWriter printWriter) throws SQLException {
075                    getDataSource().setLogWriter(printWriter);
076            }
077    
078            public <T> T unwrap(Class<T> clazz) throws SQLException {
079    
080                    // Directly implement this method for JDK 5 compatibility. Logic is
081                    // copied from org.springframework.jdbc.datasource.AbstractDataSource.
082    
083                    if (!DataSource.class.equals(clazz)) {
084                            throw new SQLException("Invalid class " + clazz);
085                    }
086    
087                    return (T)this;
088            }
089    
090            protected DataSource getDataSource() {
091                    return ShardUtil.getDataSource();
092            }
093    
094            private static ShardDataSource _instance = new ShardDataSource();
095    
096    }