/* * _____________________________________________________________________________ * * INAF - OATS National Institute for Astrophysics - Astronomical Observatory of * Trieste INAF - IA2 Italian Center for Astronomical Archives * _____________________________________________________________________________ * * Copyright (C) 2016 Istituto Nazionale di Astrofisica * * This program is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License Version 3 as published by the * Free Software Foundation. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * this program; if not, write to the Free Software Foundation, Inc., 51 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ package it.inaf.ia2.tsm.datalayer; import java.io.Serializable; import java.util.Objects; import javax.xml.bind.annotation.XmlAttribute; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Representation of credentials used to connect to a RDBMS. It can be * serialized in XML using the JAXB standard. * * @author Sonia Zorba {@literal } */ public class Credentials implements Serializable { private static final long serialVersionUID = 1153912575502196261L; private static final Logger LOG = LoggerFactory.getLogger(Credentials.class); private DatabaseType databaseType; private String hostname; private int port; private String username; private String password; private String database; public Credentials() { this(DatabaseType.MYSQL); } public Credentials(DatabaseType dbType) { this.databaseType = dbType; this.setDefaults(); } // Copy constructor public Credentials(Credentials credentials) { this(credentials.getDatabaseType()); this.hostname = credentials.getHostname(); this.port = credentials.getPort(); this.username = credentials.getUsername(); this.password = credentials.getPassword(); this.database = credentials.getDatabase(); } /** * The name of the server that hosts the RDBMS. */ @XmlAttribute(name = "hostname", required = true) public String getHostname() { return this.hostname; } public void setHostname(String hostname) { this.hostname = hostname; } /** * The RDBMS port number. */ @XmlAttribute(name = "port", required = true) public int getPort() { return this.port; } public void setPort(int port) { this.port = port; } /** * The username used for connecting to the database. */ @XmlAttribute(name = "username", required = true) public String getUsername() { return this.username; } public void setUsername(String username) { this.username = username; } /** * The password used for connecting to the database. */ @XmlAttribute(name = "password", required = true) public String getPassword() { return this.password; } public void setPassword(String password) { this.password = password; } /** * The kind of RDBMS used (e.g. MySQL, PostgreSQL, etc). */ @XmlAttribute(name = "database-type", required = true) public DatabaseType getDatabaseType() { return databaseType; } public void setDatabaseType(DatabaseType databaseType) { this.databaseType = databaseType; } /** * Only for PostgreSQL. The database to use in the * connection. The default value is "postgres". */ @XmlAttribute(name = "database") public String getDatabase() { return database; } /** * Only for PostgreSQL. The database to use in the * connection. */ public void setDatabase(String database) { this.database = database; } /** * Set default values for {@code databaseType}, {@code database} and * {@code port} fields. */ public final void setDefaults() { if (databaseType == DatabaseType.MYSQL) { this.port = 3306; this.database = null; } else if (databaseType == DatabaseType.POSTGRES) { this.port = 5432; this.database = "postgres"; } } @Override public String toString() { return String.format("[%s] type=%s, hostname=%s, port=%s, username=%s, password=%s, database=%s", Credentials.class.getCanonicalName(), databaseType, hostname, port, username, password, database); } @Override public int hashCode() { int hash = 3; hash = 97 * hash + Objects.hashCode(this.hostname); hash = 97 * hash + this.port; hash = 97 * hash + Objects.hashCode(this.username); hash = 97 * hash + Objects.hashCode(this.password); hash = 97 * hash + Objects.hashCode(this.database); hash = 97 * hash + Objects.hashCode(this.databaseType); return hash; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Credentials other = (Credentials) obj; if (this.port != other.port) { return false; } if (!Objects.equals(this.hostname, other.hostname)) { return false; } if (!Objects.equals(this.username, other.username)) { return false; } if (!Objects.equals(this.password, other.password)) { return false; } if (!Objects.equals(this.database, other.database)) { return false; } return this.databaseType == other.databaseType; } }