java/sql-dk/src/main/java/info/globalcode/sql/dk/configuration/Properties.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 24 Oct 2019 21:43:08 +0200
branchv_0
changeset 250 aae5009bd0af
parent 238 4a1864c3e867
permissions -rw-r--r--
fix license version: GNU GPLv3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
104
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * SQL-DK
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2013 František Kučera (frantovo.cz)
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
250
aae5009bd0af fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 238
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
104
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
package info.globalcode.sql.dk.configuration;
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
import java.util.ArrayList;
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
import javax.xml.bind.annotation.XmlTransient;
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
import static info.globalcode.sql.dk.Functions.findByName;
105
39d8b427e20f cloneable property and properties
František Kučera <franta-hg@frantovo.cz>
parents: 104
diff changeset
    22
import java.util.Collections;
104
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
/**
191
862d0a8747ac avoid NullPointerException (value = null) while duplicating to java.util.Properties
František Kučera <franta-hg@frantovo.cz>
parents: 155
diff changeset
    25
 * <p>
862d0a8747ac avoid NullPointerException (value = null) while duplicating to java.util.Properties
František Kučera <franta-hg@frantovo.cz>
parents: 155
diff changeset
    26
 * List of configurables.</p>
104
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
 *
191
862d0a8747ac avoid NullPointerException (value = null) while duplicating to java.util.Properties
František Kučera <franta-hg@frantovo.cz>
parents: 155
diff changeset
    28
 * <p>
862d0a8747ac avoid NullPointerException (value = null) while duplicating to java.util.Properties
František Kučera <franta-hg@frantovo.cz>
parents: 155
diff changeset
    29
 * Can be backed by defaults – if value for given name is nof found in this instance, we will
155
eb3676c6929b more JavaDoc
František Kučera <franta-hg@frantovo.cz>
parents: 133
diff changeset
    30
 * look into defaults. Methods also accept defaultValue parameter – is used if property is nof found
eb3676c6929b more JavaDoc
František Kučera <franta-hg@frantovo.cz>
parents: 133
diff changeset
    31
 * even in default properties.</p>
104
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
 *
191
862d0a8747ac avoid NullPointerException (value = null) while duplicating to java.util.Properties
František Kučera <franta-hg@frantovo.cz>
parents: 155
diff changeset
    33
 * <p>
862d0a8747ac avoid NullPointerException (value = null) while duplicating to java.util.Properties
František Kučera <franta-hg@frantovo.cz>
parents: 155
diff changeset
    34
 * Typical use: </p>
104
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
 * <ul>
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
 * <li>this instance – ad-hoc properties from CLI options</li>
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
 * <li>default properties – from config file</li>
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
 * <li>defaultValue – hardcoded default</li>
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
 * </ul>
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
 *
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
 * @author Ing. František Kučera (frantovo.cz)
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
 */
105
39d8b427e20f cloneable property and properties
František Kučera <franta-hg@frantovo.cz>
parents: 104
diff changeset
    43
