java/sql-dk/src/info/globalcode/sql/dk/configuration/DatabaseDefinition.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 17 May 2015 13:51:40 +0200
branchv_0
changeset 194 629c9c7eab01
parent 188 54bacc7ed42b
child 203 504c4ba56d1c
permissions -rw-r--r--
JDBC driver class can be specified in the database configuration as an optional parameter (useful especially while embedding jdbc-dk-driver into other application that does not support automatic driver discovery)
     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 	@XmlElement(name = "name", namespace = CONFIGURATION)
    62 	@Override
    63 	public String getName() {
    64 		return name;
    65 	}
    66 
    67 	public void setName(String name) {
    68 		this.name = name;
    69 	}
    70 
    71 	@XmlElement(name = "url", namespace = CONFIGURATION)
    72 	public String getUrl() {
    73 		return url;
    74 	}
    75 
    76 	public void setUrl(String url) {
    77 		this.url = url;
    78 	}
    79 
    80 	@XmlElement(name = "userName", namespace = CONFIGURATION)
    81 	public String getUserName() {
    82 		return userName;
    83 	}
    84 
    85 	public void setUserName(String userName) {
    86 		this.userName = userName;
    87 	}
    88 
    89 	@XmlElement(name = "password", namespace = CONFIGURATION)
    90 	public String getPassword() {
    91 		return password;
    92 	}
    93 
    94 	public void setPassword(String password) {
    95 		this.password = password;
    96 	}
    97 
    98 	public String getDriver() {
    99 		return driver;
   100 	}
   101 
   102 	public void setDriver(String driver) {
   103 		this.driver = driver;
   104 	}
   105 
   106 	@XmlElement(name = "property", namespace = CONFIGURATION)
   107 	public Properties getProperties() {
   108 		return properties;
   109 	}
   110 
   111 	public void setProperties(Properties properties) {
   112 		this.properties = properties;
   113 	}
   114 
   115 	/**
   116 	 * @param properties ad-hoc properties from CLI options (for the JDBC driver)
   117 	 * @param jmxBean JMX management bean for progress reporting | null = disable JMX
   118 	 * @return
   119 	 * @throws java.sql.SQLException
   120 	 */
   121 	public DatabaseConnection connect(Properties properties, ConnectionManagement jmxBean) throws SQLException {
   122 		return new DatabaseConnection(this, properties, jmxBean);
   123 	}
   124 
   125 	/**
   126 	 * @param properties
   127 	 * @return
   128 	 * @throws java.sql.SQLException
   129 	 * @see #connect(info.globalcode.sql.dk.configuration.Properties, java.lang.String)
   130 	 * With disabled JMX reporting.
   131 	 */
   132 	public DatabaseConnection connect(Properties properties) throws SQLException {
   133 		return new DatabaseConnection(this, properties, null);
   134 	}
   135 }