You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by me...@apache.org on 2022/09/03 13:23:46 UTC
[shardingsphere] branch master updated: Add ShardingSpherePreconditions (#20750)
This is an automated email from the ASF dual-hosted git repository.
menghaoran 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 6004043f3da Add ShardingSpherePreconditions (#20750)
6004043f3da is described below
commit 6004043f3da22c59368a5fe174115d5194aac0a3
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sat Sep 3 21:23:34 2022 +0800
Add ShardingSpherePreconditions (#20750)
---
...MGRMySQLDatabaseDiscoveryProviderAlgorithm.java | 5 +-
.../exception/ShardingSpherePreconditions.java | 55 ++++++++++++++++++++++
2 files changed, 57 insertions(+), 3 deletions(-)
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/MGRMySQLDatabaseDiscoveryProviderAlgorithm.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/MGRMySQLDatabaseDiscoveryProviderAlgorithm.java
index 78c601a5097..036efe67709 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/MGRMySQLDatabaseDiscoveryProviderAlgorithm.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/MGRMySQLDatabaseDiscoveryProviderAlgorithm.java
@@ -26,6 +26,7 @@ import org.apache.shardingsphere.dbdiscovery.spi.DatabaseDiscoveryProviderAlgori
import org.apache.shardingsphere.dbdiscovery.spi.ReplicaDataSourceStatus;
import org.apache.shardingsphere.infra.database.metadata.dialect.MySQLDataSourceMetaData;
import org.apache.shardingsphere.infra.executor.kernel.ExecutorEngine;
+import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.util.exception.external.sql.SQLWrapperException;
import javax.sql.DataSource;
@@ -105,9 +106,7 @@ public final class MGRMySQLDatabaseDiscoveryProviderAlgorithm implements Databas
private void checkPluginActive(final String databaseName, final Statement statement) throws SQLException {
try (ResultSet resultSet = statement.executeQuery(QUERY_PLUGIN_STATUS)) {
- if (!resultSet.next() || !"ACTIVE".equals(resultSet.getString("PLUGIN_STATUS"))) {
- throw new InvalidMGRPluginException(databaseName);
- }
+ ShardingSpherePreconditions.checkStatus(resultSet.next() && "ACTIVE".equals(resultSet.getString("PLUGIN_STATUS")), new InvalidMGRPluginException(databaseName));
}
}
diff --git a/shardingsphere-infra/shardingsphere-infra-util/src/main/java/org/apache/shardingsphere/infra/util/exception/ShardingSpherePreconditions.java b/shardingsphere-infra/shardingsphere-infra-util/src/main/java/org/apache/shardingsphere/infra/util/exception/ShardingSpherePreconditions.java
new file mode 100644
index 00000000000..1537b450aca
--- /dev/null
+++ b/shardingsphere-infra/shardingsphere-infra-util/src/main/java/org/apache/shardingsphere/infra/util/exception/ShardingSpherePreconditions.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 org.apache.shardingsphere.infra.util.exception;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.infra.util.exception.external.ShardingSphereExternalException;
+import org.apache.shardingsphere.infra.util.exception.internal.ShardingSphereInternalException;
+
+/**
+ * ShardingSphere preconditions.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class ShardingSpherePreconditions {
+
+ /**
+ * Ensures the truth of an expression involving the state of the calling instance.
+ *
+ * @param expectedExpression expected expression
+ * @param exceptionIfUnexpected exception thrown if expression is unexpected
+ */
+ public static void checkStatus(final boolean expectedExpression, final ShardingSphereExternalException exceptionIfUnexpected) {
+ if (!expectedExpression) {
+ throw exceptionIfUnexpected;
+ }
+ }
+
+ /**
+ * Ensures the truth of an expression involving the state of the calling instance.
+ *
+ * @param expectedExpression expected expression
+ * @param exceptionIfUnexpected exception thrown if expression is unexpected
+ * @throws ShardingSphereInternalException ShardingSphere internal exception
+ */
+ public static void checkStatus(final boolean expectedExpression, final ShardingSphereInternalException exceptionIfUnexpected) throws ShardingSphereInternalException {
+ if (!expectedExpression) {
+ throw exceptionIfUnexpected;
+ }
+ }
+}