public class Properties extends ArrayList<Property> implements Cloneable {
104
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
	private Properties defaults;
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
105
39d8b427e20f cloneable property and properties
František Kučera <franta-hg@frantovo.cz>
parents: 104
diff changeset
    47
	public Properties() {
39d8b427e20f cloneable property and properties
František Kučera <franta-hg@frantovo.cz>
parents: 104
diff changeset
    48
	}
39d8b427e20f cloneable property and properties
František Kučera <franta-hg@frantovo.cz>
parents: 104
diff changeset
    49
39d8b427e20f cloneable property and properties
František Kučera <franta-hg@frantovo.cz>
parents: 104
diff changeset
    50
	public Properties(int initialCapacity) {
39d8b427e20f cloneable property and properties
František Kučera <franta-hg@frantovo.cz>
parents: 104
diff changeset
    51
		super(initialCapacity);
39d8b427e20f cloneable property and properties
František Kučera <franta-hg@frantovo.cz>
parents: 104
diff changeset
    52
	}
39d8b427e20f cloneable property and properties
František Kučera <franta-hg@frantovo.cz>
parents: 104
diff changeset
    53
104
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
	@XmlTransient
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
	public Properties getDefaults() {
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
		return defaults;
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	}
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
	public void setDefaults(Properties defaults) {
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
		this.defaults = defaults;
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
	}
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
133
a1bf1465df89 Properties: setLastDefaults()
František Kučera <franta-hg@frantovo.cz>
parents: 108
diff changeset
    63
	/**
a1bf1465df89 Properties: setLastDefaults()
František Kučera <franta-hg@frantovo.cz>
parents: 108
diff changeset
    64
	 * @param defaults the last/deepest defaults
a1bf1465df89 Properties: setLastDefaults()
František Kučera <franta-hg@frantovo.cz>
parents: 108
diff changeset
    65
	 */
a1bf1465df89 Properties: setLastDefaults()
František Kučera <franta-hg@frantovo.cz>
parents: 108
diff changeset
    66
	public void setLastDefaults(Properties defaults) {
a1bf1465df89 Properties: setLastDefaults()
František Kučera <franta-hg@frantovo.cz>
parents: 108
diff changeset
    67
		if (this.defaults == null) {
a1bf1465df89 Properties: setLastDefaults()
František Kučera <franta-hg@frantovo.cz>
parents: 108
diff changeset
    68
			this.defaults = defaults;
a1bf1465df89 Properties: setLastDefaults()
František Kučera <franta-hg@frantovo.cz>
parents: 108
diff changeset
    69
		} else {
a1bf1465df89 Properties: setLastDefaults()
František Kučera <franta-hg@frantovo.cz>
parents: 108
diff changeset
    70
			this.defaults.setLastDefaults(defaults);
a1bf1465df89 Properties: setLastDefaults()
František Kučera <franta-hg@frantovo.cz>
parents: 108
diff changeset
    71
		}
a1bf1465df89 Properties: setLastDefaults()
František Kučera <franta-hg@frantovo.cz>
parents: 108
diff changeset
    72
	}
a1bf1465df89 Properties: setLastDefaults()
František Kučera <franta-hg@frantovo.cz>
parents: 108
diff changeset
    73
104
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
	private Property findProperty(String name) {
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
		Property p = findByName(this, name);
133
a1bf1465df89 Properties: setLastDefaults()
František Kučera <franta-hg@frantovo.cz>
parents: 108
diff changeset
    76
		if (p == null && defaults != null) {
a1bf1465df89 Properties: setLastDefaults()
František Kučera <franta-hg@frantovo.cz>
parents: 108
diff changeset
    77
			p = defaults.findProperty(name);
104
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
		}
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
		return p;
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
	}
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
	public String getString(String name, String defaultValue) {
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
		Property p = findProperty(name);
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
		return p == null ? defaultValue : p.getValue();
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
	}
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
	public boolean getBoolean(String name, boolean defaultValue) {
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
		Property p = findProperty(name);
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
		return p == null ? defaultValue : Boolean.valueOf(p.getValue());
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
	}
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
	public int getInteger(String name, int defaultValue) {
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
		Property p = findProperty(name);
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
		return p == null ? defaultValue : Integer.valueOf(p.getValue());
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
	}
105
39d8b427e20f cloneable property and properties
František Kučera <franta-hg@frantovo.cz>
parents: 104
diff changeset
    96
108
d06d90b28217 DB credentials can be CLI options + log warning: insecure
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
    97
	public boolean hasProperty(String name) {
d06d90b28217 DB credentials can be CLI options + log warning: insecure
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
    98
		return findByName(this, name) != null;
d06d90b28217 DB credentials can be CLI options + log warning: insecure
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
    99
	}
d06d90b28217 DB credentials can be CLI options + log warning: insecure
František Kučera <franta-hg@frantovo.cz>
parents: 107
diff changeset
   100
105
39d8b427e20f cloneable property and properties
František Kučera <franta-hg@frantovo.cz>
parents: 104
diff changeset
   101
	@Override
39d8b427e20f cloneable property and properties
František Kučera <franta-hg@frantovo.cz>
parents: 104
diff changeset
   102
	public Properties clone() {
39d8b427e20f cloneable property and properties
František Kučera <franta-hg@frantovo.cz>
parents: 104
diff changeset
   103
		Properties clone = new Properties(size());
39d8b427e20f cloneable property and properties
František Kučera <franta-hg@frantovo.cz>
parents: 104
diff changeset
   104
		Collections.copy(clone, this);
39d8b427e20f cloneable property and properties
František Kučera <franta-hg@frantovo.cz>
parents: 104
diff changeset
   105
		return clone;
39d8b427e20f cloneable property and properties
František Kučera <franta-hg@frantovo.cz>
parents: 104
diff changeset
   106
	}
106
e9c3583580c8 use database properties
František Kučera <franta-hg@frantovo.cz>
parents: 105
diff changeset
   107
e9c3583580c8 use database properties
František Kučera <franta-hg@frantovo.cz>
parents: 105
diff changeset
   108
	/**
e9c3583580c8 use database properties
František Kučera <franta-hg@frantovo.cz>
parents: 105
diff changeset
   109
	 * @return merged this and backing defaults as Java Properties
e9c3583580c8 use database properties
František Kučera <franta-hg@frantovo.cz>
parents: 105
diff changeset
   110
	 */
e9c3583580c8 use database properties
František Kučera <franta-hg@frantovo.cz>
parents: 105
diff changeset
   111
	public java.util.Properties getJavaProperties() {
e9c3583580c8 use database properties
František Kučera <franta-hg@frantovo.cz>
parents: 105
diff changeset
   112
		java.util.Properties javaProperties = new java.util.Properties();
e9c3583580c8 use database properties
František Kučera <franta-hg@frantovo.cz>
parents: 105
diff changeset
   113
		duplicateTo(javaProperties);
e9c3583580c8 use database properties
František Kučera <franta-hg@frantovo.cz>
parents: 105
diff changeset
   114
		return javaProperties;
e9c3583580c8 use database properties
František Kučera <franta-hg@frantovo.cz>
parents: 105
diff changeset
   115
	}
e9c3583580c8 use database properties
František Kučera <franta-hg@frantovo.cz>
parents: 105
diff changeset
   116
e9c3583580c8 use database properties
František Kučera <franta-hg@frantovo.cz>
parents: 105
diff changeset
   117
	private void duplicateTo(java.util.Properties javaProperties) {
107
8189a4a28cd8 database/formatter properties also as CLI options
František Kučera <franta-hg@frantovo.cz>
parents: 106
diff changeset
   118
		if (defaults != null) {
8189a4a28cd8 database/formatter properties also as CLI options
František Kučera <franta-hg@frantovo.cz>
parents: 106
diff changeset
   119
			defaults.duplicateTo(javaProperties);
8189a4a28cd8 database/formatter properties also as CLI options
František Kučera <franta-hg@frantovo.cz>
parents: 106
diff changeset
   120
		}
106
e9c3583580c8 use database properties
František Kučera <franta-hg@frantovo.cz>
parents: 105
diff changeset
   121
		for (Property p : this) {
191
862d0a8747ac avoid NullPointerException (value = null) while duplicating to java.util.Properties
František Kučera <franta-hg@frantovo.cz>
parents: 155
diff changeset
   122
			String value = p.getValue();
862d0a8747ac avoid NullPointerException (value = null) while duplicating to java.util.Properties
František Kučera <franta-hg@frantovo.cz>
parents: 155
diff changeset
   123
			if (value != null) {
862d0a8747ac avoid NullPointerException (value = null) while duplicating to java.util.Properties
František Kučera <franta-hg@frantovo.cz>
parents: 155
diff changeset
   124
				javaProperties.setProperty(p.getName(), value);
862d0a8747ac avoid NullPointerException (value = null) while duplicating to java.util.Properties
František Kučera <franta-hg@frantovo.cz>
parents: 155
diff changeset
   125
			}
106
e9c3583580c8 use database properties
František Kučera <franta-hg@frantovo.cz>
parents: 105
diff changeset
   126
		}
e9c3583580c8 use database properties
František Kučera <franta-hg@frantovo.cz>
parents: 105
diff changeset
   127
	}
104
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   128
}