From 4e8e65d2a2569aee45f20a09f4f34c6abcf81f59 Mon Sep 17 00:00:00 2001 From: Keith Wall Date: Mon, 31 Mar 2014 11:22:44 +0000 Subject: QPID-5624: Refactor implementation of the 1.3 StoreUpgrader git-svn-id: https://svn.apache.org/repos/asf/qpid/branches/java-broker-bdb-ha2@1583300 13f79535-47bb-0310-9956-ffa450edef68 --- .../configuration/startup/StoreUpgrader1_3.java | 389 ++++++++++++--------- 1 file changed, 216 insertions(+), 173 deletions(-) diff --git a/qpid/java/broker-core/src/main/java/org/apache/qpid/server/configuration/startup/StoreUpgrader1_3.java b/qpid/java/broker-core/src/main/java/org/apache/qpid/server/configuration/startup/StoreUpgrader1_3.java index 913ed4d773..9d30ce754e 100644 --- a/qpid/java/broker-core/src/main/java/org/apache/qpid/server/configuration/startup/StoreUpgrader1_3.java +++ b/qpid/java/broker-core/src/main/java/org/apache/qpid/server/configuration/startup/StoreUpgrader1_3.java @@ -23,10 +23,11 @@ package org.apache.qpid.server.configuration.startup; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.Collections; import java.util.HashMap; import java.util.HashSet; +import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.Set; import org.apache.qpid.server.configuration.ConfigurationEntry; @@ -37,6 +38,7 @@ import org.apache.qpid.server.model.Broker; @SuppressWarnings("serial") public final class StoreUpgrader1_3 extends StoreUpgrader { + public static final String VERSION = "1.3"; private Map _vhostUpgraderMap = new HashMap() @@ -83,263 +85,304 @@ public final class StoreUpgrader1_3 extends StoreUpgrader store.save(changed.toArray(new ConfigurationEntry[changed.size()])); } - public interface VirtualHostEntryUpgrader + private interface VirtualHostEntryUpgrader { ConfigurationEntry upgrade(ConfigurationEntryStore store, ConfigurationEntry vhost); } - public class BdbHaVirtualHostUpgrader implements VirtualHostEntryUpgrader + private class StandardVirtualHostUpgrader implements VirtualHostEntryUpgrader { - private final String[] HA_ATTRIBUTES = - { "storePath", "haNodeName", "haGroupName", "haHelperAddress", "haCoalescingSync", "haNodeAddress", "haDurability", - "haDesignatedPrimary", "haReplicationConfig", "bdbEnvironmentConfig" }; + Map _messageStoreAttributeTransformers = new HashMap() + {{ + put("DERBY", new AttributesTransformer(). + addAttributeTransformer("storePath", copyAttribute()). + addAttributeTransformer("storeUnderfullSize", copyAttribute()). + addAttributeTransformer("storeOverfullSize", copyAttribute()). + addAttributeTransformer("storeType", mutateAttributeValue("DERBY"))); + put("MEMORY", new AttributesTransformer(). + addAttributeTransformer("storeType", mutateAttributeValue("Memory"))); + put("BDB", new AttributesTransformer(). + addAttributeTransformer("storePath", copyAttribute()). + addAttributeTransformer("storeUnderfullSize", copyAttribute()). + addAttributeTransformer("storeOverfullSize", copyAttribute()). + addAttributeTransformer("bdbEnvironmentConfig", copyAttribute()). + addAttributeTransformer("storeType", mutateAttributeValue("BDB"))); + put("JDBC", new AttributesTransformer(). + addAttributeTransformer("storePath", mutateAttributeName("connectionURL")). + addAttributeTransformer("connectionURL", copyAttribute()). + addAttributeTransformer("connectionPool", copyAttribute()). + addAttributeTransformer("jdbcBigIntType", copyAttribute()). + addAttributeTransformer("jdbcBytesForBlob", copyAttribute()). + addAttributeTransformer("jdbcBlobType", copyAttribute()). + addAttributeTransformer("jdbcVarbinaryType", copyAttribute()). + addAttributeTransformer("partitionCount", copyAttribute()). + addAttributeTransformer("maxConnectionsPerPartition", copyAttribute()). + addAttributeTransformer("minConnectionsPerPartition", copyAttribute()). + addAttributeTransformer("storeType", mutateAttributeValue("JDBC"))); + }}; + + Map _configurationStoreAttributeTransformers = new HashMap() + {{ + put("DERBY", new AttributesTransformer(). + addAttributeTransformer("configStorePath", mutateAttributeName("storePath")). + addAttributeTransformer("configStoreType", mutateAttributeName("storeType"), mutateAttributeValue("DERBY"))); + put("MEMORY", new AttributesTransformer(). + addAttributeTransformer("configStoreType", mutateAttributeValue("Memory"))); + put("JSON", new AttributesTransformer(). + addAttributeTransformer("configStorePath", mutateAttributeName("storePath")). + addAttributeTransformer("configStoreType", mutateAttributeName("storeType"), mutateAttributeValue("JSON"))); + put("BDB", new AttributesTransformer(). + addAttributeTransformer("configStorePath", mutateAttributeName("storePath")). + addAttributeTransformer("bdbEnvironmentConfig", copyAttribute()). + addAttributeTransformer("configStoreType", mutateAttributeName("storeType"), mutateAttributeValue("BDB"))); + put("JDBC", new AttributesTransformer(). + addAttributeTransformer("configStorePath", mutateAttributeName("connectionURL")). + addAttributeTransformer("configConnectionURL", mutateAttributeName("connectionURL")). + addAttributeTransformer("connectionPool", copyAttribute()). + addAttributeTransformer("jdbcBigIntType", copyAttribute()). + addAttributeTransformer("jdbcBytesForBlob", copyAttribute()). + addAttributeTransformer("jdbcBlobType", copyAttribute()). + addAttributeTransformer("jdbcVarbinaryType", copyAttribute()). + addAttributeTransformer("partitionCount", copyAttribute()). + addAttributeTransformer("maxConnectionsPerPartition", copyAttribute()). + addAttributeTransformer("minConnectionsPerPartition", copyAttribute()). + addAttributeTransformer("configStoreType", mutateAttributeName("storeType"), mutateAttributeValue("JDBC"))); + }}; @Override public ConfigurationEntry upgrade(ConfigurationEntryStore store, ConfigurationEntry vhost) { Map attributes = vhost.getAttributes(); Map newAttributes = new HashMap(attributes); - Map messageStoreSettings = new HashMap(); - for (String haAttribute : HA_ATTRIBUTES) + String capitalisedStoreType = String.valueOf(attributes.get("storeType")).toUpperCase(); + AttributesTransformer vhAttrsToMessageStoreSettings = _messageStoreAttributeTransformers.get(capitalisedStoreType); + Map messageStoreSettings = null; + if (vhAttrsToMessageStoreSettings != null) { - if (attributes.containsKey(haAttribute)) - { - messageStoreSettings.put(haAttribute, newAttributes.remove(haAttribute)); - } + messageStoreSettings = vhAttrsToMessageStoreSettings.upgrade(attributes); } - if (attributes.containsKey("storeUnderfullSize")) + if (attributes.containsKey("configStoreType")) { - messageStoreSettings.put("storeUnderfullSize", newAttributes.remove("storeUnderfullSize")); + String capitaliseConfigStoreType = ((String) attributes.get("configStoreType")).toUpperCase(); + AttributesTransformer vhAttrsToConfigurationStoreSettings = _configurationStoreAttributeTransformers + .get(capitaliseConfigStoreType); + Map configurationStoreSettings = vhAttrsToConfigurationStoreSettings.upgrade(attributes); + newAttributes.keySet().removeAll(vhAttrsToConfigurationStoreSettings.getNamesToBeDeleted()); + newAttributes.put("configurationStoreSettings", configurationStoreSettings); } - if (attributes.containsKey("storeOverfullSize")) + + if (vhAttrsToMessageStoreSettings != null) { - messageStoreSettings.put("storeOverfullSize", newAttributes.remove("storeOverfullSize")); + newAttributes.keySet().removeAll(vhAttrsToMessageStoreSettings.getNamesToBeDeleted()); + newAttributes.put("messageStoreSettings", messageStoreSettings); } - newAttributes.remove("storeType"); - newAttributes.put("messageStoreSettings", messageStoreSettings); + return new ConfigurationEntry(vhost.getId(), vhost.getType(), newAttributes, vhost.getChildrenIds(), store); } - } - public interface StoreEntryUpgrader + private class BdbHaVirtualHostUpgrader implements VirtualHostEntryUpgrader { - Map upgrade(Map attributes); - Set getNamesToBeDeleted(); + private final AttributesTransformer haAttributesTransformer = new AttributesTransformer(). + addAttributeTransformer("storePath", copyAttribute()). + addAttributeTransformer("storeUnderfullSize", copyAttribute()). + addAttributeTransformer("storeOverfullSize", copyAttribute()). + addAttributeTransformer("haNodeName", copyAttribute()). + addAttributeTransformer("haGroupName", copyAttribute()). + addAttributeTransformer("haHelperAddress", copyAttribute()). + addAttributeTransformer("haCoalescingSync", copyAttribute()). + addAttributeTransformer("haNodeAddress", copyAttribute()). + addAttributeTransformer("haDurability", copyAttribute()). + addAttributeTransformer("haDesignatedPrimary", copyAttribute()). + addAttributeTransformer("haReplicationConfig", copyAttribute()). + addAttributeTransformer("bdbEnvironmentConfig", copyAttribute()). + addAttributeTransformer("storeType", removeAttribute()); + + @Override + public ConfigurationEntry upgrade(ConfigurationEntryStore store, ConfigurationEntry vhost) + { + Map attributes = vhost.getAttributes(); + + Map messageStoreSettings = haAttributesTransformer.upgrade(attributes); + + Map newAttributes = new HashMap(attributes); + newAttributes.keySet().removeAll(haAttributesTransformer.getNamesToBeDeleted()); + newAttributes.put("messageStoreSettings", messageStoreSettings); + + return new ConfigurationEntry(vhost.getId(), vhost.getType(), newAttributes, vhost.getChildrenIds(), store); + } } - public class GenericMessageStoreEntryUpgrader implements StoreEntryUpgrader + private class AttributesTransformer { - private Map _oldToNewNamesMap; - private String _storeType; + private final Map> _transformers = new HashMap>(); + private Set _namesToBeDeleted = new HashSet(); - public GenericMessageStoreEntryUpgrader(String storeType, Map oldToNewNamesMap) + public AttributesTransformer addAttributeTransformer(String string, AttributeTransformer... attributeTransformers) { - _oldToNewNamesMap = oldToNewNamesMap; - _storeType = storeType; + _transformers.put(string, Arrays.asList(attributeTransformers)); + return this; } - @Override public Map upgrade(Map attributes) { - Map messageStoreSettings = new HashMap(); - for (Map.Entry nameMapEntry : _oldToNewNamesMap.entrySet()) + Map settings = new HashMap(); + for (Entry> entry : _transformers.entrySet()) { - String attributeName = nameMapEntry.getKey(); + String attributeName = entry.getKey(); if (attributes.containsKey(attributeName)) { - messageStoreSettings.put(nameMapEntry.getValue(), attributes.get(attributeName)); + Object attributeValue = attributes.get(attributeName); + MutatableEntry newEntry = new MutatableEntry(attributeName, attributeValue); + + List transformers = entry.getValue(); + for (AttributeTransformer attributeTransformer : transformers) + { + newEntry = attributeTransformer.transform(newEntry); + if (newEntry == null) + { + break; + } + } + if (newEntry != null) + { + settings.put(newEntry.getKey(), newEntry.getValue()); + } + + _namesToBeDeleted.add(attributeName); } } - messageStoreSettings.put("storeType", _storeType); - return messageStoreSettings; + return settings; } - @Override public Set getNamesToBeDeleted() { - Set names = new HashSet(_oldToNewNamesMap.keySet()); - names.add("storeType"); - return names; + return _namesToBeDeleted; } + } + private AttributeTransformer copyAttribute() + { + return CopyAttribute.INSTANCE; } - public class JDBCMessageStoreEntryUpgrader implements StoreEntryUpgrader + private AttributeTransformer removeAttribute() { - private final String[] JDBC_ATTRIBUTES = - { "connectionURL", "connectionPool", "jdbcBigIntType", "jdbcBytesForBlob", "jdbcVarbinaryType", "jdbcBlobType", - "partitionCount", "maxConnectionsPerPartition", "minConnectionsPerPartition" }; + return RemoveAttribute.INSTANCE; + } - @Override - public Map upgrade(Map attributes) - { - Map messageStoreSettings = new HashMap(); + private AttributeTransformer mutateAttributeValue(Object newValue) + { + return new MutateAttributeValue(newValue); + } - if (attributes.containsKey("storePath")) - { - messageStoreSettings.put("connectionURL", attributes.get("storePath")); - } + private AttributeTransformer mutateAttributeName(String newName) + { + return new MutateAttributeName(newName); + } - copyJdbcStoreSettings(attributes, messageStoreSettings); + private interface AttributeTransformer + { + MutatableEntry transform(MutatableEntry entry); + } - messageStoreSettings.put("storeType", "JDBC"); - return messageStoreSettings; - } + private static class CopyAttribute implements AttributeTransformer + { + private static final CopyAttribute INSTANCE = new CopyAttribute(); - @Override - public Set getNamesToBeDeleted() + private CopyAttribute() { - Set names = new HashSet(); - names.addAll(Arrays.asList(JDBC_ATTRIBUTES)); - names.add("storePath"); - names.add("storeType"); - return names; } - private void copyJdbcStoreSettings(Map attributes, Map messageStoreSettings) + @Override + public MutatableEntry transform(MutatableEntry entry) { - for (String jdbcAttribute : JDBC_ATTRIBUTES) - { - if (attributes.containsKey(jdbcAttribute)) - { - messageStoreSettings.put(jdbcAttribute, attributes.get(jdbcAttribute)); - } - } + return entry; } - } - public class JDBCConfigurationStoreEntryUpgrader implements StoreEntryUpgrader + private static class RemoveAttribute implements AttributeTransformer { + private static final RemoveAttribute INSTANCE = new RemoveAttribute(); - private final String[] JDBC_ATTRIBUTES = - { "connectionPool", "jdbcBigIntType", "jdbcBytesForBlob", "jdbcVarbinaryType", "jdbcBlobType", "partitionCount", - "maxConnectionsPerPartition", "minConnectionsPerPartition" }; + private RemoveAttribute() + { + } @Override - public Map upgrade(Map attributes) + public MutatableEntry transform(MutatableEntry entry) { - Map messageStoreSettings = new HashMap(); - - if (attributes.containsKey("configStorePath")) - { - messageStoreSettings.put("connectionURL", attributes.get("configStorePath")); - } - - if (attributes.containsKey("configConnectionURL")) - { - messageStoreSettings.put("connectionURL", attributes.get("configConnectionURL")); - } + return null; + } + } - copyJdbcStoreSettings(attributes, messageStoreSettings); + private class MutateAttributeName implements AttributeTransformer + { + private final String _newName; - messageStoreSettings.put("storeType", "JDBC"); - return messageStoreSettings; + public MutateAttributeName(String newName) + { + _newName = newName; } @Override - public Set getNamesToBeDeleted() + public MutatableEntry transform(MutatableEntry entry) { - Set names = new HashSet(); - names.addAll(Arrays.asList(JDBC_ATTRIBUTES)); - names.add("configStorePath"); - names.add("configStoreType"); - names.add("configConnectionURL"); - return names; + entry.setKey(_newName); + return entry; } + } + + private static class MutateAttributeValue implements AttributeTransformer + { + private final Object _newValue; - private void copyJdbcStoreSettings(Map attributes, Map messageStoreSettings) + public MutateAttributeValue(Object newValue) { - for (String jdbcAttribute : JDBC_ATTRIBUTES) - { - if (attributes.containsKey(jdbcAttribute)) - { - messageStoreSettings.put(jdbcAttribute, attributes.get(jdbcAttribute)); - } - } + _newValue = newValue; + } + + @Override + public MutatableEntry transform(MutatableEntry entry) + { + entry.setValue(_newValue); + return entry; } } - public class StandardVirtualHostUpgrader implements VirtualHostEntryUpgrader + private static class MutatableEntry { - Map _messageStoreEntryUpgrader = new HashMap() - {{ - put("JDBC", new JDBCMessageStoreEntryUpgrader()); - put("BDB", new GenericMessageStoreEntryUpgrader("BDB", new HashMap() - {{ - put("storePath", "storePath"); - put("bdbEnvironmentConfig", "bdbEnvironmentConfig"); - put("storeUnderfullSize", "storeUnderfullSize"); - put("storeOverfullSize", "storeOverfullSize"); - }})); - put("DERBY", new GenericMessageStoreEntryUpgrader("DERBY", new HashMap() - {{ - put("storePath", "storePath"); - put("storeUnderfullSize", "storeUnderfullSize"); - put("storeOverfullSize", "storeOverfullSize"); - }})); - put("MEMORY", new GenericMessageStoreEntryUpgrader("Memory", Collections. emptyMap())); - }}; - Map _configurationStoreEntryUpgrader = new HashMap() - {{ - put("JDBC", new JDBCConfigurationStoreEntryUpgrader()); - put("DERBY", new GenericMessageStoreEntryUpgrader("DERBY", new HashMap() - {{ - put("configStorePath", "storePath"); - put("configStoreType", "storeType"); - }})); - put("BDB", new GenericMessageStoreEntryUpgrader("BDB", new HashMap() - {{ - put("configStoreType", "storeType"); - put("configStorePath", "storePath"); - put("bdbEnvironmentConfig", "bdbEnvironmentConfig"); - }})); - put("MEMORY", new GenericMessageStoreEntryUpgrader("Memory", - Collections. singletonMap("configStoreType", "storeType"))); - put("JSON", new GenericMessageStoreEntryUpgrader("JSON", new HashMap() - {{ - put("configStorePath", "storePath"); - put("configStoreType", "storeType"); - }})); - }}; + private String _key; + private Object _value; - @Override - public ConfigurationEntry upgrade(ConfigurationEntryStore store, ConfigurationEntry vhost) + public MutatableEntry(String key, Object value) { - Map attributes = vhost.getAttributes(); - Map newAttributes = new HashMap(attributes); - - String capitalisedStoreType = String.valueOf(attributes.get("storeType")).toUpperCase(); - StoreEntryUpgrader messageStoreSettingsUpgrader = _messageStoreEntryUpgrader.get(capitalisedStoreType); - Map messageStoreSettings = null; - if (messageStoreSettingsUpgrader != null) - { - messageStoreSettings = messageStoreSettingsUpgrader.upgrade(attributes); - } + _key = key; + _value = value; + } - if (attributes.containsKey("configStoreType")) - { - String capitaliseConfigStoreType = ((String) attributes.get("configStoreType")).toUpperCase(); - StoreEntryUpgrader configurationStoreSettingsUpgrader = _configurationStoreEntryUpgrader - .get(capitaliseConfigStoreType); - Map configurationStoreSettings = configurationStoreSettingsUpgrader.upgrade(attributes); - newAttributes.keySet().removeAll(configurationStoreSettingsUpgrader.getNamesToBeDeleted()); - newAttributes.put("configurationStoreSettings", configurationStoreSettings); - } + public String getKey() + { + return _key; + } - if (messageStoreSettingsUpgrader != null) - { - newAttributes.keySet().removeAll(messageStoreSettingsUpgrader.getNamesToBeDeleted()); - newAttributes.put("messageStoreSettings", messageStoreSettings); - } + public void setKey(String key) + { + _key = key; + } - return new ConfigurationEntry(vhost.getId(), vhost.getType(), newAttributes, vhost.getChildrenIds(), store); + public Object getValue() + { + return _value; } + public void setValue(Object value) + { + _value = value; + } } } \ No newline at end of file -- cgit v1.2.1