Skip to content

Commit

Permalink
[code quality] [nacos-sys] [env/file] the constants export, redundant…
Browse files Browse the repository at this point in the history
… symbol remove (alibaba#5907)
  • Loading branch information
brotherlu-xcq committed Jun 1, 2021
1 parent 11b49cc commit 9ab1d62
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 19 deletions.
57 changes: 41 additions & 16 deletions sys/src/main/java/com/alibaba/nacos/sys/env/EnvUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,34 @@ public class EnvUtil {

private static String contextPath = null;

private static final String FILE_PREFIX = "file:";

private static final String SERVER_PORT_PROPERTY = "server.port";

private static final int DEFAULT_SERVER_PORT = 8848;

private static final String DEFAULT_WEB_CONTEXT_PATH = "/nacos";

private static final String MEMBER_LIST_PROPERTY = "nacos.member.list";

private static final String NACOS_HOME_PROPERTY = "user.home";

private static final String CUSTOM_CONFIG_LOCATION_PROPERTY = "spring.config.additional-location";

private static final String DEFAULT_CONFIG_LOCATION = "application.properties";

private static final String DEFAULT_RESOURCE_PATH = "/application.properties";

private static final String DEFAULT_ADDITIONAL_PATH = "conf";

private static final String DEFAULT_ADDITIONAL_FILE = "cluster.conf";

private static final String NACOS_HOME_ADDITIONAL_FILEPATH = "nacos";

private static final String NACOS_TEMP_DIR_1 = "data";

private static final String NACOS_TEMP_DIR_2 = "tmp";

@JustForTest
private static String confPath = "";

Expand Down Expand Up @@ -156,7 +184,7 @@ public static void setLocalAddress(String localAddress) {

public static int getPort() {
if (port == -1) {
port = getProperty("server.port", Integer.class, 8848);
port = getProperty(SERVER_PORT_PROPERTY, Integer.class, DEFAULT_SERVER_PORT);
}
return port;
}
Expand All @@ -167,7 +195,7 @@ public static void setPort(int port) {

public static String getContextPath() {
if (Objects.isNull(contextPath)) {
contextPath = getProperty(Constants.WEB_CONTEXT_PATH, "/nacos");
contextPath = getProperty(Constants.WEB_CONTEXT_PATH, DEFAULT_WEB_CONTEXT_PATH);
if (Constants.ROOT_WEB_CONTEXT_PATH.equals(contextPath)) {
contextPath = StringUtils.EMPTY;
}
Expand Down Expand Up @@ -208,7 +236,7 @@ public static String getFunctionMode() {

public static String getNacosTmpDir() {
if (StringUtils.isBlank(nacosTmpDir)) {
nacosTmpDir = Paths.get(getNacosHome(), "data", "tmp").toString();
nacosTmpDir = Paths.get(getNacosHome(), NACOS_TEMP_DIR_1, NACOS_TEMP_DIR_2).toString();
}
return nacosTmpDir;
}
Expand All @@ -217,7 +245,7 @@ public static String getNacosHome() {
if (StringUtils.isBlank(nacosHomePath)) {
String nacosHome = System.getProperty(NACOS_HOME_KEY);
if (StringUtils.isBlank(nacosHome)) {
nacosHome = Paths.get(System.getProperty("user.home"), "nacos").toString();
nacosHome = Paths.get(System.getProperty(NACOS_HOME_PROPERTY), NACOS_HOME_ADDITIONAL_FILEPATH).toString();
}
return nacosHome;
}
Expand Down Expand Up @@ -262,7 +290,7 @@ public static String getConfPath() {
if (StringUtils.isNotBlank(EnvUtil.confPath)) {
return EnvUtil.confPath;
}
EnvUtil.confPath = Paths.get(getNacosHome(), "conf").toString();
EnvUtil.confPath = Paths.get(getNacosHome(), DEFAULT_ADDITIONAL_PATH).toString();
return confPath;
}

Expand All @@ -271,7 +299,7 @@ public static void setConfPath(final String confPath) {
}

public static String getClusterConfFilePath() {
return Paths.get(getNacosHome(), "conf", "cluster.conf").toString();
return Paths.get(getNacosHome(), DEFAULT_ADDITIONAL_PATH, DEFAULT_ADDITIONAL_FILE).toString();
}

/**
Expand Down Expand Up @@ -336,14 +364,14 @@ public static void writeClusterConf(String content) throws IOException {
}

public static String getMemberList() {
String val = null;
String val;
if (environment == null) {
val = System.getenv("nacos.member.list");
val = System.getenv(MEMBER_LIST_PROPERTY);
if (StringUtils.isBlank(val)) {
val = System.getProperty("nacos.member.list");
val = System.getProperty(MEMBER_LIST_PROPERTY);
}
} else {
val = getProperty("nacos.member.list");
val = getProperty(MEMBER_LIST_PROPERTY);
}
return val;
}
Expand All @@ -359,20 +387,17 @@ public static String getMemberList() {
return new OriginTrackedPropertiesLoader(resource).load();
}

private static final String FILE_PREFIX = "file:";

public static Resource getApplicationConfFileResource() {
Resource customResource = getCustomFileResource();
return customResource == null ? getDefaultResource() : customResource;
}

private static Resource getCustomFileResource() {
String path = getProperty("spring.config.additional-location");
InputStream inputStream = null;
String path = getProperty(CUSTOM_CONFIG_LOCATION_PROPERTY);
if (StringUtils.isNotBlank(path) && path.contains(FILE_PREFIX)) {
String[] paths = path.split(",", -1);
path = paths[paths.length - 1].substring(FILE_PREFIX.length());
return getRelativePathResource(path, "application.properties");
return getRelativePathResource(path, DEFAULT_CONFIG_LOCATION);
}
return null;
}
Expand All @@ -387,7 +412,7 @@ private static Resource getRelativePathResource(String parentPath, String path)
}

private static Resource getDefaultResource() {
InputStream inputStream = EnvUtil.class.getResourceAsStream("/application.properties");
InputStream inputStream = EnvUtil.class.getResourceAsStream(DEFAULT_RESOURCE_PATH);
return new InputStreamResource(inputStream);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public static void shutdown() {
try {
entry.getValue().shutdown();
} catch (Throwable e) {
LOGGER.error("[WatchFileCenter] shutdown has error : {}", e);
LOGGER.error("[WatchFileCenter] shutdown has error : ", e);
}
}
MANAGER.clear();
Expand Down Expand Up @@ -230,7 +230,7 @@ public void run() {
} catch (InterruptedException ignore) {
Thread.interrupted();
} catch (Throwable ex) {
LOGGER.error("An exception occurred during file listening : {}", ex);
LOGGER.error("An exception occurred during file listening : ", ex);
}
}
}
Expand All @@ -251,7 +251,7 @@ public void run() {
try {
job.run();
} catch (Throwable ex) {
LOGGER.error("File change event callback error : {}", ex);
LOGGER.error("File change event callback error : ", ex);
}
} else {
executor.execute(job);
Expand Down

0 comments on commit 9ab1d62

Please sign in to comment.