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 2017/12/25 10:36:33 UTC
[incubator-servicecomb-saga] 09/14: SCB-96 checked existence of
matching compensation method on startup
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/incubator-servicecomb-saga.git
commit fa6030e49f21045776235bc5756c13c32157219c
Author: seanyinx <se...@huawei.com>
AuthorDate: Sat Dec 23 17:23:04 2017 +0800
SCB-96 checked existence of matching compensation method on startup
Signed-off-by: seanyinx <se...@huawei.com>
---
.../spring/CompensableAnnotationProcessor.java | 42 +++++++++++++++++
.../spring/CompensableMethodCheckingCallback.java | 55 ++++++++++++++++++++++
.../spring/CompensableAnnotationCheckingTest.java | 42 +++++++++++++++++
.../transaction/spring/MisconfiguredService.java | 32 +++++++++++++
4 files changed, 171 insertions(+)
diff --git a/omega/omega-spring-tx/src/main/java/io/servicecomb/saga/omega/transaction/spring/CompensableAnnotationProcessor.java b/omega/omega-spring-tx/src/main/java/io/servicecomb/saga/omega/transaction/spring/CompensableAnnotationProcessor.java
new file mode 100644
index 0000000..743c216
--- /dev/null
+++ b/omega/omega-spring-tx/src/main/java/io/servicecomb/saga/omega/transaction/spring/CompensableAnnotationProcessor.java
@@ -0,0 +1,42 @@
+/*
+ * 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 io.servicecomb.saga.omega.transaction.spring;
+
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.config.BeanPostProcessor;
+import org.springframework.stereotype.Component;
+import org.springframework.util.ReflectionUtils;
+
+@Component
+class CompensableAnnotationProcessor implements BeanPostProcessor {
+
+ @Override
+ public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
+ checkMethod(bean);
+ return bean;
+ }
+
+ @Override
+ public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
+ return bean;
+ }
+
+ private void checkMethod(Object bean) {
+ ReflectionUtils.doWithMethods(bean.getClass(), new CompensableMethodCheckingCallback(bean));
+ }
+}
diff --git a/omega/omega-spring-tx/src/main/java/io/servicecomb/saga/omega/transaction/spring/CompensableMethodCheckingCallback.java b/omega/omega-spring-tx/src/main/java/io/servicecomb/saga/omega/transaction/spring/CompensableMethodCheckingCallback.java
new file mode 100644
index 0000000..c29061c
--- /dev/null
+++ b/omega/omega-spring-tx/src/main/java/io/servicecomb/saga/omega/transaction/spring/CompensableMethodCheckingCallback.java
@@ -0,0 +1,55 @@
+/*
+ * 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 io.servicecomb.saga.omega.transaction.spring;
+
+import java.lang.invoke.MethodHandles;
+import java.lang.reflect.Method;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.util.ReflectionUtils.MethodCallback;
+
+import io.servicecomb.saga.omega.transaction.annotations.Compensable;
+
+class CompensableMethodCheckingCallback implements MethodCallback {
+ private static Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+
+ private final Object bean;
+
+ CompensableMethodCheckingCallback(Object bean) {
+ this.bean = bean;
+ }
+
+ @Override
+ public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
+ if (!method.isAnnotationPresent(Compensable.class)) {
+ return;
+ }
+
+ String compensationMethod = method.getAnnotation(Compensable.class).compensationMethod();
+
+ try {
+ bean.getClass().getDeclaredMethod(compensationMethod, method.getParameterTypes());
+ LOG.debug("Found compensation method [{}] in {}", compensationMethod, bean.getClass().getCanonicalName());
+ } catch (NoSuchMethodException e) {
+ throw new IllegalArgumentException(
+ "No such compensation method [" + compensationMethod + "] found in " + bean.getClass().getCanonicalName(),
+ e);
+ }
+ }
+}
diff --git a/omega/omega-spring-tx/src/test/java/io/servicecomb/saga/omega/transaction/spring/CompensableAnnotationCheckingTest.java b/omega/omega-spring-tx/src/test/java/io/servicecomb/saga/omega/transaction/spring/CompensableAnnotationCheckingTest.java
new file mode 100644
index 0000000..e83a621
--- /dev/null
+++ b/omega/omega-spring-tx/src/test/java/io/servicecomb/saga/omega/transaction/spring/CompensableAnnotationCheckingTest.java
@@ -0,0 +1,42 @@
+/*
+ * 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 io.servicecomb.saga.omega.transaction.spring;
+
+import static com.seanyinx.github.unit.scaffolding.AssertUtils.expectFailing;
+import static org.hamcrest.CoreMatchers.startsWith;
+import static org.junit.Assert.assertThat;
+
+import org.junit.Test;
+import org.springframework.beans.factory.BeanCreationException;
+import org.springframework.boot.builder.SpringApplicationBuilder;
+import org.springframework.context.ConfigurableApplicationContext;
+
+public class CompensableAnnotationCheckingTest {
+ @Test
+ public void blowsUpWhenCompensableMethodIsNotFound() throws Exception {
+ try {
+ try (ConfigurableApplicationContext ignored = new SpringApplicationBuilder(TransactionTestMain.class)
+ .profiles("annotation-checking")
+ .run()) {
+ expectFailing(IllegalArgumentException.class);
+ }
+ } catch (BeanCreationException e) {
+ assertThat(e.getCause().getMessage(), startsWith("No such compensation method [none]"));
+ }
+ }
+}
diff --git a/omega/omega-spring-tx/src/test/java/io/servicecomb/saga/omega/transaction/spring/MisconfiguredService.java b/omega/omega-spring-tx/src/test/java/io/servicecomb/saga/omega/transaction/spring/MisconfiguredService.java
new file mode 100644
index 0000000..854959a
--- /dev/null
+++ b/omega/omega-spring-tx/src/test/java/io/servicecomb/saga/omega/transaction/spring/MisconfiguredService.java
@@ -0,0 +1,32 @@
+/*
+ * 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 io.servicecomb.saga.omega.transaction.spring;
+
+import org.springframework.context.annotation.Profile;
+import org.springframework.stereotype.Component;
+
+import io.servicecomb.saga.omega.transaction.annotations.Compensable;
+
+@Profile("annotation-checking")
+@Component
+class MisconfiguredService {
+
+ @Compensable(compensationMethod = "none")
+ void doSomething() {
+ }
+}
--
To stop receiving notification emails like this one, please contact
"commits@servicecomb.apache.org" <co...@servicecomb.apache.org>.