You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by ni...@apache.org on 2019/07/27 11:30:42 UTC

[servicecomb-pack] 04/05: SCB-1391 Polish code with suggestion of Willem Jiang

This is an automated email from the ASF dual-hosted git repository.

ningjiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-pack.git

commit 58584d7e979a7deb815ccfd7795514bef45eb74a
Author: Daniel Qian <ch...@gmail.com>
AuthorDate: Thu Jul 25 09:47:58 2019 +0800

    SCB-1391 Polish code with suggestion of Willem Jiang
---
 .../pack/omega/transaction/TransactionAspect.java  | 12 ++--
 .../transaction/TransactionContextHelper.java      | 59 +++++++++++++++++++
 .../omega/transaction/TransactionContextUtils.java | 40 -------------
 .../transaction/tcc/TccParticipatorAspect.java     | 16 ++++--
 ...Test.java => TransactionContextHelperTest.java} | 66 ++++++++++++++--------
 5 files changed, 120 insertions(+), 73 deletions(-)

diff --git a/omega/omega-transaction/src/main/java/org/apache/servicecomb/pack/omega/transaction/TransactionAspect.java b/omega/omega-transaction/src/main/java/org/apache/servicecomb/pack/omega/transaction/TransactionAspect.java
index 34275a6..5729973 100644
--- a/omega/omega-transaction/src/main/java/org/apache/servicecomb/pack/omega/transaction/TransactionAspect.java
+++ b/omega/omega-transaction/src/main/java/org/apache/servicecomb/pack/omega/transaction/TransactionAspect.java
@@ -30,11 +30,9 @@ import org.aspectj.lang.reflect.MethodSignature;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import static org.apache.servicecomb.pack.omega.transaction.TransactionContextUtils.extractTransactionContext;
-import static org.apache.servicecomb.pack.omega.transaction.TransactionContextUtils.populateOmegaContext;
-
 @Aspect
