From ef1bdd26bd80743c12ee44f7d8d950eefa1c9107 Mon Sep 17 00:00:00 2001 From: Victor Verbitsky Date: Fri, 11 Jul 2014 11:21:44 +0400 Subject: [PATCH] Wrapping for enums. It's needed because jackson statically caching enums classes. After redeploing it's leads to ClassLoader hell. --- .../serial/graph/node/EnumNode.java | 78 +++++++++++++++++++ .../serial/graph/node/NodeBuilder.java | 8 +- 2 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 serial/base/src/main/java/org/switchyard/serial/graph/node/EnumNode.java diff --git a/serial/base/src/main/java/org/switchyard/serial/graph/node/EnumNode.java b/serial/base/src/main/java/org/switchyard/serial/graph/node/EnumNode.java new file mode 100644 index 000000000..acf85ca70 --- /dev/null +++ b/serial/base/src/main/java/org/switchyard/serial/graph/node/EnumNode.java @@ -0,0 +1,78 @@ +package org.switchyard.serial.graph.node; + +import org.switchyard.common.type.Classes; +import org.switchyard.serial.graph.Graph; + +/** + * A node representing a Enum value. + */ +@SuppressWarnings("serial") +public class EnumNode implements Node { + + private String _enumType; + private String _valueName; + + /** + * Default constructor. + */ + public EnumNode() { + } + + /** + * Gets the enum type. + * + * @return the enum type + */ + public String getEnumType() { + return _enumType; + } + + /** + * Sets the enum type. + * + * @param enumType + * the enum type + */ + public void setEnumType(String enumType) { + _enumType = enumType; + } + + /** + * Gets the enum value name. + * + * @return the enum value name + */ + public String getValueName() { + return _valueName; + } + + /** + * Sets the enum value name. + * + * @param valueName + * the enum value name + */ + public void setValueName(String valueName) { + _valueName = valueName; + } + + /** + * {@inheritDoc} + */ + @Override + public void compose(Object obj, Graph graph) { + Enum enumValue = (Enum) obj; + setEnumType(enumValue.getClass().getName()); + setValueName(enumValue.name()); + } + + /** + * {@inheritDoc} + */ + @Override + @SuppressWarnings({ "unchecked", "rawtypes" }) + public Object decompose(Graph graph) { + Class enumClass = Classes.forName(getEnumType(), getClass().getClassLoader(), Thread.currentThread().getContextClassLoader()); + return Enum.valueOf(enumClass, getValueName()); + } +} diff --git a/serial/base/src/main/java/org/switchyard/serial/graph/node/NodeBuilder.java b/serial/base/src/main/java/org/switchyard/serial/graph/node/NodeBuilder.java index cdad6d7cd..dee79153b 100644 --- a/serial/base/src/main/java/org/switchyard/serial/graph/node/NodeBuilder.java +++ b/serial/base/src/main/java/org/switchyard/serial/graph/node/NodeBuilder.java @@ -54,7 +54,11 @@ public static Integer build(Object obj, Graph graph) { return id; } Class clazz = obj.getClass(); - if (isSimple(clazz)) { + if (clazz.isEnum()) { + Node node = new EnumNode(); + graph.putReference(id, node); + node.compose(obj, graph); + } else if (isSimple(clazz)) { graph.putReference(id, obj); } else if (isArray(clazz)) { if (isSimple(clazz.getComponentType())) { @@ -147,7 +151,7 @@ static boolean isQName(Class clazz) { } static boolean isSimple(Class clazz) { - if (clazz.isPrimitive() || clazz.isEnum()) { + if (clazz.isPrimitive()) { return true; } for (Class st : SIMPLE_TYPES) {