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