Skip to content

Commit

Permalink
Merge pull request #1561 from hanbingleixue/codecheck
Browse files Browse the repository at this point in the history
Fix the issues detected by Codecheck
  • Loading branch information
Sherlockhan authored Jun 28, 2024
2 parents 271e03c + 1344a11 commit 6b79f67
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (C) 2024-2024 Sermant Authors. All rights reserved.
*
* Licensed 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 io.sermant.core.exception;

/**
* Sermant Runtime Exception
*
* @author zhp
* @since 2024-06-27
*/
public class SermantRuntimeException extends RuntimeException {
/**
* Constructor
*
* @param message message
*/
public SermantRuntimeException(String message) {
super(message);
}

/**
* Constructor
*
* @param cause The cause of the exception
*/
public SermantRuntimeException(Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.sermant.core.utils;

import io.sermant.core.exception.SermantRuntimeException;
import io.sermant.core.service.httpserver.annotation.HttpRouteMapping;

import org.kohsuke.MetaInfServices;
Expand Down Expand Up @@ -118,7 +119,7 @@ private void writeToMetaInf(String fileName, Set<String> services) {
pw.println(service);
}
} catch (IOException e) {
throw new RuntimeException("Failed to write generated files: " + e);
throw new SermantRuntimeException("Failed to write generated files: " + e);
} finally {
if (pw != null) {
pw.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package io.sermant.implement.config;

import io.sermant.core.exception.SermantRuntimeException;

import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.constructor.Constructor;

Expand Down Expand Up @@ -52,11 +54,11 @@ protected Class<?> getClassForName(String name) throws ClassNotFoundException {
* set classLoader
*
* @param classLoader the classLoader for config object
* @throws NullPointerException no classloader is provided
* @throws SermantRuntimeException no classloader is provided
*/
public void setLoader(ClassLoader classLoader) {
if (classLoader == null) {
throw new NullPointerException("classLoader must be provided.");
throw new SermantRuntimeException("classLoader must be provided.");
}
this.loader = classLoader;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import org.apache.http.HttpStatus;

import java.util.Iterator;
import java.util.List;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
*/
public class NacosClient implements ConfigClient {
private static final Logger LOGGER = LoggerFactory.getLogger(NacosClient.class.getName());

/**
* HTTP protocol
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public class NacosDynamicConfigService extends DynamicConfigService {
private final ServiceMeta serviceMeta;

private final List<NacosListener> listeners;

/**
* The thread pool for updating listeners periodically
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

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


import io.sermant.core.common.CommonConstant;
import io.sermant.core.common.LoggerFactory;
import io.sermant.core.config.ConfigManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class ZooKeeperClient implements ConfigClient {
*/
public static final char ZK_PATH_SEPARATOR = '/';
private static final Logger LOGGER = LoggerFactory.getLogger(ZooKeeperClient.class.getName());

/**
* ZK client
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import io.sermant.core.common.LoggerFactory;
import io.sermant.core.config.ConfigManager;
import io.sermant.core.exception.SermantRuntimeException;
import io.sermant.core.service.BaseService;
import io.sermant.core.service.httpserver.HttpServerService;
import io.sermant.core.service.httpserver.config.HttpServerConfig;
Expand Down Expand Up @@ -56,7 +57,7 @@ public void start() {
this.httpServerProvider.start();
} catch (Exception e) {
LOGGER.warning("HttpServerService start failed, " + e.getMessage());
throw new RuntimeException(e);
throw new SermantRuntimeException(e);
}
LOGGER.info("HttpServerService started.");
}
Expand All @@ -70,7 +71,7 @@ public void stop() {
this.httpServerProvider.stop();
} catch (Exception e) {
LOGGER.warning("HttpServerService stop failed, " + e.getMessage());
throw new RuntimeException(e);
throw new SermantRuntimeException(e);
}
LOGGER.info("HttpServerService stopped.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.alibaba.fastjson.JSONObject;
import com.sun.net.httpserver.HttpExchange;

import io.sermant.core.exception.SermantRuntimeException;
import io.sermant.core.service.httpserver.api.HttpRequest;
import io.sermant.core.service.httpserver.exception.HttpServerException;
import io.sermant.core.utils.CollectionUtils;
Expand Down Expand Up @@ -53,13 +54,10 @@ public class SimpleHttpRequest implements HttpRequest {
private static final int BODY_BYTE_SIZE = 512;

private final HttpExchange exchange;

private final Map<String, String> params = new HashMap<>();
private String originalPath;

private String path;

private final Map<String, String> params = new HashMap<>();

/**
* Create a SimpleHttpRequest object.
*
Expand Down Expand Up @@ -160,7 +158,7 @@ public Map<String, String> getParams() {
}
}
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
throw new SermantRuntimeException(e);
}
return params;
}
Expand Down Expand Up @@ -202,17 +200,18 @@ public <T> List<T> getBodyAsList(Class<T> clazz) throws HttpServerException {

@Override
public byte[] getBodyAsBytes() throws HttpServerException {
try (InputStream ins = getBodyAsStream()) {
try (InputStream ins = getBodyAsStream();) {
if (ins == null) {
return new byte[0];
}
ByteArrayOutputStream outs = new ByteArrayOutputStream();
int len;
byte[] buf = new byte[BODY_BYTE_SIZE];
while ((len = ins.read(buf)) != -1) {
outs.write(buf, 0, len);
try (ByteArrayOutputStream outs = new ByteArrayOutputStream()) {
int len;
byte[] buf = new byte[BODY_BYTE_SIZE];
while ((len = ins.read(buf)) != -1) {
outs.write(buf, 0, len);
}
return outs.toByteArray();
}
return outs.toByteArray();
} catch (Exception e) {
throw new HttpServerException(HttpCodeEnum.SERVER_ERROR.getCode(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ public class XdsServiceDiscoveryImpl implements XdsServiceDiscovery {

private static final ReentrantLock LOCK = new ReentrantLock();

private final XdsClient client;

private EdsHandler edsHandler;

/**
Expand All @@ -56,7 +54,6 @@ public class XdsServiceDiscoveryImpl implements XdsServiceDiscovery {
* @param client xds client
*/
public XdsServiceDiscoveryImpl(XdsClient client) {
this.client = client;
CdsHandler cdsHandler = new CdsHandler(client);
edsHandler = new EdsHandler(client);
cdsHandler.subscribe(XdsConstant.CDS_ALL_RESOURCE, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -138,7 +139,7 @@ public static Map<String, String> resolveGroupLabels(String group) {
curGroup = LabelGroupUtils.createLabelGroup(Collections.singletonMap(DEFAULT_GROUP_KEY, curGroup));
}
try {
final String decode = URLDecoder.decode(curGroup, "UTF-8");
final String decode = URLDecoder.decode(curGroup, StandardCharsets.UTF_8.name());
final String[] labels = decode.split("&");
for (String label : labels) {
final String[] labelKv = label.split("=");
Expand Down Expand Up @@ -180,7 +181,7 @@ public static String getLabelCondition(String group) {

private static String buildSingleLabel(String key, String value) {
try {
return URLEncoder.encode(key + LABEL_QUERY_SEPARATOR + value, "UTF-8");
return URLEncoder.encode(key + LABEL_QUERY_SEPARATOR + value, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
LOGGER.warn("UnsupportedEncodingException, msg is {0}.", e.getMessage());
return DynamicConstants.EMPTY_STRING;
Expand Down
2 changes: 1 addition & 1 deletion sermant-integration-tests/scripts/AgentLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* @since 2023-09-26
*/
public class AgentLoader {
private static Logger logger = Logger.getLogger("io.sermant.script.AgentLoader");
private static final Logger logger = Logger.getLogger("io.sermant.script.AgentLoader");

private AgentLoader() {
}
Expand Down

0 comments on commit 6b79f67

Please sign in to comment.