[New] added basic configuration modl
This commit is contained in:
parent
2f8900374f
commit
8a6b212248
|
@ -0,0 +1,105 @@
|
|||
package li.strolch.runtime;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
import java.util.Map;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import ch.eitchnet.utils.helper.StringHelper;
|
||||
|
||||
public class ComponentConfiguration {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ComponentConfiguration.class);
|
||||
|
||||
private final String componentName;
|
||||
private final Map<String, String> configurationValues;
|
||||
|
||||
public ComponentConfiguration(String componentName, Map<String, String> configurationValues) {
|
||||
this.componentName = componentName;
|
||||
this.configurationValues = configurationValues;
|
||||
}
|
||||
|
||||
public String getComponentName() {
|
||||
return this.componentName;
|
||||
}
|
||||
|
||||
public String getString(String key, String defValue) {
|
||||
String value = this.configurationValues.get(key);
|
||||
if (!StringHelper.isEmpty(value)) {
|
||||
return value;
|
||||
}
|
||||
|
||||
assertDefValueExist(key, defValue);
|
||||
logDefValueUse(key, defValue);
|
||||
return defValue;
|
||||
}
|
||||
|
||||
public boolean getBoolean(String key, Boolean defValue) {
|
||||
String value = this.configurationValues.get(key);
|
||||
if (!StringHelper.isEmpty(value)) {
|
||||
if (value.equalsIgnoreCase("true")) //$NON-NLS-1$
|
||||
return true;
|
||||
else if (value.equalsIgnoreCase("false")) //$NON-NLS-1$
|
||||
return false;
|
||||
|
||||
String msg = "Component {0} has non-boolean configuration value for {1} = {2}!"; //$NON-NLS-1$
|
||||
msg = MessageFormat.format(msg, this.componentName, key, value);
|
||||
throw new StrolchConfigurationException(msg);
|
||||
}
|
||||
|
||||
assertDefValueExist(key, defValue);
|
||||
logDefValueUse(key, defValue);
|
||||
return defValue;
|
||||
}
|
||||
|
||||
public int getInt(String key, int defValue) {
|
||||
String value = this.configurationValues.get(key);
|
||||
if (!StringHelper.isEmpty(value)) {
|
||||
|
||||
try {
|
||||
return Integer.parseInt(value);
|
||||
} catch (NumberFormatException e) {
|
||||
String msg = "Component {0} has non-integer configuration value for {1} = {2}!"; //$NON-NLS-1$
|
||||
msg = MessageFormat.format(msg, this.componentName, key, value);
|
||||
throw new StrolchConfigurationException(msg);
|
||||
}
|
||||
}
|
||||
|
||||
assertDefValueExist(key, defValue);
|
||||
logDefValueUse(key, defValue);
|
||||
return defValue;
|
||||
}
|
||||
|
||||
public long getLong(String key, long defValue) {
|
||||
String value = this.configurationValues.get(key);
|
||||
if (!StringHelper.isEmpty(value)) {
|
||||
|
||||
try {
|
||||
return Long.parseLong(value);
|
||||
} catch (NumberFormatException e) {
|
||||
String msg = "Component {0} has non-long configuration value for {1} = {2}!"; //$NON-NLS-1$
|
||||
msg = MessageFormat.format(msg, this.componentName, key, value);
|
||||
throw new StrolchConfigurationException(msg);
|
||||
}
|
||||
}
|
||||
|
||||
assertDefValueExist(key, defValue);
|
||||
logDefValueUse(key, defValue);
|
||||
return defValue;
|
||||
}
|
||||
|
||||
private void logDefValueUse(String key, Object defValue) {
|
||||
String msg = "Using default for key {0}={1}"; //$NON-NLS-1$
|
||||
msg = MessageFormat.format(msg, key, defValue);
|
||||
logger.info(msg);
|
||||
}
|
||||
|
||||
private void assertDefValueExist(String key, Object defValue) {
|
||||
if (defValue == null) {
|
||||
String msg = "Component {0} is missing the configuration value for key {1} does not exist!"; //$NON-NLS-1$
|
||||
msg = MessageFormat.format(msg, this.componentName, key);
|
||||
throw new StrolchConfigurationException(msg);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package li.strolch.runtime;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class RuntimeConfiguration extends ComponentConfiguration {
|
||||
|
||||
private final String rootPath;
|
||||
|
||||
public RuntimeConfiguration(String componentName, Map<String, String> configurationValues, String rootPath) {
|
||||
super(componentName, configurationValues);
|
||||
this.rootPath = rootPath;
|
||||
}
|
||||
|
||||
public String getRootPath() {
|
||||
return this.rootPath;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package li.strolch.runtime;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
import java.util.Map;
|
||||
|
||||
public class StrolchConfiguration {
|
||||
|
||||
private final RuntimeConfiguration runtimeConfiguration;
|
||||
private final Map<String, ComponentConfiguration> configurationByComponent;
|
||||
|
||||
public StrolchConfiguration(RuntimeConfiguration runtimeConfiguration,
|
||||
Map<String, ComponentConfiguration> configurationByComponent) {
|
||||
this.runtimeConfiguration = runtimeConfiguration;
|
||||
this.configurationByComponent = configurationByComponent;
|
||||
}
|
||||
|
||||
public RuntimeConfiguration getRuntimeConfiguration() {
|
||||
return this.runtimeConfiguration;
|
||||
}
|
||||
|
||||
public ComponentConfiguration getComponentConfiguration(String componentName) {
|
||||
ComponentConfiguration componentConfiguration = this.configurationByComponent.get(componentName);
|
||||
if (componentConfiguration == null) {
|
||||
String msg = "No configuration exists for the component {0}"; //$NON-NLS-1$
|
||||
msg = MessageFormat.format(msg, componentName);
|
||||
throw new StrolchConfigurationException(msg);
|
||||
}
|
||||
return componentConfiguration;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package li.strolch.runtime;
|
||||
|
||||
public class StrolchConfigurationException extends RuntimeException {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public StrolchConfigurationException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public StrolchConfigurationException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue