java/sql-dk/src/info/globalcode/sql/dk/configuration/DatabaseDefinition.java
author František Kučera <franta-hg@frantovo.cz>
Tue, 26 Feb 2019 18:19:49 +0100
branchv_0
changeset 236 a3ec71fa8e17
parent 203 504c4ba56d1c
permissions -rw-r--r--
Avoid reusing/rewriting the DB connection properties.
There was weird random errors while testing connection to multiple DB in parallel when one of them was meta connection to same DB connection.
Two kinds of exception: 1) missing password 2) „Passing DB password as CLI parameter is insecure!“
franta-hg@26
     1
/**
franta-hg@26
     2
 * SQL-DK
franta-hg@26
     3
 * Copyright © 2013 František Kučera (frantovo.cz)
franta-hg@26
     4
 *
franta-hg@26
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@26
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@26
     7
 * the Free Software Foundation, either version 3 of the License, or
franta-hg@26
     8
 * (at your option) any later version.
franta-hg@26
     9
 *
franta-hg@26
    10
 * This program is distributed in the hope that it will be useful,
franta-hg@26
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@26
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@26
    13
 * GNU General Public License for more details.
franta-hg@26
    14
 *
franta-hg@26
    15
 * You should have received a copy of the GNU General Public License
franta-hg@26
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@26
    17
 */
franta-hg@26
    18
package info.globalcode.sql.dk.configuration;
franta-hg@26
    19
franta-hg@30
    20
import static info.globalcode.sql.dk.Xmlns.CONFIGURATION;
franta-hg@27
    21
import info.globalcode.sql.dk.DatabaseConnection;
franta-hg@179
    22
import info.globalcode.sql.dk.jmx.ConnectionManagement;
franta-hg@29
    23
import java.sql.SQLException;
franta-hg@179
    24
import java.util.logging.Logger;
franta-hg@26
    25
import javax.xml.bind.annotation.XmlElement;
franta-hg@26
    26
franta-hg@26
    27
/**
franta-hg@155
    28
 * Configured (but not yet connected) database connection.
franta-hg@26
    29
 *
franta-hg@26
    30
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@26
    31
 */
franta-hg@26
    32
