0,0 → 1,81 |
package ak.hostadmiral.util; |
|
import java.util.Map; |
|
public class ConfigUtils |
{ |
private Map params; |
protected boolean returnDefValue; |
|
public ConfigUtils(Map params) |
{ |
this.params = params; |
} |
|
public String getString(String key, String defValue, |
boolean optional, boolean mayBeEmpty) |
throws ModelException |
{ |
String s = (String)getObject(key, defValue, optional); |
|
if(returnDefValue) return s; |
|
if(!mayBeEmpty && s.equals("")) { |
throw new ModelException("Configuration parameter '" + key + "' may not be empty"); |
} |
|
return s; |
} |
|
public Integer getInteger(String key, Integer defValue, |
boolean optional) |
throws ModelException |
{ |
Object value = getObject(key, defValue, optional); |
|
if(returnDefValue) return defValue; |
|
return new Integer((String)value); |
} |
|
protected synchronized Object getObject(String key, Object defValue, boolean optional) |
throws ModelException |
{ |
returnDefValue = false; |
|
Object obj = params.get(key); |
|
if(obj == null) { |
if(optional) { |
returnDefValue = true; |
return defValue; |
} |
else |
throw new ModelException("Configuration parameter '" + key + "' must be present"); |
} |
|
if(!(obj instanceof String[])) { |
throw new ModelException("Configuration parameter '" + key |
+ "' expected to be an array of strings"); |
} |
|
String[] sa = (String[])obj; |
|
if(sa.length == 0) { |
if(optional) { |
returnDefValue = true; |
return defValue; |
} |
else |
throw new ModelException("Value of configuration parameter '" |
+ key + "' must be present"); |
} |
|
if(sa.length > 1) { |
throw new ModelException("Value of configuration parameter '" |
+ key + "' may not be an array"); |
} |
|
return sa[0]; |
} |
} |