You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2026/09/19 02:34:08 UTC
(shardingsphere) branch master updated: Validate configured SPI types with Bean Validation (#39891)
This is an automated email from the ASF dual-hosted git repository.
terrymanu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 68b11bc20cc Validate configured SPI types with Bean Validation (#39891)
68b11bc20cc is described below
commit 68b11bc20ccfcb7284946a2e82f6372aa99cbf98
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sat Sep 19 10:34:00 2026 +0800
Validate configured SPI types with Bean Validation (#39891)
* Validate configured SPI types with Bean Validation
* Validate configured SPI types with Bean Validation
---
.../validator/constraint/spi/SPITypeExists.java | 64 +++++++++++
.../constraint/spi/SPITypeExistsValidator.java | 67 ++++++++++++
.../constraint/spi/SPITypeExistsValidatorTest.java | 119 +++++++++++++++++++++
.../constraint/spi/fixture/SPITypeFixture.java | 25 +----
.../spi/fixture/SPITypeFixtureImpl.java} | 23 ++--
...validator.constraint.spi.fixture.SPITypeFixture | 18 ++++
.../infra/spi/type/typed/TypedSPILoader.java | 17 +++
.../infra/spi/type/typed/TypedSPILoaderTest.java | 31 +++++-
.../type/typed/fixture/MissingTypedSPIFixture.java | 23 +---
.../typed/fixture/impl/TypedSPIFixtureImpl.java | 5 +
.../config/AuthorityRuleConfiguration.java | 2 +
.../config/AuthorityRuleConfigurationTest.java | 8 +-
.../config/fixture/PrivilegeProviderFixture.java | 28 ++---
....shardingsphere.authority.spi.PrivilegeProvider | 18 ++++
.../config/SQLTranslatorRuleConfiguration.java | 4 +-
.../config/SQLTranslatorRuleConfigurationTest.java | 5 +-
.../config/fixture/SQLTranslatorFixture.java | 41 +++++++
....shardingsphere.sqltranslator.spi.SQLTranslator | 18 ++++
.../update/AlterSQLTranslatorRuleExecutor.java | 7 --
.../config/TransactionRuleConfiguration.java | 2 +
.../TransactionProviderTypeValidator.java | 49 +++++++++
.../validator/ValidTransactionProviderType.java | 57 ++++++++++
.../config/TransactionRuleConfigurationTest.java | 6 +-
.../update/AlterTransactionRuleExecutor.java | 41 -------
.../resources/cases/alter-transaction-rule.xml | 9 --
.../xa/XAShardingSphereTransactionManager.java | 10 +-
.../config/TransactionRuleConfigurationTest.java | 42 +++++---
.../xa/XAShardingSphereTransactionManagerTest.java | 13 +++
28 files changed, 585 insertions(+), 167 deletions(-)
diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/config/rule/validator/constraint/spi/SPITypeExists.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/config/rule/validator/constraint/spi/SPITypeExists.java
new file mode 100644
index 00000000000..162d44cc548
--- /dev/null
+++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/config/rule/validator/constraint/spi/SPITypeExists.java
@@ -0,0 +1,64 @@
+/*
+ * 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.shardingsphere.infra.config.rule.validator.constraint.spi;
+
+import javax.validation.Constraint;
+import javax.validation.Payload;
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * SPI type exists constraint.
+ */
+@Target(ElementType.FIELD)
+@Retention(RetentionPolicy.RUNTIME)
+@Constraint(validatedBy = SPITypeExistsValidator.class)
+@Documented
+public @interface SPITypeExists {
+
+ /**
+ * Get SPI class name.
+ *
+ * @return SPI class name
+ */
+ String spiClassName();
+
+ /**
+ * Get message.
+ *
+ * @return message
+ */
+ String message() default "does not match an available SPI implementation";
+
+ /**
+ * Get groups.
+ *
+ * @return groups
+ */
+ Class<?>[] groups() default {};
+
+ /**
+ * Get payload.
+ *
+ * @return payload
+ */
+ Class<? extends Payload>[] payload() default {};
+}
diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/config/rule/validator/constraint/spi/SPITypeExistsValidator.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/config/rule/validator/constraint/spi/SPITypeExistsValidator.java
new file mode 100644
index 00000000000..8d6b58f41c1
--- /dev/null
+++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/config/rule/validator/constraint/spi/SPITypeExistsValidator.java
@@ -0,0 +1,67 @@
+/*
+ * 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.shardingsphere.infra.config.rule.validator.constraint.spi;
+
+import org.apache.shardingsphere.infra.exception.ShardingSpherePreconditions;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPI;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+import javax.validation.ValidationException;
+import java.lang.reflect.Method;
+
+/**
+ * SPI type exists validator.
+ */
+public final class SPITypeExistsValidator implements ConstraintValidator<SPITypeExists, Object> {
+
+ private String spiClassName;
+
+ @Override
+ public void initialize(final SPITypeExists constraintAnnotation) {
+ spiClassName = constraintAnnotation.spiClassName();
+ }
+
+ @Override
+ public boolean isValid(final Object value, final ConstraintValidatorContext context) {
+ return null == value || containsService(spiClassName, getTypeValue(value));
+ }
+
+ private boolean containsService(final String spiClassName, final Object type) {
+ try {
+ Class<?> spiClass = Class.forName(spiClassName, false, Thread.currentThread().getContextClassLoader());
+ ShardingSpherePreconditions.checkState(TypedSPI.class.isAssignableFrom(spiClass), () -> new ValidationException(String.format("Class `%s` does not implement TypedSPI.", spiClassName)));
+ return TypedSPILoader.containsService(spiClass.asSubclass(TypedSPI.class), type);
+ } catch (final ClassNotFoundException ignored) {
+ return false;
+ }
+ }
+
+ private Object getTypeValue(final Object value) {
+ if (value instanceof String) {
+ return value;
+ }
+ try {
+ Method getter = value.getClass().getMethod("getType");
+ return getter.invoke(value);
+ } catch (final ReflectiveOperationException ex) {
+ throw new ValidationException(String.format("Can not read property `type` from `%s`.", value.getClass().getName()), ex);
+ }
+ }
+}
diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/config/rule/validator/constraint/spi/SPITypeExistsValidatorTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/config/rule/validator/constraint/spi/SPITypeExistsValidatorTest.java
new file mode 100644
index 00000000000..f359c1d944d
--- /dev/null
+++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/config/rule/validator/constraint/spi/SPITypeExistsValidatorTest.java
@@ -0,0 +1,119 @@
+/*
+ * 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.shardingsphere.infra.config.rule.validator.constraint.spi;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import org.apache.bval.jsr.ApacheValidationProvider;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import javax.validation.ConstraintViolation;
+import javax.validation.Validation;
+import javax.validation.ValidationException;
+import javax.validation.Validator;
+import java.util.Collection;
+import java.util.stream.Stream;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+class SPITypeExistsValidatorTest {
+
+ private static final Validator VALIDATOR = Validation.byProvider(ApacheValidationProvider.class).configure().buildValidatorFactory().getValidator();
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("validityArguments")
+ void assertIsValid(final String name, final Object config, final boolean expected) {
+ assertThat(VALIDATOR.validate(config).isEmpty(), is(expected));
+ }
+
+ private static Stream<Arguments> validityArguments() {
+ return Stream.of(
+ Arguments.of("Null", new DirectConfiguration(null), true),
+ Arguments.of("Type", new DirectConfiguration("FIXTURE"), true),
+ Arguments.of("Case insensitive type", new DirectConfiguration("fixture"), true),
+ Arguments.of("Alias", new DirectConfiguration("ALIAS"), true),
+ Arguments.of("Missing type", new DirectConfiguration("MISSING"), false),
+ Arguments.of("Blank type", new DirectConfiguration(" "), false),
+ Arguments.of("Nested type", new NestedConfiguration(new SPIConfiguration("FIXTURE")), true),
+ Arguments.of("Missing nested type", new NestedConfiguration(new SPIConfiguration("MISSING")), false),
+ Arguments.of("Missing SPI class", new MissingSPIClassConfiguration("FIXTURE"), false));
+ }
+
+ @Test
+ void assertNestedPropertyPath() {
+ Collection<ConstraintViolation<NestedConfiguration>> actual = VALIDATOR.validate(new NestedConfiguration(new SPIConfiguration("MISSING")));
+ assertThat(actual.iterator().next().getPropertyPath().toString(), is("provider"));
+ }
+
+ @Test
+ void assertInvalidSPIClass() {
+ assertThrows(ValidationException.class, () -> VALIDATOR.validate(new InvalidSPIClassConfiguration("FIXTURE")));
+ }
+
+ @Test
+ void assertMissingTypeGetter() {
+ assertThrows(ValidationException.class, () -> VALIDATOR.validate(new MissingTypeGetterConfiguration(new Object())));
+ }
+
+ @RequiredArgsConstructor
+ private static final class DirectConfiguration {
+
+ @SPITypeExists(spiClassName = "org.apache.shardingsphere.infra.config.rule.validator.constraint.spi.fixture.SPITypeFixture")
+ private final String type;
+ }
+
+ @RequiredArgsConstructor
+ private static final class NestedConfiguration {
+
+ @SPITypeExists(spiClassName = "org.apache.shardingsphere.infra.config.rule.validator.constraint.spi.fixture.SPITypeFixture")
+ private final SPIConfiguration provider;
+ }
+
+ @RequiredArgsConstructor
+ @Getter
+ public static final class SPIConfiguration {
+
+ private final String type;
+ }
+
+ @RequiredArgsConstructor
+ private static final class MissingSPIClassConfiguration {
+
+ @SPITypeExists(spiClassName = "org.apache.shardingsphere.infra.config.rule.validator.constraint.spi.fixture.MissingSPITypeFixture")
+ private final String type;
+ }
+
+ @RequiredArgsConstructor
+ private static final class InvalidSPIClassConfiguration {
+
+ @SPITypeExists(spiClassName = "java.lang.String")
+ private final String type;
+ }
+
+ @RequiredArgsConstructor
+ private static final class MissingTypeGetterConfiguration {
+
+ @SPITypeExists(spiClassName = "org.apache.shardingsphere.infra.config.rule.validator.constraint.spi.fixture.SPITypeFixture")
+ private final Object provider;
+ }
+}
diff --git a/kernel/sql-translator/api/src/main/java/org/apache/shardingsphere/sqltranslator/config/SQLTranslatorRuleConfiguration.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/config/rule/validator/constraint/spi/fixture/SPITypeFixture.java
similarity index 55%
copy from kernel/sql-translator/api/src/main/java/org/apache/shardingsphere/sqltranslator/config/SQLTranslatorRuleConfiguration.java
copy to infra/common/src/test/java/org/apache/shardingsphere/infra/config/rule/validator/constraint/spi/fixture/SPITypeFixture.java
index 2f0b644f297..7bdb86bacfe 100644
--- a/kernel/sql-translator/api/src/main/java/org/apache/shardingsphere/sqltranslator/config/SQLTranslatorRuleConfiguration.java
+++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/config/rule/validator/constraint/spi/fixture/SPITypeFixture.java
@@ -15,26 +15,11 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.sqltranslator.config;
+package org.apache.shardingsphere.infra.config.rule.validator.constraint.spi.fixture;
-import lombok.Getter;
-import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.infra.config.rule.scope.GlobalRuleConfiguration;
+import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPI;
-import javax.validation.constraints.Pattern;
-import java.util.Properties;
-
-/**
- * SQL translator rule configuration.
- */
-@RequiredArgsConstructor
-@Getter
-public final class SQLTranslatorRuleConfiguration implements GlobalRuleConfiguration {
-
- @Pattern(regexp = ".*\\S.*", flags = Pattern.Flag.DOTALL)
- private final String type;
-
- private final Properties props;
-
- private final boolean useOriginalSQLWhenTranslatingFailed;
+@SingletonSPI
+public interface SPITypeFixture extends TypedSPI {
}
diff --git a/infra/spi/src/test/java/org/apache/shardingsphere/infra/spi/type/typed/fixture/impl/TypedSPIFixtureImpl.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/config/rule/validator/constraint/spi/fixture/SPITypeFixtureImpl.java
similarity index 67%
copy from infra/spi/src/test/java/org/apache/shardingsphere/infra/spi/type/typed/fixture/impl/TypedSPIFixtureImpl.java
copy to infra/common/src/test/java/org/apache/shardingsphere/infra/config/rule/validator/constraint/spi/fixture/SPITypeFixtureImpl.java
index 117a811d1c7..0fc1c07b402 100644
--- a/infra/spi/src/test/java/org/apache/shardingsphere/infra/spi/type/typed/fixture/impl/TypedSPIFixtureImpl.java
+++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/config/rule/validator/constraint/spi/fixture/SPITypeFixtureImpl.java
@@ -15,32 +15,25 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.infra.spi.type.typed.fixture.impl;
-
-import lombok.Getter;
-import org.apache.shardingsphere.infra.spi.type.typed.fixture.TypedSPIFixture;
+package org.apache.shardingsphere.infra.config.rule.validator.constraint.spi.fixture;
import java.util.Collection;
import java.util.Collections;
-import java.util.Properties;
-@Getter
-public final class TypedSPIFixtureImpl implements TypedSPIFixture {
-
- private String value;
+public final class SPITypeFixtureImpl implements SPITypeFixture {
@Override
- public void init(final Properties props) {
- value = props.getProperty("key");
+ public String getType() {
+ return "FIXTURE";
}
@Override
- public String getType() {
- return "TYPED.FIXTURE";
+ public Collection<Object> getTypeAliases() {
+ return Collections.singleton("ALIAS");
}
@Override
- public Collection<Object> getTypeAliases() {
- return Collections.singleton("TYPED.ALIAS");
+ public boolean isDefault() {
+ return true;
}
}
diff --git a/infra/common/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.config.rule.validator.constraint.spi.fixture.SPITypeFixture b/infra/common/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.config.rule.validator.constraint.spi.fixture.SPITypeFixture
new file mode 100644
index 00000000000..56a45a7afde
--- /dev/null
+++ b/infra/common/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.config.rule.validator.constraint.spi.fixture.SPITypeFixture
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.shardingsphere.infra.config.rule.validator.constraint.spi.fixture.SPITypeFixtureImpl
diff --git a/infra/spi/src/main/java/org/apache/shardingsphere/infra/spi/type/typed/TypedSPILoader.java b/infra/spi/src/main/java/org/apache/shardingsphere/infra/spi/type/typed/TypedSPILoader.java
index 4d0c534e75e..c6d93550994 100644
--- a/infra/spi/src/main/java/org/apache/shardingsphere/infra/spi/type/typed/TypedSPILoader.java
+++ b/infra/spi/src/main/java/org/apache/shardingsphere/infra/spi/type/typed/TypedSPILoader.java
@@ -110,6 +110,23 @@ public final class TypedSPILoader {
return findService(serviceInterface, type, props).orElseGet(() -> findService(serviceInterface, null, props).orElseThrow(() -> new ServiceProviderNotFoundException(serviceInterface, type)));
}
+ /**
+ * Judge whether service exists.
+ *
+ * @param serviceInterface typed SPI service interface
+ * @param type type
+ * @param <T> SPI class type
+ * @return service exists or not
+ */
+ public static <T extends TypedSPI> boolean containsService(final Class<T> serviceInterface, final Object type) {
+ for (T each : ShardingSphereServiceLoader.getServiceInstances(serviceInterface)) {
+ if (null == type ? each.isDefault() : matchesType(type, each)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
/**
* Check service.
*
diff --git a/infra/spi/src/test/java/org/apache/shardingsphere/infra/spi/type/typed/TypedSPILoaderTest.java b/infra/spi/src/test/java/org/apache/shardingsphere/infra/spi/type/typed/TypedSPILoaderTest.java
index df91b1926dc..e35cd3ca63f 100644
--- a/infra/spi/src/test/java/org/apache/shardingsphere/infra/spi/type/typed/TypedSPILoaderTest.java
+++ b/infra/spi/src/test/java/org/apache/shardingsphere/infra/spi/type/typed/TypedSPILoaderTest.java
@@ -18,11 +18,16 @@
package org.apache.shardingsphere.infra.spi.type.typed;
import org.apache.shardingsphere.infra.spi.exception.ServiceProviderNotFoundException;
+import org.apache.shardingsphere.infra.spi.type.typed.fixture.MissingTypedSPIFixture;
import org.apache.shardingsphere.infra.spi.type.typed.fixture.TypedSPIFixture;
import org.apache.shardingsphere.infra.spi.type.typed.fixture.impl.TypedSPIFixtureImpl;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
import java.util.Properties;
+import java.util.stream.Stream;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
@@ -62,6 +67,30 @@ class TypedSPILoaderTest {
@Test
void assertGetServiceWhenTypeIsNotExist() {
- assertThrows(ServiceProviderNotFoundException.class, () -> TypedSPILoader.getService(TypedSPIFixture.class, "NOT_EXISTED"));
+ assertThrows(ServiceProviderNotFoundException.class, () -> TypedSPILoader.getService(MissingTypedSPIFixture.class, "NOT_EXISTED"));
+ }
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("containsServiceArguments")
+ void assertContainsService(final String name, final Object type, final boolean expected) {
+ assertThat(TypedSPILoader.containsService(TypedSPIFixture.class, type), is(expected));
+ }
+
+ private static Stream<Arguments> containsServiceArguments() {
+ return Stream.of(
+ Arguments.of("Type", "TYPED.FIXTURE", true),
+ Arguments.of("Case insensitive type", "typed.fixture", true),
+ Arguments.of("Alias", "TYPED.ALIAS", true),
+ Arguments.of("Default", null, true),
+ Arguments.of("Missing type", "NOT_EXISTED", false));
+ }
+
+ @Test
+ void assertContainsServiceDoesNotInitializeService() {
+ Properties props = new Properties();
+ props.setProperty("key", "expected");
+ TypedSPIFixtureImpl service = (TypedSPIFixtureImpl) TypedSPILoader.getService(TypedSPIFixture.class, "TYPED.FIXTURE", props);
+ assertTrue(TypedSPILoader.containsService(TypedSPIFixture.class, "TYPED.FIXTURE"));
+ assertThat(service.getValue(), is("expected"));
}
}
diff --git a/kernel/sql-translator/api/src/main/java/org/apache/shardingsphere/sqltranslator/config/SQLTranslatorRuleConfiguration.java b/infra/spi/src/test/java/org/apache/shardingsphere/infra/spi/type/typed/fixture/MissingTypedSPIFixture.java
similarity index 55%
copy from kernel/sql-translator/api/src/main/java/org/apache/shardingsphere/sqltranslator/config/SQLTranslatorRuleConfiguration.java
copy to infra/spi/src/test/java/org/apache/shardingsphere/infra/spi/type/typed/fixture/MissingTypedSPIFixture.java
index 2f0b644f297..f3221469062 100644
--- a/kernel/sql-translator/api/src/main/java/org/apache/shardingsphere/sqltranslator/config/SQLTranslatorRuleConfiguration.java
+++ b/infra/spi/src/test/java/org/apache/shardingsphere/infra/spi/type/typed/fixture/MissingTypedSPIFixture.java
@@ -15,26 +15,9 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.sqltranslator.config;
+package org.apache.shardingsphere.infra.spi.type.typed.fixture;
-import lombok.Getter;
-import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.infra.config.rule.scope.GlobalRuleConfiguration;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPI;
-import javax.validation.constraints.Pattern;
-import java.util.Properties;
-
-/**
- * SQL translator rule configuration.
- */
-@RequiredArgsConstructor
-@Getter
-public final class SQLTranslatorRuleConfiguration implements GlobalRuleConfiguration {
-
- @Pattern(regexp = ".*\\S.*", flags = Pattern.Flag.DOTALL)
- private final String type;
-
- private final Properties props;
-
- private final boolean useOriginalSQLWhenTranslatingFailed;
+public interface MissingTypedSPIFixture extends TypedSPI {
}
diff --git a/infra/spi/src/test/java/org/apache/shardingsphere/infra/spi/type/typed/fixture/impl/TypedSPIFixtureImpl.java b/infra/spi/src/test/java/org/apache/shardingsphere/infra/spi/type/typed/fixture/impl/TypedSPIFixtureImpl.java
index 117a811d1c7..2d4acf833c1 100644
--- a/infra/spi/src/test/java/org/apache/shardingsphere/infra/spi/type/typed/fixture/impl/TypedSPIFixtureImpl.java
+++ b/infra/spi/src/test/java/org/apache/shardingsphere/infra/spi/type/typed/fixture/impl/TypedSPIFixtureImpl.java
@@ -43,4 +43,9 @@ public final class TypedSPIFixtureImpl implements TypedSPIFixture {
public Collection<Object> getTypeAliases() {
return Collections.singleton("TYPED.ALIAS");
}
+
+ @Override
+ public boolean isDefault() {
+ return true;
+ }
}
diff --git a/kernel/authority/api/src/main/java/org/apache/shardingsphere/authority/config/AuthorityRuleConfiguration.java b/kernel/authority/api/src/main/java/org/apache/shardingsphere/authority/config/AuthorityRuleConfiguration.java
index 2f6844659ba..106fc233ad4 100644
--- a/kernel/authority/api/src/main/java/org/apache/shardingsphere/authority/config/AuthorityRuleConfiguration.java
+++ b/kernel/authority/api/src/main/java/org/apache/shardingsphere/authority/config/AuthorityRuleConfiguration.java
@@ -21,6 +21,7 @@ import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.config.rule.scope.GlobalRuleConfiguration;
+import org.apache.shardingsphere.infra.config.rule.validator.constraint.spi.SPITypeExists;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
@@ -40,6 +41,7 @@ public final class AuthorityRuleConfiguration implements GlobalRuleConfiguration
private final Collection<@NotNull UserConfiguration> users;
@NotNull
+ @SPITypeExists(spiClassName = "org.apache.shardingsphere.authority.spi.PrivilegeProvider")
private final AlgorithmConfiguration privilegeProvider;
@NotNull
diff --git a/kernel/authority/api/src/test/java/org/apache/shardingsphere/authority/config/AuthorityRuleConfigurationTest.java b/kernel/authority/api/src/test/java/org/apache/shardingsphere/authority/config/AuthorityRuleConfigurationTest.java
index 5535fb9632a..8b2c0d08304 100644
--- a/kernel/authority/api/src/test/java/org/apache/shardingsphere/authority/config/AuthorityRuleConfigurationTest.java
+++ b/kernel/authority/api/src/test/java/org/apache/shardingsphere/authority/config/AuthorityRuleConfigurationTest.java
@@ -40,7 +40,7 @@ class AuthorityRuleConfigurationTest {
}
private static Stream<Arguments> validRuleConfigurationArguments() {
- AlgorithmConfiguration privilegeProvider = createAlgorithmConfiguration("ALL_PERMITTED");
+ AlgorithmConfiguration privilegeProvider = createAlgorithmConfiguration("FIXTURE");
UserConfiguration userConfig = new UserConfiguration("foo_user", null, "%", null, false);
return Stream.of(
Arguments.of("Empty users and authenticators", new AuthorityRuleConfiguration(
@@ -65,7 +65,7 @@ class AuthorityRuleConfigurationTest {
}
private static Stream<Arguments> invalidRuleConfigurationArguments() {
- AlgorithmConfiguration privilegeProvider = createAlgorithmConfiguration("ALL_PERMITTED");
+ AlgorithmConfiguration privilegeProvider = createAlgorithmConfiguration("FIXTURE");
AlgorithmConfiguration authenticator = createAlgorithmConfiguration("MD5");
return Stream.of(
Arguments.of("Null users", new AuthorityRuleConfiguration(null, privilegeProvider, Collections.emptyMap(), null)),
@@ -74,6 +74,8 @@ class AuthorityRuleConfigurationTest {
Arguments.of("Empty username", createRuleConfiguration(new UserConfiguration("", null, "%", null, false))),
Arguments.of("Blank username", createRuleConfiguration(new UserConfiguration(" ", null, "%", null, false))),
Arguments.of("Null privilege provider", new AuthorityRuleConfiguration(Collections.emptyList(), null, Collections.emptyMap(), null)),
+ Arguments.of("Missing privilege provider", new AuthorityRuleConfiguration(
+ Collections.emptyList(), createAlgorithmConfiguration("MISSING"), Collections.emptyMap(), null)),
Arguments.of("Null authenticators", new AuthorityRuleConfiguration(Collections.emptyList(), privilegeProvider, null, null)),
Arguments.of("Null authenticator name", new AuthorityRuleConfiguration(
Collections.emptyList(), privilegeProvider, Collections.singletonMap(null, authenticator), null)),
@@ -86,6 +88,6 @@ class AuthorityRuleConfigurationTest {
}
private static AuthorityRuleConfiguration createRuleConfiguration(final UserConfiguration userConfig) {
- return new AuthorityRuleConfiguration(Collections.singleton(userConfig), createAlgorithmConfiguration("ALL_PERMITTED"), Collections.emptyMap(), null);
+ return new AuthorityRuleConfiguration(Collections.singleton(userConfig), createAlgorithmConfiguration("FIXTURE"), Collections.emptyMap(), null);
}
}
diff --git a/infra/spi/src/test/java/org/apache/shardingsphere/infra/spi/type/typed/fixture/impl/TypedSPIFixtureImpl.java b/kernel/authority/api/src/test/java/org/apache/shardingsphere/authority/config/fixture/PrivilegeProviderFixture.java
similarity index 59%
copy from infra/spi/src/test/java/org/apache/shardingsphere/infra/spi/type/typed/fixture/impl/TypedSPIFixtureImpl.java
copy to kernel/authority/api/src/test/java/org/apache/shardingsphere/authority/config/fixture/PrivilegeProviderFixture.java
index 117a811d1c7..4c5d7534088 100644
--- a/infra/spi/src/test/java/org/apache/shardingsphere/infra/spi/type/typed/fixture/impl/TypedSPIFixtureImpl.java
+++ b/kernel/authority/api/src/test/java/org/apache/shardingsphere/authority/config/fixture/PrivilegeProviderFixture.java
@@ -15,32 +15,22 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.infra.spi.type.typed.fixture.impl;
+package org.apache.shardingsphere.authority.config.fixture;
-import lombok.Getter;
-import org.apache.shardingsphere.infra.spi.type.typed.fixture.TypedSPIFixture;
+import org.apache.shardingsphere.authority.config.AuthorityRuleConfiguration;
+import org.apache.shardingsphere.authority.model.ShardingSpherePrivileges;
+import org.apache.shardingsphere.authority.spi.PrivilegeProvider;
+import org.apache.shardingsphere.infra.metadata.user.Grantee;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Properties;
-
-@Getter
-public final class TypedSPIFixtureImpl implements TypedSPIFixture {
-
- private String value;
+public final class PrivilegeProviderFixture implements PrivilegeProvider {
@Override
- public void init(final Properties props) {
- value = props.getProperty("key");
+ public ShardingSpherePrivileges build(final AuthorityRuleConfiguration ruleConfig, final Grantee grantee) {
+ return database -> true;
}
@Override
public String getType() {
- return "TYPED.FIXTURE";
- }
-
- @Override
- public Collection<Object> getTypeAliases() {
- return Collections.singleton("TYPED.ALIAS");
+ return "FIXTURE";
}
}
diff --git a/kernel/authority/api/src/test/resources/META-INF/services/org.apache.shardingsphere.authority.spi.PrivilegeProvider b/kernel/authority/api/src/test/resources/META-INF/services/org.apache.shardingsphere.authority.spi.PrivilegeProvider
new file mode 100644
index 00000000000..0075f83f839
--- /dev/null
+++ b/kernel/authority/api/src/test/resources/META-INF/services/org.apache.shardingsphere.authority.spi.PrivilegeProvider
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.shardingsphere.authority.config.fixture.PrivilegeProviderFixture
diff --git a/kernel/sql-translator/api/src/main/java/org/apache/shardingsphere/sqltranslator/config/SQLTranslatorRuleConfiguration.java b/kernel/sql-translator/api/src/main/java/org/apache/shardingsphere/sqltranslator/config/SQLTranslatorRuleConfiguration.java
index 2f0b644f297..bdf795fe04e 100644
--- a/kernel/sql-translator/api/src/main/java/org/apache/shardingsphere/sqltranslator/config/SQLTranslatorRuleConfiguration.java
+++ b/kernel/sql-translator/api/src/main/java/org/apache/shardingsphere/sqltranslator/config/SQLTranslatorRuleConfiguration.java
@@ -20,8 +20,8 @@ package org.apache.shardingsphere.sqltranslator.config;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.config.rule.scope.GlobalRuleConfiguration;
+import org.apache.shardingsphere.infra.config.rule.validator.constraint.spi.SPITypeExists;
-import javax.validation.constraints.Pattern;
import java.util.Properties;
/**
@@ -31,7 +31,7 @@ import java.util.Properties;
@Getter
public final class SQLTranslatorRuleConfiguration implements GlobalRuleConfiguration {
- @Pattern(regexp = ".*\\S.*", flags = Pattern.Flag.DOTALL)
+ @SPITypeExists(spiClassName = "org.apache.shardingsphere.sqltranslator.spi.SQLTranslator")
private final String type;
private final Properties props;
diff --git a/kernel/sql-translator/api/src/test/java/org/apache/shardingsphere/sqltranslator/config/SQLTranslatorRuleConfigurationTest.java b/kernel/sql-translator/api/src/test/java/org/apache/shardingsphere/sqltranslator/config/SQLTranslatorRuleConfigurationTest.java
index cf326020121..b3e195505a1 100644
--- a/kernel/sql-translator/api/src/test/java/org/apache/shardingsphere/sqltranslator/config/SQLTranslatorRuleConfigurationTest.java
+++ b/kernel/sql-translator/api/src/test/java/org/apache/shardingsphere/sqltranslator/config/SQLTranslatorRuleConfigurationTest.java
@@ -40,7 +40,7 @@ class SQLTranslatorRuleConfigurationTest {
private static Stream<Arguments> validRuleConfigurationArguments() {
return Stream.of(
Arguments.of("Null type and properties", new SQLTranslatorRuleConfiguration(null, null, true)),
- Arguments.of("Type and properties", new SQLTranslatorRuleConfiguration("NATIVE", new Properties(), false)));
+ Arguments.of("Type and properties", new SQLTranslatorRuleConfiguration("FIXTURE", new Properties(), false)));
}
@ParameterizedTest(name = "{0}")
@@ -53,6 +53,7 @@ class SQLTranslatorRuleConfigurationTest {
return Stream.of(
Arguments.of("Empty type", new SQLTranslatorRuleConfiguration("", null, true)),
Arguments.of("Blank type", new SQLTranslatorRuleConfiguration(" ", null, true)),
- Arguments.of("Multiline blank type", new SQLTranslatorRuleConfiguration("\n\t", null, true)));
+ Arguments.of("Multiline blank type", new SQLTranslatorRuleConfiguration("\n\t", null, true)),
+ Arguments.of("Missing type", new SQLTranslatorRuleConfiguration("MISSING", null, true)));
}
}
diff --git a/kernel/sql-translator/api/src/test/java/org/apache/shardingsphere/sqltranslator/config/fixture/SQLTranslatorFixture.java b/kernel/sql-translator/api/src/test/java/org/apache/shardingsphere/sqltranslator/config/fixture/SQLTranslatorFixture.java
new file mode 100644
index 00000000000..794a49316ff
--- /dev/null
+++ b/kernel/sql-translator/api/src/test/java/org/apache/shardingsphere/sqltranslator/config/fixture/SQLTranslatorFixture.java
@@ -0,0 +1,41 @@
+/*
+ * 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.shardingsphere.sqltranslator.config.fixture;
+
+import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
+import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
+import org.apache.shardingsphere.infra.session.query.QueryContext;
+import org.apache.shardingsphere.sqltranslator.context.SQLTranslatorContext;
+import org.apache.shardingsphere.sqltranslator.spi.SQLTranslator;
+
+import java.util.List;
+
+public final class SQLTranslatorFixture implements SQLTranslator {
+
+ @Override
+ public SQLTranslatorContext translate(final String sql, final List<Object> parameters, final QueryContext queryContext, final DatabaseType storageType, final ShardingSphereDatabase database,
+ final RuleMetaData globalRuleMetaData) {
+ return new SQLTranslatorContext(sql, parameters);
+ }
+
+ @Override
+ public String getType() {
+ return "FIXTURE";
+ }
+}
diff --git a/kernel/sql-translator/api/src/test/resources/META-INF/services/org.apache.shardingsphere.sqltranslator.spi.SQLTranslator b/kernel/sql-translator/api/src/test/resources/META-INF/services/org.apache.shardingsphere.sqltranslator.spi.SQLTranslator
new file mode 100644
index 00000000000..eed26547e00
--- /dev/null
+++ b/kernel/sql-translator/api/src/test/resources/META-INF/services/org.apache.shardingsphere.sqltranslator.spi.SQLTranslator
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.shardingsphere.sqltranslator.config.fixture.SQLTranslatorFixture
diff --git a/kernel/sql-translator/distsql/handler/src/main/java/org/apache/shardingsphere/sqltranslator/distsql/handler/update/AlterSQLTranslatorRuleExecutor.java b/kernel/sql-translator/distsql/handler/src/main/java/org/apache/shardingsphere/sqltranslator/distsql/handler/update/AlterSQLTranslatorRuleExecutor.java
index 78fd70c9b75..c00a11880f9 100644
--- a/kernel/sql-translator/distsql/handler/src/main/java/org/apache/shardingsphere/sqltranslator/distsql/handler/update/AlterSQLTranslatorRuleExecutor.java
+++ b/kernel/sql-translator/distsql/handler/src/main/java/org/apache/shardingsphere/sqltranslator/distsql/handler/update/AlterSQLTranslatorRuleExecutor.java
@@ -19,11 +19,9 @@ package org.apache.shardingsphere.sqltranslator.distsql.handler.update;
import lombok.Setter;
import org.apache.shardingsphere.distsql.handler.engine.update.rdl.rule.spi.global.GlobalRuleDefinitionExecutor;
-import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.sqltranslator.config.SQLTranslatorRuleConfiguration;
import org.apache.shardingsphere.sqltranslator.distsql.statement.updateable.AlterSQLTranslatorRuleStatement;
import org.apache.shardingsphere.sqltranslator.rule.SQLTranslatorRule;
-import org.apache.shardingsphere.sqltranslator.spi.SQLTranslator;
/**
* Alter SQL translator rule executor.
@@ -33,11 +31,6 @@ public final class AlterSQLTranslatorRuleExecutor implements GlobalRuleDefinitio
private SQLTranslatorRule rule;
- @Override
- public void checkBeforeUpdate(final AlterSQLTranslatorRuleStatement sqlStatement) {
- TypedSPILoader.checkService(SQLTranslator.class, sqlStatement.getProvider().getName(), sqlStatement.getProvider().getProps());
- }
-
@Override
public SQLTranslatorRuleConfiguration buildToBeAlteredRuleConfiguration(final AlterSQLTranslatorRuleStatement sqlStatement) {
boolean useOriginalSQLWhenTranslatingFailed = null == sqlStatement.getUseOriginalSQLWhenTranslatingFailed()
diff --git a/kernel/transaction/api/src/main/java/org/apache/shardingsphere/transaction/config/TransactionRuleConfiguration.java b/kernel/transaction/api/src/main/java/org/apache/shardingsphere/transaction/config/TransactionRuleConfiguration.java
index 310227b6bce..5392e36289d 100644
--- a/kernel/transaction/api/src/main/java/org/apache/shardingsphere/transaction/config/TransactionRuleConfiguration.java
+++ b/kernel/transaction/api/src/main/java/org/apache/shardingsphere/transaction/config/TransactionRuleConfiguration.java
@@ -21,6 +21,7 @@ import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.config.rule.scope.GlobalRuleConfiguration;
+import org.apache.shardingsphere.transaction.config.validator.ValidTransactionProviderType;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
@@ -29,6 +30,7 @@ import java.util.Properties;
/**
* Transaction rule configuration.
*/
+@ValidTransactionProviderType
@RequiredArgsConstructor
@Getter
@EqualsAndHashCode
diff --git a/kernel/transaction/api/src/main/java/org/apache/shardingsphere/transaction/config/validator/TransactionProviderTypeValidator.java b/kernel/transaction/api/src/main/java/org/apache/shardingsphere/transaction/config/validator/TransactionProviderTypeValidator.java
new file mode 100644
index 00000000000..1accb35fd9a
--- /dev/null
+++ b/kernel/transaction/api/src/main/java/org/apache/shardingsphere/transaction/config/validator/TransactionProviderTypeValidator.java
@@ -0,0 +1,49 @@
+/*
+ * 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.shardingsphere.transaction.config.validator;
+
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.apache.shardingsphere.transaction.api.TransactionType;
+import org.apache.shardingsphere.transaction.config.TransactionRuleConfiguration;
+import org.apache.shardingsphere.transaction.spi.ShardingSphereDistributedTransactionManager;
+
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+/**
+ * Transaction provider type validator.
+ */
+public final class TransactionProviderTypeValidator implements ConstraintValidator<ValidTransactionProviderType, TransactionRuleConfiguration> {
+
+ @Override
+ public boolean isValid(final TransactionRuleConfiguration value, final ConstraintValidatorContext context) {
+ if (null == value || !TransactionType.XA.name().equalsIgnoreCase(value.getDefaultType())) {
+ return true;
+ }
+ if (containsProviderType(value.getProviderType())) {
+ return true;
+ }
+ context.disableDefaultConstraintViolation();
+ context.buildConstraintViolationWithTemplate(context.getDefaultConstraintMessageTemplate()).addPropertyNode("providerType").addConstraintViolation();
+ return false;
+ }
+
+ private boolean containsProviderType(final String providerType) {
+ return TypedSPILoader.findService(ShardingSphereDistributedTransactionManager.class, TransactionType.XA.name()).map(optional -> optional.containsProviderType(providerType)).orElse(false);
+ }
+}
diff --git a/kernel/transaction/api/src/main/java/org/apache/shardingsphere/transaction/config/validator/ValidTransactionProviderType.java b/kernel/transaction/api/src/main/java/org/apache/shardingsphere/transaction/config/validator/ValidTransactionProviderType.java
new file mode 100644
index 00000000000..12aea0e4c5f
--- /dev/null
+++ b/kernel/transaction/api/src/main/java/org/apache/shardingsphere/transaction/config/validator/ValidTransactionProviderType.java
@@ -0,0 +1,57 @@
+/*
+ * 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.shardingsphere.transaction.config.validator;
+
+import javax.validation.Constraint;
+import javax.validation.Payload;
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Valid transaction provider type constraint.
+ */
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.RUNTIME)
+@Constraint(validatedBy = TransactionProviderTypeValidator.class)
+@Documented
+public @interface ValidTransactionProviderType {
+
+ /**
+ * Get message.
+ *
+ * @return message
+ */
+ String message() default "does not match an available SPI implementation";
+
+ /**
+ * Get groups.
+ *
+ * @return groups
+ */
+ Class<?>[] groups() default {};
+
+ /**
+ * Get payload.
+ *
+ * @return payload
+ */
+ Class<? extends Payload>[] payload() default {};
+}
diff --git a/kernel/transaction/api/src/test/java/org/apache/shardingsphere/transaction/config/TransactionRuleConfigurationTest.java b/kernel/transaction/api/src/test/java/org/apache/shardingsphere/transaction/config/TransactionRuleConfigurationTest.java
index 19bafd592b1..63b58018705 100644
--- a/kernel/transaction/api/src/test/java/org/apache/shardingsphere/transaction/config/TransactionRuleConfigurationTest.java
+++ b/kernel/transaction/api/src/test/java/org/apache/shardingsphere/transaction/config/TransactionRuleConfigurationTest.java
@@ -42,7 +42,8 @@ class TransactionRuleConfigurationTest {
Arguments.of("Null optional values", new TransactionRuleConfiguration("LOCAL", null, null)),
Arguments.of("Lowercase default type", new TransactionRuleConfiguration("local", null, null)),
Arguments.of("Mixed case default type", new TransactionRuleConfiguration("Base", null, null)),
- Arguments.of("Provider type", new TransactionRuleConfiguration("XA", "Atomikos", null)),
+ Arguments.of("Provider ignored for local transaction", new TransactionRuleConfiguration("LOCAL", "MISSING", null)),
+ Arguments.of("Provider ignored for base transaction", new TransactionRuleConfiguration("BASE", "MISSING", null)),
Arguments.of("Properties", new TransactionRuleConfiguration("BASE", null, new Properties())));
}
@@ -58,6 +59,7 @@ class TransactionRuleConfigurationTest {
Arguments.of("Empty default type", new TransactionRuleConfiguration("", null, null)),
Arguments.of("Blank default type", new TransactionRuleConfiguration(" ", null, null)),
Arguments.of("Unsupported default type", new TransactionRuleConfiguration("FOO", null, null)),
- Arguments.of("Padded default type", new TransactionRuleConfiguration(" XA ", null, null)));
+ Arguments.of("Padded default type", new TransactionRuleConfiguration(" XA ", null, null)),
+ Arguments.of("XA module is unavailable", new TransactionRuleConfiguration("XA", "Atomikos", null)));
}
}
diff --git a/kernel/transaction/distsql/handler/src/main/java/org/apache/shardingsphere/transaction/distsql/handler/update/AlterTransactionRuleExecutor.java b/kernel/transaction/distsql/handler/src/main/java/org/apache/shardingsphere/transaction/distsql/handler/update/AlterTransactionRuleExecutor.java
index 9a62ae53894..667dce67d1e 100644
--- a/kernel/transaction/distsql/handler/src/main/java/org/apache/shardingsphere/transaction/distsql/handler/update/AlterTransactionRuleExecutor.java
+++ b/kernel/transaction/distsql/handler/src/main/java/org/apache/shardingsphere/transaction/distsql/handler/update/AlterTransactionRuleExecutor.java
@@ -18,56 +18,15 @@
package org.apache.shardingsphere.transaction.distsql.handler.update;
import org.apache.shardingsphere.distsql.handler.engine.update.rdl.rule.spi.global.GlobalRuleDefinitionExecutor;
-import org.apache.shardingsphere.infra.exception.ShardingSpherePreconditions;
-import org.apache.shardingsphere.infra.exception.kernel.metadata.rule.InvalidRuleConfigurationException;
-import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
-import org.apache.shardingsphere.transaction.api.TransactionType;
import org.apache.shardingsphere.transaction.config.TransactionRuleConfiguration;
import org.apache.shardingsphere.transaction.distsql.statement.updatable.AlterTransactionRuleStatement;
import org.apache.shardingsphere.transaction.rule.TransactionRule;
-import org.apache.shardingsphere.transaction.spi.ShardingSphereDistributedTransactionManager;
-
-import java.util.Collection;
-import java.util.Optional;
/**
* Alter transaction rule executor.
*/
public final class AlterTransactionRuleExecutor implements GlobalRuleDefinitionExecutor<AlterTransactionRuleStatement, TransactionRule> {
- @Override
- public void checkBeforeUpdate(final AlterTransactionRuleStatement sqlStatement) {
- checkTransactionType(sqlStatement);
- TransactionType transactionType = TransactionType.valueOf(sqlStatement.getDefaultType().toUpperCase());
- if (TransactionType.LOCAL != transactionType) {
- checkTransactionManager(sqlStatement, transactionType);
- }
- }
-
- private void checkTransactionType(final AlterTransactionRuleStatement statement) {
- try {
- TransactionType.valueOf(statement.getDefaultType().toUpperCase());
- } catch (final IllegalArgumentException ignored) {
- throw new InvalidRuleConfigurationException("Transaction", String.format("Unsupported transaction type `%s`", statement.getDefaultType()));
- }
- }
-
- private void checkTransactionManager(final AlterTransactionRuleStatement statement, final TransactionType transactionType) {
- Collection<ShardingSphereDistributedTransactionManager> distributedTransactionManagers = ShardingSphereServiceLoader.getServiceInstances(ShardingSphereDistributedTransactionManager.class);
- Optional<ShardingSphereDistributedTransactionManager> distributedTransactionManager =
- distributedTransactionManagers.stream().filter(each -> transactionType == each.getTransactionType()).findFirst();
- ShardingSpherePreconditions.checkState(distributedTransactionManager.isPresent(),
- () -> new InvalidRuleConfigurationException("Transaction", String.format("No transaction manager with type `%s`", statement.getDefaultType())));
- if (TransactionType.XA == transactionType) {
- checkTransactionManagerProviderType(distributedTransactionManager.get(), statement.getProvider().getProviderType());
- }
- }
-
- private void checkTransactionManagerProviderType(final ShardingSphereDistributedTransactionManager distributedTransactionManager, final String providerType) {
- ShardingSpherePreconditions.checkState(distributedTransactionManager.containsProviderType(providerType),
- () -> new InvalidRuleConfigurationException("Transaction", String.format("No transaction manager provider with type `%s`", providerType)));
- }
-
@Override
public TransactionRuleConfiguration buildToBeAlteredRuleConfiguration(final AlterTransactionRuleStatement sqlStatement) {
return new TransactionRuleConfiguration(sqlStatement.getDefaultType(), sqlStatement.getProvider().getProviderType(), sqlStatement.getProvider().getProps());
diff --git a/kernel/transaction/distsql/handler/src/test/resources/cases/alter-transaction-rule.xml b/kernel/transaction/distsql/handler/src/test/resources/cases/alter-transaction-rule.xml
index 58ca7dd7247..62bc1ac1c9a 100644
--- a/kernel/transaction/distsql/handler/src/test/resources/cases/alter-transaction-rule.xml
+++ b/kernel/transaction/distsql/handler/src/test/resources/cases/alter-transaction-rule.xml
@@ -17,15 +17,6 @@
-->
<distsql-rule-definition-executor-test-cases>
- <test-case dist-sql="ALTER TRANSACTION RULE (DEFAULT="Invalid", TYPE(NAME="", PROPERTIES()))"
- current-rule-config-yaml-file="cases/alter-transaction-rule-empty-current.yaml"
- expected-exception="org.apache.shardingsphere.infra.exception.kernel.metadata.rule.InvalidRuleConfigurationException"/>
- <test-case dist-sql="ALTER TRANSACTION RULE (DEFAULT="BASE", TYPE(NAME="", PROPERTIES()))"
- current-rule-config-yaml-file="cases/alter-transaction-rule-empty-current.yaml"
- expected-exception="org.apache.shardingsphere.infra.exception.kernel.metadata.rule.InvalidRuleConfigurationException"/>
- <test-case dist-sql="ALTER TRANSACTION RULE (DEFAULT="XA", TYPE(NAME="Invalid", PROPERTIES()))"
- current-rule-config-yaml-file="cases/alter-transaction-rule-empty-current.yaml"
- expected-exception="org.apache.shardingsphere.infra.exception.kernel.metadata.rule.InvalidRuleConfigurationException"/>
<test-case dist-sql="ALTER TRANSACTION RULE (DEFAULT="XA", TYPE(NAME="Atomikos", PROPERTIES("k"="v")))"
current-rule-config-yaml-file="cases/alter-transaction-rule-normal-current.yaml"
expected-rule-config-yaml-file="cases/alter-transaction-rule-expected-config.yaml"/>
diff --git a/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/XAShardingSphereTransactionManager.java b/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/XAShardingSphereTransactionManager.java
index b2461a077ab..6cb15d6c97f 100644
--- a/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/XAShardingSphereTransactionManager.java
+++ b/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/XAShardingSphereTransactionManager.java
@@ -25,7 +25,6 @@ import org.apache.shardingsphere.database.connector.core.spi.DatabaseTypedSPILoa
import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
import org.apache.shardingsphere.infra.exception.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.session.connection.transaction.TransactionOptionReplayCallback;
-import org.apache.shardingsphere.infra.spi.exception.ServiceProviderNotFoundException;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.transaction.api.TransactionType;
import org.apache.shardingsphere.transaction.core.ResourceDataSource;
@@ -47,7 +46,6 @@ import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
-import java.util.Properties;
/**
* ShardingSphere Transaction manager for XA.
@@ -137,13 +135,7 @@ public final class XAShardingSphereTransactionManager implements ShardingSphereD
@Override
public boolean containsProviderType(final String providerType) {
- try {
- TypedSPILoader.checkService(XATransactionManagerProvider.class, providerType, new Properties());
- return true;
- } catch (final ServiceProviderNotFoundException ex) {
- return false;
- }
-
+ return TypedSPILoader.containsService(XATransactionManagerProvider.class, providerType);
}
@Override
diff --git a/kernel/transaction/api/src/test/java/org/apache/shardingsphere/transaction/config/TransactionRuleConfigurationTest.java b/kernel/transaction/type/xa/core/src/test/java/org/apache/shardingsphere/transaction/config/TransactionRuleConfigurationTest.java
similarity index 55%
copy from kernel/transaction/api/src/test/java/org/apache/shardingsphere/transaction/config/TransactionRuleConfigurationTest.java
copy to kernel/transaction/type/xa/core/src/test/java/org/apache/shardingsphere/transaction/config/TransactionRuleConfigurationTest.java
index 19bafd592b1..73500078aef 100644
--- a/kernel/transaction/api/src/test/java/org/apache/shardingsphere/transaction/config/TransactionRuleConfigurationTest.java
+++ b/kernel/transaction/type/xa/core/src/test/java/org/apache/shardingsphere/transaction/config/TransactionRuleConfigurationTest.java
@@ -17,47 +17,55 @@
package org.apache.shardingsphere.transaction.config;
+import org.apache.bval.jsr.ApacheValidationProvider;
import org.apache.shardingsphere.infra.config.rule.validator.RuleConfigurationValidator;
import org.apache.shardingsphere.infra.exception.kernel.metadata.rule.InvalidRuleConfigurationException;
+import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
-import java.util.Properties;
+import javax.validation.ConstraintViolation;
+import javax.validation.Validation;
+import javax.validation.Validator;
+import java.util.Collection;
import java.util.stream.Stream;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
class TransactionRuleConfigurationTest {
+ private static final Validator VALIDATOR = Validation.byProvider(ApacheValidationProvider.class).configure().buildValidatorFactory().getValidator();
+
@ParameterizedTest(name = "{0}")
- @MethodSource("validRuleConfigurationArguments")
- void assertValidateValidRuleConfiguration(final String name, final TransactionRuleConfiguration ruleConfig) {
+ @MethodSource("validXAConfigurationArguments")
+ void assertValidateValidXAConfiguration(final String name, final TransactionRuleConfiguration ruleConfig) {
assertDoesNotThrow(() -> RuleConfigurationValidator.validate(ruleConfig));
}
- private static Stream<Arguments> validRuleConfigurationArguments() {
+ private static Stream<Arguments> validXAConfigurationArguments() {
return Stream.of(
- Arguments.of("Null optional values", new TransactionRuleConfiguration("LOCAL", null, null)),
- Arguments.of("Lowercase default type", new TransactionRuleConfiguration("local", null, null)),
- Arguments.of("Mixed case default type", new TransactionRuleConfiguration("Base", null, null)),
Arguments.of("Provider type", new TransactionRuleConfiguration("XA", "Atomikos", null)),
- Arguments.of("Properties", new TransactionRuleConfiguration("BASE", null, new Properties())));
+ Arguments.of("Case insensitive provider type", new TransactionRuleConfiguration("xa", "atomikos", null)),
+ Arguments.of("Default provider", new TransactionRuleConfiguration("XA", null, null)));
}
@ParameterizedTest(name = "{0}")
- @MethodSource("invalidRuleConfigurationArguments")
- void assertValidateInvalidRuleConfiguration(final String name, final TransactionRuleConfiguration ruleConfig) {
+ @MethodSource("invalidXAConfigurationArguments")
+ void assertValidateInvalidXAConfiguration(final String name, final TransactionRuleConfiguration ruleConfig) {
assertThrows(InvalidRuleConfigurationException.class, () -> RuleConfigurationValidator.validate(ruleConfig));
}
- private static Stream<Arguments> invalidRuleConfigurationArguments() {
- return Stream.of(
- Arguments.of("Null default type", new TransactionRuleConfiguration(null, null, null)),
- Arguments.of("Empty default type", new TransactionRuleConfiguration("", null, null)),
- Arguments.of("Blank default type", new TransactionRuleConfiguration(" ", null, null)),
- Arguments.of("Unsupported default type", new TransactionRuleConfiguration("FOO", null, null)),
- Arguments.of("Padded default type", new TransactionRuleConfiguration(" XA ", null, null)));
+ private static Stream<Arguments> invalidXAConfigurationArguments() {
+ return Stream.of(Arguments.of("Missing provider", new TransactionRuleConfiguration("XA", "MISSING", null)));
+ }
+
+ @Test
+ void assertInvalidProviderTypePropertyPath() {
+ Collection<ConstraintViolation<TransactionRuleConfiguration>> actual = VALIDATOR.validate(new TransactionRuleConfiguration("XA", "MISSING", null));
+ assertThat(actual.iterator().next().getPropertyPath().toString(), is("providerType"));
}
}
diff --git a/kernel/transaction/type/xa/core/src/test/java/org/apache/shardingsphere/transaction/xa/XAShardingSphereTransactionManagerTest.java b/kernel/transaction/type/xa/core/src/test/java/org/apache/shardingsphere/transaction/xa/XAShardingSphereTransactionManagerTest.java
index 3bbd3b17580..64a0d3ea081 100644
--- a/kernel/transaction/type/xa/core/src/test/java/org/apache/shardingsphere/transaction/xa/XAShardingSphereTransactionManagerTest.java
+++ b/kernel/transaction/type/xa/core/src/test/java/org/apache/shardingsphere/transaction/xa/XAShardingSphereTransactionManagerTest.java
@@ -28,6 +28,8 @@ import org.apache.shardingsphere.transaction.xa.jta.datasource.XATransactionData
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
import org.mockito.internal.configuration.plugins.Plugins;
import javax.sql.DataSource;
@@ -135,6 +137,17 @@ class XAShardingSphereTransactionManagerTest {
assertFalse(xaTransactionManager.isInTransaction());
}
+ @ParameterizedTest(name = "{0}")
+ @CsvSource({"Atomikos, true", "atomikos, true", "MISSING, false"})
+ void assertContainsProviderType(final String providerType, final boolean expected) {
+ assertThat(xaTransactionManager.containsProviderType(providerType), is(expected));
+ }
+
+ @Test
+ void assertContainsDefaultProviderType() {
+ assertTrue(xaTransactionManager.containsProviderType(null));
+ }
+
@SneakyThrows(ReflectiveOperationException.class)
@SuppressWarnings("unchecked")
private Map<String, XATransactionDataSource> getCachedDataSources() {