RetCodes
This commit is contained in:
@@ -43,6 +43,7 @@ import org.redkale.net.http.*;
|
||||
import org.redkale.net.sncp.*;
|
||||
import org.redkale.props.spi.PropertiesModule;
|
||||
import org.redkale.scheduled.spi.ScheduledModuleEngine;
|
||||
import org.redkale.service.RetCodes;
|
||||
import org.redkale.service.Service;
|
||||
import org.redkale.source.*;
|
||||
import org.redkale.source.spi.SourceModuleEngine;
|
||||
@@ -369,6 +370,8 @@ public final class Application {
|
||||
this.onEnvironmentLoaded();
|
||||
// init起始回调
|
||||
this.onAppPreInit();
|
||||
// 加载错误码
|
||||
this.initRetCodes();
|
||||
// 设置WorkExecutor
|
||||
this.initWorkExecutor();
|
||||
// 回调Listener
|
||||
@@ -710,6 +713,22 @@ public final class Application {
|
||||
}
|
||||
}
|
||||
|
||||
/** 加载错误码 */
|
||||
private void initRetCodes() throws IOException {
|
||||
ClassFilter<RetCodes> filter = new ClassFilter(this.getClassLoader(), RetCodes.class);
|
||||
loadClassByFilters(filter);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
filter.getFilterEntrys().forEach(en -> {
|
||||
if (en.getType() != RetCodes.class) {
|
||||
int c = RetCodes.load(en.getType());
|
||||
sb.append("Load RetCodes (type=").append(en.getType().getName() + ") " + c + " records\r\n");
|
||||
}
|
||||
});
|
||||
if (sb.length() > 0) {
|
||||
logger.log(Level.INFO, sb.toString().trim());
|
||||
}
|
||||
}
|
||||
|
||||
/** 设置WorkExecutor */
|
||||
private void initWorkExecutor() {
|
||||
int bufferCapacity = 32 * 1024;
|
||||
|
||||
@@ -63,6 +63,10 @@ public final class ClassFilter<T> {
|
||||
|
||||
private final ClassLoader classLoader;
|
||||
|
||||
public ClassFilter(RedkaleClassLoader classLoader, Class superClass) {
|
||||
this(classLoader, null, superClass, (Class[]) null, null);
|
||||
}
|
||||
|
||||
public ClassFilter(RedkaleClassLoader classLoader, Class<? extends Annotation> annotationClass, Class superClass) {
|
||||
this(classLoader, annotationClass, superClass, (Class[]) null, null);
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import java.text.MessageFormat;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* 错误码加载器
|
||||
@@ -20,41 +20,33 @@ import java.util.concurrent.locks.ReentrantLock;
|
||||
*/
|
||||
public abstract class RetCodes {
|
||||
|
||||
private static final ReentrantLock loadLock = new ReentrantLock();
|
||||
|
||||
private static Map<String, Map<Integer, String>> rets = new LinkedHashMap<>();
|
||||
|
||||
private static Map<Integer, String> defret = new LinkedHashMap<>();
|
||||
|
||||
protected RetCodes() {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
|
||||
public static void load(Class codeClass) {
|
||||
load(RetLabel.RetLoader.loadMap(codeClass));
|
||||
public static int load(Class codeClass) {
|
||||
return load(RetInnerCache.loadMap(codeClass));
|
||||
}
|
||||
|
||||
public static void load(Map<String, Map<Integer, String>> map) {
|
||||
public static int load(Map<String, Map<Integer, String>> map) {
|
||||
if (map.isEmpty()) {
|
||||
return;
|
||||
return 0;
|
||||
}
|
||||
loadLock.lock();
|
||||
AtomicInteger counter = new AtomicInteger();
|
||||
RetInnerCache.loadLock.lock();
|
||||
try {
|
||||
Map<String, Map<Integer, String>> newMap = new LinkedHashMap<>();
|
||||
rets.forEach((k, v) -> newMap.put(k, new LinkedHashMap<>(v)));
|
||||
RetInnerCache.allRets.forEach((k, v) -> newMap.put(k, new LinkedHashMap<>(v)));
|
||||
map.forEach((k, v) -> {
|
||||
Map<Integer, String> m = newMap.get(k);
|
||||
if (m != null) {
|
||||
m.putAll(v);
|
||||
} else {
|
||||
newMap.put(k, v);
|
||||
}
|
||||
newMap.computeIfAbsent(k, n -> new LinkedHashMap<>()).putAll(v);
|
||||
counter.addAndGet(v.size());
|
||||
});
|
||||
rets = newMap;
|
||||
defret = rets.get("");
|
||||
RetInnerCache.allRets = newMap;
|
||||
RetInnerCache.defRets = newMap.get("");
|
||||
} finally {
|
||||
loadLock.unlock();
|
||||
RetInnerCache.loadLock.unlock();
|
||||
}
|
||||
return counter.get();
|
||||
}
|
||||
|
||||
public static RetResult retResult(int retcode) {
|
||||
@@ -135,7 +127,7 @@ public abstract class RetCodes {
|
||||
if (retcode == 0) {
|
||||
return "Success";
|
||||
}
|
||||
return defret.getOrDefault(retcode, "Error");
|
||||
return RetInnerCache.defRets.getOrDefault(retcode, "Error");
|
||||
}
|
||||
|
||||
public static String retInfo(String locale, int retcode) {
|
||||
@@ -145,7 +137,7 @@ public abstract class RetCodes {
|
||||
if (retcode == 0) {
|
||||
return "Success";
|
||||
}
|
||||
Map<Integer, String> map = rets.get(locale);
|
||||
Map<Integer, String> map = RetInnerCache.allRets.get(locale);
|
||||
if (map == null) {
|
||||
return "Error";
|
||||
}
|
||||
|
||||
13
src/main/java/org/redkale/service/RetInfoTransfer.java
Normal file
13
src/main/java/org/redkale/service/RetInfoTransfer.java
Normal file
@@ -0,0 +1,13 @@
|
||||
/*
|
||||
|
||||
*/
|
||||
|
||||
package org.redkale.service;
|
||||
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public interface RetInfoTransfer extends BiFunction<Integer, String, String> {}
|
||||
105
src/main/java/org/redkale/service/RetInnerCache.java
Normal file
105
src/main/java/org/redkale/service/RetInnerCache.java
Normal file
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
|
||||
*/
|
||||
|
||||
package org.redkale.service;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.ServiceLoader;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import static org.redkale.boot.Application.SYSNAME_APP_CONF_DIR;
|
||||
import static org.redkale.boot.Application.SYSNAME_APP_HOME;
|
||||
import org.redkale.util.RedkaleClassLoader;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
class RetInnerCache {
|
||||
|
||||
static final ReentrantLock loadLock = new ReentrantLock();
|
||||
|
||||
static Map<String, Map<Integer, String>> allRets = new LinkedHashMap<>();
|
||||
|
||||
static Map<Integer, String> defRets = new LinkedHashMap<>();
|
||||
|
||||
private RetInnerCache() {}
|
||||
|
||||
static Map<String, Map<Integer, String>> loadMap(Class clazz) {
|
||||
final Map<String, Map<Integer, String>> allRetMap = new LinkedHashMap<>();
|
||||
ServiceLoader<RetInfoTransfer> loader = ServiceLoader.load(RetInfoTransfer.class);
|
||||
RedkaleClassLoader.putServiceLoader(RetInfoTransfer.class);
|
||||
Iterator<RetInfoTransfer> it = loader.iterator();
|
||||
RetInfoTransfer func = it.hasNext() ? it.next() : null;
|
||||
if (func != null) {
|
||||
RedkaleClassLoader.putReflectionPublicConstructors(
|
||||
func.getClass(), func.getClass().getName());
|
||||
}
|
||||
RedkaleClassLoader.putReflectionPublicFields(clazz.getName());
|
||||
for (Field field : clazz.getFields()) {
|
||||
if (!Modifier.isStatic(field.getModifiers())) {
|
||||
continue;
|
||||
}
|
||||
if (field.getType() != int.class) {
|
||||
continue;
|
||||
}
|
||||
RetLabel[] infos = field.getAnnotationsByType(RetLabel.class);
|
||||
if (infos == null || infos.length == 0) {
|
||||
continue;
|
||||
}
|
||||
int value;
|
||||
try {
|
||||
value = field.getInt(null);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
continue;
|
||||
}
|
||||
for (RetLabel info : infos) {
|
||||
allRetMap
|
||||
.computeIfAbsent(info.locale(), k -> new LinkedHashMap<>())
|
||||
.put(value, func == null ? info.value() : func.apply(value, info.value()));
|
||||
}
|
||||
}
|
||||
try {
|
||||
File homePath = new File(System.getProperty(SYSNAME_APP_HOME, ""), "conf");
|
||||
File propPath = new File(System.getProperty(SYSNAME_APP_CONF_DIR, homePath.getPath()));
|
||||
if (propPath.isDirectory() && propPath.canRead()) {
|
||||
final String prefix = clazz.getSimpleName().toLowerCase();
|
||||
for (File propFile : propPath.listFiles(
|
||||
f -> f.getName().startsWith(prefix) && f.getName().endsWith(".properties"))) {
|
||||
if (propFile.isFile() && propFile.canRead()) {
|
||||
String locale =
|
||||
propFile.getName().substring(prefix.length()).replaceAll("\\.\\d+", "");
|
||||
locale = locale.substring(0, locale.indexOf(".properties"));
|
||||
Map<Integer, String> defRetMap = allRetMap.get(locale);
|
||||
if (defRetMap != null) {
|
||||
InputStreamReader in =
|
||||
new InputStreamReader(new FileInputStream(propFile), StandardCharsets.UTF_8);
|
||||
Properties prop = new Properties();
|
||||
prop.load(in);
|
||||
in.close();
|
||||
prop.forEach((k, v) -> {
|
||||
int retcode = Integer.parseInt(k.toString());
|
||||
if (defRetMap.containsKey(retcode)) {
|
||||
defRetMap.put(retcode, v.toString());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// do nothing
|
||||
}
|
||||
return allRetMap;
|
||||
}
|
||||
}
|
||||
@@ -5,17 +5,9 @@
|
||||
*/
|
||||
package org.redkale.service;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.annotation.*;
|
||||
import static java.lang.annotation.ElementType.*;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
import java.lang.reflect.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
import java.util.function.BiFunction;
|
||||
import static org.redkale.boot.Application.SYSNAME_APP_CONF_DIR;
|
||||
import static org.redkale.boot.Application.SYSNAME_APP_HOME;
|
||||
import org.redkale.util.RedkaleClassLoader;
|
||||
|
||||
/**
|
||||
* 用于定义错误码的注解 <br>
|
||||
@@ -46,81 +38,4 @@ public @interface RetLabel {
|
||||
RetLabel[] value();
|
||||
}
|
||||
|
||||
public static interface RetInfoTransfer extends BiFunction<Integer, String, String> {}
|
||||
|
||||
public abstract static class RetLoader {
|
||||
|
||||
private RetLoader() {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
|
||||
public static Map<String, Map<Integer, String>> loadMap(Class clazz) {
|
||||
final Map<String, Map<Integer, String>> rets = new LinkedHashMap<>();
|
||||
ServiceLoader<RetInfoTransfer> loader = ServiceLoader.load(RetInfoTransfer.class);
|
||||
RedkaleClassLoader.putServiceLoader(RetInfoTransfer.class);
|
||||
Iterator<RetInfoTransfer> it = loader.iterator();
|
||||
RetInfoTransfer func = it.hasNext() ? it.next() : null;
|
||||
if (func != null) {
|
||||
RedkaleClassLoader.putReflectionPublicConstructors(
|
||||
func.getClass(), func.getClass().getName());
|
||||
}
|
||||
RedkaleClassLoader.putReflectionPublicFields(clazz.getName());
|
||||
for (Field field : clazz.getFields()) {
|
||||
if (!Modifier.isStatic(field.getModifiers())) {
|
||||
continue;
|
||||
}
|
||||
if (field.getType() != int.class) {
|
||||
continue;
|
||||
}
|
||||
RetLabel[] infos = field.getAnnotationsByType(RetLabel.class);
|
||||
if (infos == null || infos.length == 0) {
|
||||
continue;
|
||||
}
|
||||
int value;
|
||||
try {
|
||||
value = field.getInt(null);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
continue;
|
||||
}
|
||||
for (RetLabel info : infos) {
|
||||
rets.computeIfAbsent(info.locale(), k -> new LinkedHashMap<>())
|
||||
.put(value, func == null ? info.value() : func.apply(value, info.value()));
|
||||
}
|
||||
}
|
||||
try {
|
||||
File homePath = new File(System.getProperty(SYSNAME_APP_HOME, ""), "conf");
|
||||
File propPath = new File(System.getProperty(SYSNAME_APP_CONF_DIR, homePath.getPath()));
|
||||
if (propPath.isDirectory() && propPath.canRead()) {
|
||||
final String prefix = clazz.getSimpleName().toLowerCase();
|
||||
for (File propFile : propPath.listFiles(
|
||||
f -> f.getName().startsWith(prefix) && f.getName().endsWith(".properties"))) {
|
||||
if (propFile.isFile() && propFile.canRead()) {
|
||||
String locale = propFile.getName()
|
||||
.substring(prefix.length())
|
||||
.replaceAll("\\.\\d+", "");
|
||||
locale = locale.substring(0, locale.indexOf(".properties"));
|
||||
Map<Integer, String> defrets = rets.get(locale);
|
||||
if (defrets != null) {
|
||||
InputStreamReader in =
|
||||
new InputStreamReader(new FileInputStream(propFile), StandardCharsets.UTF_8);
|
||||
Properties prop = new Properties();
|
||||
prop.load(in);
|
||||
in.close();
|
||||
prop.forEach((k, v) -> {
|
||||
int retcode = Integer.parseInt(k.toString());
|
||||
if (defrets.containsKey(retcode)) {
|
||||
defrets.put(retcode, v.toString());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return rets;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user