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; } private static boolean updateConfig(String name, Map value) { Connection conn = null; PreparedStatement pstmt = null; try { conn = DataSourceUtils.getConnection(); String sql = "update global_configuration set value=?::json where name=?"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, CommonUtils.getJsonFromObject(value)); pstmt.setString(2, name); pstmt.executeUpdate(); } catch (Exception e) { logger.error("execute sql exception:", e); return false; } finally { DataSourceUtils.closeResource(pstmt, conn); } return true; } private static boolean addConfig(String name, Map value) { Connection conn = null; PreparedStatement pstmt = null; try { conn = DataSourceUtils.getConnection(); String sql = "INSERT INTO global_configuration(name, value) VALUES (?, ?::json)"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, name); pstmt.setString(2, CommonUtils.getJsonFromObject(value)); pstmt.executeUpdate(); } catch (Exception e) { logger.error("execute sql exception:", e); return false; } finally { DataSourceUtils.closeResource(pstmt, conn); } return true; } public static ObjectMapper getObjectMapper() { return objectMapper; } public static T convertJsonToObject(String content, Class valueType) { if (content == null) { return null; } try { return objectMapper.readValue(content.getBytes(StandardCharsets.UTF_8), valueType); } catch (IOException e) { logger.error("convert json to Object exception:", e); } return null; } public static List convertJsonToList(String content, Class valueType) { if (content == null || content.length() == 0) { return new ArrayList<>(); } try { List list = objectMapper.readValue(content.getBytes(StandardCharsets.UTF_8), new TypeReference>() { }); List ret = new ArrayList<>(); for (T t : list) { ret.add(objectMapper.convertValue(t, valueType)); } return ret; } catch (IOException e) { logger.error("convert json to Object exception:", e); } return null; } public static String getJsonFromObject(Object object) { String json = null; try { json = CommonUtils.getObjectMapper().writeValueAsString(object); } catch (JsonProcessingException ignored) { } return json; } public static String getJsonValue(String jsonString, String key) { Map jsonMap; try { jsonMap = objectMapper.readValue(jsonString, new TypeReference>() { }); } catch (IOException ignored) { return null; } return jsonMap.get(key); } public static String getJsonString(Map map) { String result = ""; try { result = objectMapper.writeValueAsString(map); } catch (JsonProcessingException ignored) { } return result; } public static Map getJsonMap(String jsonString) { Map jsonMap; try { jsonMap = objectMapper.readValue(jsonString, new TypeReference>() { }); } catch (IOException ignored) { jsonMap = new HashMap<>(); } return jsonMap; } public static Map getMapFromJson(String jsonString) { try { return objectMapper.readValue(jsonString, new TypeReference>() { }); } catch (IOException ignored) { } return new HashMap<>(); } public static String getPropertyValue(String propertyName) { Properties properties = loadPropertyFile("sdwan.common.cfg"); if (properties != null) { return properties.getProperty(propertyName); } return null; } 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 List 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()); // } }