From 76938fe44a98eb802a707ae630cc4f2d3008085e Mon Sep 17 00:00:00 2001 From: Clebert Suconic Date: Tue, 26 Jul 2016 16:01:36 -0400 Subject: [PATCH] ARTEMIS-565 more tweaks on the JSON replacement --- .../activemq/artemis/api/core/JsonUtil.java | 38 +++++++++++++++++++ .../api/core/management/ManagementHelper.java | 18 +++++---- .../impl/ManagementServiceImpl.java | 4 ++ .../ActiveMQServerControlUsingCoreTest.java | 24 ++++++------ .../management/CoreMessagingProxy.java | 11 +----- 5 files changed, 66 insertions(+), 29 deletions(-) diff --git a/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/core/JsonUtil.java b/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/core/JsonUtil.java index dbfec9aae9..5a7a5263fa 100644 --- a/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/core/JsonUtil.java +++ b/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/core/JsonUtil.java @@ -260,6 +260,44 @@ public final class JsonUtil { return Json.createReader(new StringReader(jsonString)).readObject(); } + public static Object convertJsonValue(Object jsonValue, Class desiredType) { + if (jsonValue instanceof JsonNumber) { + JsonNumber number = (JsonNumber) jsonValue; + + if (desiredType == null || desiredType == Long.class || desiredType == Long.TYPE) { + return number.longValue(); + } + else if (desiredType == Integer.class || desiredType == Integer.TYPE) { + return number.intValue(); + } + else if (desiredType == Double.class || desiredType == Double.TYPE) { + return number.doubleValue(); + } + else { + return number.longValue(); + } + } + else if (jsonValue instanceof JsonString) { + return ((JsonString) jsonValue).getString(); + } + else if (jsonValue instanceof JsonValue) { + if (jsonValue == JsonValue.TRUE) { + return true; + } + else if (jsonValue == JsonValue.FALSE) { + return false; + } + else { + return jsonValue.toString(); + } + } + else { + return jsonValue; + } + } + + + private JsonUtil() { } diff --git a/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/core/management/ManagementHelper.java b/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/core/management/ManagementHelper.java index a4c08805dd..78405b0ecf 100644 --- a/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/core/management/ManagementHelper.java +++ b/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/core/management/ManagementHelper.java @@ -21,7 +21,6 @@ import org.apache.activemq.artemis.api.core.Message; import org.apache.activemq.artemis.api.core.SimpleString; import javax.json.JsonArray; -import javax.json.JsonNumber; /** * Helper class to use ActiveMQ Artemis Core messages to manage server resources. @@ -216,15 +215,20 @@ public final class ManagementHelper { * and the result will be a String corresponding to the server exception. */ public static Object getResult(final Message message) throws Exception { + return getResult(message, null); + } + + /** + * Returns the result of an operation invocation or an attribute value. + *
+ * If an error occurred on the server, {@link #hasOperationSucceeded(Message)} will return {@code false}. + * and the result will be a String corresponding to the server exception. + */ + public static Object getResult(final Message message, Class desiredType) throws Exception { Object[] res = ManagementHelper.getResults(message); if (res != null) { - if (res[0] instanceof JsonNumber) { - return ((JsonNumber)res[0]).longValue(); - } - else { - return res[0]; - } + return JsonUtil.convertJsonValue(res[0], desiredType); } else { return null; diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/management/impl/ManagementServiceImpl.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/management/impl/ManagementServiceImpl.java index a43c666ca7..22b4e50926 100644 --- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/management/impl/ManagementServiceImpl.java +++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/management/impl/ManagementServiceImpl.java @@ -33,6 +33,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledExecutorService; import org.apache.activemq.artemis.api.core.BroadcastGroupConfiguration; +import org.apache.activemq.artemis.api.core.JsonUtil; import org.apache.activemq.artemis.api.core.SimpleString; import org.apache.activemq.artemis.api.core.TransportConfiguration; import org.apache.activemq.artemis.api.core.management.AcceptorControl; @@ -721,6 +722,9 @@ public class ManagementServiceImpl implements ManagementService { if (params[i] == null) { continue; } + + params[i] = JsonUtil.convertJsonValue(params[i], paramTypes[i]); + if (paramTypes[i].isAssignableFrom(params[i].getClass()) || paramTypes[i] == Long.TYPE && params[i].getClass() == Integer.class || paramTypes[i] == Double.TYPE && params[i].getClass() == Integer.class || paramTypes[i] == Long.TYPE && params[i].getClass() == Long.class || diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/management/ActiveMQServerControlUsingCoreTest.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/management/ActiveMQServerControlUsingCoreTest.java index 498c31f8f9..89debb624e 100644 --- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/management/ActiveMQServerControlUsingCoreTest.java +++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/management/ActiveMQServerControlUsingCoreTest.java @@ -175,7 +175,7 @@ public class ActiveMQServerControlUsingCoreTest extends ActiveMQServerControlTes @Override public int getConnectionCount() { - return (Integer) proxy.retrieveAttributeValue("connectionCount"); + return (Integer) proxy.retrieveAttributeValue("connectionCount", Integer.class); } @Override @@ -245,7 +245,7 @@ public class ActiveMQServerControlUsingCoreTest extends ActiveMQServerControlTes @Override public int getIDCacheSize() { - return (Integer) proxy.retrieveAttributeValue("IDCacheSize"); + return (Integer) proxy.retrieveAttributeValue("IDCacheSize", Integer.class); } public String[] getInterceptorClassNames() { @@ -269,17 +269,17 @@ public class ActiveMQServerControlUsingCoreTest extends ActiveMQServerControlTes @Override public int getJournalFileSize() { - return (Integer) proxy.retrieveAttributeValue("journalFileSize"); + return (Integer) proxy.retrieveAttributeValue("journalFileSize", Integer.class); } @Override public int getJournalMaxIO() { - return (Integer) proxy.retrieveAttributeValue("journalMaxIO"); + return (Integer) proxy.retrieveAttributeValue("journalMaxIO", Integer.class); } @Override public int getJournalMinFiles() { - return (Integer) proxy.retrieveAttributeValue("journalMinFiles"); + return (Integer) proxy.retrieveAttributeValue("journalMinFiles", Integer.class); } @Override @@ -329,12 +329,12 @@ public class ActiveMQServerControlUsingCoreTest extends ActiveMQServerControlTes @Override public int getScheduledThreadPoolMaxSize() { - return (Integer) proxy.retrieveAttributeValue("scheduledThreadPoolMaxSize"); + return (Integer) proxy.retrieveAttributeValue("scheduledThreadPoolMaxSize", Integer.class); } @Override public int getThreadPoolMaxSize() { - return (Integer) proxy.retrieveAttributeValue("threadPoolMaxSize"); + return (Integer) proxy.retrieveAttributeValue("threadPoolMaxSize", Integer.class); } @Override @@ -354,7 +354,7 @@ public class ActiveMQServerControlUsingCoreTest extends ActiveMQServerControlTes @Override public String getVersion() { - return (String) proxy.retrieveAttributeValue("version"); + return proxy.retrieveAttributeValue("version").toString(); } @Override @@ -517,22 +517,22 @@ public class ActiveMQServerControlUsingCoreTest extends ActiveMQServerControlTes @Override public int getJournalBufferSize() { - return (Integer) proxy.retrieveAttributeValue("JournalBufferSize"); + return (Integer) proxy.retrieveAttributeValue("JournalBufferSize", Integer.class); } @Override public int getJournalBufferTimeout() { - return (Integer) proxy.retrieveAttributeValue("JournalBufferTimeout"); + return (Integer) proxy.retrieveAttributeValue("JournalBufferTimeout", Integer.class); } @Override public int getJournalCompactMinFiles() { - return (Integer) proxy.retrieveAttributeValue("JournalCompactMinFiles"); + return (Integer) proxy.retrieveAttributeValue("JournalCompactMinFiles", Integer.class); } @Override public int getJournalCompactPercentage() { - return (Integer) proxy.retrieveAttributeValue("JournalCompactPercentage"); + return (Integer) proxy.retrieveAttributeValue("JournalCompactPercentage", Integer.class); } @Override diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/management/CoreMessagingProxy.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/management/CoreMessagingProxy.java index fca1d1f0a0..e9168a25f0 100644 --- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/management/CoreMessagingProxy.java +++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/management/CoreMessagingProxy.java @@ -63,16 +63,7 @@ public class CoreMessagingProxy { ClientMessage reply; try { reply = requestor.request(m); - Object result = ManagementHelper.getResult(reply); - - if (desiredType != null && desiredType != result.getClass()) { - // Conversions - if (desiredType == Long.class && result.getClass() == Integer.class) { - Integer in = (Integer) result; - - result = new Long(in.intValue()); - } - } + Object result = ManagementHelper.getResult(reply, desiredType); return result; }