From c26c970ddca54882d8de14cf6e0fba502cf777fb Mon Sep 17 00:00:00 2001 From: Justin Bertram Date: Mon, 12 Feb 2018 13:18:57 -0600 Subject: [PATCH] ARTEMIS-1666 refactor to avoid duplicate code and fix bug --- .../jms/server/impl/JMSServerManagerImpl.java | 107 +----------------- .../jms/transaction/JMSTransactionDetail.java | 2 +- .../impl/ActiveMQServerControlImpl.java | 13 ++- .../transaction/TransactionDetailFactory.java | 24 ++++ .../impl/CoreTransactionDetail.java | 2 +- 5 files changed, 39 insertions(+), 109 deletions(-) create mode 100644 artemis-server/src/main/java/org/apache/activemq/artemis/core/transaction/TransactionDetailFactory.java diff --git a/artemis-jms-server/src/main/java/org/apache/activemq/artemis/jms/server/impl/JMSServerManagerImpl.java b/artemis-jms-server/src/main/java/org/apache/activemq/artemis/jms/server/impl/JMSServerManagerImpl.java index 420b22b0f3..69d4b90bc5 100644 --- a/artemis-jms-server/src/main/java/org/apache/activemq/artemis/jms/server/impl/JMSServerManagerImpl.java +++ b/artemis-jms-server/src/main/java/org/apache/activemq/artemis/jms/server/impl/JMSServerManagerImpl.java @@ -16,11 +16,7 @@ */ package org.apache.activemq.artemis.jms.server.impl; -import javax.json.JsonArray; -import javax.json.JsonArrayBuilder; -import javax.json.JsonObject; import javax.naming.NamingException; -import javax.transaction.xa.Xid; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; @@ -28,14 +24,11 @@ import java.net.InetAddress; import java.net.URL; import java.net.UnknownHostException; import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.Map.Entry; import java.util.Set; import java.util.concurrent.atomic.AtomicBoolean; @@ -64,9 +57,6 @@ import org.apache.activemq.artemis.core.server.management.Notification; import org.apache.activemq.artemis.core.server.reload.ReloadCallback; import org.apache.activemq.artemis.core.server.reload.ReloadManager; import org.apache.activemq.artemis.core.settings.impl.AddressSettings; -import org.apache.activemq.artemis.core.transaction.ResourceManager; -import org.apache.activemq.artemis.core.transaction.Transaction; -import org.apache.activemq.artemis.core.transaction.TransactionDetail; import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; import org.apache.activemq.artemis.jms.client.ActiveMQDestination; import org.apache.activemq.artemis.jms.client.ActiveMQQueue; @@ -90,7 +80,6 @@ import org.apache.activemq.artemis.jms.server.config.impl.FileJMSConfiguration; import org.apache.activemq.artemis.jms.server.management.JMSNotificationType; import org.apache.activemq.artemis.jms.transaction.JMSTransactionDetail; import org.apache.activemq.artemis.spi.core.naming.BindingRegistry; -import org.apache.activemq.artemis.utils.JsonLoader; import org.apache.activemq.artemis.utils.SelectorTranslator; import org.apache.activemq.artemis.utils.TimeAndCounterIDGenerator; import org.apache.activemq.artemis.utils.XMLUtil; @@ -1337,104 +1326,12 @@ public class JMSServerManagerImpl implements JMSServerManager, ActivateCallback @Override public String listPreparedTransactionDetailsAsJSON() throws Exception { - ResourceManager resourceManager = server.getResourceManager(); - Map xids = resourceManager.getPreparedTransactionsWithCreationTime(); - if (xids == null || xids.size() == 0) { - return ""; - } - - ArrayList> xidsSortedByCreationTime = new ArrayList<>(xids.entrySet()); - Collections.sort(xidsSortedByCreationTime, new Comparator>() { - @Override - public int compare(final Entry entry1, final Entry entry2) { - // sort by creation time, oldest first - return (int) (entry1.getValue() - entry2.getValue()); - } - }); - - JsonArrayBuilder txDetailListJson = JsonLoader.createArrayBuilder(); - for (Map.Entry entry : xidsSortedByCreationTime) { - Xid xid = entry.getKey(); - Transaction tx = resourceManager.getTransaction(xid); - if (tx == null) { - continue; - } - TransactionDetail detail = new JMSTransactionDetail(xid, tx, entry.getValue()); - txDetailListJson.add(detail.toJSON()); - } - return txDetailListJson.toString(); + return server.getActiveMQServerControl().listPreparedTransactionDetailsAsJSON((xid, tx, creation) -> new JMSTransactionDetail(xid, tx, creation)); } @Override public String listPreparedTransactionDetailsAsHTML() throws Exception { - ResourceManager resourceManager = server.getResourceManager(); - Map xids = resourceManager.getPreparedTransactionsWithCreationTime(); - if (xids == null || xids.size() == 0) { - return "

*** Prepared Transaction Details ***

No entry.

"; - } - - ArrayList> xidsSortedByCreationTime = new ArrayList<>(xids.entrySet()); - Collections.sort(xidsSortedByCreationTime, new Comparator>() { - @Override - public int compare(final Entry entry1, final Entry entry2) { - // sort by creation time, oldest first - return (int) (entry1.getValue() - entry2.getValue()); - } - }); - - StringBuilder html = new StringBuilder(); - html.append("

*** Prepared Transaction Details ***

"); - - for (Map.Entry entry : xidsSortedByCreationTime) { - Xid xid = entry.getKey(); - Transaction tx = resourceManager.getTransaction(xid); - if (tx == null) { - continue; - } - TransactionDetail detail = new JMSTransactionDetail(xid, tx, entry.getValue()); - JsonObject txJson = detail.toJSON(); - - html.append(""); - html.append(""); - html.append(""); - html.append(""); - html.append(""); - html.append(""); - html.append(""); - html.append(""); - html.append(""); - html.append(""); - html.append(""); - - html.append(""); - html.append(""); - html.append("
creation_time" + txJson.get(TransactionDetail.KEY_CREATION_TIME) + "xid_as_base_64" + txJson.get(TransactionDetail.KEY_XID_AS_BASE64) + "
xid_format_id" + txJson.get(TransactionDetail.KEY_XID_FORMAT_ID) + "xid_global_txid" + txJson.get(TransactionDetail.KEY_XID_GLOBAL_TXID) + "xid_branch_qual" + txJson.get(TransactionDetail.KEY_XID_BRANCH_QUAL) + "
Message List
"); - html.append(""); - - JsonArray msgs = txJson.getJsonArray(TransactionDetail.KEY_TX_RELATED_MESSAGES); - for (int i = 0; i < msgs.size(); i++) { - JsonObject msgJson = msgs.getJsonObject(i); - JsonObject props = msgJson.getJsonObject(TransactionDetail.KEY_MSG_PROPERTIES); - StringBuilder propstr = new StringBuilder(); - - for (String key : props.keySet()) { - propstr.append(key); - propstr.append("="); - propstr.append(props.get(key)); - propstr.append(", "); - } - - html.append(""); - html.append(""); - html.append(""); - html.append(""); - html.append(""); - } - html.append("
operation_type" + msgJson.get(TransactionDetail.KEY_MSG_OP_TYPE) + ""); - html.append("message_type" + msgJson.get(TransactionDetail.KEY_MSG_TYPE) + "
properties" + propstr.toString() + "

"); - } - - return html.toString(); + return server.getActiveMQServerControl().listPreparedTransactionDetailsAsHTML((xid, tx, creation) -> new JMSTransactionDetail(xid, tx, creation)); } // Public -------------------------------------------------------- diff --git a/artemis-jms-server/src/main/java/org/apache/activemq/artemis/jms/transaction/JMSTransactionDetail.java b/artemis-jms-server/src/main/java/org/apache/activemq/artemis/jms/transaction/JMSTransactionDetail.java index ecb4ccb9b5..f703522930 100644 --- a/artemis-jms-server/src/main/java/org/apache/activemq/artemis/jms/transaction/JMSTransactionDetail.java +++ b/artemis-jms-server/src/main/java/org/apache/activemq/artemis/jms/transaction/JMSTransactionDetail.java @@ -32,7 +32,7 @@ import org.apache.activemq.artemis.jms.client.ActiveMQTextMessage; public class JMSTransactionDetail extends TransactionDetail { - public JMSTransactionDetail(Xid xid, Transaction tx, Long creation) throws Exception { + public JMSTransactionDetail(Xid xid, Transaction tx, Long creation) { super(xid, tx, creation); } diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/management/impl/ActiveMQServerControlImpl.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/management/impl/ActiveMQServerControlImpl.java index 41d7b30dc7..73a731d516 100644 --- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/management/impl/ActiveMQServerControlImpl.java +++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/management/impl/ActiveMQServerControlImpl.java @@ -114,6 +114,7 @@ import org.apache.activemq.artemis.core.settings.impl.SlowConsumerPolicy; import org.apache.activemq.artemis.core.transaction.ResourceManager; import org.apache.activemq.artemis.core.transaction.Transaction; import org.apache.activemq.artemis.core.transaction.TransactionDetail; +import org.apache.activemq.artemis.core.transaction.TransactionDetailFactory; import org.apache.activemq.artemis.core.transaction.impl.CoreTransactionDetail; import org.apache.activemq.artemis.core.transaction.impl.XidImpl; import org.apache.activemq.artemis.spi.core.protocol.RemotingConnection; @@ -1241,6 +1242,10 @@ public class ActiveMQServerControlImpl extends AbstractControl implements Active @Override public String listPreparedTransactionDetailsAsJSON() throws Exception { + return listPreparedTransactionDetailsAsJSON((xid, tx, creation) -> new CoreTransactionDetail(xid, tx, creation)); + } + + public String listPreparedTransactionDetailsAsJSON(TransactionDetailFactory factory) throws Exception { checkStarted(); clearIO(); @@ -1269,7 +1274,7 @@ public class ActiveMQServerControlImpl extends AbstractControl implements Active continue; } - TransactionDetail detail = new CoreTransactionDetail(xid, tx, entry.getValue()); + TransactionDetail detail = factory.createTransactionDetail(xid, tx, entry.getValue()); txDetailListJson.add(detail.toJSON()); } @@ -1281,6 +1286,10 @@ public class ActiveMQServerControlImpl extends AbstractControl implements Active @Override public String listPreparedTransactionDetailsAsHTML() throws Exception { + return listPreparedTransactionDetailsAsHTML((xid, tx, creation) -> new CoreTransactionDetail(xid, tx, creation)); + } + + public String listPreparedTransactionDetailsAsHTML(TransactionDetailFactory factory) throws Exception { checkStarted(); clearIO(); @@ -1311,7 +1320,7 @@ public class ActiveMQServerControlImpl extends AbstractControl implements Active continue; } - TransactionDetail detail = new CoreTransactionDetail(xid, tx, entry.getValue()); + TransactionDetail detail = factory.createTransactionDetail(xid, tx, entry.getValue()); JsonObject txJson = detail.toJSON(); diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/transaction/TransactionDetailFactory.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/transaction/TransactionDetailFactory.java new file mode 100644 index 0000000000..7f5e8523ec --- /dev/null +++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/transaction/TransactionDetailFactory.java @@ -0,0 +1,24 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.activemq.artemis.core.transaction; + +import javax.transaction.xa.Xid; + +public interface TransactionDetailFactory { + TransactionDetail createTransactionDetail(Xid xid, Transaction tx, Long creation); +} diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/transaction/impl/CoreTransactionDetail.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/transaction/impl/CoreTransactionDetail.java index 95036daa98..e0ea8910d6 100644 --- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/transaction/impl/CoreTransactionDetail.java +++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/transaction/impl/CoreTransactionDetail.java @@ -26,7 +26,7 @@ import org.apache.activemq.artemis.core.transaction.TransactionDetail; public class CoreTransactionDetail extends TransactionDetail { - public CoreTransactionDetail(Xid xid, Transaction tx, Long creation) throws Exception { + public CoreTransactionDetail(Xid xid, Transaction tx, Long creation) { super(xid, tx, creation); }