All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
top.wboost.common.system.code.CodeMessageManager Maven / Gradle / Ivy
package top.wboost.common.system.code;
import java.util.List;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import top.wboost.common.base.core.ExecutorsDaemon;
import top.wboost.common.log.entity.Logger;
import top.wboost.common.log.util.LoggerUtil;
import top.wboost.common.util.StringUtil;
import top.wboost.common.utils.web.utils.PropertiesUtil;
/**
* 返回码对应信息管理器
* @className CodeMessageManager
* @author jwSun
* @date 2017年8月1日 下午4:35:52
* @version 1.0.0
*/
public class CodeMessageManager implements ApplicationListener {
private static Logger log = LoggerUtil.getLogger(CodeMessageManager.class);
private final static String systemMessagePath = "classpath*:sys/properties/system-message-code_zh_CN.properties";
private final String messagePath;
private final String fileName;
private final String language;
private String realMessagePath;
//不需要加载说明
public static final int NO_MESSAGE_CODE = 0;
//默认重新读取配置文件时间(分钟)
private static final long defaultLoadTime = 30;
/**更新配置文件信息定时器**/
private ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1,
ExecutorsDaemon.getDaemonThreadFactory("codeMessageManagerPool"));
/**key:code,value:message**/
private static ConcurrentHashMap codeMessageMap = new ConcurrentHashMap();
static {
log.debug("init system code and message...");
for (SystemCode businessCode : SystemCode.values()) {
String message = PropertiesUtil.getProperty(String.valueOf(businessCode.getCode()), systemMessagePath);
codeMessageMap.put(businessCode.getCode(), message == null ? "" : message);
}
log.debug("init system code and message end");
}
public CodeMessageManager(String messagePath, String fileName) {
this(messagePath, fileName, "zh_CN", defaultLoadTime);
}
public CodeMessageManager(String messagePath, String fileName, String language) {
this(messagePath, fileName, language, defaultLoadTime);
}
public CodeMessageManager(String messagePath, String fileName, String language, long loadTime) {
super();
this.messagePath = messagePath;
this.fileName = fileName;
this.language = language;
this.realMessagePath = messagePath + "/" + fileName + "_" + language + ".properties";
//Executors.defaultThreadFactory();
executorService.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
loadMessage();
}
}, 0, loadTime, TimeUnit.MINUTES);
}
/**
* 读取配置文件信息
*/
private void loadMessage() {
log.debug("init code and message...");
Properties messageProperties = PropertiesUtil.loadProperties(this.realMessagePath);
for (Entry businessCode : messageProperties.entrySet()) {
try {
Integer code = Integer.valueOf((String) businessCode.getKey());
String message = (String) businessCode.getValue();
codeMessageMap.put(code, message == null ? "" : message);
} catch (Exception e) {
log.error("key : {},value: {} ,error is : {}", businessCode.getKey(), businessCode.getValue(),
e.getLocalizedMessage());
}
}
log.debug("init code and message end...");
}
public static void loadMessageByPath(String path) {
log.debug("loadMessageByPath... {}", path);
Properties messageProperties = PropertiesUtil.loadProperties(path);
for (Entry businessCode : messageProperties.entrySet()) {
try {
Integer code = Integer.valueOf((String) businessCode.getKey());
String message = (String) businessCode.getValue();
codeMessageMap.put(code, message == null ? "" : message);
} catch (Exception e) {
log.error("key : {},value: {} ,error is : {}", businessCode.getKey(), businessCode.getValue(),
e.getLocalizedMessage());
}
}
log.debug("loadMessageByPath end...");
}
public static String getMessageByCodeOrDefault(int code, String message, Object... params) {
if (NO_MESSAGE_CODE == code) {
return message;
} else {
return getMessageByCode(code, params);
}
}
public static String getMessageByCode(int code, Object... params) {
return replaceMessage(codeMessageMap.get(code), params);
}
/**
* 根据返回码获得详细提示信息
* @param code 代码
* @param message 提示信息
* @param params 提示信息,可在配置文件中以{index}保存
* @return
*/
public static String getDetailMessageByCodeOrDefault(int code, String message, Object... params) {
String firstMessage = replaceMessage(message, params);
if (NO_MESSAGE_CODE == code) {
return firstMessage;
} else {
return getDetailMessageByCode(code, params) + ",at first message is " + firstMessage;
}
}
public static String getDetailMessageByCode(int code, Object... params) {
return "code : " + code + ",code message is : " + getMessageByCode(code, params);
}
// 匹配{}
private static final String PATTERN_COMPILE = "\\{(.*?)\\}";
private static String replaceMessage(String message, Object... params) {
if (message == null)
return message;
try {
Object[] replaceParams = params;
List replaceList = StringUtil.getPatternMattcherList(message, PATTERN_COMPILE, 1);
for (String replaceIndex : replaceList) {
int index = Integer.parseInt(replaceIndex);
if (replaceParams != null && index < replaceParams.length && index >= 0) {
message = message.replaceAll("\\{" + index + "\\}", replaceParams[index].toString());
} else {
message = message.replaceAll("\\{" + index + "\\}", "");
}
}
} catch (Exception e) {
log.error(" replace message error ,{}", e.getLocalizedMessage());
}
return message.trim();
}
public String getSystemMessagePath() {
return systemMessagePath;
}
public String getMessagePath() {
return messagePath;
}
public String getFileName() {
return fileName;
}
public String getLanguage() {
return language;
}
public String getRealMessagePath() {
return realMessagePath;
}
@Override
public void onApplicationEvent(ContextClosedEvent event) {
if (!executorService.isShutdown()) {
executorService.shutdown();
}
}
/*public static String getMessageByBusinessCode(BusinessCode businessCode) {
return codeMessageMap.get(businessCode.getCode());
}*/
}