java/sql-dk/src/info/globalcode/sql/dk/configuration/Properties.java
author František Kučera <franta-hg@frantovo.cz>
Tue, 26 Feb 2019 18:19:49 +0100
branchv_0
changeset 236 a3ec71fa8e17
parent 191 862d0a8747ac
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!“

/**
 * SQL-DK
 * Copyright © 2013 František Kučera (frantovo.cz)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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, see <http://www.gnu.org/licenses/>.
 */
package info.globalcode.sql.dk.configuration;

import java.util.ArrayList;
import javax.xml.bind.annotation.XmlTransient;
import static info.globalcode.sql.dk.Functions.findByName;
import java.util.Collections;

/**
 * <p>
 * List of configurables.</p>
 *
 * <p>
 * Can be backed by defaults – if value for given name is nof found in this instance, we will
 * look into defaults. Methods also accept defaultValue parameter – is used if property is nof found
 * even in default properties.</p>
 *
 * <p>
 * Typical use: </p>
 * <ul>
 * <li>this instance – ad-hoc properties from CLI options</li>
 * <li>default properties – from config file</li>
 * <li>defaultValue – hardcoded default</li>
 * </ul>
 *
 * @author Ing. František Kučera (frantovo.cz)
 */
public class Properties extends ArrayList<Property> implements Cloneable {

	private Properties defaults;

	public Properties() {
	}

	public Properties(int initialCapacity) {
		super(initialCapacity);
	}

	@XmlTransient
	public Properties getDefaults() {
		return defaults;
	}

	public void setDefaults(Properties defaults) {
		this.defaults = defaults;
	}

	/**
	 * @param defaults the last/deepest defaults
	 */
	public void setLastDefaults(Properties defaults) {
		if (this.defaults == null) {
			this.defaults = defaults;
		} else {
			this.defaults.setLastDefaults(defaults);
		}
	}

	private Property findProperty(String name) {
		Property p = findByName(this, name);
		if (p == null && defaults != null) {
			p = defaults.findProperty(name);
		}
		return p;
	}

	public String getString(String name, String defaultValue) {
		Property p = findProperty(name);
		return p == null ? defaultValue : p.getValue();
	}

	public boolean getBoolean(String name, boolean defaultValue) {
		Property p = findProperty(name);
		return p == null ? defaultValue : Boolean.valueOf(p.getValue());
	}

	public int getInteger(String name, int defaultValue) {
		Property p = findProperty(name);
		return p == null ? defaultValue : Integer.valueOf(p.getValue());
	}

	public boolean hasProperty(String name) {
		return findByName(this, name) != null;
	}

	@Override
	public Properties clone() {
		Properties clone = new Properties(size());
		Collections.copy(clone, this);
		return clone;
	}

	/**
	 * @return merged this and backing defaults as Java Properties
	 */
	public java.util.Properties getJavaProperties() {
		java.util.Properties javaProperties = new java.util.Properties();
		duplicateTo(javaProperties);
		return javaProperties;
	}

	private void duplicateTo(java.util.Properties javaProperties) {
		if (defaults != null) {
			defaults.duplicateTo(javaProperties);
		}
		for (Property p : this) {
			String value = p.getValue();
			if (value != null) {
				javaProperties.setProperty(p.getName(), value);
			}
		}
	}
}