Advertisement
Andre_Aga

Untitled

Feb 13th, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. package cz.cuni.mff.xrg.uv.boost.dpu.config;
  2.  
  3. import cz.cuni.mff.xrg.uv.boost.dpu.config.ConfigException;
  4. import cz.cuni.mff.xrg.uv.boost.dpu.config.ConfigHistoryEntry;
  5. import cz.cuni.mff.xrg.uv.boost.dpu.config.VersionedConfig;
  6. import cz.cuni.mff.xrg.uv.service.serialization.xml.SerializationXmlFailure;
  7. import cz.cuni.mff.xrg.uv.service.serialization.xml.SerializationXmlGeneral;
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10.  
  11. public class ConfigHistory<CONFIG> {
  12. private static final Logger LOG = LoggerFactory.getLogger(ConfigHistory.class);
  13. private final ConfigHistoryEntry<?, CONFIG> endOfHistory;
  14. private final Class<CONFIG> finalClass;
  15.  
  16. ConfigHistory(ConfigHistoryEntry<?, CONFIG> endOfHistory, Class<CONFIG> finalClass) {
  17. this.endOfHistory = endOfHistory;
  18. if(finalClass == null) {
  19. this.finalClass = endOfHistory.configClass;
  20. } else {
  21. this.finalClass = finalClass;
  22. }
  23.  
  24. }
  25.  
  26. CONFIG parse(String config, SerializationXmlGeneral serializer) throws SerializationXmlFailure, ConfigException {
  27. String finalClassName = getClassName(this.finalClass);
  28. if(config.contains(finalClassName)) {
  29. return serializer.convert(this.finalClass, config);
  30. } else if(this.endOfHistory == null) {
  31. LOG.error("Can\'t parse config for ({}), there is no history, value is: {}", finalClassName, config);
  32. throw new ConfigException("Can\'t parse given object");
  33. } else {
  34. Object object = null;
  35. ConfigHistoryEntry current = this.endOfHistory;
  36.  
  37. do {
  38. if(config.contains(current.alias)) {
  39. object = serializer.convert(current.configClass, config);
  40. break;
  41. }
  42.  
  43. current = current.previous;
  44. } while(current != null);
  45.  
  46. if(object == null) {
  47. throw new ConfigException("Can\'t parse given object");
  48. } else {
  49. while(!object.getClass().equals(this.finalClass)) {
  50. if(!(object instanceof VersionedConfig)) {
  51. throw new ConfigException("Can\'t update given configuration to current.");
  52. }
  53.  
  54. object = ((VersionedConfig)object).toNextVersion();
  55. }
  56.  
  57. return object;
  58. }
  59. }
  60. }
  61.  
  62. public Class<CONFIG> getFinalClass() {
  63. return this.finalClass;
  64. }
  65.  
  66. private static String getClassName(Class<?> clazz) {
  67. String className = clazz.getCanonicalName().replace("_", "__");
  68. if(clazz.getEnclosingClass() != null) {
  69. int lastDot = className.lastIndexOf(".");
  70. className = className.substring(0, lastDot) + "_-" + className.substring(lastDot + 1);
  71. }
  72.  
  73. return className;
  74. }
  75.  
  76. public static <T, S extends VersionedConfig<T>> ConfigHistoryEntry<S, T> create(Class<S> clazz) {
  77. return create(clazz, getClassName(clazz));
  78. }
  79.  
  80. public static <T, S extends VersionedConfig<T>> ConfigHistoryEntry<S, T> create(Class<S> clazz, String alias) {
  81. return new ConfigHistoryEntry(alias, clazz, (ConfigHistoryEntry)null);
  82. }
  83.  
  84. public static <T> ConfigHistory<T> createNoHistory(Class<T> clazz) {
  85. return new ConfigHistory((ConfigHistoryEntry)null, clazz);
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement