java/sql-dk/src/info/globalcode/sql/dk/configuration/Properties.java
author František Kučera <franta-hg@frantovo.cz>
Wed, 01 Jan 2014 02:56:08 +0100
branchv_0
changeset 108 d06d90b28217
parent 107 8189a4a28cd8
child 133 a1bf1465df89
permissions -rw-r--r--
DB credentials can be CLI options + log warning: 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 java.util.ArrayList;
    21 import javax.xml.bind.annotation.XmlTransient;
    22 import static info.globalcode.sql.dk.Functions.findByName;
    23 import java.util.Collections;
    24 
    25 /**
    26  * <p>List of configurables.</p>
    27  *
    28  * <p>Can be backed by defaults – if value for given name is nof found in this instance, we will
    29  * look
    30  * into defaults. Methods also accept defaultValue parameter – is used if property is nof found even
    31  * in default properties.</p>
    32  *
    33  * <p>Typical use: </p>
    34  * <ul>
    35  * <li>this instance – ad-hoc properties from CLI options</li>
    36  * <li>default properties – from config file</li>
    37  * <li>defaultValue – hardcoded default</li>
    38  * </ul>
    39  *
    40  * @author Ing. František Kučera (frantovo.cz)
    41  */
    42 public class Properties extends ArrayList<Property> implements Cloneable {
    43 
    44 	private Properties defaults;
    45 
    46 	public Properties() {
    47 	}
    48 
    49 	public Properties(int initialCapacity) {
    50 		super(initialCapacity);
    51 	}
    52 
    53 	@XmlTransient
    54 	public Properties getDefaults() {
    55 		return defaults;
    56 	}
    57 
    58 	public void setDefaults(Properties defaults) {
    59 		this.defaults = defaults;
    60 	}
    61 
    62 	private Property findProperty(String name) {
    63 		Property p = findByName(this, name);
    64 		if (p == null) {
    65 			p = findByName(defaults, name);
    66 		}
    67 		return p;
    68 	}
    69 
    70 	public String getString(String name, String defaultValue) {
    71 		Property p = findProperty(name);
    72 		return p == null ? defaultValue : p.getValue();
    73 	}
    74 
    75 	public boolean getBoolean(String name, boolean defaultValue) {
    76 		Property p = findProperty(name);
    77 		return p == null ? defaultValue : Boolean.valueOf(p.getValue());
    78 	}
    79 
    80 	public int getInteger(String name, int defaultValue) {
    81 		Property p = findProperty(name);
    82 		return p == null ? defaultValue : Integer.valueOf(p.getValue());
    83 	}
    84 
    85 	public boolean hasProperty(String name) {
    86 		return findByName(this, name) != null;
    87 	}
    88 
    89 	@Override
    90 	public Properties clone() {
    91 		Properties clone = new Properties(size());
    92 		Collections.copy(clone, this);
    93 		return clone;
    94 	}
    95 
    96 	/**
    97 	 * @return merged this and backing defaults as Java Properties
    98 	 */
    99 	public java.util.Properties getJavaProperties() {
   100 		java.util.Properties javaProperties = new java.util.Properties();
   101 		duplicateTo(javaProperties);
   102 		return javaProperties;
   103 	}
   104 
   105 	private void duplicateTo(java.util.Properties javaProperties) {
   106 		if (defaults != null) {
   107 			defaults.duplicateTo(javaProperties);
   108 		}
   109 		for (Property p : this) {
   110 			javaProperties.setProperty(p.getName(), p.getValue());
   111 		}
   112 	}
   113 }