Skip to content

Commit 6b79f67

Browse files
authored
Merge pull request #1561 from hanbingleixue/codecheck
Fix the issues detected by Codecheck
2 parents 271e03c + 1344a11 commit 6b79f67

File tree

13 files changed

+70
-25
lines changed

13 files changed

+70
-25
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (C) 2024-2024 Sermant Authors. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.sermant.core.exception;
18+
19+
/**
20+
* Sermant Runtime Exception
21+
*
22+
* @author zhp
23+
* @since 2024-06-27
24+
*/
25+
public class SermantRuntimeException extends RuntimeException {
26+
/**
27+
* Constructor
28+
*
29+
* @param message message
30+
*/
31+
public SermantRuntimeException(String message) {
32+
super(message);
33+
}
34+
35+
/**
36+
* Constructor
37+
*
38+
* @param cause The cause of the exception
39+
*/
40+
public SermantRuntimeException(Throwable cause) {
41+
super(cause);
42+
}
43+
}

sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/utils/ServicesMetaInfUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package io.sermant.core.utils;
1818

19+
import io.sermant.core.exception.SermantRuntimeException;
1920
import io.sermant.core.service.httpserver.annotation.HttpRouteMapping;
2021

2122
import org.kohsuke.MetaInfServices;
@@ -118,7 +119,7 @@ private void writeToMetaInf(String fileName, Set<String> services) {
118119
pw.println(service);
119120
}
120121
} catch (IOException e) {
121-
throw new RuntimeException("Failed to write generated files: " + e);
122+
throw new SermantRuntimeException("Failed to write generated files: " + e);
122123
} finally {
123124
if (pw != null) {
124125
pw.close();

sermant-agentcore/sermant-agentcore-implement/src/main/java/io/sermant/implement/config/SermantYamlConstructor.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package io.sermant.implement.config;
1818

19+
import io.sermant.core.exception.SermantRuntimeException;
20+
1921
import org.yaml.snakeyaml.LoaderOptions;
2022
import org.yaml.snakeyaml.constructor.Constructor;
2123

@@ -52,11 +54,11 @@ protected Class<?> getClassForName(String name) throws ClassNotFoundException {
5254
* set classLoader
5355
*
5456
* @param classLoader the classLoader for config object
55-
* @throws NullPointerException no classloader is provided
57+
* @throws SermantRuntimeException no classloader is provided
5658
*/
5759
public void setLoader(ClassLoader classLoader) {
5860
if (classLoader == null) {
59-
throw new NullPointerException("classLoader must be provided.");
61+
throw new SermantRuntimeException("classLoader must be provided.");
6062
}
6163
this.loader = classLoader;
6264
}

sermant-agentcore/sermant-agentcore-implement/src/main/java/io/sermant/implement/service/dynamicconfig/kie/client/kie/ResultHandler.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
import org.apache.http.HttpStatus;
2424

25-
import java.util.Iterator;
2625
import java.util.List;
2726

2827
/**

sermant-agentcore/sermant-agentcore-implement/src/main/java/io/sermant/implement/service/dynamicconfig/nacos/NacosClient.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
*/
5757
public class NacosClient implements ConfigClient {
5858
private static final Logger LOGGER = LoggerFactory.getLogger(NacosClient.class.getName());
59+
5960
/**
6061
* HTTP protocol
6162
*/

sermant-agentcore/sermant-agentcore-implement/src/main/java/io/sermant/implement/service/dynamicconfig/nacos/NacosDynamicConfigService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public class NacosDynamicConfigService extends DynamicConfigService {
7272
private final ServiceMeta serviceMeta;
7373

7474
private final List<NacosListener> listeners;
75+
7576
/**
7677
* The thread pool for updating listeners periodically
7778
*/

sermant-agentcore/sermant-agentcore-implement/src/main/java/io/sermant/implement/service/dynamicconfig/zookeeper/ZooKeeperBufferedClient.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package io.sermant.implement.service.dynamicconfig.zookeeper;
1818

19-
2019
import io.sermant.core.common.CommonConstant;
2120
import io.sermant.core.common.LoggerFactory;
2221
import io.sermant.core.config.ConfigManager;

sermant-agentcore/sermant-agentcore-implement/src/main/java/io/sermant/implement/service/dynamicconfig/zookeeper/ZooKeeperClient.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public class ZooKeeperClient implements ConfigClient {
4949
*/
5050
public static final char ZK_PATH_SEPARATOR = '/';
5151
private static final Logger LOGGER = LoggerFactory.getLogger(ZooKeeperClient.class.getName());
52+
5253
/**
5354
* ZK client
5455
*/

sermant-agentcore/sermant-agentcore-implement/src/main/java/io/sermant/implement/service/httpserver/HttpServerServiceImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import io.sermant.core.common.LoggerFactory;
2020
import io.sermant.core.config.ConfigManager;
21+
import io.sermant.core.exception.SermantRuntimeException;
2122
import io.sermant.core.service.BaseService;
2223
import io.sermant.core.service.httpserver.HttpServerService;
2324
import io.sermant.core.service.httpserver.config.HttpServerConfig;
@@ -56,7 +57,7 @@ public void start() {
5657
this.httpServerProvider.start();
5758
} catch (Exception e) {
5859
LOGGER.warning("HttpServerService start failed, " + e.getMessage());
59-
throw new RuntimeException(e);
60+
throw new SermantRuntimeException(e);
6061
}
6162
LOGGER.info("HttpServerService started.");
6263
}
@@ -70,7 +71,7 @@ public void stop() {
7071
this.httpServerProvider.stop();
7172
} catch (Exception e) {
7273
LOGGER.warning("HttpServerService stop failed, " + e.getMessage());
73-
throw new RuntimeException(e);
74+
throw new SermantRuntimeException(e);
7475
}
7576
LOGGER.info("HttpServerService stopped.");
7677
}

sermant-agentcore/sermant-agentcore-implement/src/main/java/io/sermant/implement/service/httpserver/simple/SimpleHttpRequest.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.alibaba.fastjson.JSONObject;
2121
import com.sun.net.httpserver.HttpExchange;
2222

23+
import io.sermant.core.exception.SermantRuntimeException;
2324
import io.sermant.core.service.httpserver.api.HttpRequest;
2425
import io.sermant.core.service.httpserver.exception.HttpServerException;
2526
import io.sermant.core.utils.CollectionUtils;
@@ -53,13 +54,10 @@ public class SimpleHttpRequest implements HttpRequest {
5354
private static final int BODY_BYTE_SIZE = 512;
5455

5556
private final HttpExchange exchange;
56-
57+
private final Map<String, String> params = new HashMap<>();
5758
private String originalPath;
58-
5959
private String path;
6060

61-
private final Map<String, String> params = new HashMap<>();
62-
6361
/**
6462
* Create a SimpleHttpRequest object.
6563
*
@@ -160,7 +158,7 @@ public Map<String, String> getParams() {
160158
}
161159
}
162160
} catch (UnsupportedEncodingException e) {
163-
throw new RuntimeException(e);
161+
throw new SermantRuntimeException(e);
164162
}
165163
return params;
166164
}
@@ -202,17 +200,18 @@ public <T> List<T> getBodyAsList(Class<T> clazz) throws HttpServerException {
202200

203201
@Override
204202
public byte[] getBodyAsBytes() throws HttpServerException {
205-
try (InputStream ins = getBodyAsStream()) {
203+
try (InputStream ins = getBodyAsStream();) {
206204
if (ins == null) {
207205
return new byte[0];
208206
}
209-
ByteArrayOutputStream outs = new ByteArrayOutputStream();
210-
int len;
211-
byte[] buf = new byte[BODY_BYTE_SIZE];
212-
while ((len = ins.read(buf)) != -1) {
213-
outs.write(buf, 0, len);
207+
try (ByteArrayOutputStream outs = new ByteArrayOutputStream()) {
208+
int len;
209+
byte[] buf = new byte[BODY_BYTE_SIZE];
210+
while ((len = ins.read(buf)) != -1) {
211+
outs.write(buf, 0, len);
212+
}
213+
return outs.toByteArray();
214214
}
215-
return outs.toByteArray();
216215
} catch (Exception e) {
217216
throw new HttpServerException(HttpCodeEnum.SERVER_ERROR.getCode(), e);
218217
}

sermant-agentcore/sermant-agentcore-implement/src/main/java/io/sermant/implement/service/xds/discovery/XdsServiceDiscoveryImpl.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ public class XdsServiceDiscoveryImpl implements XdsServiceDiscovery {
4646

4747
private static final ReentrantLock LOCK = new ReentrantLock();
4848

49-
private final XdsClient client;
50-
5149
private EdsHandler edsHandler;
5250

5351
/**
@@ -56,7 +54,6 @@ public class XdsServiceDiscoveryImpl implements XdsServiceDiscovery {
5654
* @param client xds client
5755
*/
5856
public XdsServiceDiscoveryImpl(XdsClient client) {
59-
this.client = client;
6057
CdsHandler cdsHandler = new CdsHandler(client);
6158
edsHandler = new EdsHandler(client);
6259
cdsHandler.subscribe(XdsConstant.CDS_ALL_RESOURCE, null);

sermant-agentcore/sermant-agentcore-implement/src/main/java/io/sermant/implement/utils/LabelGroupUtils.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.io.UnsupportedEncodingException;
2525
import java.net.URLDecoder;
2626
import java.net.URLEncoder;
27+
import java.nio.charset.StandardCharsets;
2728
import java.util.ArrayList;
2829
import java.util.Collections;
2930
import java.util.LinkedHashMap;
@@ -138,7 +139,7 @@ public static Map<String, String> resolveGroupLabels(String group) {
138139
curGroup = LabelGroupUtils.createLabelGroup(Collections.singletonMap(DEFAULT_GROUP_KEY, curGroup));
139140
}
140141
try {
141-
final String decode = URLDecoder.decode(curGroup, "UTF-8");
142+
final String decode = URLDecoder.decode(curGroup, StandardCharsets.UTF_8.name());
142143
final String[] labels = decode.split("&");
143144
for (String label : labels) {
144145
final String[] labelKv = label.split("=");
@@ -180,7 +181,7 @@ public static String getLabelCondition(String group) {
180181

181182
private static String buildSingleLabel(String key, String value) {
182183
try {
183-
return URLEncoder.encode(key + LABEL_QUERY_SEPARATOR + value, "UTF-8");
184+
return URLEncoder.encode(key + LABEL_QUERY_SEPARATOR + value, StandardCharsets.UTF_8.name());
184185
} catch (UnsupportedEncodingException e) {
185186
LOGGER.warn("UnsupportedEncodingException, msg is {0}.", e.getMessage());
186187
return DynamicConstants.EMPTY_STRING;

sermant-integration-tests/scripts/AgentLoader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
* @since 2023-09-26
3333
*/
3434
public class AgentLoader {
35-
private static Logger logger = Logger.getLogger("io.sermant.script.AgentLoader");
35+
private static final Logger logger = Logger.getLogger("io.sermant.script.AgentLoader");
3636

3737
private AgentLoader() {
3838
}

0 commit comments

Comments
 (0)