| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: Alessandro Ventura |
| 5 |
*/ |
| 6 |
package org.h2.security.auth; |
| 7 |
|
| 8 |
import java.util.Arrays; |
| 9 |
import java.util.Collection; |
| 10 |
import java.util.HashMap; |
| 11 |
|
| 12 |
import org.h2.util.Utils; |
| 13 |
|
| 14 |
/** |
| 15 |
* wrapper for configuration properties |
| 16 |
*/ |
| 17 |
public class ConfigProperties { |
| 18 |
|
| 19 |
private HashMap<String, String> properties; |
| 20 |
|
| 21 |
public ConfigProperties() { |
| 22 |
properties = new HashMap<>(); |
| 23 |
} |
| 24 |
|
| 25 |
public ConfigProperties(PropertyConfig... configProperties) { |
| 26 |
this(configProperties == null ? null : Arrays.asList(configProperties)); |
| 27 |
} |
| 28 |
|
| 29 |
public ConfigProperties(Collection<PropertyConfig> configProperties) { |
| 30 |
properties = new HashMap<>(); |
| 31 |
if (configProperties != null) { |
| 32 |
for (PropertyConfig currentProperty : configProperties) { |
| 33 |
if (properties.putIfAbsent(currentProperty.getName(), currentProperty.getValue()) != null) { |
| 34 |
throw new AuthConfigException("duplicate property " + currentProperty.getName()); |
| 35 |
} |
| 36 |
} |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Returns the string value of specified property. |
| 42 |
* |
| 43 |
* @param name property name. |
| 44 |
* @param defaultValue default value. |
| 45 |
* @return the string property value or {@code defaultValue} if the property is missing. |
| 46 |
*/ |
| 47 |
public String getStringValue(String name, String defaultValue) { |
| 48 |
String result = properties.get(name); |
| 49 |
if (result == null) { |
| 50 |
return defaultValue; |
| 51 |
} |
| 52 |
return result; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Returns the string value of specified property. |
| 57 |
* |
| 58 |
* @param name property name. |
| 59 |
* @return the string property value. |
| 60 |
* @throws AuthConfigException if the property is missing. |
| 61 |
*/ |
| 62 |
public String getStringValue(String name) { |
| 63 |
String result = properties.get(name); |
| 64 |
if (result == null) { |
| 65 |
throw new AuthConfigException("missing config property " + name); |
| 66 |
} |
| 67 |
return result; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Returns the integer value of specified property. |
| 72 |
* |
| 73 |
* @param name property name. |
| 74 |
* @param defaultValue default value. |
| 75 |
* @return the integer property value or {@code defaultValue} if the property is missing. |
| 76 |
*/ |
| 77 |
public int getIntValue(String name, int defaultValue) { |
| 78 |
String result = properties.get(name); |
| 79 |
if (result == null) { |
| 80 |
return defaultValue; |
| 81 |
} |
| 82 |
return Integer.parseInt(result); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Returns the integer value of specified property. |
| 87 |
* |
| 88 |
* @param name property name. |
| 89 |
* @return the integer property value. |
| 90 |
* @throws AuthConfigException if the property is missing. |
| 91 |
*/ |
| 92 |
public int getIntValue(String name) { |
| 93 |
String result = properties.get(name); |
| 94 |
if (result == null) { |
| 95 |
throw new AuthConfigException("missing config property " + name); |
| 96 |
} |
| 97 |
return Integer.parseInt(result); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Returns the boolean value of specified property. |
| 102 |
* |
| 103 |
* @param name property name. |
| 104 |
* @param defaultValue default value. |
| 105 |
* @return the boolean property value or {@code defaultValue} if the property is missing. |
| 106 |
*/ |
| 107 |
public boolean getBooleanValue(String name, boolean defaultValue) { |
| 108 |
String result = properties.get(name); |
| 109 |
if (result == null) { |
| 110 |
return defaultValue; |
| 111 |
} |
| 112 |
return Utils.parseBoolean(result, defaultValue, true); |
| 113 |
} |
| 114 |
|
| 115 |
} |