Skip to content
Credentials.java 4.36 KiB
Newer Older
/* 
 * _____________________________________________________________________________
 * 
 * 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.oats.ia2.tapschemamanager.api;
Sonia Zorba's avatar
Sonia Zorba committed

import it.inaf.oats.ia2.tapschemamanager.api.contract.DatabaseType;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlAttribute;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Sonia Zorba's avatar
Sonia Zorba committed

/**
 * Representation of credentials used to connect to a RDBMS. It can be
 * serialized in XML using the JAXB standard.
Sonia Zorba's avatar
Sonia Zorba committed
 *
 * @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
Sonia Zorba's avatar
Sonia Zorba committed
 */
public class Credentials implements Serializable {
    private static final long serialVersionUID = 1153912575502196261L;
    private static final Logger log = LoggerFactory.getLogger(Credentials.class);

Sonia Zorba's avatar
Sonia Zorba committed
    private String hostname;
    private int port;
    private String username;
    private String password;
    private String database;
Sonia Zorba's avatar
Sonia Zorba committed

    private DatabaseType databaseType;

Sonia Zorba's avatar
Sonia Zorba committed
    public Credentials() {
        this(DatabaseType.MYSQL);
Sonia Zorba's avatar
Sonia Zorba committed
    }

    public Credentials(DatabaseType dbType) {
        this.databaseType = dbType;
        this.setDefaults();
    }

    /**
     * The name of the server that hosts the RDBMS.
     */
    @XmlAttribute(name = "hostname", required = true)
Sonia Zorba's avatar
Sonia Zorba committed
    public String getHostname() {
        return this.hostname;
    }

    public void setHostname(String hostname) {
        this.hostname = hostname;
    }

    /**
     * The RDBMS port number.
     */
    @XmlAttribute(name = "port", required = true)
Sonia Zorba's avatar
Sonia Zorba committed
    public int getPort() {
        return this.port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    @XmlAttribute(name = "username", required = true)
Sonia Zorba's avatar
Sonia Zorba committed
    public String getUsername() {
        return this.username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @XmlAttribute(name = "password", required = true)
Sonia Zorba's avatar
Sonia Zorba committed
    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * The kind of RDBMS used.
     */
    @XmlAttribute(name = "database-type", required = true)
    public DatabaseType getDatabaseType() {
        return databaseType;
    }

    public void setDatabaseType(DatabaseType databaseType) {
        this.databaseType = databaseType;
    }

     * <strong>Only for POSTGRES</strong>. The database to use in the
     * connection. The default value is "postgres".
     */
    @XmlAttribute(name = "database")
    public String getDatabase() {
        return database;
    }

    /**
     * <strong>Only for POSTGRES</strong>. The database to use in the
     * connection.
     */
    public void setDatabase(String database) {
        this.database = database;
    }

    /**
     * Set default values for <code>databaseType</code>, <code>database</code>
     * and <code>port</code> properties.
     */
    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";
        }
    }

    /**
     * The class name of the RDBMS driver.
     */
    public String getDatabaseDriverClass() {
        switch (getDatabaseType()) {
            case MYSQL:
                return "com.mysql.jdbc.Driver";
            case POSTGRES:
                return "org.postgresql.Driver";
        }
        return null;
    }
Sonia Zorba's avatar
Sonia Zorba committed
}