From dca3e351ed28888522bec83104ff1356b8797cda Mon Sep 17 00:00:00 2001 From: po-168 Date: Fri, 8 Nov 2024 19:16:19 +0800 Subject: [PATCH] [improve] code according to code specifications (#2809) Co-authored-by: shown Co-authored-by: aias00 --- .../hertzbeat/alert/service/impl/AlertServiceImpl.java | 3 +-- .../collect/ipmi2/protocol/common/IpmiCode.java | 6 ++++-- .../ipmi2/protocol/common/IpmiEncapsulation.java | 6 ++++-- .../protocol/ipmi/AbstractIpmiSessionWrapper.java | 3 ++- .../protocol/ipmi/command/AbstractIpmiCommand.java | 1 + .../ipmi/security/confidentiality/AesCbc128.java | 10 +++++++--- .../collect/ipmi2/protocol/rmcp/RmcpPacket.java | 3 ++- .../collector/collect/kafka/KafkaConnect.java | 6 +++--- 8 files changed, 24 insertions(+), 14 deletions(-) diff --git a/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertServiceImpl.java b/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertServiceImpl.java index b411ecdd810..646f669e13b 100644 --- a/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertServiceImpl.java +++ b/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertServiceImpl.java @@ -25,7 +25,6 @@ import java.time.LocalDateTime; import java.time.ZoneId; import java.util.ArrayList; -import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -190,7 +189,7 @@ public void addNewAlertReportFromCloud(String cloudServiceName, String alertRepo alert = AlertReport.builder() .content("error do not has cloud service api") .alertName("/api/alerts/report/" + cloudServiceName) - .alertTime(new Date().getTime()) + .alertTime(Instant.now().getEpochSecond()) .priority(1) .reportType(1) .build(); diff --git a/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/ipmi2/protocol/common/IpmiCode.java b/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/ipmi2/protocol/common/IpmiCode.java index 1f6625f49a4..042c6c1187f 100644 --- a/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/ipmi2/protocol/common/IpmiCode.java +++ b/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/ipmi2/protocol/common/IpmiCode.java @@ -38,9 +38,11 @@ default String getDescription() { } public static & IpmiCode.Code> T fromByte(Class type, byte code) { - for (T value : type.getEnumConstants()) - if (value.getCode() == code) + for (T value : type.getEnumConstants()) { + if (value.getCode() == code) { return value; + } + } throw new IllegalArgumentException("Unknown " + type.getSimpleName() + " code 0x" + UnsignedBytes.toString(code, 16)); } diff --git a/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/ipmi2/protocol/common/IpmiEncapsulation.java b/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/ipmi2/protocol/common/IpmiEncapsulation.java index 4eb74842e50..6c0835f2464 100644 --- a/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/ipmi2/protocol/common/IpmiEncapsulation.java +++ b/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/ipmi2/protocol/common/IpmiEncapsulation.java @@ -30,10 +30,12 @@ public interface Encapsulation { } public static T getEncapsulated(Class type, Object value) { - if (type.isInstance(value)) + if (type.isInstance(value)) { return type.cast(value); - if (value instanceof Encapsulation) + } + if (value instanceof Encapsulation) { return ((Encapsulation) value).getEncapsulated(type); + } return null; } diff --git a/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/ipmi2/protocol/ipmi/AbstractIpmiSessionWrapper.java b/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/ipmi2/protocol/ipmi/AbstractIpmiSessionWrapper.java index 9bf2ed9ae08..e9f106d852c 100644 --- a/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/ipmi2/protocol/ipmi/AbstractIpmiSessionWrapper.java +++ b/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/ipmi2/protocol/ipmi/AbstractIpmiSessionWrapper.java @@ -71,8 +71,9 @@ public void setIpmiPayload(IpmiPayload ipmiPayload) { @Override public T getEncapsulated(Class type) { - if (type.isInstance(this)) + if (type.isInstance(this)) { return type.cast(this); + } return IpmiEncapsulation.getEncapsulated(type, getIpmiPayload()); } diff --git a/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/ipmi2/protocol/ipmi/command/AbstractIpmiCommand.java b/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/ipmi2/protocol/ipmi/command/AbstractIpmiCommand.java index e56e6d98c67..7c59be27cc0 100644 --- a/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/ipmi2/protocol/ipmi/command/AbstractIpmiCommand.java +++ b/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/ipmi2/protocol/ipmi/command/AbstractIpmiCommand.java @@ -61,6 +61,7 @@ public IpmiLun getRqLun() { return rqLun; } + @Override public void setRqLun(IpmiLun rqLun) { this.rqLun = rqLun; } diff --git a/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/ipmi2/protocol/ipmi/security/confidentiality/AesCbc128.java b/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/ipmi2/protocol/ipmi/security/confidentiality/AesCbc128.java index da4bc25849b..3bb421a798c 100644 --- a/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/ipmi2/protocol/ipmi/security/confidentiality/AesCbc128.java +++ b/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/ipmi2/protocol/ipmi/security/confidentiality/AesCbc128.java @@ -75,9 +75,11 @@ public ByteBuffer decrypt(IpmiSession session, ByteBuffer in) throws InvalidAlgo this.init(Cipher.DECRYPT_MODE, secretKey, iv); this.update(in, buffer); int padLength = ByteConvertUtils.byteToInt(buffer.get(buffer.position() - 1)); - for (int i = 1; i <= padLength; i++) - if (ByteConvertUtils.byteToInt(buffer.get(buffer.position() - i - 1)) != padLength - i + 1) + for (int i = 1; i <= padLength; i++) { + if (ByteConvertUtils.byteToInt(buffer.get(buffer.position() - i - 1)) != padLength - i + 1) { throw new IllegalArgumentException("Bad pad byte " + i); + } + } buffer.limit(buffer.position() - padLength - 1); buffer.flip(); return buffer; @@ -86,8 +88,9 @@ public ByteBuffer decrypt(IpmiSession session, ByteBuffer in) throws InvalidAlgo @Override public int pad(int length) { int t = length % 16; - if (t == 0) + if (t == 0) { return 0; + } return 16 - t; } @@ -96,6 +99,7 @@ public int getEncryptedLength(int length) { return 16 + length + pad(length); } + @Override public void update(ByteBuffer input, ByteBuffer output) throws ShortBufferException { super.update(input, output); } diff --git a/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/ipmi2/protocol/rmcp/RmcpPacket.java b/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/ipmi2/protocol/rmcp/RmcpPacket.java index f879979a5d7..cffb2b6a21e 100644 --- a/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/ipmi2/protocol/rmcp/RmcpPacket.java +++ b/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/ipmi2/protocol/rmcp/RmcpPacket.java @@ -78,8 +78,9 @@ public Packet withData(RmcpData data) { @Override public T getEncapsulated(Class type) { - if (type.isInstance(this)) + if (type.isInstance(this)) { return type.cast(this); + } return IpmiEncapsulation.getEncapsulated(type, getData()); } diff --git a/hertzbeat-collector/hertzbeat-collector-kafka/src/main/java/org/apache/hertzbeat/collector/collect/kafka/KafkaConnect.java b/hertzbeat-collector/hertzbeat-collector-kafka/src/main/java/org/apache/hertzbeat/collector/collect/kafka/KafkaConnect.java index 2d0bbb11bb6..a898c6824f6 100644 --- a/hertzbeat-collector/hertzbeat-collector-kafka/src/main/java/org/apache/hertzbeat/collector/collect/kafka/KafkaConnect.java +++ b/hertzbeat-collector/hertzbeat-collector-kafka/src/main/java/org/apache/hertzbeat/collector/collect/kafka/KafkaConnect.java @@ -46,9 +46,9 @@ public AdminClient getConnection() { } @Override - public void closeConnection() throws Exception { - if (this.adminClient != null) { - this.adminClient.close(); + public void closeConnection() { + if (adminClient != null) { + adminClient.close(); } }