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!“
3 * Copyright © 2013 František Kučera (frantovo.cz)
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.
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.
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/>.
18 package info.globalcode.sql.dk.configuration;
20 import java.util.ArrayList;
21 import javax.xml.bind.annotation.XmlTransient;
22 import static info.globalcode.sql.dk.Functions.findByName;
23 import java.util.Collections;
27 * List of configurables.</p>
30 * Can be backed by defaults – if value for given name is nof found in this instance, we will
31 * look into defaults. Methods also accept defaultValue parameter – is used if property is nof found
32 * even in default properties.</p>
37 * <li>this instance – ad-hoc properties from CLI options</li>
38 * <li>default properties – from config file</li>
39 * <li>defaultValue – hardcoded default</li>
42 * @author Ing. František Kučera (frantovo.cz)
44 public class Properties extends ArrayList<Property> implements Cloneable {
46 private Properties defaults;
51 public Properties(int initialCapacity) {
52 super(initialCapacity);
56 public Properties getDefaults() {
60 public void setDefaults(Properties defaults) {
61 this.defaults = defaults;
65 * @param defaults the last/deepest defaults
67 public void setLastDefaults(Properties defaults) {
68 if (this.defaults == null) {
69 this.defaults = defaults;
71 this.defaults.setLastDefaults(defaults);
75 private Property findProperty(String name) {
76 Property p = findByName(this, name);
77 if (p == null && defaults != null) {
78 p = defaults.findProperty(name);
83 public String getString(String name, String defaultValue) {
84 Property p = findProperty(name);
85 return p == null ? defaultValue : p.getValue();
88 public boolean getBoolean(String name, boolean defaultValue) {
89 Property p = findProperty(name);
90 return p == null ? defaultValue : Boolean.valueOf(p.getValue());
93 public int getInteger(String name, int defaultValue) {
94 Property p = findProperty(name);
95 return p == null ? defaultValue : Integer.valueOf(p.getValue());
98 public boolean hasProperty(String name) {
99 return findByName(this, name) != null;
103 public Properties clone() {
104 Properties clone = new Properties(size());
105 Collections.copy(clone, this);
110 * @return merged this and backing defaults as Java Properties
112 public java.util.Properties getJavaProperties() {
113 java.util.Properties javaProperties = new java.util.Properties();
114 duplicateTo(javaProperties);
115 return javaProperties;
118 private void duplicateTo(java.util.Properties javaProperties) {
119 if (defaults != null) {
120 defaults.duplicateTo(javaProperties);
122 for (Property p : this) {
123 String value = p.getValue();
125 javaProperties.setProperty(p.getName(), value);