Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cz.cuni.mff.xrg.uv.boost.dpu.config;
- import cz.cuni.mff.xrg.uv.boost.dpu.config.ConfigException;
- import cz.cuni.mff.xrg.uv.boost.dpu.config.ConfigHistoryEntry;
- import cz.cuni.mff.xrg.uv.boost.dpu.config.VersionedConfig;
- import cz.cuni.mff.xrg.uv.service.serialization.xml.SerializationXmlFailure;
- import cz.cuni.mff.xrg.uv.service.serialization.xml.SerializationXmlGeneral;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- public class ConfigHistory<CONFIG> {
- private static final Logger LOG = LoggerFactory.getLogger(ConfigHistory.class);
- private final ConfigHistoryEntry<?, CONFIG> endOfHistory;
- private final Class<CONFIG> finalClass;
- ConfigHistory(ConfigHistoryEntry<?, CONFIG> endOfHistory, Class<CONFIG> finalClass) {
- this.endOfHistory = endOfHistory;
- if(finalClass == null) {
- this.finalClass = endOfHistory.configClass;
- } else {
- this.finalClass = finalClass;
- }
- }
- CONFIG parse(String config, SerializationXmlGeneral serializer) throws SerializationXmlFailure, ConfigException {
- String finalClassName = getClassName(this.finalClass);
- if(config.contains(finalClassName)) {
- return serializer.convert(this.finalClass, config);
- } else if(this.endOfHistory == null) {
- LOG.error("Can\'t parse config for ({}), there is no history, value is: {}", finalClassName, config);
- throw new ConfigException("Can\'t parse given object");
- } else {
- Object object = null;
- ConfigHistoryEntry current = this.endOfHistory;
- do {
- if(config.contains(current.alias)) {
- object = serializer.convert(current.configClass, config);
- break;
- }
- current = current.previous;
- } while(current != null);
- if(object == null) {
- throw new ConfigException("Can\'t parse given object");
- } else {
- while(!object.getClass().equals(this.finalClass)) {
- if(!(object instanceof VersionedConfig)) {
- throw new ConfigException("Can\'t update given configuration to current.");
- }
- object = ((VersionedConfig)object).toNextVersion();
- }
- return object;
- }
- }
- }
- public Class<CONFIG> getFinalClass() {
- return this.finalClass;
- }
- private static String getClassName(Class<?> clazz) {
- String className = clazz.getCanonicalName().replace("_", "__");
- if(clazz.getEnclosingClass() != null) {
- int lastDot = className.lastIndexOf(".");
- className = className.substring(0, lastDot) + "_-" + className.substring(lastDot + 1);
- }
- return className;
- }
- public static <T, S extends VersionedConfig<T>> ConfigHistoryEntry<S, T> create(Class<S> clazz) {
- return create(clazz, getClassName(clazz));
- }
- public static <T, S extends VersionedConfig<T>> ConfigHistoryEntry<S, T> create(Class<S> clazz, String alias) {
- return new ConfigHistoryEntry(alias, clazz, (ConfigHistoryEntry)null);
- }
- public static <T> ConfigHistory<T> createNoHistory(Class<T> clazz) {
- return new ConfigHistory((ConfigHistoryEntry)null, clazz);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement