You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2023/04/28 03:25:12 UTC

[shardingsphere] branch master updated: Fix PMD violations in Proxy and protocols (#25382)

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

panjuan 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 acba70a4286 Fix PMD violations in Proxy and protocols (#25382)
acba70a4286 is described below

commit acba70a4286ff63b2595fe71dc3c7f4f56005eae
Author: 吴伟杰 <wu...@apache.org>
AuthorDate: Fri Apr 28 11:25:04 2023 +0800

    Fix PMD violations in Proxy and protocols (#25382)
---
 .../query/text/MySQLTextResultSetRowPacket.java    | 32 ++++++++++++----------
 .../command/query/PostgreSQLDataRowPacket.java     |  2 +-
 .../frontend/command/CommandExecutorTask.java      |  6 ++--
 .../netty/FrontendChannelInboundHandler.java       |  6 ++--
 .../proxy/frontend/ssl/SSLUtils.java               | 15 ++++++++--
 5 files changed, 38 insertions(+), 23 deletions(-)

diff --git a/db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/command/query/text/MySQLTextResultSetRowPacket.java b/db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/command/query/text/MySQLTextResultSetRowPacket.java
index 6473d4e5afb..ce959432210 100644
--- a/db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/command/query/text/MySQLTextResultSetRowPacket.java
+++ b/db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/command/query/text/MySQLTextResultSetRowPacket.java
@@ -56,21 +56,25 @@ public final class MySQLTextResultSetRowPacket implements MySQLPacket {
         for (Object each : data) {
             if (null == each) {
                 payload.writeInt1(NULL);
-            } else {
-                if (each instanceof byte[]) {
-                    payload.writeBytesLenenc((byte[]) each);
-                } else if (each instanceof Timestamp && 0 == ((Timestamp) each).getNanos()) {
-                    payload.writeStringLenenc(each.toString().split("\\.")[0]);
-                } else if (each instanceof BigDecimal) {
-                    payload.writeStringLenenc(((BigDecimal) each).toPlainString());
-                } else if (each instanceof Boolean) {
-                    payload.writeBytesLenenc((Boolean) each ? new byte[]{1} : new byte[]{0});
-                } else if (each instanceof LocalDateTime) {
-                    payload.writeStringLenenc(DATE_TIME_FORMATTER.format((LocalDateTime) each));
-                } else {
-                    payload.writeStringLenenc(each.toString());
-                }
+                continue;
             }
+            writeDataIntoPayload(payload, each);
+        }
+    }
+    
+    private void writeDataIntoPayload(final MySQLPacketPayload payload, final Object data) {
+        if (data instanceof byte[]) {
+            payload.writeBytesLenenc((byte[]) data);
+        } else if (data instanceof Timestamp && 0 == ((Timestamp) data).getNanos()) {
+            payload.writeStringLenenc(data.toString().split("\\.")[0]);
+        } else if (data instanceof BigDecimal) {
+            payload.writeStringLenenc(((BigDecimal) data).toPlainString());
+        } else if (data instanceof Boolean) {
+            payload.writeBytesLenenc((Boolean) data ? new byte[]{1} : new byte[]{0});
+        } else if (data instanceof LocalDateTime) {
+            payload.writeStringLenenc(DATE_TIME_FORMATTER.format((LocalDateTime) data));
+        } else {
+            payload.writeStringLenenc(data.toString());
         }
     }
 }
diff --git a/db-protocol/postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/PostgreSQLDataRowPacket.java b/db-protocol/postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/PostgreSQLDataRowPacket.java
index baa800b46dc..c2bc4b65718 100644
--- a/db-protocol/postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/PostgreSQLDataRowPacket.java
+++ b/db-protocol/postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/PostgreSQLDataRowPacket.java
@@ -84,7 +84,7 @@ public final class PostgreSQLDataRowPacket implements PostgreSQLIdentifierPacket
             payload.writeInt4(dataBytes.length);
             payload.writeBytes(dataBytes);
         } catch (final SQLException ex) {
-            throw new RuntimeException(ex);
+            throw new IllegalStateException(ex);
         }
     }
     
diff --git a/proxy/frontend/core/src/main/java/org/apache/shardingsphere/proxy/frontend/command/CommandExecutorTask.java b/proxy/frontend/core/src/main/java/org/apache/shardingsphere/proxy/frontend/command/CommandExecutorTask.java
index 83b0d33f991..4fbac705911 100644
--- a/proxy/frontend/core/src/main/java/org/apache/shardingsphere/proxy/frontend/command/CommandExecutorTask.java
+++ b/proxy/frontend/core/src/main/java/org/apache/shardingsphere/proxy/frontend/command/CommandExecutorTask.java
@@ -131,10 +131,10 @@ public final class CommandExecutorTask implements Runnable {
     }
     
     private void processException(final Exception cause) {
-        if (!ExpectedExceptions.isExpected(cause.getClass())) {
-            log.error("Exception occur: ", cause);
-        } else if (log.isDebugEnabled()) {
+        if (ExpectedExceptions.isExpected(cause.getClass())) {
             log.debug("Exception occur: ", cause);
+        } else {
+            log.error("Exception occur: ", cause);
         }
         context.write(databaseProtocolFrontendEngine.getCommandExecuteEngine().getErrorPacket(cause));
         Optional<DatabasePacket<?>> databasePacket = databaseProtocolFrontendEngine.getCommandExecuteEngine().getOtherPacket(connectionSession);
diff --git a/proxy/frontend/core/src/main/java/org/apache/shardingsphere/proxy/frontend/netty/FrontendChannelInboundHandler.java b/proxy/frontend/core/src/main/java/org/apache/shardingsphere/proxy/frontend/netty/FrontendChannelInboundHandler.java
index b2de0495633..67ca9335bd4 100644
--- a/proxy/frontend/core/src/main/java/org/apache/shardingsphere/proxy/frontend/netty/FrontendChannelInboundHandler.java
+++ b/proxy/frontend/core/src/main/java/org/apache/shardingsphere/proxy/frontend/netty/FrontendChannelInboundHandler.java
@@ -86,10 +86,10 @@ public final class FrontendChannelInboundHandler extends ChannelInboundHandlerAd
             // CHECKSTYLE:OFF
         } catch (final Exception ex) {
             // CHECKSTYLE:ON
-            if (!ExpectedExceptions.isExpected(ex.getClass())) {
-                log.error("Exception occur: ", ex);
-            } else if (log.isDebugEnabled()) {
+            if (ExpectedExceptions.isExpected(ex.getClass())) {
                 log.debug("Exception occur: ", ex);
+            } else {
+                log.error("Exception occur: ", ex);
             }
             context.writeAndFlush(databaseProtocolFrontendEngine.getCommandExecuteEngine().getErrorPacket(ex));
             context.close();
diff --git a/proxy/frontend/core/src/main/java/org/apache/shardingsphere/proxy/frontend/ssl/SSLUtils.java b/proxy/frontend/core/src/main/java/org/apache/shardingsphere/proxy/frontend/ssl/SSLUtils.java
index 8bd9db1e359..2750ee14abf 100644
--- a/proxy/frontend/core/src/main/java/org/apache/shardingsphere/proxy/frontend/ssl/SSLUtils.java
+++ b/proxy/frontend/core/src/main/java/org/apache/shardingsphere/proxy/frontend/ssl/SSLUtils.java
@@ -54,15 +54,26 @@ public final class SSLUtils {
         Security.addProvider(new BouncyCastleProvider());
     }
     
+    /**
+     * Generate a 4096 RSA key pair.
+     *
+     * @return RSA key pair
+     */
     @SneakyThrows({NoSuchProviderException.class, NoSuchAlgorithmException.class})
-    static KeyPair generateRSAKeyPair() {
+    public static KeyPair generateRSAKeyPair() {
         KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", BouncyCastleProvider.PROVIDER_NAME);
         keyPairGenerator.initialize(4096, new SecureRandom());
         return keyPairGenerator.generateKeyPair();
     }
     
+    /**
+     * Generate a self-signed X.509 certificate with provided key pair.
+     *
+     * @param keyPair key pair
+     * @return self-signed X.509 certificate
+     */
     @SneakyThrows({OperatorCreationException.class, CertificateException.class})
-    static X509Certificate generateSelfSignedX509Certificate(final KeyPair keyPair) {
+    public static X509Certificate generateSelfSignedX509Certificate(final KeyPair keyPair) {
         long now = System.currentTimeMillis();
         Date startDate = new Date(now - TimeUnit.DAYS.toMillis(1));
         X500Name dnName = new X500NameBuilder(BCStyle.INSTANCE)