WillemJiang closed pull request #136: SCB-243 able to intercept old java 
chassis requests
URL: https://github.com/apache/incubator-servicecomb-saga/pull/136
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/omega/omega-transport/omega-transport-resttemplate/src/test/java/org/apache/servicecomb/saga/omega/transport/resttemplate/UniqueIdGeneratorTest.java
 
b/omega/omega-context/src/test/java/org/apache/servicecomb/saga/omega/context/UniqueIdGeneratorTest.java
similarity index 91%
rename from 
omega/omega-transport/omega-transport-resttemplate/src/test/java/org/apache/servicecomb/saga/omega/transport/resttemplate/UniqueIdGeneratorTest.java
rename to 
omega/omega-context/src/test/java/org/apache/servicecomb/saga/omega/context/UniqueIdGeneratorTest.java
index 88ffac2e..bcedcfa2 100644
--- 
a/omega/omega-transport/omega-transport-resttemplate/src/test/java/org/apache/servicecomb/saga/omega/transport/resttemplate/UniqueIdGeneratorTest.java
+++ 
b/omega/omega-context/src/test/java/org/apache/servicecomb/saga/omega/context/UniqueIdGeneratorTest.java
@@ -6,17 +6,16 @@
  * (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
+ *     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.saga.omega.transport.resttemplate;
+package org.apache.servicecomb.saga.omega.context;
 
 import static org.hamcrest.core.Is.is;
 import static org.junit.Assert.assertThat;
@@ -32,7 +31,6 @@
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
 
-import org.apache.servicecomb.saga.omega.context.UniqueIdGenerator;
 import org.junit.Test;
 
 public class UniqueIdGeneratorTest {
diff --git a/omega/omega-transport/omega-transport-servicecomb/pom.xml 
b/omega/omega-transport/omega-transport-servicecomb/pom.xml
new file mode 100644
index 00000000..2798f7d4
--- /dev/null
+++ b/omega/omega-transport/omega-transport-servicecomb/pom.xml
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ 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.
+  -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0";
+  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd";>
+  <parent>
+    <artifactId>omega-transport</artifactId>
+    <groupId>org.apache.servicecomb.saga</groupId>
+    <version>0.0.3-SNAPSHOT</version>
+  </parent>
+  <modelVersion>4.0.0</modelVersion>
+
+  <artifactId>omega-transport-servicecomb</artifactId>
+
+  <dependencies>
+    <dependency>
+      <groupId>io.servicecomb</groupId>
+      <artifactId>java-chassis-core</artifactId>
+      <version>${java.chassis.version}</version>
+    </dependency>
+  </dependencies>
+
+</project>
\ No newline at end of file
diff --git 
a/omega/omega-transport/omega-transport-servicecomb/src/main/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaConsumerHandler.java
 
b/omega/omega-transport/omega-transport-servicecomb/src/main/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaConsumerHandler.java
new file mode 100644
index 00000000..8fd296fe
--- /dev/null
+++ 
b/omega/omega-transport/omega-transport-servicecomb/src/main/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaConsumerHandler.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.saga.omega.transport.servicecomb;
+
+import static 
org.apache.servicecomb.saga.omega.context.OmegaContext.GLOBAL_TX_ID_KEY;
+import static 
org.apache.servicecomb.saga.omega.context.OmegaContext.LOCAL_TX_ID_KEY;
+
+import java.lang.invoke.MethodHandles;
+
+import org.apache.servicecomb.saga.omega.context.OmegaContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import io.servicecomb.core.Handler;
+import io.servicecomb.core.Invocation;
+import io.servicecomb.swagger.invocation.AsyncResponse;
+
+public class SagaConsumerHandler implements Handler {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+  private final OmegaContext omegaContext;
+
+  @Autowired
+  public SagaConsumerHandler(OmegaContext omegaContext) {
+    this.omegaContext = omegaContext;
+  }
+
+  @Override
+  public void handle(Invocation invocation, AsyncResponse asyncResponse) 
throws Exception {
+    if (omegaContext.globalTxId() != null) {
+      invocation.getContext().put(GLOBAL_TX_ID_KEY, omegaContext.globalTxId());
+      invocation.getContext().put(LOCAL_TX_ID_KEY, omegaContext.localTxId());
+
+      LOG.debug("Added {} {} and {} {} to request header",
+          GLOBAL_TX_ID_KEY,
+          omegaContext.globalTxId(),
+          LOCAL_TX_ID_KEY,
+          omegaContext.localTxId());
+    }
+
+    invocation.next(asyncResponse);
+  }
+}
diff --git 
a/omega/omega-transport/omega-transport-servicecomb/src/main/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaProviderHandler.java
 
b/omega/omega-transport/omega-transport-servicecomb/src/main/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaProviderHandler.java
new file mode 100644
index 00000000..d639db53
--- /dev/null
+++ 
b/omega/omega-transport/omega-transport-servicecomb/src/main/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaProviderHandler.java
@@ -0,0 +1,56 @@
+/*
+ * 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.saga.omega.transport.servicecomb;
+
+import static 
org.apache.servicecomb.saga.omega.context.OmegaContext.GLOBAL_TX_ID_KEY;
+import static 
org.apache.servicecomb.saga.omega.context.OmegaContext.LOCAL_TX_ID_KEY;
+
+import java.lang.invoke.MethodHandles;
+
+import org.apache.servicecomb.saga.omega.context.OmegaContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import io.servicecomb.core.Handler;
+import io.servicecomb.core.Invocation;
+import io.servicecomb.swagger.invocation.AsyncResponse;
+
+public class SagaProviderHandler implements Handler {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+  private final OmegaContext omegaContext;
+
+  @Autowired
+  public SagaProviderHandler(OmegaContext omegaContext) {
+    this.omegaContext = omegaContext;
+  }
+
+  @Override
+  public void handle(Invocation invocation, AsyncResponse asyncResponse) 
throws Exception {
+    String globalTxId = invocation.getContext().get(GLOBAL_TX_ID_KEY);
+    if (globalTxId == null) {
+      LOG.debug("no such header: {}", GLOBAL_TX_ID_KEY);
+    } else {
+      omegaContext.setGlobalTxId(globalTxId);
+      omegaContext.setLocalTxId(invocation.getContext().get(LOCAL_TX_ID_KEY));
+    }
+
+    invocation.next(asyncResponse);
+  }
+}
diff --git 
a/omega/omega-transport/omega-transport-servicecomb/src/main/resources/config/cse.handler.xml
 
b/omega/omega-transport/omega-transport-servicecomb/src/main/resources/config/cse.handler.xml
new file mode 100644
index 00000000..95158152
--- /dev/null
+++ 
b/omega/omega-transport/omega-transport-servicecomb/src/main/resources/config/cse.handler.xml
@@ -0,0 +1,23 @@
+<!--
+  ~ 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.
+  -->
+
+<config>
+  <handler id="saga-consumer"
+    
class="org.apache.servicecomb.saga.omega.transport.servicecomb.SagaConsumerHandler"
 />
+  <handler id="saga-provider"
+    
class="org.apache.servicecomb.saga.omega.transport.servicecomb.SagaProviderHandler"
 />
+</config>
\ No newline at end of file
diff --git 
a/omega/omega-transport/omega-transport-servicecomb/src/test/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaConsumerHandlerTest.java
 
b/omega/omega-transport/omega-transport-servicecomb/src/test/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaConsumerHandlerTest.java
new file mode 100644
index 00000000..d23ad2b9
--- /dev/null
+++ 
b/omega/omega-transport/omega-transport-servicecomb/src/test/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaConsumerHandlerTest.java
@@ -0,0 +1,78 @@
+/*
+ * 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.saga.omega.transport.servicecomb;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
+
+import org.apache.servicecomb.saga.omega.context.IdGenerator;
+import org.apache.servicecomb.saga.omega.context.OmegaContext;
+import org.junit.Before;
+import org.junit.Test;
+
+import io.servicecomb.core.Invocation;
+import io.servicecomb.swagger.invocation.AsyncResponse;
+
+public class SagaConsumerHandlerTest {
+
+  private static final String globalTxId = UUID.randomUUID().toString();
+  private static final String localTxId = UUID.randomUUID().toString();
+  @SuppressWarnings("unchecked")
+  private final IdGenerator<String> idGenerator = mock(IdGenerator.class);
+
+  private final OmegaContext omegaContext = new OmegaContext(() -> "ignored");
+  private final Invocation invocation = mock(Invocation.class);
+  private final AsyncResponse asyncResponse = mock(AsyncResponse.class);
+
+  private final SagaConsumerHandler handler = new 
SagaConsumerHandler(omegaContext);
+
+  @Before
+  public void setUp() {
+    when(idGenerator.nextId()).thenReturn(globalTxId, localTxId);
+  }
+
+  @Test
+  public void keepHeaderUnchangedIfContextAbsent() throws Exception {
+    Map<String, String> context = new HashMap<>();
+    when(invocation.getContext()).thenReturn(context);
+
+    handler.handle(invocation, asyncResponse);
+
+    assertThat(invocation.getContext().isEmpty(), is(true));
+  }
+
+  @Test
+  public void interceptTransactionIdInHeaderIfContextPresent() throws 
Exception {
+    omegaContext.setGlobalTxId(globalTxId);
+    omegaContext.setLocalTxId(localTxId);
+
+    Map<String, String> context = new HashMap<>();
+    when(invocation.getContext()).thenReturn(context);
+
+    handler.handle(invocation, asyncResponse);
+
+    assertThat(invocation.getContext().get(OmegaContext.GLOBAL_TX_ID_KEY), 
is(globalTxId));
+    assertThat(invocation.getContext().get(OmegaContext.LOCAL_TX_ID_KEY), 
is(localTxId));
+  }
+}
diff --git 
a/omega/omega-transport/omega-transport-servicecomb/src/test/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaProviderHandlerTest.java
 
b/omega/omega-transport/omega-transport-servicecomb/src/test/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaProviderHandlerTest.java
new file mode 100644
index 00000000..a9b7e375
--- /dev/null
+++ 
b/omega/omega-transport/omega-transport-servicecomb/src/test/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaProviderHandlerTest.java
@@ -0,0 +1,78 @@
+/*
+ * 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.saga.omega.transport.servicecomb;
+
+import static java.util.Collections.emptyMap;
+import static 
org.apache.servicecomb.saga.omega.context.OmegaContext.GLOBAL_TX_ID_KEY;
+import static 
org.apache.servicecomb.saga.omega.context.OmegaContext.LOCAL_TX_ID_KEY;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.nullValue;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
+
+import org.apache.servicecomb.saga.omega.context.OmegaContext;
+import org.junit.Before;
+import org.junit.Test;
+
+import io.servicecomb.core.Invocation;
+import io.servicecomb.swagger.invocation.AsyncResponse;
+
+public class SagaProviderHandlerTest {
+
+  private static final String globalTxId = UUID.randomUUID().toString();
+  private static final String localTxId = UUID.randomUUID().toString();
+  private final OmegaContext omegaContext = new OmegaContext(() -> "ignored");
+  private final Invocation invocation = mock(Invocation.class);
+  private final AsyncResponse asyncResponse = mock(AsyncResponse.class);
+
+  private final SagaProviderHandler handler = new 
SagaProviderHandler(omegaContext);
+
+  @Before
+  public void setUp() {
+    omegaContext.clear();
+  }
+
+  @Test
+  public void setUpOmegaContextInTransactionRequest() throws Exception {
+    Map<String, String> context = new HashMap<>();
+    context.put(GLOBAL_TX_ID_KEY, globalTxId);
+    context.put(LOCAL_TX_ID_KEY, localTxId);
+    when(invocation.getContext()).thenReturn(context);
+
+    handler.handle(invocation, asyncResponse);
+
+    assertThat(omegaContext.globalTxId(), is(globalTxId));
+    assertThat(omegaContext.localTxId(), is(localTxId));
+  }
+
+  @Test
+  public void doNothingInNonTransactionRequest() throws Exception {
+    when(invocation.getContext()).thenReturn(emptyMap());
+
+    handler.handle(invocation, asyncResponse);
+
+    assertThat(omegaContext.globalTxId(), is(nullValue()));
+    assertThat(omegaContext.localTxId(), is(nullValue()));
+  }
+
+}
diff --git a/omega/omega-transport/pom.xml b/omega/omega-transport/pom.xml
index 4cbe189e..352926a2 100644
--- a/omega/omega-transport/pom.xml
+++ b/omega/omega-transport/pom.xml
@@ -33,6 +33,7 @@
   <packaging>pom</packaging>
   <modules>
     <module>omega-transport-resttemplate</module>
+    <module>omega-transport-servicecomb</module>
   </modules>
 
   <dependencies>
diff --git a/pom.xml b/pom.xml
index f263683c..85e94050 100755
--- a/pom.xml
+++ b/pom.xml
@@ -54,7 +54,7 @@
     <log4j.version>2.6.2</log4j.version>
     <spring.boot.version>1.4.5.RELEASE</spring.boot.version>
     <spring.cloud.version>Camden.SR6</spring.cloud.version>
-    <java.chassis.version>0.4.0</java.chassis.version>
+    <java.chassis.version>0.5.0</java.chassis.version>
     <spring.version>4.3.7.RELEASE</spring.version>
     <akka.version>2.5.6</akka.version>
     <rat.version>0.12</rat.version>


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to