-public class TransactionAspect {
+public class TransactionAspect extends TransactionContextHelper {
+
   private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
 
   private final OmegaContext context;
@@ -53,7 +51,7 @@ public class TransactionAspect {
     // just check if we need to setup the transaction context information first
     TransactionContext transactionContext = extractTransactionContext(joinPoint.getArgs());
     if (transactionContext != null) {
-      populateOmegaContext(context, transactionContext, LOG);
+      populateOmegaContext(context, transactionContext);
     }
 
     String localTxId = context.localTxId();
@@ -70,5 +68,9 @@ public class TransactionAspect {
     }
   }
 
+  @Override
+  protected Logger getLogger() {
+    return LOG;
+  }
 
 }
diff --git a/omega/omega-transaction/src/main/java/org/apache/servicecomb/pack/omega/transaction/TransactionContextHelper.java b/omega/omega-transaction/src/main/java/org/apache/servicecomb/pack/omega/transaction/TransactionContextHelper.java
new file mode 100644
index 0000000..8513322
--- /dev/null
+++ b/omega/omega-transaction/src/main/java/org/apache/servicecomb/pack/omega/transaction/TransactionContextHelper.java
@@ -0,0 +1,59 @@
+/*
+ * 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.servicecomb.pack.omega.transaction;
+
+import org.apache.servicecomb.pack.omega.context.OmegaContext;
+import org.apache.servicecomb.pack.omega.context.TransactionContext;
+import org.apache.servicecomb.pack.omega.context.TransactionContextProperties;
+import org.slf4j.Logger;
+
+public abstract class TransactionContextHelper {
+
+  protected TransactionContext extractTransactionContext(Object[] args) {
+    if (args != null && args.length > 0) {
+      for (Object arg : args) {
+        // check the TransactionContext first
+        if (arg instanceof TransactionContext) {
+          return (TransactionContext) arg;
+        }
+        if (arg instanceof TransactionContextProperties) {
+          TransactionContextProperties transactionContextProperties = (TransactionContextProperties) arg;
+          return new TransactionContext(transactionContextProperties.getGlobalTxId(),
+              transactionContextProperties.getLocalTxId());
+        }
+      }
+    }
+    return null;
+  }
+
+  protected void populateOmegaContext(OmegaContext context, TransactionContext transactionContext) {
+    if (context.globalTxId() != null) {
+      getLogger()
+          .warn("The context {}'s globalTxId is not empty. Update it for globalTxId:{} and localTxId:{}", context,
+              transactionContext.globalTxId(), transactionContext.localTxId());
+    } else {
+      getLogger()
+          .debug("Updated context {} for globalTxId:{} and localTxId:{}", context,
+              transactionContext.globalTxId(), transactionContext.localTxId());
+    }
+    context.setGlobalTxId(transactionContext.globalTxId());
+    context.setLocalTxId(transactionContext.localTxId());
+  }
+
+  protected abstract Logger getLogger();
+}
diff --git a/omega/omega-transaction/src/main/java/org/apache/servicecomb/pack/omega/transaction/TransactionContextUtils.java b/omega/omega-transaction/src/main/java/org/apache/servicecomb/pack/omega/transaction/TransactionContextUtils.java
deleted file mode 100644
index b7c74f6..0000000
--- a/omega/omega-transaction/src/main/java/org/apache/servicecomb/pack/omega/transaction/TransactionContextUtils.java
+++ /dev/null
@@ -1,40 +0,0 @@
-package org.apache.servicecomb.pack.omega.transaction;
-
-import org.apache.servicecomb.pack.omega.context.OmegaContext;
-import org.apache.servicecomb.pack.omega.context.TransactionContext;
-import org.apache.servicecomb.pack.omega.context.TransactionContextProperties;
-import org.slf4j.Logger;
-
-public abstract class TransactionContextUtils {
-  private TransactionContextUtils() {
-    // singleton
-  }
-  public static TransactionContext extractTransactionContext(Object[] args) {
-    if (args != null && args.length > 0) {
-      for (Object arg : args) {
-        // check the TransactionContext first
-        if (arg instanceof TransactionContext) {
-          return (TransactionContext) arg;
-        }
-        if (arg instanceof TransactionContextProperties) {
-          TransactionContextProperties transactionContextProperties = (TransactionContextProperties) arg;
-          return new TransactionContext(transactionContextProperties.getGlobalTxId(), transactionContextProperties.getLocalTxId());
-        }
-      }
-    }
-    return null;
-  }
-
-  public static void populateOmegaContext(OmegaContext context, TransactionContext transactionContext, Logger logger) {
-    if (context.globalTxId() != null) {
-      logger.warn("The context {}'s globalTxId is not empty. Update it for globalTxId:{} and localTxId:{}", context,
-        transactionContext.globalTxId(), transactionContext.localTxId());
-    } else {
-      logger.debug("Updated context {} for globalTxId:{} and localTxId:{}", context,
-        transactionContext.globalTxId(), transactionContext.localTxId());
-    }
-    context.setGlobalTxId(transactionContext.globalTxId());
-    context.setLocalTxId(transactionContext.localTxId());
-  }
-
-}
diff --git a/omega/omega-transaction/src/main/java/org/apache/servicecomb/pack/omega/transaction/tcc/TccParticipatorAspect.java b/omega/omega-transaction/src/main/java/org/apache/servicecomb/pack/omega/transaction/tcc/TccParticipatorAspect.java
index c9573c2..a321168 100644
--- a/omega/omega-transaction/src/main/java/org/apache/servicecomb/pack/omega/transaction/tcc/TccParticipatorAspect.java
+++ b/omega/omega-transaction/src/main/java/org/apache/servicecomb/pack/omega/transaction/tcc/TccParticipatorAspect.java
@@ -19,9 +19,11 @@ package org.apache.servicecomb.pack.omega.transaction.tcc;
 
 import java.lang.invoke.MethodHandles;
 import java.lang.reflect.Method;
+
 import org.apache.servicecomb.pack.common.TransactionStatus;
 import org.apache.servicecomb.pack.omega.context.OmegaContext;
 import org.apache.servicecomb.pack.omega.context.TransactionContext;
+import org.apache.servicecomb.pack.omega.transaction.TransactionContextHelper;
 import org.apache.servicecomb.pack.omega.transaction.annotations.Participate;
 import org.apache.servicecomb.pack.omega.transaction.tcc.events.ParticipationEndedEvent;
 import org.apache.servicecomb.pack.omega.transaction.tcc.events.ParticipationStartedEvent;
@@ -32,11 +34,10 @@ import org.aspectj.lang.reflect.MethodSignature;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import static org.apache.servicecomb.pack.omega.transaction.TransactionContextUtils.extractTransactionContext;
-import static org.apache.servicecomb.pack.omega.transaction.TransactionContextUtils.populateOmegaContext;
 
 @Aspect
-public class TccParticipatorAspect {
+public class TccParticipatorAspect extends TransactionContextHelper {
+
   private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
 
   private final OmegaContext context;
@@ -58,13 +59,13 @@ public class TccParticipatorAspect {
     Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
     TransactionContext transactionContext = extractTransactionContext(joinPoint.getArgs());
     if (transactionContext != null) {
-      populateOmegaContext(context, transactionContext, LOG);
+      populateOmegaContext(context, transactionContext);
     }
 
     String localTxId = context.localTxId();
     String cancelMethod = callbackMethodSignature(joinPoint, participate.cancelMethod(), method);
     String confirmMethod = callbackMethodSignature(joinPoint, participate.confirmMethod(), method);
-    
+
     context.newLocalTxId();
     LOG.debug("Updated context {} for participate method {} ", context, method.toString());
 
@@ -98,5 +99,8 @@ public class TccParticipatorAspect {
         .toString();
   }
 
-
+  @Override
+  protected Logger getLogger() {
+    return LOG;
+  }
 }
diff --git a/omega/omega-transaction/src/test/java/org/apache/servicecomb/pack/omega/transaction/TransactionContextUtilsTest.java b/omega/omega-transaction/src/test/java/org/apache/servicecomb/pack/omega/transaction/TransactionContextHelperTest.java
similarity index 53%
rename from omega/omega-transaction/src/test/java/org/apache/servicecomb/pack/omega/transaction/TransactionContextUtilsTest.java
rename to omega/omega-transaction/src/test/java/org/apache/servicecomb/pack/omega/transaction/TransactionContextHelperTest.java
index 2a27dd1..c5a3097 100644
--- a/omega/omega-transaction/src/test/java/org/apache/servicecomb/pack/omega/transaction/TransactionContextUtilsTest.java
+++ b/omega/omega-transaction/src/test/java/org/apache/servicecomb/pack/omega/transaction/TransactionContextHelperTest.java
@@ -1,5 +1,31 @@
+/*
+ * 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.servicecomb.pack.omega.transaction;
 
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.UUID;
+
 import org.apache.servicecomb.pack.omega.context.OmegaContext;
 import org.apache.servicecomb.pack.omega.context.TransactionContext;
 import org.apache.servicecomb.pack.omega.context.TransactionContextProperties;
@@ -7,26 +33,25 @@ import org.junit.Before;
 import org.junit.Test;
 import org.mockito.Mockito;
 import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
-import java.util.UUID;
-
-import static org.apache.servicecomb.pack.omega.transaction.TransactionContextUtils.extractTransactionContext;
-import static org.apache.servicecomb.pack.omega.transaction.TransactionContextUtils.populateOmegaContext;
-import static org.hamcrest.core.Is.is;
-import static org.junit.Assert.*;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-public class TransactionContextUtilsTest {
-
-  private final Logger logger = mock(Logger.class);
+public class TransactionContextHelperTest {
 
   private final String transactionGlobalTxId = UUID.randomUUID().toString();
+
   private final String transactionLocalTxId = UUID.randomUUID().toString();
 
   private final TransactionContext txContext = new TransactionContext(transactionGlobalTxId, transactionLocalTxId);
+
   private final TransactionContextProperties txContextProperties = mock(TransactionContextProperties.class);
 
+  private final TransactionContextHelper transactionContextHelper = new TransactionContextHelper() {
+    @Override
+    protected Logger getLogger() {
+      return LoggerFactory.getLogger(getClass());
+    }
+  };
+
   @Before
   public void setUp() {
     when(txContextProperties.getGlobalTxId()).thenReturn(transactionGlobalTxId);
@@ -36,21 +61,21 @@ public class TransactionContextUtilsTest {
   @Test
   public void testExtractTransactionContext() {
 
-    TransactionContext result = extractTransactionContext(new Object[] { txContextProperties });
+    TransactionContext result = transactionContextHelper.extractTransactionContext(new Object[] {txContextProperties});
     assertThat(result.globalTxId(), is(transactionGlobalTxId));
     assertThat(result.localTxId(), is(transactionLocalTxId));
 
-    result = extractTransactionContext(new Object[] {});
+    result = transactionContextHelper.extractTransactionContext(new Object[] {});
     assertNull(result);
 
-    result = extractTransactionContext(null);
+    result = transactionContextHelper.extractTransactionContext(null);
     assertNull(result);
 
-    result = extractTransactionContext(new Object[] { txContext });
+    result = transactionContextHelper.extractTransactionContext(new Object[] {txContext});
     assertThat(result, is(txContext));
 
     TransactionContext otherTx = Mockito.mock(TransactionContext.class);
-    result = extractTransactionContext(new Object[] { otherTx, txContextProperties });
+    result = transactionContextHelper.extractTransactionContext(new Object[] {otherTx, txContextProperties});
     assertThat(result, is(otherTx));
   }
 
@@ -59,11 +84,10 @@ public class TransactionContextUtilsTest {
 
     OmegaContext omegaContext = new OmegaContext(null);
 
-    populateOmegaContext(omegaContext, txContext, logger);
+    transactionContextHelper.populateOmegaContext(omegaContext, txContext);
 
     assertEquals(transactionGlobalTxId, omegaContext.globalTxId());
     assertEquals(transactionLocalTxId, omegaContext.localTxId());
-
   }
 
   @Test
@@ -73,11 +97,9 @@ public class TransactionContextUtilsTest {
     omegaContext.setGlobalTxId("global-tx-id");
     omegaContext.setLocalTxId("local-tx-id");
 
-    populateOmegaContext(omegaContext, txContext, logger);
+    transactionContextHelper.populateOmegaContext(omegaContext, txContext);
 
     assertEquals(transactionGlobalTxId, omegaContext.globalTxId());
     assertEquals(transactionLocalTxId, omegaContext.localTxId());
-
   }
-
 }