public class DatabaseDefinition implements NameIdentified {
franta-hg@26
    33
franta-hg@179
    34
	private static final Logger log = Logger.getLogger(DatabaseDefinition.class.getName());
franta-hg@194
    35
	/**
franta-hg@194
    36
	 * database name in SQL-DK configuration
franta-hg@194
    37
	 */
franta-hg@26
    38
	private String name;
franta-hg@194
    39
	/**
franta-hg@194
    40
	 * JDBC URL
franta-hg@194
    41
	 */
franta-hg@26
    42
	private String url;
franta-hg@194
    43
	/**
franta-hg@194
    44
	 * JDBC user name
franta-hg@194
    45
	 */
franta-hg@26
    46
	private String userName;
franta-hg@194
    47
	/**
franta-hg@194
    48
	 * JDBC password
franta-hg@194
    49
	 */
franta-hg@26
    50
	private String password;
franta-hg@194
    51
	/**
franta-hg@194
    52
	 * optional JDBC driver – if empty, the DriverManager is used to lookup specific Driver for
franta-hg@194
    53
	 * given URL
franta-hg@194
    54
	 */
franta-hg@194
    55
	private String driver;
franta-hg@194
    56
	/**
franta-hg@194
    57
	 * JDBC properties
franta-hg@194
    58
	 */
franta-hg@104
    59
	private Properties properties = new Properties();
franta-hg@203
    60
	/**
franta-hg@203
    61
	 * optional definition of tunnel to the remote database
franta-hg@203
    62
	 */
franta-hg@203
    63
	private TunnelDefinition tunnel;
franta-hg@26
    64
franta-hg@30
    65
	@XmlElement(name = "name", namespace = CONFIGURATION)
franta-hg@26
    66
	@Override
franta-hg@26
    67
	public String getName() {
franta-hg@26
    68
		return name;
franta-hg@26
    69
	}
franta-hg@26
    70
franta-hg@26
    71
	public void setName(String name) {
franta-hg@26
    72
		this.name = name;
franta-hg@26
    73
	}
franta-hg@26
    74
franta-hg@30
    75
	@XmlElement(name = "url", namespace = CONFIGURATION)
franta-hg@26
    76
	public String getUrl() {
franta-hg@26
    77
		return url;
franta-hg@26
    78
	}
franta-hg@26
    79
franta-hg@26
    80
	public void setUrl(String url) {
franta-hg@26
    81
		this.url = url;
franta-hg@26
    82
	}
franta-hg@26
    83
franta-hg@30
    84
	@XmlElement(name = "userName", namespace = CONFIGURATION)
franta-hg@26
    85
	public String getUserName() {
franta-hg@26
    86
		return userName;
franta-hg@26
    87
	}
franta-hg@26
    88
franta-hg@26
    89
	public void setUserName(String userName) {
franta-hg@26
    90
		this.userName = userName;
franta-hg@26
    91
	}
franta-hg@26
    92
franta-hg@30
    93
	@XmlElement(name = "password", namespace = CONFIGURATION)
franta-hg@26
    94
	public String getPassword() {
franta-hg@26
    95
		return password;
franta-hg@26
    96
	}
franta-hg@26
    97
franta-hg@26
    98
	public void setPassword(String password) {
franta-hg@26
    99
		this.password = password;
franta-hg@26
   100
	}
franta-hg@26
   101
franta-hg@194
   102
	public String getDriver() {
franta-hg@194
   103
		return driver;
franta-hg@194
   104
	}
franta-hg@194
   105
franta-hg@194
   106
	public void setDriver(String driver) {
franta-hg@194
   107
		this.driver = driver;
franta-hg@194
   108
	}
franta-hg@194
   109
franta-hg@104
   110
	@XmlElement(name = "property", namespace = CONFIGURATION)
franta-hg@104
   111
	public Properties getProperties() {
franta-hg@104
   112
		return properties;
franta-hg@104
   113
	}
franta-hg@104
   114
franta-hg@104
   115
	public void setProperties(Properties properties) {
franta-hg@104
   116
		this.properties = properties;
franta-hg@104
   117
	}
franta-hg@104
   118
franta-hg@203
   119
	public TunnelDefinition getTunnel() {
franta-hg@203
   120
		return tunnel;
franta-hg@203
   121
	}
franta-hg@203
   122
franta-hg@203
   123
	public void setTunnel(TunnelDefinition tunnel) {
franta-hg@203
   124
		this.tunnel = tunnel;
franta-hg@203
   125
	}
franta-hg@203
   126
franta-hg@106
   127
	/**
franta-hg@155
   128
	 * @param properties ad-hoc properties from CLI options (for the JDBC driver)
franta-hg@179
   129
	 * @param jmxBean JMX management bean for progress reporting | null = disable JMX
franta-hg@194
   130
	 * @return
franta-hg@188
   131
	 * @throws java.sql.SQLException
franta-hg@179
   132
	 */
franta-hg@179
   133
	public DatabaseConnection connect(Properties properties, ConnectionManagement jmxBean) throws SQLException {
franta-hg@179
   134
		return new DatabaseConnection(this, properties, jmxBean);
franta-hg@179
   135
	}
franta-hg@179
   136
franta-hg@179
   137
	/**
franta-hg@188
   138
	 * @param properties
franta-hg@194
   139
	 * @return
franta-hg@188
   140
	 * @throws java.sql.SQLException
franta-hg@179
   141
	 * @see #connect(info.globalcode.sql.dk.configuration.Properties, java.lang.String)
franta-hg@179
   142
	 * With disabled JMX reporting.
franta-hg@106
   143
	 */
franta-hg@106
   144
	public DatabaseConnection connect(Properties properties) throws SQLException {
franta-hg@179
   145
		return new DatabaseConnection(this, properties, null);
franta-hg@26
   146
	}
franta-hg@26
   147
}