NO-JIRA Fix Possible NPE in connectToNodeInReplicatedCluster
authorMichael André Pearce <michael.andre.pearce@me.com>
Fri, 18 Jan 2019 23:09:53 +0000 (23:09 +0000)
committerHoward Gao <howard.gao@gmail.com>
Mon, 21 Jan 2019 05:26:24 +0000 (13:26 +0800)
Variable possibleLive may be null, as signified by trace log above, also dedupe code.

artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/SharedNothingBackupActivation.java

index 4c360f0..587b8f0 100644 (file)
@@ -213,22 +213,13 @@ public final class SharedNothingBackupActivation extends Activation {
                activeMQServer.getNodeManager().setNodeID(nodeID);
             }
 
-            try {
-               if (logger.isTraceEnabled()) {
-                  logger.trace("Calling clusterController.connectToNodeInReplicatedCluster(" + possibleLive != null ? possibleLive.getA() : null + ")");
-               }
-               clusterControl = clusterController.connectToNodeInReplicatedCluster(possibleLive.getA());
-            } catch (Exception e) {
-               logger.debug(e.getMessage(), e);
-               if (possibleLive != null && possibleLive.getB() != null) {
-                  try {
-                     clusterControl = clusterController.connectToNodeInReplicatedCluster(possibleLive.getB());
-                  } catch (Exception e1) {
-                     clusterControl = null;
-                  }
-               } else {
-                  clusterControl = null;
+            if (possibleLive != null) {
+               clusterControl = tryConnectToNodeInReplicatedCluster(clusterController, possibleLive.getA());
+               if (clusterControl == null) {
+                  clusterControl = tryConnectToNodeInReplicatedCluster(clusterController, possibleLive.getB());
                }
+            } else {
+               clusterControl = null;
             }
             if (clusterControl == null) {
 
@@ -367,6 +358,20 @@ public final class SharedNothingBackupActivation extends Activation {
       }
    }
 
+   private static ClusterControl tryConnectToNodeInReplicatedCluster(ClusterController clusterController, TransportConfiguration tc) {
+      try {
+         if (logger.isTraceEnabled()) {
+            logger.trace("Calling clusterController.connectToNodeInReplicatedCluster(" + tc + ")");
+         }
+         if (tc != null) {
+            return clusterController.connectToNodeInReplicatedCluster(tc);
+         }
+      } catch (Exception e) {
+         logger.debug(e.getMessage(), e);
+      }
+      return null;
+   }
+
    @Override
    public void close(final boolean permanently, boolean restarting) throws Exception {
       synchronized (this) {