代码重构
parent
f50b651ce2
commit
cca355204b
@ -1,17 +1,20 @@
|
|||||||
package com.xydl.controller;
|
package com.xydl.controller;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
|
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
|
@Slf4j
|
||||||
public class TestController {
|
public class TestController {
|
||||||
|
// private static final Logger logger = LoggerFactory.getLogger(TestController.class);
|
||||||
|
|
||||||
@RequestMapping("/eia")
|
@RequestMapping("/test")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public String test() {
|
public String test() {
|
||||||
System.out.println("===");
|
log.info("===");
|
||||||
return "测试成功";
|
return "测试成功";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,145 +0,0 @@
|
|||||||
package com.xydl.util;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.sql.Array;
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.PreparedStatement;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.text.SimpleDateFormat;
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
public class CommonUtils {
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(CommonUtils.class);
|
|
||||||
|
|
||||||
private static final boolean languageZh = getPropertyValue("system.language", "zh").equals("zh");
|
|
||||||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private static final long startupTime = System.currentTimeMillis();
|
|
||||||
private static final int startingTime = Integer.parseInt(getPropertyValue("system.startup.time", "60000"));
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static long getStartupTime() {
|
|
||||||
return startupTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean started() {
|
|
||||||
return System.currentTimeMillis() - startupTime > startingTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean isLanguageZh() {
|
|
||||||
return languageZh;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static ObjectMapper getObjectMapper() {
|
|
||||||
return objectMapper;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static String getPropertyValue(String propertyName, String defaultValue) {
|
|
||||||
Properties properties = loadPropertyFile("sdwan.common.cfg");
|
|
||||||
if (properties != null) {
|
|
||||||
String value = properties.getProperty(propertyName);
|
|
||||||
return value == null ? defaultValue : value;
|
|
||||||
}
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Properties loadPropertyFile(String fileName) {
|
|
||||||
String filePath = System.getProperty("user.dir").concat("/etc/").concat(fileName);
|
|
||||||
File file = new File(filePath);
|
|
||||||
if (!file.exists()) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
try (InputStream is = new FileInputStream(file)) {
|
|
||||||
Properties properties = new Properties();
|
|
||||||
properties.load(is);
|
|
||||||
return properties;
|
|
||||||
} catch (IOException e) {
|
|
||||||
logger.error("load property file exception:", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean setProperty(String fileName, String key, String value) {
|
|
||||||
Properties properties = loadPropertyFile(fileName);
|
|
||||||
if (properties == null) {
|
|
||||||
properties = new Properties();
|
|
||||||
}
|
|
||||||
properties.setProperty(key, value);
|
|
||||||
return savePropertyFile(fileName, properties);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean savePropertyFile(String fileName, Properties properties) {
|
|
||||||
String filePath = System.getProperty("user.dir").concat("/etc/").concat(fileName);
|
|
||||||
File file = new File(filePath);
|
|
||||||
if (!file.exists()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
try (OutputStream os = new FileOutputStream(file)) {
|
|
||||||
properties.store(os, null);
|
|
||||||
} catch (IOException e) {
|
|
||||||
logger.error("write file exception:", e);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public static <T> List<T> getListFromArray(Array array) {
|
|
||||||
if (array == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
return Arrays.asList((T[]) array.getArray());
|
|
||||||
} catch (SQLException ignored) {
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static String formatDate(Date date) {
|
|
||||||
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// public static void main(String args[]) {
|
|
||||||
// System.out.println(CommonUtils.getLanguage());
|
|
||||||
// }
|
|
||||||
}
|
|
Loading…
Reference in New Issue