001    /*
002     * The MIT License
003     *
004     * Copyright (c) 2012, Ninja Squad
005     *
006     * Permission is hereby granted, free of charge, to any person obtaining a copy
007     * of this software and associated documentation files (the "Software"), to deal
008     * in the Software without restriction, including without limitation the rights
009     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
010     * copies of the Software, and to permit persons to whom the Software is
011     * furnished to do so, subject to the following conditions:
012     *
013     * The above copyright notice and this permission notice shall be included in
014     * all copies or substantial portions of the Software.
015     *
016     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
017     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
018     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
019     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
020     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
021     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
022     * THE SOFTWARE.
023     */
024    
025    package com.ninja_squad.dbsetup.destination;
026    
027    import com.ninja_squad.dbsetup.util.Preconditions;
028    
029    import javax.annotation.Nonnull;
030    import javax.annotation.concurrent.Immutable;
031    import javax.sql.DataSource;
032    import java.sql.Connection;
033    import java.sql.SQLException;
034    
035    /**
036     * A destination which wraps a DataSource and gets its connection from the wrapped DataSource
037     * @author JB Nizet
038     */
039    @Immutable
040    public final class DataSourceDestination implements Destination {
041        private final DataSource dataSource;
042    
043        /**
044         * Constructor
045         * @param dataSource the wrapped DataSource
046         */
047        public DataSourceDestination(@Nonnull DataSource dataSource) {
048            Preconditions.checkNotNull(dataSource, "dataSource may not be null");
049            this.dataSource = dataSource;
050        }
051    
052        /**
053         * Factory method creating a new DataSourceDestination. This allows a more readable style than using the
054         * constructor:
055         *
056         * <pre>
057         *    DbSetup dbSetup = new DbSetup(DataSourceDestination.with(dataSource), operation);
058         * </pre>
059         *
060         * or, if this method is statically imported:
061         *
062         * <pre>
063         *    DbSetup dbSetup = new DbSetup(with(dataSource), operation);
064         * </pre>
065         *
066         * instead of
067         *
068         * <pre>
069         *    DbSetup dbSetup = new DbSetup(new DataSourceDestination(dataSource), operation);
070         * </pre>
071         *
072         * @param dataSource the wrapped DataSource
073         */
074        public static DataSourceDestination with(@Nonnull DataSource dataSource) {
075            return new DataSourceDestination(dataSource);
076        }
077    
078        @Override
079        public Connection getConnection() throws SQLException {
080            return dataSource.getConnection();
081        }
082    
083        @Override
084        public String toString() {
085            return "DataSourceDestination [dataSource=" + dataSource + "]";
086        }
087    
088        @Override
089        public int hashCode() {
090            return dataSource.hashCode();
091        }
092    
093        @Override
094        public boolean equals(Object obj) {
095            if (this == obj) {
096                return true;
097            }
098            if (obj == null) {
099                return false;
100            }
101            if (getClass() != obj.getClass()) {
102                return false;
103            }
104            DataSourceDestination other = (DataSourceDestination) obj;
105            return dataSource.equals(other.dataSource);
106        }
107    }