diff --git a/pom.xml b/pom.xml
index 34479517e..b8acc94aa 100644
--- a/pom.xml
+++ b/pom.xml
@@ -1,183 +1,183 @@
-
-
- 4.0.0
- org.redkale
- redkale
- jar
- RedkaleProject
- https://redkale.org
- redkale -- java framework
- 2.8.0-SNAPSHOT
-
-
- UTF-8
- 11
- 11
-
- 5.9.0
- 3.4.0
- 3.13.0
- 3.2.5
- 3.2.5
- 2.43.0
- 2.46.0
-
-
-
-
-
- Apache 2
- https://www.apache.org/licenses/
- repo
- Apache License
-
-
-
-
-
- org.junit.jupiter
- junit-jupiter
- ${junit.version}
- test
-
-
-
-
-
- central
- Central Repository
- https://repo.maven.apache.org/maven2
-
-
- sonatype-nexus-snapshots
- Sonatype Nexus Snapshots
- https://oss.sonatype.org/content/repositories/snapshots
-
-
-
-
-
- Redkale
- redkale
- redkale@qq.com
- https://redkale.org
-
- Project Manager
- Architect
-
- redkale
- https://redkale.org
-
- No
-
- 8
-
-
-
-
- https://github.com/redkale/redkale
- scm:git:git@github.com/redkale/redkale.git
- scm:git:git@github.com:redkale/redkale.git
-
-
-
-
-
-
- org.apache.maven.plugins
- maven-compiler-plugin
- ${maven-compiler-plugin.version}
-
- UTF-8
-
- -parameters
-
-
-
-
-
- org.apache.maven.plugins
- maven-jar-plugin
- ${maven-jar-plugin.version}
-
-
- false
-
- org.redkale.boot.Application
-
-
-
-
-
-
- org.apache.maven.plugins
- maven-surefire-plugin
- ${maven-surefire-plugin.version}
-
- once
- -Dfile.encoding=UTF-8
-
-
-
-
- org.apache.maven.plugins
- maven-failsafe-plugin
- ${maven-failsafe-plugin.version}
-
-
-
- com.diffplug.spotless
- spotless-maven-plugin
- ${spotless-maven-plugin.version}
-
- UNIX
-
-
-
- src/**/java/**/*.java
-
-
-
-
- true
- 4
-
-
-
-
-
- ${palantir-java-format.version}
-
- true
-
-
-
-
-
-
- check
-
- compile
-
-
-
-
-
-
-
+
+
+ 4.0.0
+ org.redkale
+ redkale
+ jar
+ RedkaleProject
+ https://redkale.org
+ redkale -- java framework
+ 2.8.0-SNAPSHOT
+
+
+ UTF-8
+ 11
+ 11
+
+ 5.9.0
+ 3.4.0
+ 3.13.0
+ 3.2.5
+ 3.2.5
+ 2.43.0
+ 2.46.0
+
+
+
+
+
+ Apache 2
+ https://www.apache.org/licenses/
+ repo
+ Apache License
+
+
+
+
+
+ org.junit.jupiter
+ junit-jupiter
+ ${junit.version}
+ test
+
+
+
+
+
+ central
+ Central Repository
+ https://repo.maven.apache.org/maven2
+
+
+ sonatype-nexus-snapshots
+ Sonatype Nexus Snapshots
+ https://oss.sonatype.org/content/repositories/snapshots
+
+
+
+
+
+ Redkale
+ redkale
+ redkale@qq.com
+ https://redkale.org
+
+ Project Manager
+ Architect
+
+ redkale
+ https://redkale.org
+
+ No
+
+ 8
+
+
+
+
+ https://github.com/redkale/redkale
+ scm:git:git@github.com/redkale/redkale.git
+ scm:git:git@github.com:redkale/redkale.git
+
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+ ${maven-compiler-plugin.version}
+
+ UTF-8
+
+ -parameters
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-jar-plugin
+ ${maven-jar-plugin.version}
+
+
+ false
+
+ org.redkale.boot.Application
+
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-surefire-plugin
+ ${maven-surefire-plugin.version}
+
+ once
+ -Dfile.encoding=UTF-8
+
+
+
+
+ org.apache.maven.plugins
+ maven-failsafe-plugin
+ ${maven-failsafe-plugin.version}
+
+
+
+ com.diffplug.spotless
+ spotless-maven-plugin
+ ${spotless-maven-plugin.version}
+
+ UNIX
+
+
+
+ src/**/java/**/*.java
+
+
+
+
+ true
+ 4
+
+
+
+
+
+ ${palantir-java-format.version}
+
+ true
+
+
+
+
+
+
+ check
+
+ compile
+
+
+
+
+
+
+
diff --git a/src/main/java/org/redkale/boot/Application.java b/src/main/java/org/redkale/boot/Application.java
index c91fbd8d0..b973c0468 100644
--- a/src/main/java/org/redkale/boot/Application.java
+++ b/src/main/java/org/redkale/boot/Application.java
@@ -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 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;
diff --git a/src/main/java/org/redkale/boot/ClassFilter.java b/src/main/java/org/redkale/boot/ClassFilter.java
index 926743cb6..d8e9bdb0f 100644
--- a/src/main/java/org/redkale/boot/ClassFilter.java
+++ b/src/main/java/org/redkale/boot/ClassFilter.java
@@ -63,6 +63,10 @@ public final class ClassFilter {
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);
}
diff --git a/src/main/java/org/redkale/service/RetCodes.java b/src/main/java/org/redkale/service/RetCodes.java
index a4e2d7726..9e33b4b15 100644
--- a/src/main/java/org/redkale/service/RetCodes.java
+++ b/src/main/java/org/redkale/service/RetCodes.java
@@ -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> rets = new LinkedHashMap<>();
-
- private static Map 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> map) {
+ public static int load(Map> map) {
if (map.isEmpty()) {
- return;
+ return 0;
}
- loadLock.lock();
+ AtomicInteger counter = new AtomicInteger();
+ RetInnerCache.loadLock.lock();
try {
Map> 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 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 map = rets.get(locale);
+ Map map = RetInnerCache.allRets.get(locale);
if (map == null) {
return "Error";
}
diff --git a/src/main/java/org/redkale/service/RetInfoTransfer.java b/src/main/java/org/redkale/service/RetInfoTransfer.java
new file mode 100644
index 000000000..64a1e5bb4
--- /dev/null
+++ b/src/main/java/org/redkale/service/RetInfoTransfer.java
@@ -0,0 +1,13 @@
+/*
+
+*/
+
+package org.redkale.service;
+
+import java.util.function.BiFunction;
+
+/**
+ *
+ * @author zhangjx
+ */
+public interface RetInfoTransfer extends BiFunction {}
diff --git a/src/main/java/org/redkale/service/RetInnerCache.java b/src/main/java/org/redkale/service/RetInnerCache.java
new file mode 100644
index 000000000..60f66c10c
--- /dev/null
+++ b/src/main/java/org/redkale/service/RetInnerCache.java
@@ -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> allRets = new LinkedHashMap<>();
+
+ static Map defRets = new LinkedHashMap<>();
+
+ private RetInnerCache() {}
+
+ static Map> loadMap(Class clazz) {
+ final Map> allRetMap = new LinkedHashMap<>();
+ ServiceLoader loader = ServiceLoader.load(RetInfoTransfer.class);
+ RedkaleClassLoader.putServiceLoader(RetInfoTransfer.class);
+ Iterator 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 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;
+ }
+}
diff --git a/src/main/java/org/redkale/service/RetLabel.java b/src/main/java/org/redkale/service/RetLabel.java
index f447b4349..e7078a2dd 100644
--- a/src/main/java/org/redkale/service/RetLabel.java
+++ b/src/main/java/org/redkale/service/RetLabel.java
@@ -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;
/**
* 用于定义错误码的注解
@@ -46,81 +38,4 @@ public @interface RetLabel {
RetLabel[] value();
}
- public static interface RetInfoTransfer extends BiFunction {}
-
- public abstract static class RetLoader {
-
- private RetLoader() {
- throw new IllegalStateException();
- }
-
- public static Map> loadMap(Class clazz) {
- final Map> rets = new LinkedHashMap<>();
- ServiceLoader loader = ServiceLoader.load(RetInfoTransfer.class);
- RedkaleClassLoader.putServiceLoader(RetInfoTransfer.class);
- Iterator 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 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;
- }
- }
}