From 0aa8e8653cd21e5a6d3fcdfb3b07cf038e0853f0 Mon Sep 17 00:00:00 2001 From: redkale Date: Sun, 5 Feb 2023 17:19:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0RedkaleException?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/redkale/boot/Application.java | 16 +++---- .../java/org/redkale/boot/ClassFilter.java | 2 +- .../redkale/boot/LoggingSearchHandler.java | 4 +- .../java/org/redkale/boot/NodeHttpServer.java | 2 +- .../java/org/redkale/boot/NodeServer.java | 12 ++--- .../redkale/cluster/CacheClusterAgent.java | 2 +- .../org/redkale/convert/ConvertException.java | 4 +- .../redkale/convert/ext/EnumSimpledCoder.java | 2 +- .../redkale/convert/ext/IntSimpledCoder.java | 2 +- .../redkale/convert/ext/LongSimpledCoder.java | 2 +- .../convert/json/JsonByteBufferReader.java | 2 +- .../redkale/mq/HttpMessageLocalClient.java | 3 +- .../java/org/redkale/mq/MessageAgent.java | 10 ++-- .../java/org/redkale/net/AsyncConnection.java | 4 +- .../java/org/redkale/net/AsyncIOGroup.java | 8 ++-- .../java/org/redkale/net/AsyncIOThread.java | 2 +- .../net/AsyncNioTcpProtocolServer.java | 2 +- .../net/AsyncNioUdpProtocolServer.java | 2 +- .../java/org/redkale/net/ProtocolServer.java | 4 +- src/main/java/org/redkale/net/Server.java | 6 +-- src/main/java/org/redkale/net/Transport.java | 6 +-- .../org/redkale/net/TransportFactory.java | 10 ++-- .../org/redkale/net/http/HttpException.java | 4 +- .../org/redkale/net/sncp/SncpException.java | 4 +- .../java/org/redkale/service/RetResult.java | 4 +- .../redkale/service/RpcRemoteException.java | 4 +- .../org/redkale/source/SourceException.java | 4 +- src/main/java/org/redkale/util/Attribute.java | 16 +++---- src/main/java/org/redkale/util/Creator.java | 14 +++--- .../java/org/redkale/util/Environment.java | 48 +++++++++++++++++-- src/main/java/org/redkale/util/Flows.java | 8 ++-- src/main/java/org/redkale/util/Invoker.java | 4 +- .../org/redkale/util/RedkaleClassLoader.java | 2 +- .../org/redkale/util/RedkaleException.java | 31 ++++++++++++ src/main/java/org/redkale/util/Reproduce.java | 6 +-- .../org/redkale/util/ResourceFactory.java | 6 +-- .../org/redkale/util/RetcodeException.java | 4 +- src/main/java/org/redkale/util/TypeToken.java | 2 +- src/main/java/org/redkale/util/Utility.java | 30 ++++++------ 39 files changed, 190 insertions(+), 108 deletions(-) create mode 100644 src/main/java/org/redkale/util/RedkaleException.java diff --git a/src/main/java/org/redkale/boot/Application.java b/src/main/java/org/redkale/boot/Application.java index db65db9ab..3e92beb77 100644 --- a/src/main/java/org/redkale/boot/Application.java +++ b/src/main/java/org/redkale/boot/Application.java @@ -290,7 +290,7 @@ public final class Application { this.confPath = confFile.toURI(); } } catch (IOException e) { - throw new RuntimeException(e); + throw new RedkaleException(e); } { //设置系统变量 AnyValue propertiesConf = this.config.getAnyValue("properties"); @@ -450,7 +450,7 @@ public final class Application { try { loadResourceProperties(); } catch (IOException e) { - throw new RuntimeException(e); + throw new RedkaleException(e); } //------------------------------------ 配置 节点 ------------------------------------ TransportStrategy strategy = null; @@ -533,7 +533,7 @@ public final class Application { AnyValue mqConf = mqConfs[i]; String mqname = mqConf.getValue("name", ""); if (mqnames.contains(mqname)) { - throw new RuntimeException("mq.name(" + mqname + ") is repeat"); + throw new RedkaleException("mq.name(" + mqname + ") is repeat"); } mqnames.add(mqname); String namex = mqConf.getValue("names"); @@ -543,7 +543,7 @@ public final class Application { continue; } if (mqnames.contains(n.trim())) { - throw new RuntimeException("mq.name(" + n.trim() + ") is repeat"); + throw new RedkaleException("mq.name(" + n.trim() + ") is repeat"); } mqnames.add(n.trim()); } @@ -1009,7 +1009,7 @@ public final class Application { } for (char ch : name.toCharArray()) { if (!((ch >= '0' && ch <= '9') || ch == '_' || ch == '.' || ch == '-' || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))) { //不能含特殊字符 - throw new RuntimeException("name only 0-9 a-z A-Z _ - . cannot begin 0-9"); + throw new RedkaleException("name only 0-9 a-z A-Z _ - . cannot begin 0-9"); } } return name; @@ -1335,7 +1335,7 @@ public final class Application { final String group = conf.getValue("name", ""); final String protocol = conf.getValue("protocol", Transport.DEFAULT_NETPROTOCOL).toUpperCase(); if (!"TCP".equalsIgnoreCase(protocol) && !"UDP".equalsIgnoreCase(protocol)) { - throw new RuntimeException("Not supported Transport Protocol " + conf.getValue("protocol")); + throw new RedkaleException("Not supported Transport Protocol " + conf.getValue("protocol")); } TransportGroupInfo ginfo = new TransportGroupInfo(group, protocol, new LinkedHashSet<>()); for (AnyValue node : conf.getAnyValues("node")) { @@ -1576,7 +1576,7 @@ public final class Application { } } if (watchs.size() > 1) { - throw new RuntimeException("Found one more WATCH Server"); + throw new RedkaleException("Found one more WATCH Server"); } this.watching = !watchs.isEmpty(); @@ -1706,7 +1706,7 @@ public final class Application { } final Class old = nodeClasses.get(p); if (old != null && old != type) { - throw new RuntimeException("Protocol(" + p + ") had NodeServer-Class(" + old.getName() + ") but repeat NodeServer-Class(" + type.getName() + ")"); + throw new RedkaleException("Protocol(" + p + ") had NodeServer-Class(" + old.getName() + ") but repeat NodeServer-Class(" + type.getName() + ")"); } nodeClasses.put(p, type); } diff --git a/src/main/java/org/redkale/boot/ClassFilter.java b/src/main/java/org/redkale/boot/ClassFilter.java index 8d5517535..ccdeb3ca5 100644 --- a/src/main/java/org/redkale/boot/ClassFilter.java +++ b/src/main/java/org/redkale/boot/ClassFilter.java @@ -612,7 +612,7 @@ public final class ClassFilter { } else if (root.isDirectory()) { if (exclude != null && exclude.equals(root)) return; File[] lfs = root.listFiles(); - if (lfs == null) throw new RuntimeException("File(" + root + ") cannot listFiles()"); + if (lfs == null) throw new RedkaleException("File(" + root + ") cannot listFiles()"); for (File f : lfs) { loadClassFiles(exclude, f, files); } diff --git a/src/main/java/org/redkale/boot/LoggingSearchHandler.java b/src/main/java/org/redkale/boot/LoggingSearchHandler.java index 67376385f..f2cd6ddd1 100644 --- a/src/main/java/org/redkale/boot/LoggingSearchHandler.java +++ b/src/main/java/org/redkale/boot/LoggingSearchHandler.java @@ -153,12 +153,12 @@ public class LoggingSearchHandler extends LoggingBaseHandler { String cname = getClass().getName(); this.sourceResourceName = manager.getProperty(cname + ".source"); if (this.sourceResourceName == null || this.sourceResourceName.isEmpty()) { - throw new RuntimeException("not found logging.property " + cname + ".source"); + throw new RedkaleException("not found logging.property " + cname + ".source"); } String tagstr = manager.getProperty(cname + ".tag"); if (tagstr != null && !tagstr.isEmpty()) { if (!checkTagName(tagstr.replaceAll("\\$\\{.+\\}", ""))) { - throw new RuntimeException("found illegal logging.property " + cname + ".tag = " + tagstr); + throw new RedkaleException("found illegal logging.property " + cname + ".tag = " + tagstr); } this.tag = tagstr.replace("${" + RESNAME_APP_NAME + "}", System.getProperty(RESNAME_APP_NAME, "")); if (this.tag.contains("%")) { diff --git a/src/main/java/org/redkale/boot/NodeHttpServer.java b/src/main/java/org/redkale/boot/NodeHttpServer.java index 2e24b7e54..bc583ab29 100644 --- a/src/main/java/org/redkale/boot/NodeHttpServer.java +++ b/src/main/java/org/redkale/boot/NodeHttpServer.java @@ -364,7 +364,7 @@ public class NodeHttpServer extends NodeServer { if (mqname != null) { agent0 = application.getMessageAgent(mqname); if (agent0 == null) { - throw new RuntimeException("not found " + MessageAgent.class.getSimpleName() + " config for (name=" + mqname + ")"); + throw new RedkaleException("not found " + MessageAgent.class.getSimpleName() + " config for (name=" + mqname + ")"); } } final MessageAgent messageAgent = agent0; diff --git a/src/main/java/org/redkale/boot/NodeServer.java b/src/main/java/org/redkale/boot/NodeServer.java index b88b09051..7cbdb5c85 100644 --- a/src/main/java/org/redkale/boot/NodeServer.java +++ b/src/main/java/org/redkale/boot/NodeServer.java @@ -120,7 +120,7 @@ public abstract class NodeServer { RedkaleClassLoader.putReflectionDeclaredConstructors(clazz, clazz.getName(), Application.class, AnyValue.class); return clazz.getDeclaredConstructor(Application.class, AnyValue.class).newInstance(application, serconf); } catch (Exception e) { - throw new RuntimeException(e); + throw new RedkaleException(e); } } @@ -131,7 +131,7 @@ public abstract class NodeServer { this.sncpAddress = new InetSocketAddress(host.isEmpty() ? application.localAddress.getAddress().getHostAddress() : host, this.serverConf.getIntValue("port")); this.sncpGroup = application.getSncpTransportFactory().findGroupName(this.sncpAddress); //单向SNCP服务不需要对等group - //if (this.sncpGroup == null) throw new RuntimeException("Server (" + String.valueOf(config).replaceAll("\\s+", " ") + ") not found info"); + //if (this.sncpGroup == null) throw new RedkaleException("Server (" + String.valueOf(config).replaceAll("\\s+", " ") + ") not found info"); } //单点服务不会有 sncpAddress、sncpGroup if (this.sncpAddress != null) { @@ -340,7 +340,7 @@ public abstract class NodeServer { return null; //远程模式不需要注入 CacheSource } if (!(srcObj instanceof Service)) { - throw new RuntimeException("CacheSource must be inject in Service, cannot in " + srcObj); + throw new RedkaleException("CacheSource must be inject in Service, cannot in " + srcObj); } final Service srcService = (Service) srcObj; SncpOldClient client = Sncp.getSncpOldClient(srcService); @@ -467,7 +467,7 @@ public abstract class NodeServer { } } if (entry.getName().contains("$")) { - throw new RuntimeException(" value cannot contains '$' in " + entry.getProperty()); + throw new RedkaleException(" value cannot contains '$' in " + entry.getProperty()); } Service oldother = resourceFactory.find(entry.getName(), serviceImplClass); if (oldother != null) { //Server加载Service时需要判断是否已经加载过了。 @@ -523,7 +523,7 @@ public abstract class NodeServer { if (rf.find(resourceName, restype) == null) { regFactory.register(resourceName, restype, service); } else if (isSNCP() && !entry.isAutoload()) { - throw new RuntimeException(restype.getSimpleName() + "(class:" + serviceImplClass.getName() + ", name:" + resourceName + ", group:" + groups + ") is repeat."); + throw new RedkaleException(restype.getSimpleName() + "(class:" + serviceImplClass.getName() + ", name:" + resourceName + ", group:" + groups + ") is repeat."); } if (Sncp.isRemote(service)) { remoteServices.add(service); @@ -545,7 +545,7 @@ public abstract class NodeServer { } catch (RuntimeException ex) { throw ex; } catch (Exception e) { - throw new RuntimeException(e); + throw new RedkaleException(e); } }; if (entry.isExpect()) { diff --git a/src/main/java/org/redkale/cluster/CacheClusterAgent.java b/src/main/java/org/redkale/cluster/CacheClusterAgent.java index c120cc101..1dc9f9c0e 100644 --- a/src/main/java/org/redkale/cluster/CacheClusterAgent.java +++ b/src/main/java/org/redkale/cluster/CacheClusterAgent.java @@ -241,7 +241,7 @@ public class CacheClusterAgent extends ClusterAgent implements Resourcable { @Override public void register(Application application) { if (isApplicationHealth()) { - throw new RuntimeException("application.nodeid=" + nodeid + " exists in cluster"); + throw new RedkaleException("application.nodeid=" + nodeid + " exists in cluster"); } deregister(application); diff --git a/src/main/java/org/redkale/convert/ConvertException.java b/src/main/java/org/redkale/convert/ConvertException.java index 31acf0e62..758bb1b99 100644 --- a/src/main/java/org/redkale/convert/ConvertException.java +++ b/src/main/java/org/redkale/convert/ConvertException.java @@ -4,6 +4,8 @@ */ package org.redkale.convert; +import org.redkale.util.RedkaleException; + /** * 序列化自定义异常类 * @@ -12,7 +14,7 @@ package org.redkale.convert; * * @author zhangjx */ -public class ConvertException extends RuntimeException { +public class ConvertException extends RedkaleException { public ConvertException() { super(); diff --git a/src/main/java/org/redkale/convert/ext/EnumSimpledCoder.java b/src/main/java/org/redkale/convert/ext/EnumSimpledCoder.java index 13a7bc8f5..2702cdb54 100644 --- a/src/main/java/org/redkale/convert/ext/EnumSimpledCoder.java +++ b/src/main/java/org/redkale/convert/ext/EnumSimpledCoder.java @@ -62,7 +62,7 @@ public final class EnumSimpledCoder extends S out.writeSmallString("0x0"); } else { if (value < 0) { - throw new NumberFormatException("Negative values (" + value + ") are not supported"); + throw new ConvertException("Negative values (" + value + ") are not supported"); } out.writeSmallString("0x" + Integer.toHexString(value)); } diff --git a/src/main/java/org/redkale/convert/ext/LongSimpledCoder.java b/src/main/java/org/redkale/convert/ext/LongSimpledCoder.java index b3157fb6f..59cf8f867 100644 --- a/src/main/java/org/redkale/convert/ext/LongSimpledCoder.java +++ b/src/main/java/org/redkale/convert/ext/LongSimpledCoder.java @@ -48,7 +48,7 @@ public final class LongSimpledCoder extends out.writeSmallString("0x0"); } else { if (value < 0) { - throw new NumberFormatException("Negative values (" + value + ") are not supported"); + throw new ConvertException("Negative values (" + value + ") are not supported"); } out.writeSmallString("0x" + Long.toHexString(value)); } diff --git a/src/main/java/org/redkale/convert/json/JsonByteBufferReader.java b/src/main/java/org/redkale/convert/json/JsonByteBufferReader.java index 45e18acc3..8a919f17b 100644 --- a/src/main/java/org/redkale/convert/json/JsonByteBufferReader.java +++ b/src/main/java/org/redkale/convert/json/JsonByteBufferReader.java @@ -100,7 +100,7 @@ public class JsonByteBufferReader extends JsonReader { } return Character.lowSurrogate(uc); } else { - throw new RuntimeException(new UnmappableCharacterException(4)); + throw new ConvertException(new UnmappableCharacterException(4)); } } diff --git a/src/main/java/org/redkale/mq/HttpMessageLocalClient.java b/src/main/java/org/redkale/mq/HttpMessageLocalClient.java index c011eb50f..fdbad7df1 100644 --- a/src/main/java/org/redkale/mq/HttpMessageLocalClient.java +++ b/src/main/java/org/redkale/mq/HttpMessageLocalClient.java @@ -16,6 +16,7 @@ import org.redkale.boot.*; import org.redkale.convert.Convert; import org.redkale.convert.json.JsonConvert; import org.redkale.net.http.*; +import org.redkale.util.RedkaleException; /** * 没有配置MQ且也没有ClusterAgent的情况下实现的默认HttpMessageClient实例 @@ -159,7 +160,7 @@ public class HttpMessageLocalClient extends HttpMessageClient { try { servlet.execute(req, resp); } catch (Exception e) { - throw new RuntimeException(e); + throw new RedkaleException(e); } } diff --git a/src/main/java/org/redkale/mq/MessageAgent.java b/src/main/java/org/redkale/mq/MessageAgent.java index 39debe132..1aa62ba69 100644 --- a/src/main/java/org/redkale/mq/MessageAgent.java +++ b/src/main/java/org/redkale/mq/MessageAgent.java @@ -91,7 +91,7 @@ public abstract class MessageAgent implements Resourcable { } catch (RuntimeException ex) { throw ex; } catch (Exception e) { - throw new RuntimeException(e); + throw new RedkaleException(e); } } // application (it doesn't execute completion handlers). @@ -214,11 +214,11 @@ public abstract class MessageAgent implements Resourcable { return name; } if (name.charAt(0) >= '0' && name.charAt(0) <= '9') { - throw new RuntimeException("name only 0-9 a-z A-Z _ cannot begin 0-9"); + throw new RedkaleException("name only 0-9 a-z A-Z _ cannot begin 0-9"); } for (char ch : name.toCharArray()) { if (!((ch >= '0' && ch <= '9') || ch == '_' || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))) { //不能含特殊字符 - throw new RuntimeException("name only 0-9 a-z A-Z _ cannot begin 0-9"); + throw new RedkaleException("name only 0-9 a-z A-Z _ cannot begin 0-9"); } } return name; @@ -313,7 +313,7 @@ public abstract class MessageAgent implements Resourcable { httpNodesLock.lock(); try { if (messageNodes.containsKey(consumerid)) { - throw new RuntimeException("consumerid(" + consumerid + ") is repeat"); + throw new RedkaleException("consumerid(" + consumerid + ") is repeat"); } HttpMessageProcessor processor = new HttpMessageProcessor(this.logger, httpMessageClient, getHttpProducer(), ns, service, servlet); this.messageNodes.put(consumerid, new MessageConsumerNode(ns, service, servlet, processor, createConsumer(topics, consumerid, processor))); @@ -336,7 +336,7 @@ public abstract class MessageAgent implements Resourcable { sncpNodesLock.lock(); try { if (messageNodes.containsKey(consumerid)) { - throw new RuntimeException("consumerid(" + consumerid + ") is repeat"); + throw new RedkaleException("consumerid(" + consumerid + ") is repeat"); } SncpMessageProcessor processor = new SncpMessageProcessor(this.logger, sncpMessageClient, getSncpProducer(), ns, service, servlet); this.messageNodes.put(consumerid, new MessageConsumerNode(ns, service, servlet, processor, createConsumer(new String[]{topic}, consumerid, processor))); diff --git a/src/main/java/org/redkale/net/AsyncConnection.java b/src/main/java/org/redkale/net/AsyncConnection.java index a46ffbba9..9b5a42c78 100644 --- a/src/main/java/org/redkale/net/AsyncConnection.java +++ b/src/main/java/org/redkale/net/AsyncConnection.java @@ -455,7 +455,7 @@ public abstract class AsyncConnection implements ChannelContext, Channel, AutoCl public void setReadBuffer(ByteBuffer buffer) { if (this.readBuffer != null) { - throw new RuntimeException("repeat AsyncConnection.setReadBuffer"); + throw new RedkaleException("repeat AsyncConnection.setReadBuffer"); } this.readBuffer = buffer; } @@ -684,7 +684,7 @@ public abstract class AsyncConnection implements ChannelContext, Channel, AutoCl protected void setReadSSLBuffer(ByteBuffer buffer) { if (this.readSSLHalfBuffer != null) { - throw new RuntimeException("repeat AsyncConnection.setReadSSLBuffer"); + throw new RedkaleException("repeat AsyncConnection.setReadSSLBuffer"); } this.readSSLHalfBuffer = buffer; } diff --git a/src/main/java/org/redkale/net/AsyncIOGroup.java b/src/main/java/org/redkale/net/AsyncIOGroup.java index 97c88b532..350bdfbdf 100644 --- a/src/main/java/org/redkale/net/AsyncIOGroup.java +++ b/src/main/java/org/redkale/net/AsyncIOGroup.java @@ -111,7 +111,7 @@ public class AsyncIOGroup extends AsyncGroup { this.connectThread = null; } } catch (IOException e) { - throw new RuntimeException(e); + throw new RedkaleException(e); } } @@ -133,7 +133,7 @@ public class AsyncIOGroup extends AsyncGroup { return this; } if (closed.get()) { - throw new RuntimeException("group is closed"); + throw new RedkaleException("group is closed"); } for (int i = 0; i < this.ioReadThreads.length; i++) { this.ioReadThreads[i].start(); @@ -214,7 +214,7 @@ public class AsyncIOGroup extends AsyncGroup { try { return newTCPClientConnection(null); } catch (IOException e) { - throw new RuntimeException(e); + throw new RedkaleException(e); } } @@ -302,7 +302,7 @@ public class AsyncIOGroup extends AsyncGroup { try { return newUDPClientConnection(null); } catch (IOException e) { - throw new RuntimeException(e); + throw new RedkaleException(e); } } diff --git a/src/main/java/org/redkale/net/AsyncIOThread.java b/src/main/java/org/redkale/net/AsyncIOThread.java index d4138b455..0c7b19ce7 100644 --- a/src/main/java/org/redkale/net/AsyncIOThread.java +++ b/src/main/java/org/redkale/net/AsyncIOThread.java @@ -64,7 +64,7 @@ public class AsyncIOThread extends WorkThread { return; } if (key.selector() != selector) { - throw new RuntimeException("NioThread.selector not the same to SelectionKey.selector"); + throw new RedkaleException("NioThread.selector not the same to SelectionKey.selector"); } if ((key.interestOps() & opt) != 0) { return; diff --git a/src/main/java/org/redkale/net/AsyncNioTcpProtocolServer.java b/src/main/java/org/redkale/net/AsyncNioTcpProtocolServer.java index 7a013d937..cd8a2d2b2 100644 --- a/src/main/java/org/redkale/net/AsyncNioTcpProtocolServer.java +++ b/src/main/java/org/redkale/net/AsyncNioTcpProtocolServer.java @@ -188,7 +188,7 @@ class AsyncNioTcpProtocolServer extends ProtocolServer { } else if (t instanceof RuntimeException) { throw (RuntimeException) t; } else { - throw new RuntimeException(t); + throw new RedkaleException(t); } }); } diff --git a/src/main/java/org/redkale/net/AsyncNioUdpProtocolServer.java b/src/main/java/org/redkale/net/AsyncNioUdpProtocolServer.java index 7f2ce7ada..f236fc7e2 100644 --- a/src/main/java/org/redkale/net/AsyncNioUdpProtocolServer.java +++ b/src/main/java/org/redkale/net/AsyncNioUdpProtocolServer.java @@ -181,7 +181,7 @@ class AsyncNioUdpProtocolServer extends ProtocolServer { } else if (t instanceof RuntimeException) { throw (RuntimeException) t; } else { - throw new RuntimeException(t); + throw new RedkaleException(t); } }); } diff --git a/src/main/java/org/redkale/net/ProtocolServer.java b/src/main/java/org/redkale/net/ProtocolServer.java index 9c824afc8..1e3045152 100644 --- a/src/main/java/org/redkale/net/ProtocolServer.java +++ b/src/main/java/org/redkale/net/ProtocolServer.java @@ -10,7 +10,7 @@ import java.net.*; import java.util.Set; import org.redkale.annotation.Resource; import org.redkale.boot.Application; -import org.redkale.util.AnyValue; +import org.redkale.util.*; /** * 协议底层Server @@ -56,7 +56,7 @@ public abstract class ProtocolServer { } else if ("UDP".equalsIgnoreCase(protocol)) { return new AsyncNioUdpProtocolServer(context); } else { - throw new RuntimeException(ProtocolServer.class.getSimpleName() + " not support protocol " + protocol); + throw new RedkaleException(ProtocolServer.class.getSimpleName() + " not support protocol " + protocol); } } diff --git a/src/main/java/org/redkale/net/Server.java b/src/main/java/org/redkale/net/Server.java index 8bd7eb6db..511377ae9 100644 --- a/src/main/java/org/redkale/net/Server.java +++ b/src/main/java/org/redkale/net/Server.java @@ -134,7 +134,7 @@ public abstract class Server implements Serializable { try { return future.get(timeout, unit); } catch (ExecutionException ex) { - throw new RuntimeException(ex.getCause()); + throw new RedkaleException(ex.getCause()); } catch (Exception ex) { - throw new RuntimeException(ex); + throw new RedkaleException(ex); } } diff --git a/src/main/java/org/redkale/service/RpcRemoteException.java b/src/main/java/org/redkale/service/RpcRemoteException.java index 61ee4a4a2..23cc26d1b 100644 --- a/src/main/java/org/redkale/service/RpcRemoteException.java +++ b/src/main/java/org/redkale/service/RpcRemoteException.java @@ -5,6 +5,8 @@ */ package org.redkale.service; +import org.redkale.util.RedkaleException; + /** * 供RPC协议使用 * @@ -13,7 +15,7 @@ package org.redkale.service; * * @author zhangjx */ -public class RpcRemoteException extends RuntimeException { +public class RpcRemoteException extends RedkaleException { public RpcRemoteException() { super(); diff --git a/src/main/java/org/redkale/source/SourceException.java b/src/main/java/org/redkale/source/SourceException.java index 7fd9bb843..55f3a71a5 100644 --- a/src/main/java/org/redkale/source/SourceException.java +++ b/src/main/java/org/redkale/source/SourceException.java @@ -3,6 +3,8 @@ */ package org.redkale.source; +import org.redkale.util.RedkaleException; + /** * 数据源自定义异常类 * @@ -13,7 +15,7 @@ package org.redkale.source; * * @since 2.8.0 */ -public class SourceException extends RuntimeException { +public class SourceException extends RedkaleException { public SourceException() { super(); diff --git a/src/main/java/org/redkale/util/Attribute.java b/src/main/java/org/redkale/util/Attribute.java index 5dc004f1b..34d192db4 100644 --- a/src/main/java/org/redkale/util/Attribute.java +++ b/src/main/java/org/redkale/util/Attribute.java @@ -225,7 +225,7 @@ public interface Attribute { try { return create(clazz, fieldName, (Class) null, clazz.getDeclaredField(fieldName), (java.lang.reflect.Method) null, (java.lang.reflect.Method) null, null); } catch (NoSuchFieldException | SecurityException ex) { - throw new RuntimeException(ex); + throw new RedkaleException(ex); } } @@ -244,7 +244,7 @@ public interface Attribute { try { return create(clazz, fieldName, (Class) null, clazz.getDeclaredField(fieldName), (java.lang.reflect.Method) null, (java.lang.reflect.Method) null, attach); } catch (NoSuchFieldException | SecurityException ex) { - throw new RuntimeException(ex); + throw new RedkaleException(ex); } } @@ -753,7 +753,7 @@ public interface Attribute { fieldkey = (fieldAlias == null ? "" : ("0_" + fieldAlias + "_")) + "2_" + setter.getName(); } if (fieldAlias == null && fieldType == null && tgetter == null && tsetter == null && tfield == null) { - throw new RuntimeException("[" + clazz + "]have no public field or setter or getter"); + throw new RedkaleException("[" + clazz + "]have no public field or setter or getter"); } final String fieldName = fieldAlias; Class column = fieldType; @@ -768,9 +768,9 @@ public interface Attribute { column = tsetter.getParameterTypes()[0]; generictype = tsetter.getGenericParameterTypes()[0]; } else if (fieldType == null) { - throw new RuntimeException("[" + clazz + "]have no public field or setter or getter"); + throw new RedkaleException("[" + clazz + "]have no public field or setter or getter"); } else if (column == null) { - throw new RuntimeException("[" + clazz + "]have no field type"); + throw new RedkaleException("[" + clazz + "]have no field type"); } boolean checkCast = false; if (generictype instanceof java.lang.reflect.TypeVariable) { @@ -966,7 +966,7 @@ public interface Attribute { mv.visitMethodInsn(INVOKEVIRTUAL, columnName, pm.getName(), Type.getMethodDescriptor(pm), false); m = 3; } catch (Exception ex) { - throw new RuntimeException(ex); //不可能会发生 + throw new RedkaleException(ex); //不可能会发生 } } if (!tfield.getType().isPrimitive() && tfield.getGenericType() instanceof TypeVariable) { @@ -984,7 +984,7 @@ public interface Attribute { mv.visitMethodInsn(INVOKEVIRTUAL, columnName, pm.getName(), Type.getMethodDescriptor(pm), false); m = 3; } catch (Exception ex) { - throw new RuntimeException(ex); //不可能会发生 + throw new RedkaleException(ex); //不可能会发生 } } mv.visitMethodInsn(INVOKEVIRTUAL, interName, tsetter.getName(), Type.getMethodDescriptor(tsetter), false); @@ -1045,7 +1045,7 @@ public interface Attribute { RedkaleClassLoader.putReflectionField(newDynName.replace('/', '.'), _attach); return rs; } catch (Exception ex) { - throw new RuntimeException(ex); + throw new RedkaleException(ex); } } diff --git a/src/main/java/org/redkale/util/Creator.java b/src/main/java/org/redkale/util/Creator.java index 54beaca41..b2ac3d8bc 100644 --- a/src/main/java/org/redkale/util/Creator.java +++ b/src/main/java/org/redkale/util/Creator.java @@ -205,7 +205,7 @@ public interface Creator { return creator; } if (clazz.isInterface() || Modifier.isAbstract(clazz.getModifiers())) { - throw new RuntimeException("[" + clazz + "] is a interface or abstract class, cannot create it's Creator."); + throw new RedkaleException("[" + clazz + "] is a interface or abstract class, cannot create it's Creator."); } for (final Method method : clazz.getDeclaredMethods()) { //查找类中是否存在提供创建Creator实例的静态方法 if (!Modifier.isStatic(method.getModifiers())) { @@ -226,7 +226,7 @@ public interface Creator { return c; } } catch (Exception e) { - throw new RuntimeException(e); + throw new RedkaleException(e); } } final String supDynName = Creator.class.getName().replace('.', '/'); @@ -336,7 +336,7 @@ public interface Creator { final Constructor constructor = constructor0; final SimpleEntry[] constructorParameters = constructorParameters0; if (constructor == null || constructorParameters == null) { - throw new RuntimeException("[" + clazz + "] have no public or ConstructorParameters-Annotation constructor."); + throw new RedkaleException("[" + clazz + "] have no public or ConstructorParameters-Annotation constructor."); } final int[] iconsts = {ICONST_0, ICONST_1, ICONST_2, ICONST_3, ICONST_4, ICONST_5}; //------------------------------------------------------------- @@ -497,7 +497,7 @@ public interface Creator { Method pm = bigct.getMethod(ct.getSimpleName() + "Value"); mv.visitMethodInsn(INVOKEVIRTUAL, bigct.getName().replace('.', '/'), pm.getName(), Type.getMethodDescriptor(pm), false); } catch (Exception ex) { - throw new RuntimeException(ex); //不可能会发生 + throw new RedkaleException(ex); //不可能会发生 } } else { mv.visitTypeInsn(CHECKCAST, ct.getName().replace('.', '/')); @@ -550,7 +550,7 @@ public interface Creator { } } if (!ispub && resultClazz == null) { - throw new RuntimeException("[" + clazz + "] have no public or ConstructorParameters-Annotation constructor."); + throw new RedkaleException("[" + clazz + "] have no public or ConstructorParameters-Annotation constructor."); } try { if (resultClazz == null) { @@ -564,7 +564,7 @@ public interface Creator { RedkaleClassLoader.putReflectionDeclaredConstructors(resultClazz, newDynName.replace('/', '.')); return (Creator) resultClazz.getDeclaredConstructor().newInstance(); } catch (Exception ex) { - throw new RuntimeException(ex); + throw new RedkaleException(ex); } } @@ -804,7 +804,7 @@ public interface Creator { RedkaleClassLoader.putDynClass(newDynName.replace('/', '.'), bytes, resultClazz); RedkaleClassLoader.putReflectionDeclaredConstructors(resultClazz, newDynName.replace('/', '.')); return (IntFunction) resultClazz.getDeclaredConstructor().newInstance(); - } catch (Exception ex) { + } catch (Throwable ex) { //ex.printStackTrace(); //一般不会发生, native-image在没有预编译情况下会报错 return t -> (T[]) Array.newInstance(clazz, t); } diff --git a/src/main/java/org/redkale/util/Environment.java b/src/main/java/org/redkale/util/Environment.java index 5ffd6a432..1d0755b16 100644 --- a/src/main/java/org/redkale/util/Environment.java +++ b/src/main/java/org/redkale/util/Environment.java @@ -59,7 +59,9 @@ public class Environment implements java.io.Serializable { public boolean getBooleanProperty(String key, boolean defaultValue) { String val = properties.getProperty(key); - if (val == null || val.isEmpty()) return defaultValue; + if (val == null || val.isEmpty()) { + return defaultValue; + } return "true".equalsIgnoreCase(val) || "1".equalsIgnoreCase(val); } @@ -69,7 +71,9 @@ public class Environment implements java.io.Serializable { public short getShortProperty(String key, short defaultValue) { String val = properties.getProperty(key); - if (val == null || val.isEmpty()) return defaultValue; + if (val == null || val.isEmpty()) { + return defaultValue; + } try { return Short.parseShort(val); } catch (Exception e) { @@ -83,7 +87,9 @@ public class Environment implements java.io.Serializable { public int getIntProperty(String key, int defaultValue) { String val = properties.getProperty(key); - if (val == null || val.isEmpty()) return defaultValue; + if (val == null || val.isEmpty()) { + return defaultValue; + } try { return Integer.parseInt(val); } catch (Exception e) { @@ -91,13 +97,31 @@ public class Environment implements java.io.Serializable { } } + public float getFloatProperty(String key) { + return Float.parseFloat(properties.getProperty(key)); + } + + public float getFloatProperty(String key, float defaultValue) { + String val = properties.getProperty(key); + if (val == null || val.isEmpty()) { + return defaultValue; + } + try { + return Float.parseFloat(val); + } catch (Exception e) { + return defaultValue; + } + } + public long getLongProperty(String key) { return Long.parseLong(properties.getProperty(key)); } public long getLongProperty(String key, long defaultValue) { String val = properties.getProperty(key); - if (val == null || val.isEmpty()) return defaultValue; + if (val == null || val.isEmpty()) { + return defaultValue; + } try { return Long.parseLong(val); } catch (Exception e) { @@ -105,6 +129,22 @@ public class Environment implements java.io.Serializable { } } + public double getDoubleProperty(String key) { + return Double.parseDouble(properties.getProperty(key)); + } + + public double getDoubleProperty(String key, double defaultValue) { + String val = properties.getProperty(key); + if (val == null || val.isEmpty()) { + return defaultValue; + } + try { + return Double.parseDouble(val); + } catch (Exception e) { + return defaultValue; + } + } + @Override public String toString() { return properties.toString(); diff --git a/src/main/java/org/redkale/util/Flows.java b/src/main/java/org/redkale/util/Flows.java index d2f7bc02a..1c221cc85 100644 --- a/src/main/java/org/redkale/util/Flows.java +++ b/src/main/java/org/redkale/util/Flows.java @@ -108,7 +108,7 @@ public abstract class Flows { reactorMonoClass0 = Thread.currentThread().getContextClassLoader().loadClass("reactor.core.publisher.Mono"); Class> monoFuncClass = null; try { - monoFuncClass = (Class) Thread.currentThread().getContextClassLoader().loadClass("org.re" + "dkale.util.AnonymousMonoFutureFunction"); + monoFuncClass = (Class) Thread.currentThread().getContextClassLoader().loadClass("org.redkale.util.AnonymousMonoFutureFunction"); } catch (Throwable t) { } if (monoFuncClass == null) { @@ -117,7 +117,7 @@ public abstract class Flows { public final Class loadClass(String name, byte[] b) { return defineClass(name, b, 0, b.length); } - }.loadClass("org.re" + "dkale.util.AnonymousMonoFutureFunction", classBytes); + }.loadClass("org.redkale.util.AnonymousMonoFutureFunction", classBytes); RedkaleClassLoader.putDynClass(monoFuncClass.getName(), classBytes, monoFuncClass); } RedkaleClassLoader.putReflectionDeclaredConstructors(monoFuncClass, monoFuncClass.getName()); @@ -126,7 +126,7 @@ public abstract class Flows { reactorFluxClass0 = Thread.currentThread().getContextClassLoader().loadClass("reactor.core.publisher.Flux"); Class> fluxFuncClass = null; try { - fluxFuncClass = (Class) Thread.currentThread().getContextClassLoader().loadClass("org.re" + "dkale.util.AnonymousFluxFutureFunction"); + fluxFuncClass = (Class) Thread.currentThread().getContextClassLoader().loadClass("org.redkale.util.AnonymousFluxFutureFunction"); } catch (Throwable t) { } if (fluxFuncClass == null) { @@ -135,7 +135,7 @@ public abstract class Flows { public final Class loadClass(String name, byte[] b) { return defineClass(name, b, 0, b.length); } - }.loadClass("org.re" + "dkale.util.AnonymousFluxFutureFunction", classBytes); + }.loadClass("org.redkale.util.AnonymousFluxFutureFunction", classBytes); RedkaleClassLoader.putDynClass(fluxFuncClass.getName(), classBytes, fluxFuncClass); } RedkaleClassLoader.putReflectionDeclaredConstructors(fluxFuncClass, fluxFuncClass.getName()); diff --git a/src/main/java/org/redkale/util/Invoker.java b/src/main/java/org/redkale/util/Invoker.java index 55560e3e1..621cc3965 100644 --- a/src/main/java/org/redkale/util/Invoker.java +++ b/src/main/java/org/redkale/util/Invoker.java @@ -39,7 +39,7 @@ public interface Invoker { try { method = clazz.getMethod(methodName, paramTypes); } catch (Exception ex) { - throw new RuntimeException(ex); + throw new RedkaleException(ex); } return create(clazz, method); } @@ -221,7 +221,7 @@ public interface Invoker { RedkaleClassLoader.putReflectionDeclaredConstructors(resultClazz, newDynName.replace('/', '.')); return (Invoker) resultClazz.getDeclaredConstructor().newInstance(); } catch (Exception ex) { - throw new RuntimeException(ex); + throw new RedkaleException(ex); } } diff --git a/src/main/java/org/redkale/util/RedkaleClassLoader.java b/src/main/java/org/redkale/util/RedkaleClassLoader.java index daaae5561..1a775cd17 100644 --- a/src/main/java/org/redkale/util/RedkaleClassLoader.java +++ b/src/main/java/org/redkale/util/RedkaleClassLoader.java @@ -96,7 +96,7 @@ public class RedkaleClassLoader extends URLClassLoader { try { return new FileInputStream(f); } catch (FileNotFoundException e) { //几乎不会发生 - throw new RuntimeException(e); + throw new RedkaleException(e); } } } diff --git a/src/main/java/org/redkale/util/RedkaleException.java b/src/main/java/org/redkale/util/RedkaleException.java new file mode 100644 index 000000000..7ee210a60 --- /dev/null +++ b/src/main/java/org/redkale/util/RedkaleException.java @@ -0,0 +1,31 @@ +/* + * + */ +package org.redkale.util; + +/** + * redkale的异常基础类
+ *

+ * 详情见: https://redkale.org + * + * @author zhangjx + * @since 2.8.0 + */ +public class RedkaleException extends RuntimeException { + + public RedkaleException() { + super(); + } + + public RedkaleException(String s) { + super(s); + } + + public RedkaleException(String message, Throwable cause) { + super(message, cause); + } + + public RedkaleException(Throwable cause) { + super(cause); + } +} diff --git a/src/main/java/org/redkale/util/Reproduce.java b/src/main/java/org/redkale/util/Reproduce.java index 1ef81d079..7bb38b7c8 100644 --- a/src/main/java/org/redkale/util/Reproduce.java +++ b/src/main/java/org/redkale/util/Reproduce.java @@ -3,9 +3,9 @@ package org.redkale.util; import java.lang.reflect.Modifier; import java.util.Map; import java.util.function.*; -import static org.redkale.asm.Opcodes.*; -import org.redkale.asm.*; import static org.redkale.asm.ClassWriter.COMPUTE_FRAMES; +import org.redkale.asm.*; +import static org.redkale.asm.Opcodes.*; /** * JavaBean类对象的拷贝,相同的字段名会被拷贝
@@ -186,7 +186,7 @@ public interface Reproduce extends BiFunction { try { return (Reproduce) newClazz.getDeclaredConstructor().newInstance(); } catch (Exception ex) { - throw new RuntimeException(ex); + throw new RedkaleException(ex); } } diff --git a/src/main/java/org/redkale/util/ResourceFactory.java b/src/main/java/org/redkale/util/ResourceFactory.java index 746ec85a5..bb9007ce2 100644 --- a/src/main/java/org/redkale/util/ResourceFactory.java +++ b/src/main/java/org/redkale/util/ResourceFactory.java @@ -542,7 +542,7 @@ public final class ResourceFactory { checkResourceName(name); Class clz = TypeToken.typeToClass(clazz); if (clz != null && !clz.isPrimitive() && val != null && !clz.isAssignableFrom(val.getClass())) { - throw new RuntimeException(clz + "not isAssignableFrom (" + val + ") class " + val.getClass()); + throw new RedkaleException(clz + "not isAssignableFrom (" + val + ") class " + val.getClass()); } ConcurrentHashMap map = this.store.computeIfAbsent(clazz, k -> new ConcurrentHashMap()); ResourceEntry re = map.get(name); @@ -1023,7 +1023,7 @@ public final class ResourceFactory { return it == null ? findRegxTypeLoader(ft, field) : it; } - private static class ResourceInjectException extends RuntimeException { + private static class ResourceInjectException extends RedkaleException { public ResourceInjectException() { super(); @@ -1273,7 +1273,7 @@ public final class ResourceFactory { // } catch (RuntimeException ex) { // throw ex; // } catch (Exception e) { -// throw new RuntimeException(e); +// throw new RedkaleException(e); // } // } // } diff --git a/src/main/java/org/redkale/util/RetcodeException.java b/src/main/java/org/redkale/util/RetcodeException.java index fd18feb43..07a77b6e5 100644 --- a/src/main/java/org/redkale/util/RetcodeException.java +++ b/src/main/java/org/redkale/util/RetcodeException.java @@ -9,9 +9,9 @@ package org.redkale.util; * * @author zhangjx */ -public class RetcodeException extends RuntimeException { +public class RetcodeException extends RedkaleException { - private int retcode; + protected int retcode; public RetcodeException(int retcode) { this.retcode = retcode; diff --git a/src/main/java/org/redkale/util/TypeToken.java b/src/main/java/org/redkale/util/TypeToken.java index 2ebfd4d73..580fd4ab0 100644 --- a/src/main/java/org/redkale/util/TypeToken.java +++ b/src/main/java/org/redkale/util/TypeToken.java @@ -649,7 +649,7 @@ public abstract class TypeToken { try { return newClazz.getField("field").getGenericType(); } catch (Exception ex) { - throw new RuntimeException(ex); + throw new RedkaleException(ex); } } diff --git a/src/main/java/org/redkale/util/Utility.java b/src/main/java/org/redkale/util/Utility.java index 28c06732b..a14c15043 100644 --- a/src/main/java/org/redkale/util/Utility.java +++ b/src/main/java/org/redkale/util/Utility.java @@ -189,7 +189,7 @@ public final class Utility { // } // }}, null); // } catch (Exception e) { -// throw new RuntimeException(e); //不会发生 +// throw new RedkaleException(e); //不会发生 // } } @@ -2850,7 +2850,7 @@ public final class Utility { try { md5 = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException ex) { - throw new RuntimeException("Couldn't find a MD5 provider", ex); + throw new RedkaleException("Couldn't find a MD5 provider", ex); } return md5.digest(str.getBytes(StandardCharsets.UTF_8)); } @@ -2870,7 +2870,7 @@ public final class Utility { try { md5 = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException ex) { - throw new RuntimeException("Couldn't find a MD5 provider", ex); + throw new RedkaleException("Couldn't find a MD5 provider", ex); } return md5.digest(input); } @@ -2894,7 +2894,7 @@ public final class Utility { md5.update(input, offset, len); return md5.digest(); } catch (NoSuchAlgorithmException ex) { - throw new RuntimeException("Couldn't find a MD5 provider", ex); + throw new RedkaleException("Couldn't find a MD5 provider", ex); } } @@ -2972,7 +2972,7 @@ public final class Utility { try { digester = MessageDigest.getInstance("SHA-256"); } catch (NoSuchAlgorithmException ex) { - throw new RuntimeException("Couldn't find a SHA-256 provider", ex); + throw new RedkaleException("Couldn't find a SHA-256 provider", ex); } return digester.digest(str.getBytes(StandardCharsets.UTF_8)); } @@ -2992,7 +2992,7 @@ public final class Utility { try { digester = MessageDigest.getInstance("SHA-256"); } catch (NoSuchAlgorithmException ex) { - throw new RuntimeException(ex); + throw new RedkaleException(ex); } return digester.digest(input); } @@ -3016,7 +3016,7 @@ public final class Utility { digester.update(input, offset, len); return digester.digest(); } catch (Exception ex) { - throw new RuntimeException(ex); + throw new RedkaleException(ex); } } @@ -3127,7 +3127,7 @@ public final class Utility { mac.init(new SecretKeySpec(key, "HmacSHA1")); return mac.doFinal(input); } catch (InvalidKeyException | NoSuchAlgorithmException ex) { - throw new RuntimeException(ex); + throw new RedkaleException(ex); } } @@ -3151,7 +3151,7 @@ public final class Utility { mac.update(input, offset, len); return mac.doFinal(); } catch (InvalidKeyException | NoSuchAlgorithmException ex) { - throw new RuntimeException(ex); + throw new RedkaleException(ex); } } @@ -3262,7 +3262,7 @@ public final class Utility { mac.init(new SecretKeySpec(key, "HmacSHA256")); return mac.doFinal(input); } catch (InvalidKeyException | NoSuchAlgorithmException ex) { - throw new RuntimeException(ex); + throw new RedkaleException(ex); } } @@ -3286,7 +3286,7 @@ public final class Utility { mac.update(input, offset, len); return mac.doFinal(); } catch (InvalidKeyException | NoSuchAlgorithmException ex) { - throw new RuntimeException(ex); + throw new RedkaleException(ex); } } @@ -3398,7 +3398,7 @@ public final class Utility { mac.init(sk); return mac.doFinal(input); } catch (InvalidKeyException | NoSuchAlgorithmException ex) { - throw new RuntimeException(ex); + throw new RedkaleException(ex); } } @@ -3423,7 +3423,7 @@ public final class Utility { mac.update(input, offset, len); return mac.doFinal(); } catch (InvalidKeyException | NoSuchAlgorithmException ex) { - throw new RuntimeException(ex); + throw new RedkaleException(ex); } } @@ -3440,7 +3440,7 @@ public final class Utility { MessageDigest digest = MessageDigest.getInstance(algorithm.toUpperCase()); return digest.digest(input); } catch (NoSuchAlgorithmException ex) { - throw new RuntimeException("Couldn't find a " + algorithm + " provider", ex); + throw new RedkaleException("Couldn't find a " + algorithm + " provider", ex); } } @@ -3460,7 +3460,7 @@ public final class Utility { digest.update(input, offset, length); return digest.digest(); } catch (NoSuchAlgorithmException ex) { - throw new RuntimeException("Couldn't find a " + algorithm + " provider", ex); + throw new RedkaleException("Couldn't find a " + algorithm + " provider", ex); } }