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 java.sql.Connection;
028    import java.sql.DriverManager;
029    import java.sql.SQLException;
030    
031    import javax.annotation.Nonnull;
032    import javax.annotation.concurrent.Immutable;
033    
034    import com.ninja_squad.dbsetup.util.Preconditions;
035    
036    /**
037     * A destination which uses the {@link DriverManager} to get a connection
038     * @author JB Nizet
039     */
040    @Immutable
041    public final class DriverManagerDestination implements Destination {
042    
043        private final String url;
044        private final String user;
045        private final String password;
046    
047        /**
048         * Constructor
049         * @param url the URL of the database
050         * @param user the user used to get a connection
051         * @param password the password used to get a connection
052         */
053        public DriverManagerDestination(@Nonnull String url, String user, String password) {
054            Preconditions.checkNotNull(url, "url may not be null");
055            this.url = url;
056            this.user = user;
057            this.password = password;
058        }
059    
060        @Override
061        public Connection getConnection() throws SQLException {
062            return DriverManager.getConnection(url, user, password);
063        }
064    
065        @Override
066        public String toString() {
067            return "DriverManagerDestination [url="
068                   + url
069                   + ", user="
070                   + user
071                   + ", password="
072                   + password
073                   + "]";
074        }
075    
076        @Override
077        public int hashCode() {
078            final int prime = 31;
079            int result = 1;
080            result = prime * result + url.hashCode();
081            result = prime * result + ((password == null) ? 0 : password.hashCode());
082            result = prime * result + ((user == null) ? 0 : user.hashCode());
083            return result;
084        }
085    
086        @Override
087        public boolean equals(Object obj) {
088            if (this == obj) {
089                return true;
090            }
091            if (obj == null) {
092                return false;
093            }
094            if (getClass() != obj.getClass()) {
095                return false;
096            }
097            DriverManagerDestination other = (DriverManagerDestination) obj;
098            if (password == null) {
099                if (other.password != null) {
100                    return false;
101                }
102            }
103            else if (!password.equals(other.password)) {
104                return false;
105            }
106            if (!url.equals(other.url)) {
107                return false;
108            }
109            if (user == null) {
110                if (other.user != null) {
111                    return false;
112                }
113            }
114            else if (!user.equals(other.user)) {
115                return false;
116            }
117            return true;
118        }
119    }