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