[New] added basic configuration modl

This commit is contained in:
Robert von Burg 2013-10-23 23:15:26 +02:00
parent 2f8900374f
commit 8a6b212248
4 changed files with 166 additions and 0 deletions

View File

@ -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);
}
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}