Compare commits
111 Commits
2.0.0.beta
...
2.0.0.rc3
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8b69e7d02b | ||
|
|
02a10bf014 | ||
|
|
004b83172e | ||
|
|
fee4555cef | ||
|
|
758bd7de72 | ||
|
|
b2dd366640 | ||
|
|
934c82eadd | ||
|
|
c7ed6574cc | ||
|
|
2ea2667fa7 | ||
|
|
34ae2d38c5 | ||
|
|
a1c95544cb | ||
|
|
c6dc38c35c | ||
|
|
39203ab598 | ||
|
|
51a95a84aa | ||
|
|
8a8d45e642 | ||
|
|
52eb7dbc0c | ||
|
|
0e14b60f12 | ||
|
|
d373ab7204 | ||
|
|
4f9a563ba7 | ||
|
|
852da19b1e | ||
|
|
ddfc040a53 | ||
|
|
df3ccb763a | ||
|
|
f42561ca93 | ||
|
|
580e28519a | ||
|
|
9ecc1d8f19 | ||
|
|
40629ed7b9 | ||
|
|
5790135add | ||
|
|
fd862ed6c6 | ||
|
|
33763af96c | ||
|
|
7c05df3cfb | ||
|
|
f471e2d4c5 | ||
|
|
d4fd093521 | ||
|
|
40003c7789 | ||
|
|
b94f99f338 | ||
|
|
bd21644571 | ||
|
|
5f3599d9b8 | ||
|
|
1e4a30bd70 | ||
|
|
e7dc5de9f2 | ||
|
|
ccb9cb28f5 | ||
|
|
4d9b72c922 | ||
|
|
a51ae13a39 | ||
|
|
dfca186688 | ||
|
|
fadd229a89 | ||
|
|
7acc69adc4 | ||
|
|
d88e4120a1 | ||
|
|
ef98edd91a | ||
|
|
f4548bbe34 | ||
|
|
11a5faca1d | ||
|
|
c37b0e8cb5 | ||
|
|
a20570a6eb | ||
|
|
5e3edb7e1d | ||
|
|
ad8f1d2da6 | ||
|
|
24b23c894f | ||
|
|
c551d5fb81 | ||
|
|
fba43894c1 | ||
|
|
22cc7e086c | ||
|
|
1791008729 | ||
|
|
90e15dd253 | ||
|
|
7db73c076c | ||
|
|
95ad6e99d9 | ||
|
|
0b2a5d0f61 | ||
|
|
b7acce0814 | ||
|
|
8744e76cad | ||
|
|
446b3c13dc | ||
|
|
3951e28148 | ||
|
|
b74d679608 | ||
|
|
edbc878b73 | ||
|
|
f706209ec1 | ||
|
|
bf000b188f | ||
|
|
def1736a9b | ||
|
|
0242f4c0c3 | ||
|
|
5cd399b2df | ||
|
|
9ddb662016 | ||
|
|
2947275d54 | ||
|
|
e43f814872 | ||
|
|
6e16f52e28 | ||
|
|
fca13557df | ||
|
|
824a6df55a | ||
|
|
b98b526c50 | ||
|
|
8f6aa4f4a5 | ||
|
|
00a07a79b2 | ||
|
|
264dfbef2e | ||
|
|
2e27814809 | ||
|
|
f767f40e56 | ||
|
|
543ecc071a | ||
|
|
2a14f39495 | ||
|
|
95c8ae2334 | ||
|
|
338ea13828 | ||
|
|
4a8b9e5fec | ||
|
|
e281cac3d3 | ||
|
|
a495829a3c | ||
|
|
b0deed2a89 | ||
|
|
70a75abf74 | ||
|
|
1aa97f8e79 | ||
|
|
b4000235ac | ||
|
|
4b93f29a1c | ||
|
|
4892a50670 | ||
|
|
2fe0ac0ef9 | ||
|
|
333ae72148 | ||
|
|
5fd5b7f303 | ||
|
|
528cf45f3f | ||
|
|
c7308e7320 | ||
|
|
e2a49eaab7 | ||
|
|
44bd6f235c | ||
|
|
601d15b513 | ||
|
|
9e93485a97 | ||
|
|
ad87b2115d | ||
|
|
27a587d31f | ||
|
|
fc8fa27602 | ||
|
|
f9aebc8ee3 | ||
|
|
1167da8f4c |
@@ -1 +0,0 @@
|
||||
<EFBFBD>Լ<EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><EFBFBD>jarĬ<EFBFBD>Ϸ<EFBFBD><EFBFBD>ڴ˴<EFBFBD>
|
||||
@@ -16,10 +16,6 @@
|
||||
<directory>${project.basedir}/conf</directory>
|
||||
<outputDirectory>conf</outputDirectory>
|
||||
</fileSet>
|
||||
<fileSet>
|
||||
<directory>${project.basedir}/libs</directory>
|
||||
<outputDirectory>libs</outputDirectory>
|
||||
</fileSet>
|
||||
<fileSet>
|
||||
<directory>${project.basedir}/logs</directory>
|
||||
<outputDirectory>logs</outputDirectory>
|
||||
|
||||
@@ -77,6 +77,7 @@
|
||||
<version>2.6</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<addMavenDescriptor>false</addMavenDescriptor>
|
||||
<manifest>
|
||||
<mainClass>org.redkale.boot.Application</mainClass>
|
||||
</manifest>
|
||||
|
||||
@@ -116,7 +116,7 @@
|
||||
excludelibs: 排除lib.path与excludes中的正则表达式匹配的路径, 多个正则表达式用分号;隔开
|
||||
charset: 文本编码, 默认: UTF-8
|
||||
backlog: 默认10K
|
||||
threads: 线程数, 默认: CPU核数*32
|
||||
threads: 线程数, 默认: CPU核数*2,最小8个
|
||||
maxconns:最大连接数, 小于1表示无限制, 默认: 0
|
||||
maxbody: request.body最大值, 默认: 64K
|
||||
bufferCapacity: ByteBuffer的初始化大小, TCP默认: 32K; (HTTP 2.0、WebSocket,必须要16k以上); UDP默认: 1350B
|
||||
|
||||
@@ -1,63 +0,0 @@
|
||||
/** *****************************************************************************
|
||||
* Copyright (c) 2008 - 2013 Oracle Corporation. All rights reserved.
|
||||
*
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
|
||||
* which accompanies this distribution.
|
||||
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Linda DeMichiel - Java Persistence 2.1
|
||||
* Linda DeMichiel - Java Persistence 2.0
|
||||
*
|
||||
***************************************************************************** */
|
||||
package javax.persistence;
|
||||
|
||||
import java.lang.annotation.Target;
|
||||
import java.lang.annotation.Retention;
|
||||
import static java.lang.annotation.ElementType.FIELD;
|
||||
import static java.lang.annotation.ElementType.METHOD;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
/**
|
||||
* Provides for the specification of generation strategies for the
|
||||
* values of primary keys.
|
||||
*
|
||||
* <p>
|
||||
* The <code>GeneratedValue</code> annotation
|
||||
* may be applied to a primary key property or field of an entity or
|
||||
* mapped superclass in conjunction with the {@link Id} annotation.
|
||||
* The use of the <code>GeneratedValue</code> annotation is only
|
||||
* required to be supported for simple primary keys. Use of the
|
||||
* <code>GeneratedValue</code> annotation is not supported for derived
|
||||
* primary keys.
|
||||
*
|
||||
* <pre>
|
||||
*
|
||||
* Example 1:
|
||||
*
|
||||
* @Id
|
||||
* @GeneratedValue(strategy=SEQUENCE, generator="CUST_SEQ")
|
||||
* @Column(name="CUST_ID")
|
||||
* public Long getId() { return id; }
|
||||
*
|
||||
* Example 2:
|
||||
*
|
||||
* @Id
|
||||
* @GeneratedValue(strategy=TABLE, generator="CUST_GEN")
|
||||
* @Column(name="CUST_ID")
|
||||
* Long id;
|
||||
* </pre>
|
||||
*
|
||||
* @see Id
|
||||
*
|
||||
* @since Java Persistence 1.0
|
||||
*/
|
||||
@Target({METHOD, FIELD})
|
||||
@Retention(RUNTIME)
|
||||
|
||||
public @interface GeneratedValue {
|
||||
|
||||
}
|
||||
@@ -45,7 +45,7 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
* </pre>
|
||||
*
|
||||
* @see Column
|
||||
* @see GeneratedValue
|
||||
* see GeneratedValue
|
||||
*
|
||||
* @since Java Persistence 1.0
|
||||
*/
|
||||
|
||||
@@ -30,5 +30,7 @@ module org.redkale {
|
||||
exports org.redkale.util;
|
||||
exports org.redkale.watch;
|
||||
|
||||
uses org.redkale.source.SourceLoader;
|
||||
uses org.redkale.util.ResourceInjectLoader;
|
||||
}
|
||||
*/
|
||||
@@ -196,7 +196,7 @@ public final class ApiDocsService {
|
||||
final FileOutputStream out = new FileOutputStream(new File(app.getHome(), "apidoc.json"));
|
||||
out.write(json.getBytes("UTF-8"));
|
||||
out.close();
|
||||
File doctemplate = new File(app.getConfPath(), "apidoc-template.html");
|
||||
File doctemplate = new File(app.getConfPath().toString(), "apidoc-template.html");
|
||||
InputStream in = null;
|
||||
if (doctemplate.isFile() && doctemplate.canRead()) {
|
||||
in = new FileInputStream(doctemplate);
|
||||
|
||||
@@ -57,12 +57,12 @@ public final class Application {
|
||||
public static final String RESNAME_APP_TIME = "APP_TIME";
|
||||
|
||||
/**
|
||||
* 当前进程的根目录, 类型:String、File、Path
|
||||
* 当前进程的根目录, 类型:String、File、Path、URI
|
||||
*/
|
||||
public static final String RESNAME_APP_HOME = "APP_HOME";
|
||||
|
||||
/**
|
||||
* 当前进程的配置目录,如果不是绝对路径则视为HOME目录下的相对路径 类型:String、File、Path
|
||||
* 当前进程的配置目录,如果不是绝对路径则视为HOME目录下的相对路径 类型:String、File、Path、URI
|
||||
*/
|
||||
public static final String RESNAME_APP_CONF = "APP_CONF";
|
||||
|
||||
@@ -143,7 +143,7 @@ public final class Application {
|
||||
private final File home;
|
||||
|
||||
//配置文件目录
|
||||
private final File confPath;
|
||||
private final URI confPath;
|
||||
|
||||
//日志
|
||||
private final Logger logger;
|
||||
@@ -176,16 +176,19 @@ public final class Application {
|
||||
this.resourceFactory.register(RESNAME_APP_TIME, long.class, this.startTime);
|
||||
this.resourceFactory.register(RESNAME_APP_HOME, Path.class, root.toPath());
|
||||
this.resourceFactory.register(RESNAME_APP_HOME, File.class, root);
|
||||
this.resourceFactory.register(RESNAME_APP_HOME, URI.class, root.toURI());
|
||||
try {
|
||||
this.resourceFactory.register(RESNAME_APP_HOME, root.getCanonicalPath());
|
||||
this.home = root.getCanonicalFile();
|
||||
String confsubpath = System.getProperty(RESNAME_APP_CONF, "conf");
|
||||
if (confsubpath.charAt(0) == '/' || confsubpath.indexOf(':') > 0) {
|
||||
this.confPath = new File(confsubpath).getCanonicalFile();
|
||||
if (confsubpath.contains("://")) {
|
||||
this.confPath = new URI(confsubpath);
|
||||
} else if (confsubpath.charAt(0) == '/' || confsubpath.indexOf(':') > 0) {
|
||||
this.confPath = new File(confsubpath).getCanonicalFile().toURI();
|
||||
} else {
|
||||
this.confPath = new File(this.home, confsubpath).getCanonicalFile();
|
||||
this.confPath = new File(this.home, confsubpath).getCanonicalFile().toURI();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
String localaddr = config.getValue("address", "").trim();
|
||||
@@ -209,11 +212,12 @@ public final class Application {
|
||||
System.setProperty(RESNAME_APP_NODE, node);
|
||||
}
|
||||
//以下是初始化日志配置
|
||||
final File logconf = new File(confPath, "logging.properties");
|
||||
if (logconf.isFile() && logconf.canRead()) {
|
||||
final URI logConfURI = "file".equals(confPath.getScheme()) ? new File(new File(confPath), "logging.properties").toURI()
|
||||
: URI.create(confPath.toString() + (confPath.toString().endsWith("/") ? "" : "/") + "logging.properties");
|
||||
if (!"file".equals(confPath.getScheme()) || (new File(logConfURI).isFile() && new File(logConfURI).canRead())) {
|
||||
try {
|
||||
final String rootpath = root.getCanonicalPath().replace('\\', '/');
|
||||
FileInputStream fin = new FileInputStream(logconf);
|
||||
InputStream fin = logConfURI.toURL().openStream();
|
||||
Properties properties = new Properties();
|
||||
properties.load(fin);
|
||||
fin.close();
|
||||
@@ -301,7 +305,7 @@ public final class Application {
|
||||
transportExec = Executors.newFixedThreadPool(threads, (Runnable r) -> {
|
||||
Thread t = new Thread(r);
|
||||
t.setDaemon(true);
|
||||
t.setName("Transport-Thread-" + counter.incrementAndGet());
|
||||
t.setName("Redkale-Transport-Thread-" + counter.incrementAndGet());
|
||||
return t;
|
||||
});
|
||||
transportGroup = AsynchronousChannelGroup.withCachedThreadPool(transportExec, 1);
|
||||
@@ -316,7 +320,7 @@ public final class Application {
|
||||
transportExec = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 8, (Runnable r) -> {
|
||||
Thread t = new Thread(r);
|
||||
t.setDaemon(true);
|
||||
t.setName("Transport-Thread-" + counter.incrementAndGet());
|
||||
t.setName("Redkale-Transport-Thread-" + counter.incrementAndGet());
|
||||
return t;
|
||||
});
|
||||
try {
|
||||
@@ -375,7 +379,7 @@ public final class Application {
|
||||
return home;
|
||||
}
|
||||
|
||||
public File getConfPath() {
|
||||
public URI getConfPath() {
|
||||
return confPath;
|
||||
}
|
||||
|
||||
@@ -398,10 +402,14 @@ public final class Application {
|
||||
System.setProperty("convert.bson.writer.buffer.defsize", "4096");
|
||||
System.setProperty("convert.json.writer.buffer.defsize", "4096");
|
||||
|
||||
File persist = new File(this.confPath, "persistence.xml");
|
||||
final String confpath = this.confPath.toString();
|
||||
final String homepath = this.home.getCanonicalPath();
|
||||
final String confpath = this.confPath.getCanonicalPath();
|
||||
if (persist.isFile()) System.setProperty(DataSources.DATASOURCE_CONFPATH, persist.getCanonicalPath());
|
||||
if ("file".equals(this.confPath.getScheme())) {
|
||||
File persist = new File(new File(confPath), "persistence.xml");
|
||||
if (persist.isFile()) System.setProperty(DataSources.DATASOURCE_CONFPATH, persist.getCanonicalPath());
|
||||
} else {
|
||||
System.setProperty(DataSources.DATASOURCE_CONFPATH, confpath + (confpath.endsWith("/") ? "" : "/") + "persistence.xml");
|
||||
}
|
||||
String pidstr = "";
|
||||
try { //JDK 9+
|
||||
Class phclass = Class.forName("java.lang.ProcessHandle");
|
||||
@@ -425,13 +433,17 @@ public final class Application {
|
||||
if (dfloads != null) {
|
||||
for (String dfload : dfloads.split(";")) {
|
||||
if (dfload.trim().isEmpty()) continue;
|
||||
final File df = (dfload.indexOf('/') < 0) ? new File(confPath, "/" + dfload) : new File(dfload);
|
||||
if (df.isFile()) {
|
||||
final URI df = (dfload.indexOf('/') < 0) ? URI.create(confpath + (confpath.endsWith("/") ? "" : "/") + dfload) : new File(dfload).toURI();
|
||||
if (!"file".equals(df.getScheme()) || new File(df).isFile()) {
|
||||
Properties ps = new Properties();
|
||||
InputStream in = new FileInputStream(df);
|
||||
ps.load(in);
|
||||
in.close();
|
||||
ps.forEach((x, y) -> resourceFactory.register("property." + x, y.toString().replace("${APP_HOME}", homepath)));
|
||||
try {
|
||||
InputStream in = df.toURL().openStream();
|
||||
ps.load(in);
|
||||
in.close();
|
||||
ps.forEach((x, y) -> resourceFactory.register("property." + x, y.toString().replace("${APP_HOME}", homepath)));
|
||||
} catch (Exception e) {
|
||||
logger.log(Level.WARNING, "load properties(" + dfload + ") error", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -558,9 +570,10 @@ public final class Application {
|
||||
}
|
||||
|
||||
public void restoreConfig() throws IOException {
|
||||
if (!"file".equals(this.confPath.getScheme())) return;
|
||||
synchronized (this) {
|
||||
File confFile = new File(this.confPath, "application.xml");
|
||||
confFile.renameTo(new File(this.confPath, "application_" + String.format("%1$tY%1$tm%1$td%1$tH%1$tM%1$tS", System.currentTimeMillis()) + ".xml"));
|
||||
File confFile = new File(this.confPath.toString(), "application.xml");
|
||||
confFile.renameTo(new File(this.confPath.toString(), "application_" + String.format("%1$tY%1$tm%1$td%1$tH%1$tM%1$tS", System.currentTimeMillis()) + ".xml"));
|
||||
final PrintStream ps = new PrintStream(new FileOutputStream(confFile));
|
||||
ps.append(config.toXML("application"));
|
||||
ps.close();
|
||||
@@ -571,7 +584,7 @@ public final class Application {
|
||||
final Application application = this;
|
||||
new Thread() {
|
||||
{
|
||||
setName("Application-Control-Thread");
|
||||
setName("Redkale-Application-SelfServer-Thread");
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -742,7 +755,7 @@ public final class Application {
|
||||
Thread thread = new Thread() {
|
||||
{
|
||||
String host = serconf.getValue("host", "0.0.0.0").replace("0.0.0.0", "*");
|
||||
setName(serconf.getValue("protocol", "Server").toUpperCase() + "-" + host + ":" + serconf.getIntValue("port") + "-Thread");
|
||||
setName("Redkale-" + serconf.getValue("protocol", "Server").toUpperCase() + "-" + host + ":" + serconf.getIntValue("port") + "-Thread");
|
||||
this.setDaemon(true);
|
||||
}
|
||||
|
||||
@@ -843,18 +856,20 @@ public final class Application {
|
||||
final String home = new File(System.getProperty(RESNAME_APP_HOME, "")).getCanonicalPath().replace('\\', '/');
|
||||
System.setProperty(RESNAME_APP_HOME, home);
|
||||
String confsubpath = System.getProperty(RESNAME_APP_CONF, "conf");
|
||||
File appfile;
|
||||
if (confsubpath.charAt(0) == '/' || confsubpath.indexOf(':') > 0) {
|
||||
appfile = new File(confsubpath).getCanonicalFile();
|
||||
URI appconf;
|
||||
if (confsubpath.contains("://")) {
|
||||
appconf = URI.create(confsubpath + (confsubpath.endsWith("/") ? "" : "/") + "application.xml");
|
||||
} else if (confsubpath.charAt(0) == '/' || confsubpath.indexOf(':') > 0) {
|
||||
appconf = new File(confsubpath, "application.xml").toURI();
|
||||
} else {
|
||||
appfile = new File(new File(home), confsubpath);
|
||||
appconf = new File(new File(home, confsubpath), "application.xml").toURI();
|
||||
}
|
||||
File appconf = new File(appfile, "application.xml");
|
||||
return new Application(singleton, load(new FileInputStream(appconf)));
|
||||
return new Application(singleton, load(appconf.toURL().openStream()));
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
Utility.midnight(); //先初始化一下Utility
|
||||
Thread.currentThread().setName("Redkale-Application-Main-Thread");
|
||||
//运行主程序
|
||||
final Application application = Application.create(false);
|
||||
if (System.getProperty("CMD") != null) {
|
||||
|
||||
@@ -133,7 +133,7 @@ public class LogFileHandler extends Handler {
|
||||
}
|
||||
|
||||
private void open() {
|
||||
final String name = "Logging-" + getClass().getSimpleName() + "-Thread";
|
||||
final String name = "Redkale-Logging-" + getClass().getSimpleName() + "-Thread";
|
||||
new Thread() {
|
||||
{
|
||||
setName(name);
|
||||
|
||||
@@ -35,7 +35,7 @@ public class FilterWatchService extends AbstractWatchService {
|
||||
@Resource
|
||||
protected Application application;
|
||||
|
||||
@RestMapping(name = "addfilter", auth = false, comment = "动态增加Filter")
|
||||
@RestMapping(name = "addFilter", auth = false, comment = "动态增加Filter")
|
||||
public RetResult addFilter(@RestUploadFile(maxLength = 10 * 1024 * 1024, fileNameReg = "\\.jar$") byte[] jar,
|
||||
@RestParam(name = "server", comment = "Server节点名") final String serverName,
|
||||
@RestParam(name = "type", comment = "Filter类名") final String filterType) throws IOException {
|
||||
|
||||
@@ -50,7 +50,7 @@ public class ServerWatchService extends AbstractWatchService {
|
||||
return new RetResult(rs);
|
||||
}
|
||||
|
||||
@RestMapping(name = "changeaddress", comment = "更改Server的监听地址和端口")
|
||||
@RestMapping(name = "changeAddress", comment = "更改Server的监听地址和端口")
|
||||
public RetResult changeAddress(@RestParam(name = "#port:") final int oldport,
|
||||
@RestParam(name = "#newhost:") final String newhost, @RestParam(name = "#newport:") final int newport) {
|
||||
if (oldport < 1) return new RetResult(RET_WATCH_PARAMS_ILLEGAL, "not found param `oldport`");
|
||||
|
||||
@@ -30,8 +30,8 @@ public class ServiceWatchService extends AbstractWatchService {
|
||||
protected Application application;
|
||||
|
||||
@RestConvert(type = void.class)
|
||||
@RestMapping(name = "setfield", auth = false, comment = "设置Service中指定字段的内容")
|
||||
public RetResult setfield(@RestParam(name = "name", comment = "Service的资源名") String name,
|
||||
@RestMapping(name = "setField", auth = false, comment = "设置Service中指定字段的内容")
|
||||
public RetResult setField(@RestParam(name = "name", comment = "Service的资源名") String name,
|
||||
@RestParam(name = "type", comment = "Service的类名") String type,
|
||||
@RestParam(name = "field", comment = "字段名") String field,
|
||||
@RestParam(name = "value", comment = "字段值") String value) {
|
||||
@@ -65,8 +65,8 @@ public class ServiceWatchService extends AbstractWatchService {
|
||||
}
|
||||
|
||||
@RestConvert(type = void.class)
|
||||
@RestMapping(name = "getfield", auth = false, comment = "查询Service中指定字段的内容")
|
||||
public RetResult getfield(@RestParam(name = "name", comment = "Service的资源名") String name,
|
||||
@RestMapping(name = "getField", auth = false, comment = "查询Service中指定字段的内容")
|
||||
public RetResult getField(@RestParam(name = "name", comment = "Service的资源名") String name,
|
||||
@RestParam(name = "type", comment = "Service的类名") String type,
|
||||
@RestParam(name = "field", comment = "字段名") String field) {
|
||||
if (name == null) name = "";
|
||||
@@ -98,8 +98,8 @@ public class ServiceWatchService extends AbstractWatchService {
|
||||
}
|
||||
|
||||
@RestConvert(type = void.class)
|
||||
@RestMapping(name = "runmethod", auth = false, comment = "调用Service中指定方法")
|
||||
public RetResult runmethod(@RestParam(name = "name", comment = "Service的资源名") String name,
|
||||
@RestMapping(name = "runMethod", auth = false, comment = "调用Service中指定方法")
|
||||
public RetResult runMethod(@RestParam(name = "name", comment = "Service的资源名") String name,
|
||||
@RestParam(name = "type", comment = "Service的类名") String type,
|
||||
@RestParam(name = "method", comment = "Service的方法名") String method,
|
||||
@RestParam(name = "params", comment = "方法的参数值") List<String> params,
|
||||
@@ -169,28 +169,28 @@ public class ServiceWatchService extends AbstractWatchService {
|
||||
return dest;
|
||||
}
|
||||
|
||||
@RestMapping(name = "load", auth = false, comment = "动态增加Service")
|
||||
@RestMapping(name = "loadService", auth = false, comment = "动态增加Service")
|
||||
public RetResult loadService(@RestParam(name = "type", comment = "Service的类名") String type,
|
||||
@RestUploadFile(maxLength = 10 * 1024 * 1024, fileNameReg = "\\.jar$") byte[] jar) {
|
||||
//待开发
|
||||
return RetResult.success();
|
||||
}
|
||||
|
||||
@RestMapping(name = "reload", auth = false, comment = "重新加载Service")
|
||||
@RestMapping(name = "reloadService", auth = false, comment = "重新加载Service")
|
||||
public RetResult reloadService(@RestParam(name = "name", comment = "Service的资源名") String name,
|
||||
@RestParam(name = "type", comment = "Service的类名") String type) {
|
||||
//待开发
|
||||
return RetResult.success();
|
||||
}
|
||||
|
||||
@RestMapping(name = "stop", auth = false, comment = "动态停止Service")
|
||||
@RestMapping(name = "stopService", auth = false, comment = "动态停止Service")
|
||||
public RetResult stopService(@RestParam(name = "name", comment = "Service的资源名") String name,
|
||||
@RestParam(name = "type", comment = "Service的类名") String type) {
|
||||
//待开发
|
||||
return RetResult.success();
|
||||
}
|
||||
|
||||
@RestMapping(name = "find", auth = false, comment = "查找Service")
|
||||
@RestMapping(name = "findService", auth = false, comment = "查找Service")
|
||||
public RetResult find(@RestParam(name = "name", comment = "Service的资源名") String name,
|
||||
@RestParam(name = "type", comment = "Service的类名") String type) {
|
||||
//待开发
|
||||
|
||||
@@ -25,13 +25,13 @@ public class ServletWatchService extends AbstractWatchService {
|
||||
@Resource
|
||||
protected TransportFactory transportFactory;
|
||||
//
|
||||
// @RestMapping(name = "load", auth = false, comment = "动态增加Servlet")
|
||||
// @RestMapping(name = "loadServlet", auth = false, comment = "动态增加Servlet")
|
||||
// public RetResult loadServlet(String type, @RestUploadFile(maxLength = 10 * 1024 * 1024, fileNameReg = "\\.jar$") byte[] jar) {
|
||||
// //待开发
|
||||
// return RetResult.success();
|
||||
// }
|
||||
//
|
||||
// @RestMapping(name = "stop", auth = false, comment = "动态停止Servlet")
|
||||
// @RestMapping(name = "stopServlet", auth = false, comment = "动态停止Servlet")
|
||||
// public RetResult stopServlet(String type) {
|
||||
// //待开发
|
||||
// return RetResult.success();
|
||||
|
||||
@@ -7,7 +7,8 @@ package org.redkale.convert;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.function.*;
|
||||
import org.redkale.util.Attribute;
|
||||
|
||||
/**
|
||||
* 序列化/反序列化操作类
|
||||
@@ -31,6 +32,20 @@ public abstract class Convert<R extends Reader, W extends Writer> {
|
||||
return this.factory;
|
||||
}
|
||||
|
||||
protected <S extends W> S configWrite(S writer) {
|
||||
return writer;
|
||||
}
|
||||
|
||||
protected <S extends W> S fieldFunc(S writer, BiFunction<Attribute, Object, Object> objFieldFunc, Function<Object, ConvertField[]> objExtFunc) {
|
||||
writer.objFieldFunc = objFieldFunc;
|
||||
writer.objExtFunc = objExtFunc;
|
||||
return writer;
|
||||
}
|
||||
|
||||
public abstract Convert<R, W> newConvert(final BiFunction<Attribute, Object, Object> objFieldFunc);
|
||||
|
||||
public abstract Convert<R, W> newConvert(final BiFunction<Attribute, Object, Object> objFieldFunc, Function<Object, ConvertField[]> objExtFunc);
|
||||
|
||||
public abstract boolean isBinary();
|
||||
|
||||
public abstract <T> T convertFrom(final Type type, final byte[] bytes);
|
||||
|
||||
@@ -91,8 +91,9 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
||||
|
||||
this.register(Number.class, NumberSimpledCoder.instance);
|
||||
this.register(String.class, StringSimpledCoder.instance);
|
||||
this.register(StringConvertWrapper.class, StringConvertWrapperSimpledCoder.instance);
|
||||
this.register(StringWrapper.class, StringWrapperSimpledCoder.instance);
|
||||
this.register(CharSequence.class, CharSequenceSimpledCoder.instance);
|
||||
this.register(StringBuilder.class, CharSequenceSimpledCoder.StringBuilderSimpledCoder.instance);
|
||||
this.register(java.util.Date.class, DateSimpledCoder.instance);
|
||||
this.register(java.time.Duration.class, DurationSimpledCoder.instance);
|
||||
this.register(AtomicInteger.class, AtomicIntegerSimpledCoder.instance);
|
||||
@@ -132,6 +133,54 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
||||
}
|
||||
|
||||
});
|
||||
try {
|
||||
Class sqldateClass = Class.forName("java.sql.Date");
|
||||
this.register(sqldateClass, new SimpledCoder<R, W, java.sql.Date>() {
|
||||
|
||||
@Override
|
||||
public void convertTo(W out, java.sql.Date value) {
|
||||
out.writeSmallString(value == null ? null : value.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public java.sql.Date convertFrom(R in) {
|
||||
String t = in.readSmallString();
|
||||
return t == null ? null : java.sql.Date.valueOf(t);
|
||||
}
|
||||
|
||||
});
|
||||
Class sqltimeClass = Class.forName("java.sql.Time");
|
||||
this.register(sqltimeClass, new SimpledCoder<R, W, java.sql.Time>() {
|
||||
|
||||
@Override
|
||||
public void convertTo(W out, java.sql.Time value) {
|
||||
out.writeSmallString(value == null ? null : value.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public java.sql.Time convertFrom(R in) {
|
||||
String t = in.readSmallString();
|
||||
return t == null ? null : java.sql.Time.valueOf(t);
|
||||
}
|
||||
|
||||
});
|
||||
Class timestampClass = Class.forName("java.sql.Timestamp");
|
||||
this.register(timestampClass, new SimpledCoder<R, W, java.sql.Timestamp>() {
|
||||
|
||||
@Override
|
||||
public void convertTo(W out, java.sql.Timestamp value) {
|
||||
out.writeSmallString(value == null ? null : value.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public java.sql.Timestamp convertFrom(R in) {
|
||||
String t = in.readSmallString();
|
||||
return t == null ? null : java.sql.Timestamp.valueOf(t);
|
||||
}
|
||||
|
||||
});
|
||||
} catch (Throwable t) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -433,25 +482,32 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
||||
|
||||
public final boolean register(final Class type, String column, ConvertColumnEntry entry) {
|
||||
if (type == null || column == null || entry == null) return false;
|
||||
Field field = null;
|
||||
try {
|
||||
final Field field = type.getDeclaredField(column);
|
||||
String get = "get";
|
||||
if (field.getType() == boolean.class || field.getType() == Boolean.class) get = "is";
|
||||
char[] cols = column.toCharArray();
|
||||
cols[0] = Character.toUpperCase(cols[0]);
|
||||
String col2 = new String(cols);
|
||||
try {
|
||||
register(type.getMethod(get + col2), entry);
|
||||
} catch (Exception ex) {
|
||||
}
|
||||
try {
|
||||
register(type.getMethod("set" + col2, field.getType()), entry);
|
||||
} catch (Exception ex) {
|
||||
}
|
||||
return register(field, entry);
|
||||
field = type.getDeclaredField(column);
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
String get = "get";
|
||||
if (field != null && (field.getType() == boolean.class || field.getType() == Boolean.class)) get = "is";
|
||||
char[] cols = column.toCharArray();
|
||||
cols[0] = Character.toUpperCase(cols[0]);
|
||||
final String bigColumn = new String(cols);
|
||||
try {
|
||||
register(type.getMethod(get + bigColumn), entry);
|
||||
} catch (NoSuchMethodException mex) {
|
||||
if (get.length() >= 3) { //get
|
||||
try {
|
||||
register(type.getMethod("is" + bigColumn), entry);
|
||||
} catch (Exception ex) {
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
}
|
||||
try {
|
||||
register(type.getMethod("set" + bigColumn, field.getType()), entry);
|
||||
} catch (Exception ex) {
|
||||
}
|
||||
return field == null ? true : register(field, entry);
|
||||
}
|
||||
|
||||
public final <E> boolean register(final AccessibleObject field, final ConvertColumnEntry entry) {
|
||||
@@ -703,7 +759,8 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
||||
encoder = new OptionalCoder(this, type);
|
||||
} else if (clazz == Object.class) {
|
||||
return (Encodeable<W, E>) this.anyEncoder;
|
||||
} else if (!clazz.getName().startsWith("java.") || java.net.HttpCookie.class == clazz || java.util.AbstractMap.SimpleEntry.class == clazz) {
|
||||
} else if (!clazz.getName().startsWith("java.") || java.net.HttpCookie.class == clazz
|
||||
|| java.util.Map.Entry.class == clazz || java.util.AbstractMap.SimpleEntry.class == clazz) {
|
||||
Encodeable simpleCoder = null;
|
||||
for (final Method method : clazz.getDeclaredMethods()) {
|
||||
if (!Modifier.isStatic(method.getModifiers())) continue;
|
||||
|
||||
102
src/org/redkale/convert/ConvertField.java
Normal file
102
src/org/redkale/convert/ConvertField.java
Normal file
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.convert;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Type;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
|
||||
/**
|
||||
* newConvert参数中的Function返回结果的数据类
|
||||
*
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public class ConvertField implements Serializable {
|
||||
|
||||
protected String name;
|
||||
|
||||
protected Type type;
|
||||
|
||||
protected int position;
|
||||
|
||||
protected Object value;
|
||||
|
||||
public ConvertField() {
|
||||
}
|
||||
|
||||
public ConvertField(String name, Object value) {
|
||||
this.name = name;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public ConvertField(String name, int position, Object value) {
|
||||
this.name = name;
|
||||
this.position = position;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public ConvertField(String name, Type type, Object value) {
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public ConvertField(String name, Type type, int position, Object value) {
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
this.position = position;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public static ConvertField[] ofArray(Object... items) {
|
||||
int len = items.length / 2;
|
||||
ConvertField[] rs = new ConvertField[len];
|
||||
for (int i = 0; i < len; i++) {
|
||||
rs[i] = new ConvertField(items[i * 2].toString(), items[i * 2 + 1]);
|
||||
}
|
||||
return rs;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Type getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(Type type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public int getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
public void setPosition(int position) {
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
public Object getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(Object value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JsonConvert.root().convertTo(this);
|
||||
}
|
||||
}
|
||||
@@ -164,9 +164,22 @@ public class ObjectEncoder<W extends Writer, T> implements Encodeable<W, T> {
|
||||
return;
|
||||
}
|
||||
if (out.writeObjectB(value) < 0) {
|
||||
int maxPosition = 0;
|
||||
for (EnMember member : members) {
|
||||
maxPosition = member.getPosition();
|
||||
out.writeObjectField(member, value);
|
||||
}
|
||||
if (out.objExtFunc != null) {
|
||||
ConvertField[] extFields = out.objExtFunc.apply(value);
|
||||
if (extFields != null) {
|
||||
Encodeable<W, ?> anyEncoder = factory.getAnyEncoder();
|
||||
for (ConvertField en : extFields) {
|
||||
if (en == null) continue;
|
||||
maxPosition++;
|
||||
out.writeObjectField(en.getName(), en.getType(), Math.max(en.getPosition(), maxPosition), anyEncoder, en.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
out.writeObjectE(value);
|
||||
}
|
||||
|
||||
@@ -1,63 +0,0 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.convert;
|
||||
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
|
||||
/**
|
||||
* 序列化去掉引号的String对象。
|
||||
* <blockquote><pre>
|
||||
* 场景: JavaBean bean = ... ;
|
||||
* Map map = new HashMap();
|
||||
* map.put("bean", a);
|
||||
* records.add(map);
|
||||
* records需要在后期序列化写入库。 但是在这期间bean的内部字段值可能就变化了,会导致入库时并不是records.add的快照信息。
|
||||
* 所以需要使用StringConvertWrapper:
|
||||
* Map map = new HashMap();
|
||||
* map.put("bean", new StringConvertWrapper(bean.toString()));
|
||||
* records.add(map);
|
||||
* 这样既可以保持快照又不会在bean的值上面多一层引号。
|
||||
* </pre></blockquote>
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public class StringConvertWrapper {
|
||||
|
||||
protected String value;
|
||||
|
||||
public StringConvertWrapper() {
|
||||
}
|
||||
|
||||
public StringConvertWrapper(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public static StringConvertWrapper create(Object value) {
|
||||
return create(JsonConvert.root(), value);
|
||||
}
|
||||
|
||||
public static StringConvertWrapper create(TextConvert convert, Object value) {
|
||||
if (value == null) return new StringConvertWrapper(null);
|
||||
if (value instanceof String) return new StringConvertWrapper((String) value);
|
||||
return new StringConvertWrapper(convert.convertTo(value));
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return value;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,6 +6,8 @@
|
||||
package org.redkale.convert;
|
||||
|
||||
import java.lang.reflect.*;
|
||||
import java.util.function.*;
|
||||
import org.redkale.util.*;
|
||||
|
||||
/**
|
||||
* 序列化的数据输出流
|
||||
@@ -23,6 +25,12 @@ public abstract class Writer {
|
||||
//convertTo时是否以指定Type的ObjectEncoder进行处理
|
||||
protected Type specify;
|
||||
|
||||
//对某个字段值进行动态处理
|
||||
protected BiFunction<Attribute, Object, Object> objFieldFunc;
|
||||
|
||||
//对某个对象进行动态扩展字段值处理
|
||||
protected Function<Object, ConvertField[]> objExtFunc;
|
||||
|
||||
/**
|
||||
* 设置specify
|
||||
*
|
||||
@@ -38,6 +46,11 @@ public abstract class Writer {
|
||||
}
|
||||
}
|
||||
|
||||
protected boolean recycle() {
|
||||
this.objFieldFunc = null;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回specify
|
||||
*
|
||||
@@ -105,7 +118,12 @@ public abstract class Writer {
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public void writeObjectField(final EnMember member, Object obj) {
|
||||
Object value = member.attribute.get(obj);
|
||||
Object value;
|
||||
if (objFieldFunc == null) {
|
||||
value = member.attribute.get(obj);
|
||||
} else {
|
||||
value = objFieldFunc.apply(member.attribute, obj);
|
||||
}
|
||||
if (value == null) return;
|
||||
if (tiny()) {
|
||||
if (member.istring) {
|
||||
@@ -114,11 +132,49 @@ public abstract class Writer {
|
||||
if (!((Boolean) value)) return;
|
||||
}
|
||||
}
|
||||
this.writeFieldName(member);
|
||||
Attribute attr = member.getAttribute();
|
||||
this.writeFieldName(attr.field(), attr.genericType(), member.getPosition());
|
||||
member.encoder.convertTo(this, value);
|
||||
this.comma = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出一个对象的某个扩展字段
|
||||
*
|
||||
*
|
||||
* @param fieldName 字段名称
|
||||
* @param fieldType 字段类型
|
||||
* @param fieldPos 字段顺序
|
||||
* @param anyEncoder Encoder
|
||||
* @param value 写入的字段对象
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public void writeObjectField(final String fieldName, Type fieldType, int fieldPos, Encodeable anyEncoder, Object value) {
|
||||
if (value == null) return;
|
||||
if (fieldType == null) fieldType = value.getClass();
|
||||
if (tiny() && fieldType instanceof Class) {
|
||||
Class clazz = (Class) fieldType;
|
||||
if (CharSequence.class.isAssignableFrom(clazz)) {
|
||||
if (((CharSequence) value).length() == 0) return;
|
||||
} else if (clazz == boolean.class || clazz == Boolean.class) {
|
||||
if (!((Boolean) value)) return;
|
||||
}
|
||||
}
|
||||
this.writeFieldName(fieldName, fieldType, fieldPos);
|
||||
anyEncoder.convertTo(this, value);
|
||||
this.comma = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出一个字段名
|
||||
*
|
||||
* @param member 字段
|
||||
*/
|
||||
public final void writeFieldName(final EnMember member) {
|
||||
Attribute attr = member.getAttribute();
|
||||
this.writeFieldName(attr.field(), attr.genericType(), member.getPosition());
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出一个对象后的操作
|
||||
*
|
||||
@@ -176,9 +232,11 @@ public abstract class Writer {
|
||||
/**
|
||||
* 输出一个字段名
|
||||
*
|
||||
* @param member 字段的EnMember对象
|
||||
* @param fieldName 字段名称
|
||||
* @param fieldType 字段类型
|
||||
* @param fieldPos 字段顺序
|
||||
*/
|
||||
public abstract void writeFieldName(EnMember member);
|
||||
public abstract void writeFieldName(String fieldName, Type fieldType, int fieldPos);
|
||||
|
||||
/**
|
||||
* 写入一个boolean值
|
||||
@@ -262,5 +320,5 @@ public abstract class Writer {
|
||||
*
|
||||
* @param value StringConvertWrapper值
|
||||
*/
|
||||
public abstract void writeWrapper(StringConvertWrapper value);
|
||||
public abstract void writeWrapper(StringWrapper value);
|
||||
}
|
||||
|
||||
@@ -131,6 +131,7 @@ public class BsonByteBufferWriter extends BsonWriter {
|
||||
|
||||
@Override
|
||||
protected boolean recycle() {
|
||||
super.recycle();
|
||||
this.index = 0;
|
||||
this.specify = null;
|
||||
this.buffers = null;
|
||||
|
||||
@@ -37,7 +37,7 @@ import org.redkale.util.*;
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public final class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
|
||||
public class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
|
||||
|
||||
private static final ObjectPool<BsonReader> readerPool = BsonReader.createPool(Integer.getInteger("convert.bson.pool.size", 16));
|
||||
|
||||
@@ -59,6 +59,21 @@ public final class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
|
||||
return BsonFactory.root().getConvert();
|
||||
}
|
||||
|
||||
@Override
|
||||
public BsonConvert newConvert(final BiFunction<Attribute, Object, Object> fieldFunc) {
|
||||
return newConvert(fieldFunc, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BsonConvert newConvert(final BiFunction<Attribute, Object, Object> fieldFunc, Function<Object, ConvertField[]> objExtFunc) {
|
||||
return new BsonConvert(getFactory(), tiny) {
|
||||
@Override
|
||||
protected <S extends BsonWriter> S configWrite(S writer) {
|
||||
return fieldFunc(writer, fieldFunc, objExtFunc);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
//------------------------------ reader -----------------------------------------------------------
|
||||
public BsonReader pollBsonReader(final ByteBuffer... buffers) {
|
||||
return new BsonByteBufferReader((ConvertMask) null, buffers);
|
||||
@@ -78,11 +93,11 @@ public final class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
|
||||
|
||||
//------------------------------ writer -----------------------------------------------------------
|
||||
public BsonByteBufferWriter pollBsonWriter(final Supplier<ByteBuffer> supplier) {
|
||||
return new BsonByteBufferWriter(tiny, supplier);
|
||||
return configWrite(new BsonByteBufferWriter(tiny, supplier));
|
||||
}
|
||||
|
||||
public BsonWriter pollBsonWriter(final OutputStream out) {
|
||||
return new BsonStreamWriter(tiny, out);
|
||||
return configWrite(new BsonStreamWriter(tiny, out));
|
||||
}
|
||||
|
||||
public BsonWriter pollBsonWriter() {
|
||||
@@ -94,6 +109,7 @@ public final class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
|
||||
}
|
||||
|
||||
//------------------------------ convertFrom -----------------------------------------------------------
|
||||
@Override
|
||||
public <T> T convertFrom(final Type type, final byte[] bytes) {
|
||||
if (bytes == null) return null;
|
||||
return convertFrom(type, bytes, 0, bytes.length);
|
||||
@@ -173,33 +189,33 @@ public final class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
|
||||
|
||||
public void convertTo(final OutputStream out, final Object value) {
|
||||
if (value == null) {
|
||||
new BsonStreamWriter(tiny, out).writeNull();
|
||||
pollBsonWriter(out).writeNull();
|
||||
} else {
|
||||
factory.loadEncoder(value.getClass()).convertTo(new BsonStreamWriter(tiny, out), value);
|
||||
factory.loadEncoder(value.getClass()).convertTo(pollBsonWriter(out), value);
|
||||
}
|
||||
}
|
||||
|
||||
public void convertTo(final OutputStream out, final Type type, final Object value) {
|
||||
if (type == null) return;
|
||||
if (value == null) {
|
||||
new BsonStreamWriter(tiny, out).writeNull();
|
||||
pollBsonWriter(out).writeNull();
|
||||
} else {
|
||||
factory.loadEncoder(type).convertTo(new BsonStreamWriter(tiny, out), value);
|
||||
factory.loadEncoder(type).convertTo(pollBsonWriter(out), value);
|
||||
}
|
||||
}
|
||||
|
||||
public void convertMapTo(final OutputStream out, final Object... values) {
|
||||
if (values == null) {
|
||||
new BsonStreamWriter(tiny, out).writeNull();
|
||||
pollBsonWriter(out).writeNull();
|
||||
} else {
|
||||
((AnyEncoder) factory.getAnyEncoder()).convertMapTo(new BsonStreamWriter(tiny, out), values);
|
||||
((AnyEncoder) factory.getAnyEncoder()).convertMapTo(pollBsonWriter(out), values);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuffer[] convertTo(final Supplier<ByteBuffer> supplier, final Object value) {
|
||||
if (supplier == null) return null;
|
||||
BsonByteBufferWriter out = new BsonByteBufferWriter(tiny, supplier);
|
||||
BsonByteBufferWriter out = pollBsonWriter(supplier);
|
||||
if (value == null) {
|
||||
out.writeNull();
|
||||
} else {
|
||||
@@ -211,7 +227,7 @@ public final class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
|
||||
@Override
|
||||
public ByteBuffer[] convertTo(final Supplier<ByteBuffer> supplier, final Type type, final Object value) {
|
||||
if (supplier == null || type == null) return null;
|
||||
BsonByteBufferWriter out = new BsonByteBufferWriter(tiny, supplier);
|
||||
BsonByteBufferWriter out = pollBsonWriter(supplier);
|
||||
if (value == null) {
|
||||
out.writeNull();
|
||||
} else {
|
||||
@@ -223,7 +239,7 @@ public final class BsonConvert extends BinaryConvert<BsonReader, BsonWriter> {
|
||||
@Override
|
||||
public ByteBuffer[] convertMapTo(final Supplier<ByteBuffer> supplier, final Object... values) {
|
||||
if (supplier == null) return null;
|
||||
BsonByteBufferWriter out = new BsonByteBufferWriter(tiny, supplier);
|
||||
BsonByteBufferWriter out = pollBsonWriter(supplier);
|
||||
if (values == null) {
|
||||
out.writeNull();
|
||||
} else {
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
*/
|
||||
package org.redkale.convert.bson;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.nio.ByteBuffer;
|
||||
import org.redkale.convert.*;
|
||||
import org.redkale.convert.ext.ByteSimpledCoder;
|
||||
@@ -43,7 +44,7 @@ public class BsonWriter extends Writer {
|
||||
}
|
||||
|
||||
protected BsonWriter(byte[] bs) {
|
||||
this.content = bs;
|
||||
this.content = bs == null ? new byte[0] : bs;
|
||||
}
|
||||
|
||||
public BsonWriter() {
|
||||
@@ -97,10 +98,12 @@ public class BsonWriter extends Writer {
|
||||
count += len;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean recycle() {
|
||||
super.recycle();
|
||||
this.count = 0;
|
||||
this.specify = null;
|
||||
if (this.content.length > defaultSize) {
|
||||
if (this.content != null && this.content.length > defaultSize) {
|
||||
this.content = new byte[defaultSize];
|
||||
}
|
||||
return true;
|
||||
@@ -198,11 +201,10 @@ public class BsonWriter extends Writer {
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void writeFieldName(EnMember member) {
|
||||
Attribute attribute = member.getAttribute();
|
||||
public final void writeFieldName(String fieldName, Type fieldType, int fieldPos) {
|
||||
writeByte(BsonReader.SIGN_HASNEXT);
|
||||
writeSmallString(attribute.field());
|
||||
writeByte(BsonFactory.typeEnum(attribute.type()));
|
||||
writeSmallString(fieldName);
|
||||
writeByte(BsonFactory.typeEnum(fieldType));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -242,7 +244,7 @@ public class BsonWriter extends Writer {
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void writeWrapper(StringConvertWrapper value) {
|
||||
public final void writeWrapper(StringWrapper value) {
|
||||
this.writeString(value == null ? null : value.getValue());
|
||||
}
|
||||
|
||||
|
||||
@@ -8,9 +8,11 @@ package org.redkale.convert.ext;
|
||||
import org.redkale.convert.*;
|
||||
|
||||
/**
|
||||
* CharSequence 的SimpledCoder实现
|
||||
* CharSequence 的SimpledCoder实现
|
||||
*
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* <p> 详情见: https://redkale.org
|
||||
* @author zhangjx
|
||||
* @param <R> Reader输入的子类型
|
||||
* @param <W> Writer输出的子类型
|
||||
@@ -28,4 +30,20 @@ public class CharSequenceSimpledCoder<R extends Reader, W extends Writer> extend
|
||||
public CharSequence convertFrom(R in) {
|
||||
return in.readString();
|
||||
}
|
||||
|
||||
public static class StringBuilderSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, StringBuilder> {
|
||||
|
||||
public static final StringBuilderSimpledCoder instance = new StringBuilderSimpledCoder();
|
||||
|
||||
@Override
|
||||
public void convertTo(W out, StringBuilder value) {
|
||||
out.writeString(value == null ? null : value.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public StringBuilder convertFrom(R in) {
|
||||
String rs = in.readString();
|
||||
return rs == null ? null : new StringBuilder(rs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ import org.redkale.convert.*;
|
||||
*/
|
||||
public class FileSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, File> {
|
||||
|
||||
public static final PatternSimpledCoder instance = new PatternSimpledCoder();
|
||||
public static final FileSimpledCoder instance = new FileSimpledCoder();
|
||||
|
||||
@Override
|
||||
public void convertTo(W out, File value) {
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
package org.redkale.convert.ext;
|
||||
|
||||
import org.redkale.convert.*;
|
||||
import org.redkale.util.StringWrapper;
|
||||
|
||||
/**
|
||||
* String 的SimpledCoder实现
|
||||
@@ -17,18 +18,18 @@ import org.redkale.convert.*;
|
||||
* @param <R> Reader输入的子类型
|
||||
* @param <W> Writer输出的子类型
|
||||
*/
|
||||
public final class StringConvertWrapperSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, StringConvertWrapper> {
|
||||
public final class StringWrapperSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, StringWrapper> {
|
||||
|
||||
public static final StringConvertWrapperSimpledCoder instance = new StringConvertWrapperSimpledCoder();
|
||||
public static final StringWrapperSimpledCoder instance = new StringWrapperSimpledCoder();
|
||||
|
||||
@Override
|
||||
public void convertTo(W out, StringConvertWrapper value) {
|
||||
public void convertTo(W out, StringWrapper value) {
|
||||
out.writeWrapper(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public StringConvertWrapper convertFrom(R in) {
|
||||
return new StringConvertWrapper(in.readString());
|
||||
public StringWrapper convertFrom(R in) {
|
||||
return new StringWrapper(in.readString());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -48,6 +48,7 @@ public class JsonByteBufferWriter extends JsonWriter {
|
||||
|
||||
@Override
|
||||
protected boolean recycle() {
|
||||
super.recycle();
|
||||
this.index = 0;
|
||||
this.specify = null;
|
||||
this.charset = null;
|
||||
@@ -114,7 +115,7 @@ public class JsonByteBufferWriter extends JsonWriter {
|
||||
int byteLength = quote ? 2 : 0;
|
||||
ByteBuffer bb = null;
|
||||
if (charset == null) {
|
||||
byteLength += encodeUTF8Length(chs, start, len);
|
||||
byteLength += Utility.encodeUTF8Length(chs, start, len);
|
||||
} else {
|
||||
bb = charset.encode(CharBuffer.wrap(chs, start, len));
|
||||
byteLength += bb.remaining();
|
||||
@@ -133,6 +134,13 @@ public class JsonByteBufferWriter extends JsonWriter {
|
||||
} else if (c < 0x800) {
|
||||
buffer.put((byte) (0xc0 | (c >> 6)));
|
||||
buffer.put((byte) (0x80 | (c & 0x3f)));
|
||||
} else if (Character.isSurrogate(c)) { //连取两个
|
||||
int uc = Character.toCodePoint(c, chs[i + 1]);
|
||||
buffer.put((byte) (0xf0 | ((uc >> 18))));
|
||||
buffer.put((byte) (0x80 | ((uc >> 12) & 0x3f)));
|
||||
buffer.put((byte) (0x80 | ((uc >> 6) & 0x3f)));
|
||||
buffer.put((byte) (0x80 | (uc & 0x3f)));
|
||||
i++;
|
||||
} else {
|
||||
buffer.put((byte) (0xe0 | ((c >> 12))));
|
||||
buffer.put((byte) (0x80 | ((c >> 6) & 0x3f)));
|
||||
@@ -154,7 +162,34 @@ public class JsonByteBufferWriter extends JsonWriter {
|
||||
if (charset == null) { //UTF-8
|
||||
final int limit = start + len;
|
||||
for (int i = start; i < limit; i++) {
|
||||
buffer = putUTF8Char(buffer, chs[i]);
|
||||
char c = chs[i];
|
||||
if (c < 0x80) {
|
||||
if (!buffer.hasRemaining()) buffer = nextByteBuffer();
|
||||
buffer.put((byte) c);
|
||||
} else if (c < 0x800) {
|
||||
if (!buffer.hasRemaining()) buffer = nextByteBuffer();
|
||||
buffer.put((byte) (0xc0 | (c >> 6)));
|
||||
if (!buffer.hasRemaining()) buffer = nextByteBuffer();
|
||||
buffer.put((byte) (0x80 | (c & 0x3f)));
|
||||
} else if (Character.isSurrogate(c)) { //连取两个
|
||||
int uc = Character.toCodePoint(c, chs[i + 1]);
|
||||
if (!buffer.hasRemaining()) buffer = nextByteBuffer();
|
||||
buffer.put((byte) (0xf0 | ((uc >> 18))));
|
||||
if (!buffer.hasRemaining()) buffer = nextByteBuffer();
|
||||
buffer.put((byte) (0x80 | ((uc >> 12) & 0x3f)));
|
||||
if (!buffer.hasRemaining()) buffer = nextByteBuffer();
|
||||
buffer.put((byte) (0x80 | ((uc >> 6) & 0x3f)));
|
||||
if (!buffer.hasRemaining()) buffer = nextByteBuffer();
|
||||
buffer.put((byte) (0x80 | (uc & 0x3f)));
|
||||
i++;
|
||||
} else {
|
||||
if (!buffer.hasRemaining()) buffer = nextByteBuffer();
|
||||
buffer.put((byte) (0xe0 | ((c >> 12))));
|
||||
if (!buffer.hasRemaining()) buffer = nextByteBuffer();
|
||||
buffer.put((byte) (0x80 | ((c >> 6) & 0x3f)));
|
||||
if (!buffer.hasRemaining()) buffer = nextByteBuffer();
|
||||
buffer.put((byte) (0x80 | (c & 0x3f)));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
while (bb.hasRemaining()) {
|
||||
@@ -168,50 +203,18 @@ public class JsonByteBufferWriter extends JsonWriter {
|
||||
}
|
||||
}
|
||||
|
||||
private ByteBuffer putUTF8Char(ByteBuffer buffer, char c) {
|
||||
if (c < 0x80) {
|
||||
if (!buffer.hasRemaining()) buffer = nextByteBuffer();
|
||||
buffer.put((byte) c);
|
||||
} else if (c < 0x800) {
|
||||
if (!buffer.hasRemaining()) buffer = nextByteBuffer();
|
||||
buffer.put((byte) (0xc0 | (c >> 6)));
|
||||
if (!buffer.hasRemaining()) buffer = nextByteBuffer();
|
||||
buffer.put((byte) (0x80 | (c & 0x3f)));
|
||||
} else {
|
||||
if (!buffer.hasRemaining()) buffer = nextByteBuffer();
|
||||
buffer.put((byte) (0xe0 | ((c >> 12))));
|
||||
if (!buffer.hasRemaining()) buffer = nextByteBuffer();
|
||||
buffer.put((byte) (0x80 | ((c >> 6) & 0x3f)));
|
||||
if (!buffer.hasRemaining()) buffer = nextByteBuffer();
|
||||
buffer.put((byte) (0x80 | (c & 0x3f)));
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
private ByteBuffer nextByteBuffer() {
|
||||
this.buffers[this.index].flip();
|
||||
return this.buffers[++this.index];
|
||||
}
|
||||
|
||||
protected static int encodeUTF8Length(final char[] text, final int start, final int len) {
|
||||
char c;
|
||||
int size = 0;
|
||||
final char[] chars = text;
|
||||
final int limit = start + len;
|
||||
for (int i = start; i < limit; i++) {
|
||||
c = chars[i];
|
||||
size += (c < 0x80 ? 1 : (c < 0x800 ? 2 : 3));
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
protected static int encodeEscapeUTF8Length(final char[] text, final int start, final int len) {
|
||||
char c;
|
||||
int size = 0;
|
||||
final char[] chars = text;
|
||||
final char[] chs = text;
|
||||
final int limit = start + len;
|
||||
for (int i = start; i < limit; i++) {
|
||||
c = chars[i];
|
||||
c = chs[i];
|
||||
switch (c) {
|
||||
case '\n': size += 2;
|
||||
break;
|
||||
@@ -224,7 +227,7 @@ public class JsonByteBufferWriter extends JsonWriter {
|
||||
case '"': size += 2;
|
||||
break;
|
||||
default:
|
||||
size += (c < 0x80 ? 1 : (c < 0x800 ? 2 : 3));
|
||||
size += (c < 0x80 ? 1 : (c < 0x800 || Character.isSurrogate(c) ? 2 : 3));
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -238,19 +241,39 @@ public class JsonByteBufferWriter extends JsonWriter {
|
||||
* @param value String值
|
||||
*/
|
||||
@Override
|
||||
public void writeTo(final boolean quote, final String value) {
|
||||
char[] chs = Utility.charArray(value);
|
||||
writeTo(-1, quote, chs, 0, chs.length);
|
||||
public void writeLatin1To(final boolean quote, final String value) {
|
||||
byte[] bs = Utility.byteArray(value);
|
||||
int expandsize = expand(bs.length + (quote ? 2 : 0));
|
||||
if (expandsize == 0) {// 只需要一个buffer
|
||||
final ByteBuffer buffer = this.buffers[index];
|
||||
if (quote) buffer.put((byte) '"');
|
||||
buffer.put(bs);
|
||||
if (quote) buffer.put((byte) '"');
|
||||
} else {
|
||||
ByteBuffer buffer = this.buffers[index];
|
||||
if (quote) {
|
||||
if (!buffer.hasRemaining()) buffer = nextByteBuffer();
|
||||
buffer.put((byte) '"');
|
||||
}
|
||||
for (byte b : bs) {
|
||||
if (!buffer.hasRemaining()) buffer = nextByteBuffer();
|
||||
buffer.put(b);
|
||||
}
|
||||
if (quote) {
|
||||
if (!buffer.hasRemaining()) buffer = nextByteBuffer();
|
||||
buffer.put((byte) '"');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeInt(int value) {
|
||||
writeTo(false, String.valueOf(value));
|
||||
writeLatin1To(false, String.valueOf(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeLong(long value) {
|
||||
writeTo(false, String.valueOf(value));
|
||||
writeLatin1To(false, String.valueOf(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -288,7 +311,8 @@ public class JsonByteBufferWriter extends JsonWriter {
|
||||
if (expandsize == 0) { // 只需要一个buffer
|
||||
final ByteBuffer buffer = this.buffers[index];
|
||||
buffer.put((byte) '"');
|
||||
for (char c : chs) {
|
||||
for (int i = 0; i < chs.length; i++) {
|
||||
char c = chs[i];
|
||||
switch (c) {
|
||||
case '\n': buffer.put((byte) '\\').put((byte) 'n');
|
||||
break;
|
||||
@@ -306,6 +330,13 @@ public class JsonByteBufferWriter extends JsonWriter {
|
||||
} else if (c < 0x800) {
|
||||
buffer.put((byte) (0xc0 | (c >> 6)));
|
||||
buffer.put((byte) (0x80 | (c & 0x3f)));
|
||||
} else if (Character.isSurrogate(c)) { //连取两个
|
||||
int uc = Character.toCodePoint(c, chs[i + 1]);
|
||||
buffer.put((byte) (0xf0 | ((uc >> 18))));
|
||||
buffer.put((byte) (0x80 | ((uc >> 12) & 0x3f)));
|
||||
buffer.put((byte) (0x80 | ((uc >> 6) & 0x3f)));
|
||||
buffer.put((byte) (0x80 | (uc & 0x3f)));
|
||||
i++;
|
||||
} else {
|
||||
buffer.put((byte) (0xe0 | ((c >> 12))));
|
||||
buffer.put((byte) (0x80 | ((c >> 6) & 0x3f)));
|
||||
|
||||
@@ -21,7 +21,7 @@ import org.redkale.util.*;
|
||||
* @author zhangjx
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public final class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
||||
public class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
||||
|
||||
public static final Type TYPE_MAP_STRING_STRING = new TypeToken<java.util.HashMap<String, String>>() {
|
||||
}.getType();
|
||||
@@ -46,6 +46,21 @@ public final class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
||||
return JsonFactory.root().getConvert();
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonConvert newConvert(final BiFunction<Attribute, Object, Object> fieldFunc) {
|
||||
return newConvert(fieldFunc, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonConvert newConvert(final BiFunction<Attribute, Object, Object> fieldFunc, Function<Object, ConvertField[]> objExtFunc) {
|
||||
return new JsonConvert(getFactory(), tiny) {
|
||||
@Override
|
||||
protected <S extends JsonWriter> S configWrite(S writer) {
|
||||
return fieldFunc(writer, fieldFunc, objExtFunc);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
//------------------------------ reader -----------------------------------------------------------
|
||||
public JsonReader pollJsonReader(final ByteBuffer... buffers) {
|
||||
return new JsonByteBufferReader((ConvertMask) null, buffers);
|
||||
@@ -65,19 +80,19 @@ public final class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
||||
|
||||
//------------------------------ writer -----------------------------------------------------------
|
||||
public JsonByteBufferWriter pollJsonWriter(final Supplier<ByteBuffer> supplier) {
|
||||
return new JsonByteBufferWriter(tiny, supplier);
|
||||
return configWrite(new JsonByteBufferWriter(tiny, supplier));
|
||||
}
|
||||
|
||||
public JsonWriter pollJsonWriter(final OutputStream out) {
|
||||
return new JsonStreamWriter(tiny, out);
|
||||
return configWrite(new JsonStreamWriter(tiny, out));
|
||||
}
|
||||
|
||||
public JsonWriter pollJsonWriter(final Charset charset, final OutputStream out) {
|
||||
return new JsonStreamWriter(tiny, charset, out);
|
||||
return configWrite(new JsonStreamWriter(tiny, charset, out));
|
||||
}
|
||||
|
||||
public JsonWriter pollJsonWriter() {
|
||||
return writerPool.get().tiny(tiny);
|
||||
return configWrite(writerPool.get().tiny(tiny));
|
||||
}
|
||||
|
||||
public void offerJsonWriter(final JsonWriter writer) {
|
||||
@@ -85,6 +100,7 @@ public final class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
||||
}
|
||||
|
||||
//------------------------------ convertFrom -----------------------------------------------------------
|
||||
@Override
|
||||
public <T> T convertFrom(final Type type, final byte[] bytes) {
|
||||
if (bytes == null) return null;
|
||||
return convertFrom(type, new String(bytes, StandardCharsets.UTF_8));
|
||||
@@ -190,7 +206,7 @@ public final class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
||||
public String convertTo(final Type type, final Object value) {
|
||||
if (type == null) return null;
|
||||
if (value == null) return "null";
|
||||
final JsonWriter writer = writerPool.get().tiny(tiny);
|
||||
final JsonWriter writer = pollJsonWriter();
|
||||
writer.specify(type);
|
||||
factory.loadEncoder(type).convertTo(writer, value);
|
||||
String result = writer.toString();
|
||||
@@ -201,7 +217,7 @@ public final class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
||||
@Override
|
||||
public String convertMapTo(final Object... values) {
|
||||
if (values == null) return "null";
|
||||
final JsonWriter writer = writerPool.get().tiny(tiny);
|
||||
final JsonWriter writer = pollJsonWriter();
|
||||
((AnyEncoder) factory.getAnyEncoder()).convertMapTo(writer, values);
|
||||
String result = writer.toString();
|
||||
writerPool.accept(writer);
|
||||
@@ -210,7 +226,7 @@ public final class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
||||
|
||||
public void convertTo(final OutputStream out, final Object value) {
|
||||
if (value == null) {
|
||||
new JsonStreamWriter(tiny, out).writeNull();
|
||||
pollJsonWriter(out).writeNull();
|
||||
} else {
|
||||
convertTo(out, value.getClass(), value);
|
||||
}
|
||||
@@ -219,9 +235,9 @@ public final class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
||||
public void convertTo(final OutputStream out, final Type type, final Object value) {
|
||||
if (type == null) return;
|
||||
if (value == null) {
|
||||
new JsonStreamWriter(tiny, out).writeNull();
|
||||
pollJsonWriter(out).writeNull();
|
||||
} else {
|
||||
final JsonWriter writer = writerPool.get().tiny(tiny);
|
||||
final JsonWriter writer = pollJsonWriter();
|
||||
writer.specify(type);
|
||||
factory.loadEncoder(type).convertTo(writer, value);
|
||||
byte[] bs = writer.toBytes();
|
||||
@@ -236,9 +252,9 @@ public final class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
||||
|
||||
public void convertMapTo(final OutputStream out, final Object... values) {
|
||||
if (values == null) {
|
||||
new JsonStreamWriter(tiny, out).writeNull();
|
||||
pollJsonWriter(out).writeNull();
|
||||
} else {
|
||||
final JsonWriter writer = writerPool.get().tiny(tiny);
|
||||
final JsonWriter writer = pollJsonWriter();
|
||||
((AnyEncoder) factory.getAnyEncoder()).convertMapTo(writer, values);
|
||||
byte[] bs = writer.toBytes();
|
||||
writerPool.accept(writer);
|
||||
@@ -253,7 +269,7 @@ public final class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
||||
@Override
|
||||
public ByteBuffer[] convertTo(final Supplier<ByteBuffer> supplier, final Object value) {
|
||||
if (supplier == null) return null;
|
||||
JsonByteBufferWriter out = new JsonByteBufferWriter(tiny, null, supplier);
|
||||
JsonByteBufferWriter out = pollJsonWriter(supplier);
|
||||
if (value == null) {
|
||||
out.writeNull();
|
||||
} else {
|
||||
@@ -265,7 +281,7 @@ public final class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
||||
@Override
|
||||
public ByteBuffer[] convertTo(final Supplier<ByteBuffer> supplier, final Type type, final Object value) {
|
||||
if (supplier == null || type == null) return null;
|
||||
JsonByteBufferWriter out = new JsonByteBufferWriter(tiny, null, supplier);
|
||||
JsonByteBufferWriter out = pollJsonWriter(supplier);
|
||||
if (value == null) {
|
||||
out.writeNull();
|
||||
} else {
|
||||
@@ -278,7 +294,7 @@ public final class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
||||
@Override
|
||||
public ByteBuffer[] convertMapTo(final Supplier<ByteBuffer> supplier, final Object... values) {
|
||||
if (supplier == null) return null;
|
||||
JsonByteBufferWriter out = new JsonByteBufferWriter(tiny, null, supplier);
|
||||
JsonByteBufferWriter out = pollJsonWriter(supplier);
|
||||
if (values == null) {
|
||||
out.writeNull();
|
||||
} else {
|
||||
@@ -320,14 +336,14 @@ public final class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
||||
|
||||
public JsonWriter convertToWriter(final Type type, final Object value) {
|
||||
if (type == null) return null;
|
||||
final JsonWriter writer = writerPool.get().tiny(tiny);
|
||||
final JsonWriter writer = pollJsonWriter();
|
||||
writer.specify(type);
|
||||
factory.loadEncoder(type).convertTo(writer, value);
|
||||
return writer;
|
||||
}
|
||||
|
||||
public JsonWriter convertMapToWriter(final Object... values) {
|
||||
final JsonWriter writer = writerPool.get().tiny(tiny);
|
||||
final JsonWriter writer = pollJsonWriter();
|
||||
((AnyEncoder) factory.getAnyEncoder()).convertMapTo(writer, values);
|
||||
return writer;
|
||||
}
|
||||
|
||||
@@ -26,10 +26,7 @@ public final class JsonFactory extends ConvertFactory<JsonReader, JsonWriter> {
|
||||
private static final JsonFactory instance = new JsonFactory(null, Boolean.getBoolean("convert.json.tiny"));
|
||||
|
||||
static {
|
||||
instance.register(InetAddress.class, InetAddressSimpledCoder.InetAddressJsonSimpledCoder.instance);
|
||||
instance.register(InetSocketAddress.class, InetAddressSimpledCoder.InetSocketAddressJsonSimpledCoder.instance);
|
||||
instance.register(DLong.class, DLongSimpledCoder.DLongJsonSimpledCoder.instance);
|
||||
instance.register(BigInteger.class, BigIntegerSimpledCoder.BigIntegerJsonSimpledCoder.instance);
|
||||
|
||||
instance.register(Serializable.class, instance.loadEncoder(Object.class));
|
||||
|
||||
instance.register(AnyValue.class, instance.loadDecoder(AnyValue.DefaultAnyValue.class));
|
||||
@@ -38,6 +35,12 @@ public final class JsonFactory extends ConvertFactory<JsonReader, JsonWriter> {
|
||||
|
||||
private JsonFactory(JsonFactory parent, boolean tiny) {
|
||||
super(parent, tiny);
|
||||
if (parent == null) {
|
||||
this.register(InetAddress.class, InetAddressSimpledCoder.InetAddressJsonSimpledCoder.instance);
|
||||
this.register(InetSocketAddress.class, InetAddressSimpledCoder.InetSocketAddressJsonSimpledCoder.instance);
|
||||
this.register(DLong.class, DLongSimpledCoder.DLongJsonSimpledCoder.instance);
|
||||
this.register(BigInteger.class, BigIntegerSimpledCoder.BigIntegerJsonSimpledCoder.instance);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -87,19 +87,19 @@ class JsonStreamWriter extends JsonByteBufferWriter {
|
||||
* @param value String值
|
||||
*/
|
||||
@Override
|
||||
public void writeTo(final boolean quote, final String value) {
|
||||
public void writeLatin1To(final boolean quote, final String value) {
|
||||
char[] chs = Utility.charArray(value);
|
||||
writeTo(quote, chs, 0, chs.length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeInt(int value) {
|
||||
writeTo(false, String.valueOf(value));
|
||||
writeLatin1To(false, String.valueOf(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeLong(long value) {
|
||||
writeTo(false, String.valueOf(value));
|
||||
writeLatin1To(false, String.valueOf(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
*/
|
||||
package org.redkale.convert.json;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.nio.ByteBuffer;
|
||||
import org.redkale.convert.*;
|
||||
import org.redkale.util.*;
|
||||
@@ -88,7 +89,7 @@ public class JsonWriter extends Writer {
|
||||
* @param quote 是否加双引号
|
||||
* @param value 非null且不含需要转义的字符的String值
|
||||
*/
|
||||
public void writeTo(final boolean quote, final String value) {
|
||||
public void writeLatin1To(final boolean quote, final String value) {
|
||||
int len = value.length();
|
||||
expand(len + (quote ? 2 : 0));
|
||||
if (quote) content[count++] = '"';
|
||||
@@ -97,10 +98,12 @@ public class JsonWriter extends Writer {
|
||||
if (quote) content[count++] = '"';
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean recycle() {
|
||||
super.recycle();
|
||||
this.count = 0;
|
||||
this.specify = null;
|
||||
if (this.content.length > defaultSize) {
|
||||
if (this.content != null && this.content.length > defaultSize) {
|
||||
this.content = new char[defaultSize];
|
||||
}
|
||||
return true;
|
||||
@@ -157,15 +160,15 @@ public class JsonWriter extends Writer {
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void writeFieldName(EnMember member) {
|
||||
public final void writeFieldName(String fieldName, Type fieldType, int fieldPos) {
|
||||
if (this.comma) writeTo(',');
|
||||
writeTo(true, member.getAttribute().field());
|
||||
writeLatin1To(true, fieldName);
|
||||
writeTo(':');
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void writeSmallString(String value) {
|
||||
writeTo(true, value);
|
||||
writeLatin1To(true, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -311,17 +314,17 @@ public class JsonWriter extends Writer {
|
||||
|
||||
@Override
|
||||
public final void writeFloat(float value) {
|
||||
writeTo(false, String.valueOf(value));
|
||||
writeLatin1To(false, String.valueOf(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void writeDouble(double value) {
|
||||
writeTo(false, String.valueOf(value));
|
||||
writeLatin1To(false, String.valueOf(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void writeWrapper(StringConvertWrapper value) {
|
||||
writeTo(false, String.valueOf(value));
|
||||
public final void writeWrapper(StringWrapper value) {
|
||||
writeLatin1To(false, String.valueOf(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -14,7 +14,7 @@ import java.util.concurrent.*;
|
||||
import java.util.concurrent.atomic.*;
|
||||
import java.util.function.*;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import org.redkale.util.ObjectPool;
|
||||
import org.redkale.util.*;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -118,7 +118,6 @@ public abstract class AsyncConnection implements ReadableByteChannel, WritableBy
|
||||
|
||||
public abstract void read(CompletionHandler<Integer, ByteBuffer> handler);
|
||||
|
||||
|
||||
@Override
|
||||
public abstract int write(ByteBuffer src) throws IOException;
|
||||
|
||||
@@ -141,22 +140,40 @@ public abstract class AsyncConnection implements ReadableByteChannel, WritableBy
|
||||
this.readBuffer = null;
|
||||
return rs;
|
||||
}
|
||||
// Thread thread = Thread.currentThread();
|
||||
// if (thread instanceof IOThread) {
|
||||
// return ((IOThread) thread).getBufferPool().get();
|
||||
// }
|
||||
return bufferSupplier.get();
|
||||
}
|
||||
|
||||
public void offerBuffer(Buffer buffer) {
|
||||
if (buffer == null) return;
|
||||
// Thread thread = Thread.currentThread();
|
||||
// if (thread instanceof IOThread) {
|
||||
// ((IOThread) thread).getBufferPool().accept((ByteBuffer) buffer);
|
||||
// return;
|
||||
// }
|
||||
bufferConsumer.accept((ByteBuffer) buffer);
|
||||
}
|
||||
|
||||
public void offerBuffer(Buffer... buffers) {
|
||||
if (buffers == null) return;
|
||||
Consumer<ByteBuffer> consumer = this.bufferConsumer;
|
||||
// Thread thread = Thread.currentThread();
|
||||
// if (thread instanceof IOThread) {
|
||||
// consumer = ((IOThread) thread).getBufferPool();
|
||||
// }
|
||||
for (Buffer buffer : buffers) {
|
||||
bufferConsumer.accept((ByteBuffer) buffer);
|
||||
consumer.accept((ByteBuffer) buffer);
|
||||
}
|
||||
}
|
||||
|
||||
public ByteBuffer pollWriteBuffer() {
|
||||
// Thread thread = Thread.currentThread();
|
||||
// if (thread instanceof IOThread) {
|
||||
// return ((IOThread) thread).getBufferPool().get();
|
||||
// }
|
||||
return bufferSupplier.get();
|
||||
}
|
||||
|
||||
@@ -189,7 +206,12 @@ public abstract class AsyncConnection implements ReadableByteChannel, WritableBy
|
||||
}
|
||||
}
|
||||
if (this.readBuffer != null) {
|
||||
bufferConsumer.accept(this.readBuffer);
|
||||
Consumer<ByteBuffer> consumer = this.bufferConsumer;
|
||||
// Thread thread = Thread.currentThread();
|
||||
// if (thread instanceof IOThread) {
|
||||
// consumer = ((IOThread) thread).getBufferPool();
|
||||
// }
|
||||
consumer.accept(this.readBuffer);
|
||||
}
|
||||
if (attributes == null) return;
|
||||
try {
|
||||
|
||||
61
src/org/redkale/net/IOThread.java
Normal file
61
src/org/redkale/net/IOThread.java
Normal file
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.net;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import org.redkale.util.*;
|
||||
|
||||
/**
|
||||
* 协议处理的IO线程类
|
||||
*
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public class IOThread extends Thread {
|
||||
|
||||
protected Thread localThread;
|
||||
|
||||
protected final ExecutorService executor;
|
||||
|
||||
protected ObjectPool<ByteBuffer> bufferPool;
|
||||
|
||||
public IOThread(ExecutorService executor, ObjectPool<ByteBuffer> bufferPool, Runnable runner) {
|
||||
super(runner);
|
||||
this.executor = executor;
|
||||
this.bufferPool = bufferPool;
|
||||
this.setDaemon(true);
|
||||
}
|
||||
|
||||
public void runAsync(Runnable runner) {
|
||||
executor.execute(runner);
|
||||
}
|
||||
|
||||
public ExecutorService getExecutor() {
|
||||
return executor;
|
||||
}
|
||||
|
||||
public ObjectPool<ByteBuffer> getBufferPool() {
|
||||
return bufferPool;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
this.localThread = Thread.currentThread();
|
||||
super.run();
|
||||
}
|
||||
|
||||
public boolean inSameThread() {
|
||||
return this.localThread == Thread.currentThread();
|
||||
}
|
||||
|
||||
public boolean inSameThread(Thread thread) {
|
||||
return this.localThread == thread;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -60,7 +60,8 @@ public class PrepareRunner implements Runnable {
|
||||
@Override
|
||||
public void completed(Integer count, ByteBuffer buffer) {
|
||||
if (count < 1) {
|
||||
response.request.offerReadBuffer(buffer);
|
||||
buffer.clear();
|
||||
channel.setReadBuffer(buffer);
|
||||
channel.dispose();// response.init(channel); 在调用之前异常
|
||||
response.removeChannel();
|
||||
response.finish(true);
|
||||
@@ -84,7 +85,8 @@ public class PrepareRunner implements Runnable {
|
||||
|
||||
@Override
|
||||
public void failed(Throwable exc, ByteBuffer buffer) {
|
||||
response.request.offerReadBuffer(buffer);
|
||||
buffer.clear();
|
||||
channel.setReadBuffer(buffer);
|
||||
channel.dispose();// response.init(channel); 在调用之前异常
|
||||
response.removeChannel();
|
||||
response.finish(true);
|
||||
@@ -97,7 +99,7 @@ public class PrepareRunner implements Runnable {
|
||||
channel.dispose();// response.init(channel); 在调用之前异常
|
||||
response.removeChannel();
|
||||
response.finish(true);
|
||||
if (te != null && context.logger.isLoggable(Level.FINEST)) {
|
||||
if (context.logger.isLoggable(Level.FINEST)) {
|
||||
context.logger.log(Level.FINEST, "Servlet read channel erroneous, force to close channel ", te);
|
||||
}
|
||||
}
|
||||
@@ -116,7 +118,8 @@ public class PrepareRunner implements Runnable {
|
||||
if (buffer.hasRemaining()) {
|
||||
request.setMoredata(buffer);
|
||||
} else {
|
||||
response.request.offerReadBuffer(buffer);
|
||||
buffer.clear();
|
||||
channel.setReadBuffer(buffer);
|
||||
}
|
||||
preparer.prepare(request, response);
|
||||
} else {
|
||||
@@ -137,7 +140,8 @@ public class PrepareRunner implements Runnable {
|
||||
if (attachment.hasRemaining()) {
|
||||
request.setMoredata(attachment);
|
||||
} else {
|
||||
response.request.offerReadBuffer(attachment);
|
||||
attachment.clear();
|
||||
channel.setReadBuffer(attachment);
|
||||
}
|
||||
try {
|
||||
preparer.prepare(request, response);
|
||||
@@ -151,7 +155,8 @@ public class PrepareRunner implements Runnable {
|
||||
@Override
|
||||
public void failed(Throwable exc, ByteBuffer attachment) {
|
||||
preparer.illRequestCounter.incrementAndGet();
|
||||
response.request.offerReadBuffer(attachment);
|
||||
attachment.clear();
|
||||
channel.setReadBuffer(attachment);
|
||||
response.finish(true);
|
||||
if (exc != null) request.context.logger.log(Level.FINER, "Servlet read channel erroneous, force to close channel ", exc);
|
||||
}
|
||||
@@ -175,19 +180,4 @@ public class PrepareRunner implements Runnable {
|
||||
return response.removeChannel();
|
||||
}
|
||||
|
||||
protected ByteBuffer pollReadBuffer(Request request) {
|
||||
return request.pollReadBuffer();
|
||||
}
|
||||
|
||||
protected ByteBuffer pollReadBuffer(Response response) {
|
||||
return response.request.pollReadBuffer();
|
||||
}
|
||||
|
||||
protected void offerReadBuffer(Request request, ByteBuffer buffer) {
|
||||
request.offerReadBuffer(buffer);
|
||||
}
|
||||
|
||||
protected void offerReadBuffer(Response response, ByteBuffer buffer) {
|
||||
response.request.offerReadBuffer(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,8 +40,6 @@ public abstract class Request<C extends Context> {
|
||||
|
||||
protected AsyncConnection channel;
|
||||
|
||||
protected ByteBuffer readBuffer;
|
||||
|
||||
/**
|
||||
* properties 与 attributes 的区别在于:调用recycle时, attributes会被清空而properties会保留;
|
||||
* properties 通常存放需要永久绑定在request里的一些对象
|
||||
@@ -67,23 +65,6 @@ public abstract class Request<C extends Context> {
|
||||
return rs;
|
||||
}
|
||||
|
||||
protected ByteBuffer pollReadBuffer() {
|
||||
ByteBuffer buffer = this.readBuffer;
|
||||
this.readBuffer = null;
|
||||
if (buffer == null) buffer = bufferPool.get();
|
||||
return buffer;
|
||||
}
|
||||
|
||||
protected void offerReadBuffer(ByteBuffer buffer) {
|
||||
if (buffer == null) return;
|
||||
if (this.readBuffer == null) {
|
||||
buffer.clear();
|
||||
this.readBuffer = buffer;
|
||||
} else {
|
||||
bufferPool.accept(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回值:Integer.MIN_VALUE: 帧数据; -1:数据不合法; 0:解析完毕; >0: 需再读取的字节数。
|
||||
*
|
||||
|
||||
@@ -27,18 +27,12 @@ public abstract class Response<C extends Context, R extends Request<C>> {
|
||||
|
||||
protected final C context;
|
||||
|
||||
protected final ObjectPool<ByteBuffer> bufferPool;
|
||||
|
||||
protected final ObjectPool<Response> responsePool;
|
||||
|
||||
protected final R request;
|
||||
|
||||
protected AsyncConnection channel;
|
||||
|
||||
protected ByteBuffer writeHeadBuffer;
|
||||
|
||||
protected ByteBuffer writeBodyBuffer;
|
||||
|
||||
private volatile boolean inited = true;
|
||||
|
||||
protected Object output; //输出的结果对象
|
||||
@@ -49,8 +43,6 @@ public abstract class Response<C extends Context, R extends Request<C>> {
|
||||
|
||||
protected Servlet<C, R, ? extends Response<C, R>> servlet;
|
||||
|
||||
private Supplier<ByteBuffer> bodyBufferSupplier;
|
||||
|
||||
private final CompletionHandler finishHandler = new CompletionHandler<Integer, ByteBuffer>() {
|
||||
|
||||
@Override
|
||||
@@ -58,31 +50,21 @@ public abstract class Response<C extends Context, R extends Request<C>> {
|
||||
if (attachment.hasRemaining()) {
|
||||
channel.write(attachment, attachment, this);
|
||||
} else {
|
||||
offerResponseBuffer(attachment);
|
||||
channel.offerBuffer(attachment);
|
||||
ByteBuffer data = request.removeMoredata();
|
||||
final boolean more = data != null && request.keepAlive;
|
||||
request.more = more;
|
||||
finish();
|
||||
if (more) new PrepareRunner(context, responsePool, request.channel, null, Response.this).run();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void failed(Throwable exc, ByteBuffer attachment) {
|
||||
offerResponseBuffer(attachment);
|
||||
channel.offerBuffer(attachment);
|
||||
finish(true);
|
||||
}
|
||||
|
||||
private void offerResponseBuffer(ByteBuffer attachment) {
|
||||
if (writeHeadBuffer == null) {
|
||||
if (bufferPool.getRecyclerPredicate().test(attachment)) {
|
||||
writeHeadBuffer = attachment;
|
||||
}
|
||||
} else if (writeBodyBuffer == null) {
|
||||
if (bufferPool.getRecyclerPredicate().test(attachment)) {
|
||||
writeBodyBuffer = attachment;
|
||||
}
|
||||
} else {
|
||||
bufferPool.accept(attachment);
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
private final CompletionHandler finishHandler2 = new CompletionHandler<Integer, ByteBuffer[]>() {
|
||||
@@ -99,73 +81,36 @@ public abstract class Response<C extends Context, R extends Request<C>> {
|
||||
if (index >= 0) {
|
||||
channel.write(attachments, index, attachments.length - index, attachments, this);
|
||||
} else {
|
||||
offerResponseBuffer(attachments);
|
||||
for (ByteBuffer attachment : attachments) {
|
||||
channel.offerBuffer(attachment);
|
||||
}
|
||||
ByteBuffer data = request.removeMoredata();
|
||||
final boolean more = data != null && request.keepAlive;
|
||||
request.more = more;
|
||||
finish();
|
||||
if (more) new PrepareRunner(context, responsePool, request.channel, null, Response.this).run();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void failed(Throwable exc, final ByteBuffer[] attachments) {
|
||||
offerResponseBuffer(attachments);
|
||||
for (ByteBuffer attachment : attachments) {
|
||||
channel.offerBuffer(attachment);
|
||||
}
|
||||
finish(true);
|
||||
}
|
||||
|
||||
private void offerResponseBuffer(ByteBuffer[] attachments) {
|
||||
int start = 0;
|
||||
if (writeHeadBuffer == null && attachments.length > start) {
|
||||
if (bufferPool.getRecyclerPredicate().test(attachments[start])) {
|
||||
writeHeadBuffer = attachments[start];
|
||||
start++;
|
||||
}
|
||||
}
|
||||
if (writeBodyBuffer == null && attachments.length > start) {
|
||||
if (bufferPool.getRecyclerPredicate().test(attachments[start])) {
|
||||
writeBodyBuffer = attachments[start];
|
||||
start++;
|
||||
}
|
||||
}
|
||||
for (int i = start; i < attachments.length; i++) {
|
||||
bufferPool.accept(attachments[i]);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
protected Response(C context, final R request, ObjectPool<Response> responsePool) {
|
||||
this.context = context;
|
||||
this.request = request;
|
||||
this.bufferPool = request.bufferPool;
|
||||
this.responsePool = responsePool;
|
||||
this.writeHeadBuffer = bufferPool.get();
|
||||
this.writeBodyBuffer = bufferPool.get();
|
||||
this.bodyBufferSupplier = () -> {
|
||||
ByteBuffer buffer = writeBodyBuffer;
|
||||
if (buffer == null) return bufferPool.get();
|
||||
writeBodyBuffer = null;
|
||||
return buffer;
|
||||
};
|
||||
}
|
||||
|
||||
protected ByteBuffer pollWriteReadBuffer() {
|
||||
ByteBuffer buffer = this.writeHeadBuffer;
|
||||
this.writeHeadBuffer = null;
|
||||
if (buffer == null) buffer = bufferPool.get();
|
||||
return buffer;
|
||||
}
|
||||
|
||||
protected ByteBuffer pollWriteBodyBuffer() {
|
||||
ByteBuffer buffer = this.writeBodyBuffer;
|
||||
this.writeBodyBuffer = null;
|
||||
if (buffer == null) buffer = bufferPool.get();
|
||||
return buffer;
|
||||
}
|
||||
|
||||
protected Supplier<ByteBuffer> getBodyBufferSupplier() {
|
||||
return bodyBufferSupplier;
|
||||
}
|
||||
|
||||
protected void offerBuffer(ByteBuffer... buffers) {
|
||||
for (ByteBuffer buffer : buffers) {
|
||||
bufferPool.accept(buffer);
|
||||
channel.offerBuffer(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -278,7 +223,7 @@ public abstract class Response<C extends Context, R extends Request<C>> {
|
||||
public void finish(final byte[] bs) {
|
||||
if (!this.inited) return; //避免重复关闭
|
||||
if (this.context.bufferCapacity == bs.length) {
|
||||
ByteBuffer buffer = this.bufferPool.get();
|
||||
ByteBuffer buffer = channel.bufferSupplier.get();
|
||||
buffer.put(bs);
|
||||
buffer.flip();
|
||||
this.finish(buffer);
|
||||
@@ -289,33 +234,33 @@ public abstract class Response<C extends Context, R extends Request<C>> {
|
||||
|
||||
public void finish(ByteBuffer buffer) {
|
||||
if (!this.inited) return; //避免重复关闭
|
||||
ByteBuffer data = this.request.removeMoredata();
|
||||
final AsyncConnection conn = this.channel;
|
||||
final boolean more = data != null && this.request.keepAlive;
|
||||
this.request.more = more;
|
||||
// ByteBuffer data = this.request.removeMoredata();
|
||||
// final boolean more = data != null && this.request.keepAlive;
|
||||
// this.request.more = more;
|
||||
conn.write(buffer, buffer, finishHandler);
|
||||
if (more) new PrepareRunner(this.context, this.responsePool, conn, data, null).run();
|
||||
// if (more) new PrepareRunner(this.context, this.responsePool, conn, data, null).run();
|
||||
}
|
||||
|
||||
public void finish(boolean kill, ByteBuffer buffer) {
|
||||
if (!this.inited) return; //避免重复关闭
|
||||
if (kill) refuseAlive();
|
||||
ByteBuffer data = this.request.removeMoredata();
|
||||
final AsyncConnection conn = this.channel;
|
||||
final boolean more = data != null && this.request.keepAlive;
|
||||
this.request.more = more;
|
||||
// ByteBuffer data = this.request.removeMoredata();
|
||||
// final boolean more = data != null && this.request.keepAlive;
|
||||
// this.request.more = more;
|
||||
conn.write(buffer, buffer, finishHandler);
|
||||
if (more) new PrepareRunner(this.context, this.responsePool, conn, data, null).run();
|
||||
// if (more) new PrepareRunner(this.context, this.responsePool, conn, data, null).run();
|
||||
}
|
||||
|
||||
public void finish(ByteBuffer... buffers) {
|
||||
if (!this.inited) return; //避免重复关闭
|
||||
final AsyncConnection conn = this.channel;
|
||||
ByteBuffer data = this.request.removeMoredata();
|
||||
final boolean more = data != null && this.request.keepAlive;
|
||||
this.request.more = more;
|
||||
// ByteBuffer data = this.request.removeMoredata();
|
||||
// final boolean more = data != null && this.request.keepAlive;
|
||||
// this.request.more = more;
|
||||
conn.write(buffers, buffers, finishHandler2);
|
||||
if (more) new PrepareRunner(this.context, this.responsePool, conn, data, null).run();
|
||||
// if (more) new PrepareRunner(this.context, this.responsePool, conn, data, null).run();
|
||||
}
|
||||
|
||||
public void finish(boolean kill, ByteBuffer... buffers) {
|
||||
@@ -337,14 +282,14 @@ public abstract class Response<C extends Context, R extends Request<C>> {
|
||||
if (buffer.hasRemaining()) {
|
||||
channel.write(buffer, attachment, this);
|
||||
} else {
|
||||
bufferPool.accept(buffer);
|
||||
channel.offerBuffer(buffer);
|
||||
if (handler != null) handler.completed(result, attachment);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void failed(Throwable exc, A attachment) {
|
||||
bufferPool.accept(buffer);
|
||||
channel.offerBuffer(buffer);
|
||||
if (handler != null) handler.failed(exc, attachment);
|
||||
}
|
||||
|
||||
@@ -362,7 +307,7 @@ public abstract class Response<C extends Context, R extends Request<C>> {
|
||||
index = i;
|
||||
break;
|
||||
}
|
||||
bufferPool.accept(buffers[i]);
|
||||
channel.offerBuffer(buffers[i]);
|
||||
}
|
||||
if (index == 0) {
|
||||
channel.write(buffers, attachment, this);
|
||||
@@ -376,7 +321,7 @@ public abstract class Response<C extends Context, R extends Request<C>> {
|
||||
@Override
|
||||
public void failed(Throwable exc, A attachment) {
|
||||
for (ByteBuffer buffer : buffers) {
|
||||
bufferPool.accept(buffer);
|
||||
channel.offerBuffer(buffer);
|
||||
}
|
||||
if (handler != null) handler.failed(exc, attachment);
|
||||
}
|
||||
|
||||
@@ -129,7 +129,7 @@ public abstract class Server<K extends Serializable, C extends Context, R extend
|
||||
this.maxbody = parseLenth(config.getValue("maxbody"), 64 * 1024);
|
||||
int bufCapacity = parseLenth(config.getValue("bufferCapacity"), "UDP".equalsIgnoreCase(protocol) ? 1350 : 32 * 1024);
|
||||
this.bufferCapacity = "UDP".equalsIgnoreCase(protocol) ? bufCapacity : (bufCapacity < 8 * 1024 ? 8 * 1024 : bufCapacity);
|
||||
this.threads = config.getIntValue("threads", Runtime.getRuntime().availableProcessors() * 32);
|
||||
this.threads = config.getIntValue("threads", Math.max(8, Runtime.getRuntime().availableProcessors() * 2));
|
||||
this.bufferPoolSize = config.getIntValue("bufferPoolSize", this.threads * 4);
|
||||
this.responsePoolSize = config.getIntValue("responsePoolSize", this.threads * 2);
|
||||
this.name = config.getValue("name", "Server-" + protocol + "-" + this.address.getPort());
|
||||
@@ -153,7 +153,7 @@ public abstract class Server<K extends Serializable, C extends Context, R extend
|
||||
final String n = name;
|
||||
this.executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(threads, (Runnable r) -> {
|
||||
Thread t = new WorkThread(executor, r);
|
||||
t.setName(n + "-ServletThread-" + f.format(counter.incrementAndGet()));
|
||||
t.setName("Redkale-" + n + "-ServletThread-" + f.format(counter.incrementAndGet()));
|
||||
return t;
|
||||
});
|
||||
}
|
||||
@@ -283,11 +283,15 @@ public abstract class Server<K extends Serializable, C extends Context, R extend
|
||||
serverChannel.bind(address, backlog);
|
||||
serverChannel.accept(this);
|
||||
final String threadName = "[" + Thread.currentThread().getName() + "] ";
|
||||
postStart();
|
||||
logger.info(threadName + this.getClass().getSimpleName() + ("TCP".equalsIgnoreCase(protocol) ? "" : ("." + protocol)) + " listen: " + address
|
||||
+ ", threads: " + threads + ", maxbody: " + formatLenth(context.maxbody) + ", bufferCapacity: " + formatLenth(bufferCapacity) + ", bufferPoolSize: " + bufferPoolSize + ", responsePoolSize: " + responsePoolSize
|
||||
+ ", started in " + (System.currentTimeMillis() - context.getServerStartTime()) + " ms");
|
||||
}
|
||||
|
||||
protected void postStart() {
|
||||
}
|
||||
|
||||
public void changeAddress(final InetSocketAddress addr) throws IOException {
|
||||
long s = System.currentTimeMillis();
|
||||
Objects.requireNonNull(addr);
|
||||
|
||||
@@ -23,11 +23,13 @@ public abstract class Servlet<C extends Context, R extends Request<C>, P extends
|
||||
|
||||
AnyValue _conf; //当前Servlet的配置
|
||||
|
||||
//Server执行start时运行此方法
|
||||
public void init(C context, AnyValue config) {
|
||||
}
|
||||
|
||||
public abstract void execute(R request, P response) throws IOException;
|
||||
|
||||
//Server执行shutdown后运行此方法
|
||||
public void destroy(C context, AnyValue config) {
|
||||
}
|
||||
|
||||
|
||||
@@ -104,7 +104,8 @@ public class TcpAioProtocolServer extends ProtocolServer {
|
||||
|
||||
AsyncConnection conn = new TcpAioAsyncConnection(bufferPool, bufferPool, channel,
|
||||
context.getSSLContext(), null, context.readTimeoutSeconds, context.writeTimeoutSeconds, livingCounter, closedCounter);
|
||||
context.runAsync(new PrepareRunner(context, responsePool, conn, null, null));
|
||||
//context.runAsync(new PrepareRunner(context, responsePool, conn, null, null));
|
||||
new PrepareRunner(context, responsePool, conn, null, null).run();
|
||||
} catch (Throwable e) {
|
||||
context.logger.log(Level.INFO, channel + " accept error", e);
|
||||
}
|
||||
|
||||
@@ -119,7 +119,7 @@ public class TransportFactory {
|
||||
if (this.checkinterval < 2) this.checkinterval = 2;
|
||||
}
|
||||
this.scheduler = new ScheduledThreadPoolExecutor(1, (Runnable r) -> {
|
||||
final Thread t = new Thread(r, this.getClass().getSimpleName() + "-TransportFactoryTask-Thread");
|
||||
final Thread t = new Thread(r, "Redkale-" + this.getClass().getSimpleName() + "-Schedule-Thread");
|
||||
t.setDaemon(true);
|
||||
return t;
|
||||
});
|
||||
@@ -162,7 +162,7 @@ public class TransportFactory {
|
||||
ExecutorService transportExec = Executors.newFixedThreadPool(threads, (Runnable r) -> {
|
||||
Thread t = new Thread(r);
|
||||
t.setDaemon(true);
|
||||
t.setName("Transport-Thread-" + counter.incrementAndGet());
|
||||
t.setName("Redkale-Transport-Thread-" + counter.incrementAndGet());
|
||||
return t;
|
||||
});
|
||||
AsynchronousChannelGroup transportGroup = null;
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
package org.redkale.net.http;
|
||||
|
||||
import org.redkale.net.Filter;
|
||||
import org.redkale.util.AnyValue;
|
||||
|
||||
/**
|
||||
* HTTP 过滤器 <br>
|
||||
@@ -17,4 +18,7 @@ import org.redkale.net.Filter;
|
||||
*/
|
||||
public abstract class HttpFilter extends Filter<HttpContext, HttpRequest, HttpResponse> {
|
||||
|
||||
//Server执行start后运行此方法
|
||||
public void postStart(HttpContext context, AnyValue config) {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -402,6 +402,21 @@ public class HttpPrepareServlet extends PrepareServlet<String, HttpContext, Http
|
||||
return this.resourceHttpServlet;
|
||||
}
|
||||
|
||||
public void postStart(HttpContext context, AnyValue config) {
|
||||
List filters = getFilters();
|
||||
synchronized (filters) {
|
||||
if (!filters.isEmpty()) {
|
||||
for (Object filter : filters) {
|
||||
((HttpFilter) filter).postStart(context, config);
|
||||
}
|
||||
}
|
||||
}
|
||||
this.resourceHttpServlet.postStart(context, config);
|
||||
getServlets().forEach(s -> {
|
||||
s.postStart(context, getServletConf(s));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy(HttpContext context, AnyValue config) {
|
||||
super.destroy(context, config); //必须要执行
|
||||
|
||||
@@ -13,6 +13,7 @@ import java.nio.ByteBuffer;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.charset.*;
|
||||
import java.util.*;
|
||||
import java.util.logging.Level;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
import org.redkale.net.*;
|
||||
import org.redkale.util.*;
|
||||
@@ -42,6 +43,8 @@ public class HttpRequest extends Request<HttpContext> {
|
||||
|
||||
protected String requestURI;
|
||||
|
||||
private byte[] queryBytes;
|
||||
|
||||
private long contentLength = -1;
|
||||
|
||||
private String contentType;
|
||||
@@ -120,9 +123,15 @@ public class HttpRequest extends Request<HttpContext> {
|
||||
int qst = array.find(index, offset, (byte) '?');
|
||||
if (qst > 0) {
|
||||
this.requestURI = array.toDecodeString(index, qst - index, charset).trim();
|
||||
addParameter(array, qst + 1, offset - qst - 1);
|
||||
this.queryBytes = array.getBytes(qst + 1, offset - qst - 1);
|
||||
try {
|
||||
addParameter(array, qst + 1, offset - qst - 1);
|
||||
} catch (Exception e) {
|
||||
this.context.getLogger().log(Level.WARNING, "HttpRequest.addParameter error: " + array.toString(), e);
|
||||
}
|
||||
} else {
|
||||
this.requestURI = array.toDecodeString(index, offset - index, charset).trim();
|
||||
this.queryBytes = new byte[0];
|
||||
}
|
||||
index = ++offset;
|
||||
this.protocol = array.toString(index, array.size() - index, charset).trim();
|
||||
@@ -201,7 +210,9 @@ public class HttpRequest extends Request<HttpContext> {
|
||||
|
||||
private void parseBody() {
|
||||
if (this.boundary || bodyparsed) return;
|
||||
addParameter(array, 0, array.size());
|
||||
if (this.contentType != null && this.contentType.toLowerCase().contains("x-www-form-urlencoded")) {
|
||||
addParameter(array, 0, array.size());
|
||||
}
|
||||
bodyparsed = true;
|
||||
}
|
||||
|
||||
@@ -488,6 +499,7 @@ public class HttpRequest extends Request<HttpContext> {
|
||||
this.method = null;
|
||||
this.protocol = null;
|
||||
this.requestURI = null;
|
||||
this.queryBytes = null;
|
||||
this.contentType = null;
|
||||
this.host = null;
|
||||
this.connection = null;
|
||||
@@ -667,6 +679,15 @@ public class HttpRequest extends Request<HttpContext> {
|
||||
return requestURI;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求参数的byte[]
|
||||
*
|
||||
* @return byte[]
|
||||
*/
|
||||
public byte[] getQueryBytes() {
|
||||
return queryBytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* 截取getRequestURI最后的一个/后面的部分
|
||||
*
|
||||
@@ -1281,16 +1302,11 @@ public class HttpRequest extends Request<HttpContext> {
|
||||
* @return String
|
||||
*/
|
||||
public String getParametersToString(String prefix) {
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
getParameters().forEach((k, v) -> {
|
||||
if (sb.length() > 0) sb.append('&');
|
||||
try {
|
||||
sb.append(k).append('=').append(URLEncoder.encode(v, "UTF-8"));
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
});
|
||||
return (sb.length() > 0 && prefix != null) ? (prefix + sb) : sb.toString();
|
||||
byte[] rbs = queryBytes;
|
||||
if (rbs == null || rbs.length < 1) return "";
|
||||
Charset charset = this.context.getCharset();
|
||||
String str = charset == null ? new String(rbs, StandardCharsets.UTF_8) : new String(rbs, charset);
|
||||
return (prefix == null) ? str : (prefix + str);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -37,7 +37,7 @@ public class HttpResourceServlet extends HttpServlet {
|
||||
|
||||
public WatchThread(File root) throws IOException {
|
||||
this.root = root;
|
||||
this.setName("HttpResourceServlet-Watch-Thread");
|
||||
this.setName("Redkale-HttpResourceServlet-Watch-Thread");
|
||||
this.setDaemon(true);
|
||||
this.watcher = this.root.toPath().getFileSystem().newWatchService();
|
||||
}
|
||||
|
||||
@@ -50,6 +50,10 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
||||
|
||||
protected static final byte[] connectAliveBytes = "Connection: keep-alive\r\n".getBytes();
|
||||
|
||||
private static final byte[] fillContentLengthBytes = ("Content-Length: \r\n").getBytes();
|
||||
|
||||
private static final ZoneId ZONE_GMT = ZoneId.of("GMT");
|
||||
|
||||
private static final Set<OpenOption> options = new HashSet<>();
|
||||
|
||||
private static final Map<Integer, String> httpCodes = new HashMap<>();
|
||||
@@ -103,8 +107,6 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
||||
httpCodes.put(505, "HTTP Version Not Supported");
|
||||
}
|
||||
|
||||
private static final ZoneId ZONE_GMT = ZoneId.of("GMT");
|
||||
|
||||
private int status = 200;
|
||||
|
||||
private String contentType = "";
|
||||
@@ -113,9 +115,15 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
||||
|
||||
private HttpCookie[] cookies;
|
||||
|
||||
private boolean headsended = false;
|
||||
private int headWritedSize = -1; //0表示跳过header,正数表示header的字节长度。
|
||||
|
||||
private ByteBuffer headBuffer;
|
||||
|
||||
private int headLenPos = -1;
|
||||
|
||||
private BiFunction<HttpResponse, ByteBuffer[], ByteBuffer[]> bufferHandler;
|
||||
|
||||
private Supplier<ByteBuffer> bodyBufferSupplier;
|
||||
//------------------------------------------------
|
||||
|
||||
private final String plainContentType;
|
||||
@@ -163,6 +171,11 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
||||
this.hasRender = renders != null && !renders.isEmpty();
|
||||
this.onlyoneHttpRender = renders != null && renders.size() == 1 ? renders.get(0) : null;
|
||||
this.contentType = this.plainContentType;
|
||||
this.bodyBufferSupplier = () -> {
|
||||
if (headWritedSize >= 0 || bufferHandler != null) return channel.pollWriteBuffer(); //bufferHandler 需要cached的请求不能带上header
|
||||
if (contentLength < 0) contentLength = -2;
|
||||
return createHeader();
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -185,12 +198,18 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
||||
this.contentLength = -1;
|
||||
this.contentType = null;
|
||||
this.cookies = null;
|
||||
this.headsended = false;
|
||||
this.headWritedSize = -1;
|
||||
this.headBuffer = null;
|
||||
this.headLenPos = -1;
|
||||
this.header.clear();
|
||||
this.bufferHandler = null;
|
||||
return super.recycle();
|
||||
}
|
||||
|
||||
protected Supplier<ByteBuffer> getBodyBufferSupplier() {
|
||||
return bodyBufferSupplier;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void init(AsyncConnection channel) {
|
||||
super.init(channel);
|
||||
@@ -285,15 +304,6 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
||||
return context.loadAsyncHandlerCreator(handlerClass).create(createAsyncHandler());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取ByteBuffer生成器
|
||||
*
|
||||
* @return ByteBuffer生成器
|
||||
*/
|
||||
public Supplier<ByteBuffer> getBufferSupplier() {
|
||||
return getBodyBufferSupplier();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将对象以JSON格式输出
|
||||
*
|
||||
@@ -390,7 +400,9 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
||||
this.header.addValue("retcode", String.valueOf(ret.getRetcode()));
|
||||
this.header.addValue("retinfo", ret.getRetinfo());
|
||||
}
|
||||
finish(request.getJsonConvert().convertTo(getBodyBufferSupplier(), ret));
|
||||
Convert convert = ret == null ? null : ret.convert();
|
||||
if (convert == null || !(convert instanceof TextConvert)) convert = request.getJsonConvert();
|
||||
finish(convert.convertTo(getBodyBufferSupplier(), ret));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -497,6 +509,8 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
||||
context.getLogger().log(Level.WARNING, "HttpServlet finish File occur, force to close channel. request = " + getRequest(), e);
|
||||
finish(500, null);
|
||||
}
|
||||
} else if (obj instanceof org.redkale.service.RetResult) {
|
||||
finishJson((org.redkale.service.RetResult) obj);
|
||||
} else if (obj instanceof HttpResult) {
|
||||
HttpResult result = (HttpResult) obj;
|
||||
if (result.getContentType() != null) setContentType(result.getContentType());
|
||||
@@ -506,7 +520,9 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
||||
} else if (result.getResult() instanceof CharSequence) {
|
||||
finish(result.getResult().toString());
|
||||
} else {
|
||||
finish(convert, result.getResult());
|
||||
Convert cc = result.convert();
|
||||
if (cc == null || !(cc instanceof TextConvert)) cc = convert;
|
||||
finish(cc, result.getResult());
|
||||
}
|
||||
} else {
|
||||
if (hasRender) {
|
||||
@@ -631,7 +647,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
||||
public void finish(final String contentType, final byte[] bs) {
|
||||
if (isClosed()) return; //避免重复关闭
|
||||
final byte[] content = bs == null ? new byte[0] : bs;
|
||||
if (!this.headsended) {
|
||||
if (this.headWritedSize < 0) {
|
||||
this.contentType = contentType;
|
||||
this.contentLength = content.length;
|
||||
ByteBuffer headbuf = createHeader();
|
||||
@@ -675,7 +691,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
||||
@Override
|
||||
public void finish(boolean kill, ByteBuffer buffer) {
|
||||
if (isClosed()) return; //避免重复关闭
|
||||
if (!this.headsended) {
|
||||
if (this.headWritedSize < 0) {
|
||||
this.contentLength = buffer == null ? 0 : buffer.remaining();
|
||||
ByteBuffer headbuf = createHeader();
|
||||
headbuf.flip();
|
||||
@@ -713,7 +729,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
||||
if (bufs != null) buffers = bufs;
|
||||
}
|
||||
if (kill) refuseAlive();
|
||||
if (!this.headsended) {
|
||||
if (this.headWritedSize < 0) {
|
||||
long len = 0;
|
||||
for (ByteBuffer buf : buffers) {
|
||||
len += buf.remaining();
|
||||
@@ -730,6 +746,17 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
||||
super.finish(kill, newbuffers);
|
||||
}
|
||||
} else {
|
||||
if (this.headLenPos > 0 && buffers[0] == headBuffer) {
|
||||
long contentlen = -this.headWritedSize;
|
||||
for (ByteBuffer buf : buffers) {
|
||||
contentlen += buf.remaining();
|
||||
}
|
||||
byte[] lenBytes = String.valueOf(contentlen).getBytes();
|
||||
int start = this.headLenPos - lenBytes.length;
|
||||
for (int i = 0; i < lenBytes.length; i++) {
|
||||
headBuffer.put(start + i, lenBytes[i]);
|
||||
}
|
||||
}
|
||||
super.finish(kill, buffers);
|
||||
}
|
||||
}
|
||||
@@ -743,7 +770,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
||||
* @param handler 异步回调函数
|
||||
*/
|
||||
public <A> void sendBody(ByteBuffer buffer, A attachment, CompletionHandler<Integer, A> handler) {
|
||||
if (!this.headsended) {
|
||||
if (this.headWritedSize < 0) {
|
||||
if (this.contentLength < 0) this.contentLength = buffer == null ? 0 : buffer.remaining();
|
||||
ByteBuffer headbuf = createHeader();
|
||||
headbuf.flip();
|
||||
@@ -766,7 +793,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
||||
* @param handler 异步回调函数
|
||||
*/
|
||||
public <A> void sendBody(ByteBuffer[] buffers, A attachment, CompletionHandler<Integer, A> handler) {
|
||||
if (!this.headsended) {
|
||||
if (this.headWritedSize < 0) {
|
||||
if (this.contentLength < 0) {
|
||||
int len = 0;
|
||||
if (buffers != null && buffers.length > 0) {
|
||||
@@ -849,7 +876,9 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
||||
}
|
||||
this.contentLength = length;
|
||||
if (filename != null && !filename.isEmpty() && file != null) {
|
||||
addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
|
||||
if (this.header.getValue("Content-Disposition") == null) {
|
||||
addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
|
||||
}
|
||||
}
|
||||
this.contentType = MimeType.getByFilename(filename == null || filename.isEmpty() ? file.getName() : filename);
|
||||
if (this.contentType == null) this.contentType = "application/octet-stream";
|
||||
@@ -891,14 +920,19 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
||||
|
||||
//Header大小不能超过一个ByteBuffer的容量
|
||||
protected ByteBuffer createHeader() {
|
||||
this.headsended = true;
|
||||
ByteBuffer buffer = this.pollWriteReadBuffer();
|
||||
ByteBuffer buffer = this.channel.pollWriteBuffer();
|
||||
int oldpos = buffer.position();
|
||||
if (this.status == 200) {
|
||||
buffer.put(status200Bytes);
|
||||
} else {
|
||||
buffer.put(("HTTP/1.1 " + this.status + " " + httpCodes.get(this.status) + "\r\n").getBytes());
|
||||
}
|
||||
if (this.contentLength >= 0) buffer.put(("Content-Length: " + this.contentLength + "\r\n").getBytes());
|
||||
if (this.contentLength >= 0) {
|
||||
buffer.put(("Content-Length: " + this.contentLength + "\r\n").getBytes());
|
||||
} else if (this.contentLength == -2) {
|
||||
buffer.put(fillContentLengthBytes);
|
||||
this.headLenPos = buffer.position() - 2; //去掉\r\n
|
||||
}
|
||||
if (!this.request.isWebSocket()) {
|
||||
if (this.contentType == this.jsonContentType) {
|
||||
buffer.put(this.jsonContentTypeBytes);
|
||||
@@ -970,6 +1004,8 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
||||
}
|
||||
}
|
||||
buffer.put(LINE);
|
||||
this.headWritedSize = buffer.position() - oldpos;
|
||||
this.headBuffer = buffer;
|
||||
return buffer;
|
||||
}
|
||||
|
||||
@@ -995,7 +1031,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
||||
* @return HttpResponse
|
||||
*/
|
||||
public HttpResponse skipHeader() {
|
||||
this.headsended = true;
|
||||
this.headWritedSize = 0;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -1202,7 +1238,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
||||
|
||||
@Override
|
||||
public void failed(Throwable exc, ByteBuffer attachment) {
|
||||
bufferPool.accept(attachment);
|
||||
channel.offerBuffer(attachment);
|
||||
finish(true);
|
||||
try {
|
||||
filechannel.close();
|
||||
|
||||
@@ -8,6 +8,7 @@ package org.redkale.net.http;
|
||||
import java.io.Serializable;
|
||||
import java.net.HttpCookie;
|
||||
import java.util.*;
|
||||
import org.redkale.convert.*;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
|
||||
/**
|
||||
@@ -22,21 +23,28 @@ public class HttpResult<T> {
|
||||
|
||||
public static final String SESSIONID_COOKIENAME = HttpRequest.SESSIONID_NAME;
|
||||
|
||||
private Map<String, String> headers;
|
||||
protected Map<String, String> headers;
|
||||
|
||||
private List<HttpCookie> cookies;
|
||||
protected List<HttpCookie> cookies;
|
||||
|
||||
private String contentType;
|
||||
protected String contentType;
|
||||
|
||||
private T result;
|
||||
protected T result;
|
||||
|
||||
private int status = 0; //不设置则为 200
|
||||
protected int status = 0; //不设置则为 200
|
||||
|
||||
private String message;
|
||||
protected String message;
|
||||
|
||||
protected Convert convert;
|
||||
|
||||
public HttpResult() {
|
||||
}
|
||||
|
||||
public HttpResult(Convert convert, T result) {
|
||||
this.convert = convert;
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
public HttpResult(T result) {
|
||||
this.result = result;
|
||||
}
|
||||
@@ -82,6 +90,14 @@ public class HttpResult<T> {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Convert convert() {
|
||||
return convert;
|
||||
}
|
||||
|
||||
public void convert(Convert convert) {
|
||||
this.convert = convert;
|
||||
}
|
||||
|
||||
public Map<String, String> getHeaders() {
|
||||
return headers;
|
||||
}
|
||||
|
||||
@@ -56,6 +56,11 @@ public class HttpServer extends Server<String, HttpContext, HttpRequest, HttpRes
|
||||
super.init(config);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void postStart() {
|
||||
((HttpPrepareServlet) this.prepare).postStart(this.context, config);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy(final AnyValue config) throws Exception {
|
||||
super.destroy(config);
|
||||
|
||||
@@ -105,6 +105,10 @@ public class HttpServlet extends Servlet<HttpContext, HttpRequest, HttpResponse>
|
||||
void postDestroy(HttpContext context, AnyValue config) {
|
||||
}
|
||||
|
||||
//Server执行start后运行此方法
|
||||
public void postStart(HttpContext context, AnyValue config) {
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 预执行方法,在execute方法之前运行,设置当前用户信息,或者加入常规统计和基础检测,例如 : <br>
|
||||
|
||||
@@ -11,6 +11,7 @@ import java.lang.annotation.*;
|
||||
import static java.lang.annotation.ElementType.*;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
import java.lang.reflect.*;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.channels.CompletionHandler;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
@@ -859,7 +860,7 @@ public final class Rest {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (defmodulename.isEmpty() || (!pound && entrys.size() <= 6)) {
|
||||
if (defmodulename.isEmpty() || (!pound && entrys.size() <= 2)) {
|
||||
for (MappingEntry entry : entrys) {
|
||||
String suburl = (catalog.isEmpty() ? "/" : ("/" + catalog + "/")) + (defmodulename.isEmpty() ? "" : (defmodulename + "/")) + entry.name;
|
||||
urlpath += "," + suburl;
|
||||
@@ -1002,7 +1003,7 @@ public final class Rest {
|
||||
|
||||
RestHeader annhead = param.getAnnotation(RestHeader.class);
|
||||
if (annhead != null) {
|
||||
if (ptype != String.class) throw new RuntimeException("@RestHeader must on String Parameter in " + method);
|
||||
if (ptype != String.class && ptype != InetSocketAddress.class) throw new RuntimeException("@RestHeader must on String or InetSocketAddress Parameter in " + method);
|
||||
n = annhead.name();
|
||||
radix = annhead.radix();
|
||||
comment = annhead.comment();
|
||||
|
||||
@@ -10,7 +10,7 @@ import static java.lang.annotation.ElementType.*;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
/**
|
||||
* 只能注解于RestService类的方法的参数或参数内的String字段
|
||||
* 只能注解于RestService类的方法的参数或参数内的String、java.net.InetSocketAddress字段
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
|
||||
@@ -83,6 +83,8 @@ public abstract class WebSocket<G extends Serializable, T> {
|
||||
|
||||
WebSocketEngine _engine; //不可能为空
|
||||
|
||||
InetSocketAddress _sncpAddress; //分布式下不可为空
|
||||
|
||||
AsyncConnection _channel;//不可能为空
|
||||
|
||||
String _sessionid; //不可能为空
|
||||
@@ -826,6 +828,15 @@ public abstract class WebSocket<G extends Serializable, T> {
|
||||
return forceCloseWebSocket(getUserid()).thenApply((r) -> true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取分布式情况下的SNCP地址, 非分布式下为null
|
||||
*
|
||||
* @return InetSocketAddress sncpAddress
|
||||
*/
|
||||
public InetSocketAddress getSncpAddress() {
|
||||
return _sncpAddress;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Logger
|
||||
*
|
||||
|
||||
@@ -217,6 +217,7 @@ public abstract class WebSocketNode {
|
||||
*/
|
||||
@Local
|
||||
public CompletableFuture<Boolean> existsWebSocket(final Serializable userid) {
|
||||
if (userid instanceof WebSocketUserAddress) return existsWebSocket((WebSocketUserAddress) userid);
|
||||
CompletableFuture<Boolean> localFuture = null;
|
||||
if (this.localEngine != null) localFuture = CompletableFuture.completedFuture(localEngine.existsLocalWebSocket(userid));
|
||||
if (this.sncpNodeAddresses == null || this.remoteNode == null) {
|
||||
@@ -229,7 +230,7 @@ public abstract class WebSocketNode {
|
||||
CompletableFuture<Collection<InetSocketAddress>> addrsFuture = sncpNodeAddresses.getCollectionAsync(SOURCE_SNCP_USERID_PREFIX + userid, InetSocketAddress.class);
|
||||
if (semaphore != null) addrsFuture.whenComplete((r, e) -> releaseSemaphore());
|
||||
CompletableFuture<Boolean> remoteFuture = addrsFuture.thenCompose((Collection<InetSocketAddress> addrs) -> {
|
||||
if (logger.isLoggable(Level.FINEST)) logger.finest("websocket found userid:" + userid + " on " + addrs);
|
||||
//if (logger.isLoggable(Level.FINEST)) logger.finest("websocket found userid:" + userid + " on " + addrs);
|
||||
if (addrs == null || addrs.isEmpty()) return CompletableFuture.completedFuture(false);
|
||||
CompletableFuture<Boolean> future = null;
|
||||
for (InetSocketAddress addr : addrs) {
|
||||
@@ -242,6 +243,38 @@ public abstract class WebSocketNode {
|
||||
return localFuture == null ? remoteFuture : localFuture.thenCombine(remoteFuture, (a, b) -> a | b);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断指定用户是否WebSocket在线
|
||||
*
|
||||
* @param userAddress WebSocketUserAddress
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
@Local
|
||||
public CompletableFuture<Boolean> existsWebSocket(final WebSocketUserAddress userAddress) {
|
||||
CompletableFuture<Boolean> localFuture = null;
|
||||
if (this.localEngine != null) localFuture = CompletableFuture.completedFuture(localEngine.existsLocalWebSocket(userAddress.userid()));
|
||||
if (this.sncpNodeAddresses == null || this.remoteNode == null) {
|
||||
if (logger.isLoggable(Level.FINEST)) logger.finest("websocket remote node is null");
|
||||
//没有CacheSource就不会有分布式节点
|
||||
return localFuture;
|
||||
}
|
||||
Collection<InetSocketAddress> addrs = userAddress.sncpAddresses();
|
||||
if (addrs != null) addrs = new ArrayList<>(addrs); //不能修改参数内部值
|
||||
if (userAddress.sncpAddress() != null) {
|
||||
if (addrs == null) addrs = new ArrayList<>();
|
||||
addrs.add(userAddress.sncpAddress());
|
||||
}
|
||||
if (addrs == null || addrs.isEmpty()) return CompletableFuture.completedFuture(false);
|
||||
CompletableFuture<Boolean> future = null;
|
||||
for (InetSocketAddress addr : addrs) {
|
||||
if (addr == null || addr.equals(localSncpAddress)) continue;
|
||||
future = future == null ? remoteNode.existsWebSocket(userAddress.userid(), addr)
|
||||
: future.thenCombine(remoteNode.existsWebSocket(userAddress.userid(), addr), (a, b) -> a | b);
|
||||
}
|
||||
return future == null ? CompletableFuture.completedFuture(false) : future;
|
||||
}
|
||||
|
||||
/**
|
||||
* 强制关闭用户WebSocket
|
||||
*
|
||||
@@ -251,17 +284,45 @@ public abstract class WebSocketNode {
|
||||
*/
|
||||
@Local
|
||||
public CompletableFuture<Integer> forceCloseWebSocket(final Serializable userid) {
|
||||
return forceCloseWebSocket(userid, (WebSocketUserAddress) null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 强制关闭用户WebSocket
|
||||
*
|
||||
* @param userAddress WebSocketUserAddress
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
@Local
|
||||
public CompletableFuture<Integer> forceCloseWebSocket(final WebSocketUserAddress userAddress) {
|
||||
return forceCloseWebSocket(null, userAddress);
|
||||
}
|
||||
|
||||
private CompletableFuture<Integer> forceCloseWebSocket(final Serializable userid, final WebSocketUserAddress userAddress) {
|
||||
CompletableFuture<Integer> localFuture = null;
|
||||
if (this.localEngine != null) localFuture = CompletableFuture.completedFuture(localEngine.forceCloseLocalWebSocket(userid));
|
||||
if (this.localEngine != null) localFuture = CompletableFuture.completedFuture(localEngine.forceCloseLocalWebSocket(userAddress == null ? userid : userAddress.userid()));
|
||||
if (this.sncpNodeAddresses == null || this.remoteNode == null) {
|
||||
if (logger.isLoggable(Level.FINEST)) logger.finest("websocket remote node is null");
|
||||
//没有CacheSource就不会有分布式节点
|
||||
return localFuture;
|
||||
}
|
||||
//远程节点关闭
|
||||
tryAcquireSemaphore();
|
||||
CompletableFuture<Collection<InetSocketAddress>> addrsFuture = sncpNodeAddresses.getCollectionAsync(SOURCE_SNCP_USERID_PREFIX + userid, InetSocketAddress.class);
|
||||
if (semaphore != null) addrsFuture.whenComplete((r, e) -> releaseSemaphore());
|
||||
CompletableFuture<Collection<InetSocketAddress>> addrsFuture;
|
||||
if (userAddress == null) {
|
||||
tryAcquireSemaphore();
|
||||
addrsFuture = sncpNodeAddresses.getCollectionAsync(SOURCE_SNCP_USERID_PREFIX + userid, InetSocketAddress.class);
|
||||
if (semaphore != null) addrsFuture.whenComplete((r, e) -> releaseSemaphore());
|
||||
} else {
|
||||
Collection<InetSocketAddress> addrs = userAddress.sncpAddresses();
|
||||
if (addrs != null) addrs = new ArrayList<>(addrs); //不能修改参数内部值
|
||||
if (userAddress.sncpAddress() != null) {
|
||||
if (addrs == null) addrs = new ArrayList<>();
|
||||
addrs.add(userAddress.sncpAddress());
|
||||
}
|
||||
if (addrs == null || addrs.isEmpty()) return CompletableFuture.completedFuture(0);
|
||||
addrsFuture = CompletableFuture.completedFuture(addrs);
|
||||
}
|
||||
CompletableFuture<Integer> remoteFuture = addrsFuture.thenCompose((Collection<InetSocketAddress> addrs) -> {
|
||||
if (logger.isLoggable(Level.FINEST)) logger.finest("websocket found userid:" + userid + " on " + addrs);
|
||||
if (addrs == null || addrs.isEmpty()) return CompletableFuture.completedFuture(0);
|
||||
@@ -292,88 +353,88 @@ public abstract class WebSocketNode {
|
||||
* 向指定用户发送消息,先发送本地连接,再发送远程连接 <br>
|
||||
* 如果当前WebSocketNode是远程模式,此方法只发送远程连接
|
||||
*
|
||||
* @param message 消息内容
|
||||
* @param userids Stream
|
||||
* @param message 消息内容
|
||||
* @param useridOrAddrs Stream
|
||||
*
|
||||
* @return 为0表示成功, 其他值表示部分发送异常
|
||||
*/
|
||||
@Local
|
||||
public final CompletableFuture<Integer> sendMessage(Object message, final Stream<? extends Serializable> userids) {
|
||||
return sendMessage((Convert) null, message, true, userids);
|
||||
public final CompletableFuture<Integer> sendMessage(Object message, final Stream<? extends Serializable> useridOrAddrs) {
|
||||
return sendMessage((Convert) null, message, true, useridOrAddrs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 向指定用户发送消息,先发送本地连接,再发送远程连接 <br>
|
||||
* 如果当前WebSocketNode是远程模式,此方法只发送远程连接
|
||||
*
|
||||
* @param message 消息内容
|
||||
* @param userids Serializable[]
|
||||
* @param message 消息内容
|
||||
* @param useridOrAddrs Serializable[]
|
||||
*
|
||||
* @return 为0表示成功, 其他值表示部分发送异常
|
||||
*/
|
||||
@Local
|
||||
public final CompletableFuture<Integer> sendMessage(Object message, final Serializable... userids) {
|
||||
return sendMessage((Convert) null, message, true, userids);
|
||||
public final CompletableFuture<Integer> sendMessage(Object message, final Serializable... useridOrAddrs) {
|
||||
return sendMessage((Convert) null, message, true, useridOrAddrs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 向指定用户发送消息,先发送本地连接,再发送远程连接 <br>
|
||||
* 如果当前WebSocketNode是远程模式,此方法只发送远程连接
|
||||
*
|
||||
* @param convert Convert
|
||||
* @param message 消息内容
|
||||
* @param userids Stream
|
||||
* @param convert Convert
|
||||
* @param message 消息内容
|
||||
* @param useridOrAddrs Stream
|
||||
*
|
||||
* @return 为0表示成功, 其他值表示部分发送异常
|
||||
*/
|
||||
@Local
|
||||
public final CompletableFuture<Integer> sendMessage(final Convert convert, Object message, final Stream<? extends Serializable> userids) {
|
||||
return sendMessage(convert, message, true, userids);
|
||||
public final CompletableFuture<Integer> sendMessage(final Convert convert, Object message, final Stream<? extends Serializable> useridOrAddrs) {
|
||||
return sendMessage(convert, message, true, useridOrAddrs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 向指定用户发送消息,先发送本地连接,再发送远程连接 <br>
|
||||
* 如果当前WebSocketNode是远程模式,此方法只发送远程连接
|
||||
*
|
||||
* @param convert Convert
|
||||
* @param message 消息内容
|
||||
* @param userids Serializable[]
|
||||
* @param convert Convert
|
||||
* @param message 消息内容
|
||||
* @param useridOrAddrs Serializable[]
|
||||
*
|
||||
* @return 为0表示成功, 其他值表示部分发送异常
|
||||
*/
|
||||
@Local
|
||||
public final CompletableFuture<Integer> sendMessage(final Convert convert, Object message, final Serializable... userids) {
|
||||
return sendMessage(convert, message, true, userids);
|
||||
public final CompletableFuture<Integer> sendMessage(final Convert convert, Object message, final Serializable... useridOrAddrs) {
|
||||
return sendMessage(convert, message, true, useridOrAddrs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 向指定用户发送消息,先发送本地连接,再发送远程连接 <br>
|
||||
* 如果当前WebSocketNode是远程模式,此方法只发送远程连接
|
||||
*
|
||||
* @param message 消息内容
|
||||
* @param last 是否最后一条
|
||||
* @param userids Stream
|
||||
* @param message 消息内容
|
||||
* @param last 是否最后一条
|
||||
* @param useridOrAddrs Stream
|
||||
*
|
||||
* @return 为0表示成功, 其他值表示部分发送异常
|
||||
*/
|
||||
@Local
|
||||
public final CompletableFuture<Integer> sendMessage(final Object message, final boolean last, final Stream<? extends Serializable> userids) {
|
||||
return sendMessage((Convert) null, message, last, userids);
|
||||
public final CompletableFuture<Integer> sendMessage(final Object message, final boolean last, final Stream<? extends Serializable> useridOrAddrs) {
|
||||
return sendMessage((Convert) null, message, last, useridOrAddrs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 向指定用户发送消息,先发送本地连接,再发送远程连接 <br>
|
||||
* 如果当前WebSocketNode是远程模式,此方法只发送远程连接
|
||||
*
|
||||
* @param message 消息内容
|
||||
* @param last 是否最后一条
|
||||
* @param userids Serializable[]
|
||||
* @param message 消息内容
|
||||
* @param last 是否最后一条
|
||||
* @param useridOrAddrs Serializable[]
|
||||
*
|
||||
* @return 为0表示成功, 其他值表示部分发送异常
|
||||
*/
|
||||
@Local
|
||||
public final CompletableFuture<Integer> sendMessage(final Object message, final boolean last, final Serializable... userids) {
|
||||
return sendMessage((Convert) null, message, last, userids);
|
||||
public final CompletableFuture<Integer> sendMessage(final Object message, final boolean last, final Serializable... useridOrAddrs) {
|
||||
return sendMessage((Convert) null, message, last, useridOrAddrs);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -411,6 +472,13 @@ public abstract class WebSocketNode {
|
||||
@Local
|
||||
public CompletableFuture<Integer> sendMessage(final Convert convert, final Object message0, final boolean last, final Serializable... userids) {
|
||||
if (userids == null || userids.length < 1) return CompletableFuture.completedFuture(RETCODE_GROUP_EMPTY);
|
||||
if (userids[0] instanceof WebSocketUserAddress) {
|
||||
WebSocketUserAddress[] useraddrs = new WebSocketUserAddress[userids.length];
|
||||
for (int i = 0; i < useraddrs.length; i++) {
|
||||
useraddrs[i] = (WebSocketUserAddress) userids[i];
|
||||
}
|
||||
return sendMessage(convert, message0, last, useraddrs);
|
||||
}
|
||||
if (message0 instanceof CompletableFuture) return ((CompletableFuture) message0).thenApply(msg -> sendMessage(convert, msg, last, userids));
|
||||
final Object message = (convert == null || message0 instanceof WebSocketPacket) ? message0 : ((convert instanceof TextConvert) ? new WebSocketPacket(((TextConvert) convert).convertTo(message0), last) : new WebSocketPacket(((BinaryConvert) convert).convertTo(message0), last));
|
||||
if (this.localEngine != null && this.sncpNodeAddresses == null) { //本地模式且没有分布式
|
||||
@@ -446,9 +514,9 @@ public abstract class WebSocketNode {
|
||||
}
|
||||
CompletableFuture<Integer> future = null;
|
||||
for (Map.Entry<InetSocketAddress, List<Serializable>> en : addrUsers.entrySet()) {
|
||||
Serializable[] us = en.getValue().toArray(new Serializable[en.getValue().size()]);
|
||||
future = future == null ? sendOneAddrMessage(en.getKey(), remoteMessage, last, us)
|
||||
: future.thenCombine(sendOneAddrMessage(en.getKey(), remoteMessage, last, us), (a, b) -> a | b);
|
||||
Serializable[] oneaddrUserids = en.getValue().toArray(new Serializable[en.getValue().size()]);
|
||||
future = future == null ? sendOneAddrMessage(en.getKey(), remoteMessage, last, oneaddrUserids)
|
||||
: future.thenCombine(sendOneAddrMessage(en.getKey(), remoteMessage, last, oneaddrUserids), (a, b) -> a | b);
|
||||
}
|
||||
return future == null ? CompletableFuture.completedFuture(RETCODE_GROUP_EMPTY) : future;
|
||||
});
|
||||
@@ -456,10 +524,45 @@ public abstract class WebSocketNode {
|
||||
return rsfuture == null ? CompletableFuture.completedFuture(RETCODE_GROUP_EMPTY) : rsfuture;
|
||||
}
|
||||
|
||||
/**
|
||||
* 向指定用户发送消息,先发送本地连接,再发送远程连接 <br>
|
||||
* 如果当前WebSocketNode是远程模式,此方法只发送远程连接
|
||||
*
|
||||
* @param convert Convert
|
||||
* @param message0 消息内容
|
||||
* @param last 是否最后一条
|
||||
* @param useraddrs WebSocketUserAddress[]
|
||||
*
|
||||
* @return 为0表示成功, 其他值表示部分发送异常
|
||||
*/
|
||||
@Local
|
||||
public CompletableFuture<Integer> sendMessage(final Convert convert, final Object message0, final boolean last, final WebSocketUserAddress... useraddrs) {
|
||||
if (useraddrs == null || useraddrs.length < 1) return CompletableFuture.completedFuture(RETCODE_GROUP_EMPTY);
|
||||
if (message0 instanceof CompletableFuture) return ((CompletableFuture) message0).thenApply(msg -> sendMessage(convert, msg, last, useraddrs));
|
||||
final Object message = (convert == null || message0 instanceof WebSocketPacket) ? message0 : ((convert instanceof TextConvert) ? new WebSocketPacket(((TextConvert) convert).convertTo(message0), last) : new WebSocketPacket(((BinaryConvert) convert).convertTo(message0), last));
|
||||
if (this.localEngine != null && this.sncpNodeAddresses == null) { //本地模式且没有分布式
|
||||
return this.localEngine.sendLocalMessage(message, last, userAddressToUserids(useraddrs));
|
||||
}
|
||||
|
||||
final Object remoteMessage = formatRemoteMessage(message);
|
||||
final Map<InetSocketAddress, List<Serializable>> addrUsers = userAddressToAddrMap(useraddrs);
|
||||
if (logger.isLoggable(Level.FINEST)) {
|
||||
logger.finest("websocket(localaddr=" + localSncpAddress + ", useraddrs=" + JsonConvert.root().convertTo(useraddrs) + ") found message-addr-userids: " + addrUsers);
|
||||
}
|
||||
CompletableFuture<Integer> future = null;
|
||||
for (Map.Entry<InetSocketAddress, List<Serializable>> en : addrUsers.entrySet()) {
|
||||
Serializable[] oneaddrUserids = en.getValue().toArray(new Serializable[en.getValue().size()]);
|
||||
future = future == null ? sendOneAddrMessage(en.getKey(), remoteMessage, last, oneaddrUserids)
|
||||
: future.thenCombine(sendOneAddrMessage(en.getKey(), remoteMessage, last, oneaddrUserids), (a, b) -> a | b);
|
||||
}
|
||||
return future == null ? CompletableFuture.completedFuture(RETCODE_GROUP_EMPTY) : future;
|
||||
|
||||
}
|
||||
|
||||
protected CompletableFuture<Integer> sendOneUserMessage(final Object message, final boolean last, final Serializable userid) {
|
||||
if (message instanceof CompletableFuture) return ((CompletableFuture) message).thenApply(msg -> sendOneUserMessage(msg, last, userid));
|
||||
if (logger.isLoggable(Level.FINEST)) {
|
||||
logger.finest("websocket want send message {userid:" + userid + ", content:'" + (message instanceof WebSocketPacket ? ((WebSocketPacket) message).toSimpleString() : JsonConvert.root().convertTo(message)) + "'} from locale node to " + ((this.localEngine != null) ? "locale" : "remote") + " engine");
|
||||
logger.finest("websocket want send message {userid:" + userid + ", content:" + (message instanceof WebSocketPacket ? ((WebSocketPacket) message).toSimpleString() : (message instanceof CharSequence ? message : JsonConvert.root().convertTo(message))) + "} from locale node to " + ((this.localEngine != null) ? "locale" : "remote") + " engine");
|
||||
}
|
||||
CompletableFuture<Integer> localFuture = null;
|
||||
if (this.localEngine != null) localFuture = localEngine.sendLocalMessage(message, last, userid);
|
||||
@@ -492,8 +595,8 @@ public abstract class WebSocketNode {
|
||||
|
||||
protected CompletableFuture<Integer> sendOneAddrMessage(final InetSocketAddress sncpAddr, final Object message, final boolean last, final Serializable... userids) {
|
||||
if (message instanceof CompletableFuture) return ((CompletableFuture) message).thenApply(msg -> sendOneAddrMessage(sncpAddr, msg, last, userids));
|
||||
if (logger.isLoggable(Level.FINEST)) {
|
||||
logger.finest("websocket want send message {userids:" + JsonConvert.root().convertTo(userids) + ", sncpaddr:" + sncpAddr + ", content:'" + (message instanceof WebSocketPacket ? ((WebSocketPacket) message).toSimpleString() : JsonConvert.root().convertTo(message)) + "'} from locale node to " + ((this.localEngine != null) ? "locale" : "remote") + " engine");
|
||||
if (logger.isLoggable(Level.FINEST) && this.localEngine == null) { //只打印远程模式的
|
||||
logger.finest("websocket want send message {userids:" + JsonConvert.root().convertTo(userids) + ", sncpaddr:" + sncpAddr + ", content:" + (message instanceof WebSocketPacket ? ((WebSocketPacket) message).toSimpleString() : (message instanceof CharSequence ? message : JsonConvert.root().convertTo(message))) + "} from locale node to " + ((this.localEngine != null) ? "locale" : "remote") + " engine");
|
||||
}
|
||||
if (Objects.equals(sncpAddr, this.localSncpAddress)) {
|
||||
return this.localEngine == null ? CompletableFuture.completedFuture(RETCODE_GROUP_EMPTY) : localEngine.sendLocalMessage(message, last, userids);
|
||||
@@ -507,6 +610,32 @@ public abstract class WebSocketNode {
|
||||
return remoteNode.sendMessage(sncpAddr, remoteMessage, last, userids);
|
||||
}
|
||||
|
||||
protected Serializable[] userAddressToUserids(WebSocketUserAddress... useraddrs) {
|
||||
if (useraddrs == null || useraddrs.length == 1) return new Serializable[0];
|
||||
Set<Serializable> set = new HashSet<>();
|
||||
for (WebSocketUserAddress userAddress : useraddrs) {
|
||||
set.add(userAddress.userid());
|
||||
}
|
||||
return set.toArray(new Serializable[set.size()]);
|
||||
}
|
||||
|
||||
protected Map<InetSocketAddress, List<Serializable>> userAddressToAddrMap(WebSocketUserAddress... useraddrs) {
|
||||
final Map<InetSocketAddress, List<Serializable>> addrUsers = new HashMap<>();
|
||||
for (WebSocketUserAddress userAddress : useraddrs) {
|
||||
if (userAddress.sncpAddress() != null) {
|
||||
addrUsers.computeIfAbsent(userAddress.sncpAddress(), k -> new ArrayList<>()).add(userAddress.userid());
|
||||
}
|
||||
if (userAddress.sncpAddresses() != null) {
|
||||
for (InetSocketAddress addr : userAddress.sncpAddresses()) {
|
||||
if (addr != null) {
|
||||
addrUsers.computeIfAbsent(addr, k -> new ArrayList<>()).add(userAddress.userid());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return addrUsers;
|
||||
}
|
||||
|
||||
/**
|
||||
* 广播消息, 给所有人发消息
|
||||
*
|
||||
@@ -678,6 +807,13 @@ public abstract class WebSocketNode {
|
||||
@Local
|
||||
public CompletableFuture<Integer> sendAction(final WebSocketAction action, final Serializable... userids) {
|
||||
if (userids == null || userids.length < 1) return CompletableFuture.completedFuture(RETCODE_GROUP_EMPTY);
|
||||
if (userids[0] instanceof WebSocketUserAddress) {
|
||||
WebSocketUserAddress[] useraddrs = new WebSocketUserAddress[userids.length];
|
||||
for (int i = 0; i < useraddrs.length; i++) {
|
||||
useraddrs[i] = (WebSocketUserAddress) userids[i];
|
||||
}
|
||||
return sendAction(action, useraddrs);
|
||||
}
|
||||
if (this.localEngine != null && this.sncpNodeAddresses == null) { //本地模式且没有分布式
|
||||
return this.localEngine.sendLocalAction(action, userids);
|
||||
}
|
||||
@@ -710,9 +846,9 @@ public abstract class WebSocketNode {
|
||||
}
|
||||
CompletableFuture<Integer> future = null;
|
||||
for (Map.Entry<InetSocketAddress, List<Serializable>> en : addrUsers.entrySet()) {
|
||||
Serializable[] us = en.getValue().toArray(new Serializable[en.getValue().size()]);
|
||||
future = future == null ? sendOneAddrAction(en.getKey(), action, us)
|
||||
: future.thenCombine(sendOneAddrAction(en.getKey(), action, us), (a, b) -> a | b);
|
||||
Serializable[] oneaddrUserids = en.getValue().toArray(new Serializable[en.getValue().size()]);
|
||||
future = future == null ? sendOneAddrAction(en.getKey(), action, oneaddrUserids)
|
||||
: future.thenCombine(sendOneAddrAction(en.getKey(), action, oneaddrUserids), (a, b) -> a | b);
|
||||
}
|
||||
return future == null ? CompletableFuture.completedFuture(RETCODE_GROUP_EMPTY) : future;
|
||||
});
|
||||
@@ -720,6 +856,35 @@ public abstract class WebSocketNode {
|
||||
return rsfuture == null ? CompletableFuture.completedFuture(RETCODE_GROUP_EMPTY) : rsfuture;
|
||||
}
|
||||
|
||||
/**
|
||||
* 向指定用户发送操作,先发送本地连接,再发送远程连接 <br>
|
||||
* 如果当前WebSocketNode是远程模式,此方法只发送远程连接
|
||||
*
|
||||
* @param action 操作参数
|
||||
* @param useraddrs WebSocketUserAddress[]
|
||||
*
|
||||
* @return 为0表示成功, 其他值表示部分发送异常
|
||||
*/
|
||||
@Local
|
||||
public CompletableFuture<Integer> sendAction(final WebSocketAction action, final WebSocketUserAddress... useraddrs) {
|
||||
if (useraddrs == null || useraddrs.length < 1) return CompletableFuture.completedFuture(RETCODE_GROUP_EMPTY);
|
||||
if (this.localEngine != null && this.sncpNodeAddresses == null) { //本地模式且没有分布式
|
||||
return this.localEngine.sendLocalAction(action, userAddressToUserids(useraddrs));
|
||||
}
|
||||
|
||||
final Map<InetSocketAddress, List<Serializable>> addrUsers = userAddressToAddrMap(useraddrs);
|
||||
if (logger.isLoggable(Level.FINEST)) {
|
||||
logger.finest("websocket(localaddr=" + localSncpAddress + ", useraddrs=" + JsonConvert.root().convertTo(useraddrs) + ") found action-userid-addrs: " + addrUsers);
|
||||
}
|
||||
CompletableFuture<Integer> future = null;
|
||||
for (Map.Entry<InetSocketAddress, List<Serializable>> en : addrUsers.entrySet()) {
|
||||
Serializable[] oneaddrUserids = en.getValue().toArray(new Serializable[en.getValue().size()]);
|
||||
future = future == null ? sendOneAddrAction(en.getKey(), action, oneaddrUserids)
|
||||
: future.thenCombine(sendOneAddrAction(en.getKey(), action, oneaddrUserids), (a, b) -> a | b);
|
||||
}
|
||||
return future == null ? CompletableFuture.completedFuture(RETCODE_GROUP_EMPTY) : future;
|
||||
}
|
||||
|
||||
protected CompletableFuture<Integer> sendOneUserAction(final WebSocketAction action, final Serializable userid) {
|
||||
if (logger.isLoggable(Level.FINEST)) {
|
||||
logger.finest("websocket want send action {userid:" + userid + ", action:" + action + "} from locale node to " + ((this.localEngine != null) ? "locale" : "remote") + " engine");
|
||||
@@ -753,7 +918,7 @@ public abstract class WebSocketNode {
|
||||
}
|
||||
|
||||
protected CompletableFuture<Integer> sendOneAddrAction(final InetSocketAddress sncpAddr, final WebSocketAction action, final Serializable... userids) {
|
||||
if (logger.isLoggable(Level.FINEST)) {
|
||||
if (logger.isLoggable(Level.FINEST) && this.localEngine == null) { //只打印远程模式的
|
||||
logger.finest("websocket want send action {userids:" + JsonConvert.root().convertTo(userids) + ", sncpaddr:" + sncpAddr + ", action:" + action + " from locale node to " + ((this.localEngine != null) ? "locale" : "remote") + " engine");
|
||||
}
|
||||
if (Objects.equals(sncpAddr, this.localSncpAddress)) {
|
||||
|
||||
@@ -209,6 +209,7 @@ public abstract class WebSocketServlet extends HttpServlet implements Resourcabl
|
||||
webSocket._sendConvert = sendConvert;
|
||||
webSocket._remoteAddress = request.getRemoteAddress();
|
||||
webSocket._remoteAddr = request.getRemoteAddr();
|
||||
webSocket._sncpAddress = this.node.localSncpAddress;
|
||||
initRestWebSocket(webSocket);
|
||||
CompletableFuture<String> sessionFuture = webSocket.onOpen(request);
|
||||
if (sessionFuture == null) {
|
||||
|
||||
107
src/org/redkale/net/http/WebSocketUserAddress.java
Normal file
107
src/org/redkale/net/http/WebSocketUserAddress.java
Normal file
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.net.http;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.Collection;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
|
||||
/**
|
||||
* userid 与 sncpaddress组合对象
|
||||
*
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public interface WebSocketUserAddress extends Serializable {
|
||||
|
||||
Serializable userid();
|
||||
|
||||
InetSocketAddress sncpAddress();
|
||||
|
||||
Collection<InetSocketAddress> sncpAddresses();
|
||||
|
||||
public static WebSocketUserAddress create(WebSocketUserAddress userAddress) {
|
||||
return new SimpleWebSocketUserAddress(userAddress);
|
||||
}
|
||||
|
||||
public static WebSocketUserAddress create(Serializable userid, InetSocketAddress sncpAddress) {
|
||||
return new SimpleWebSocketUserAddress(userid, sncpAddress, null);
|
||||
}
|
||||
|
||||
public static WebSocketUserAddress create(Serializable userid, Collection<InetSocketAddress> sncpAddresses) {
|
||||
return new SimpleWebSocketUserAddress(userid, null, sncpAddresses);
|
||||
}
|
||||
|
||||
public static class SimpleWebSocketUserAddress implements WebSocketUserAddress {
|
||||
|
||||
private Serializable userid;
|
||||
|
||||
private InetSocketAddress sncpAddress;
|
||||
|
||||
private Collection<InetSocketAddress> sncpAddresses;
|
||||
|
||||
public SimpleWebSocketUserAddress() {
|
||||
}
|
||||
|
||||
public SimpleWebSocketUserAddress(Serializable userid, InetSocketAddress sncpAddress, Collection<InetSocketAddress> sncpAddresses) {
|
||||
this.userid = userid;
|
||||
this.sncpAddress = sncpAddress;
|
||||
this.sncpAddresses = sncpAddresses;
|
||||
}
|
||||
|
||||
public SimpleWebSocketUserAddress(WebSocketUserAddress userAddress) {
|
||||
if (userAddress == null) return;
|
||||
this.userid = userAddress.userid();
|
||||
this.sncpAddress = userAddress.sncpAddress();
|
||||
this.sncpAddresses = userAddress.sncpAddresses();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Serializable userid() {
|
||||
return userid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InetSocketAddress sncpAddress() {
|
||||
return sncpAddress;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<InetSocketAddress> sncpAddresses() {
|
||||
return sncpAddresses;
|
||||
}
|
||||
|
||||
public Serializable getUserid() {
|
||||
return userid;
|
||||
}
|
||||
|
||||
public void setUserid(Serializable userid) {
|
||||
this.userid = userid;
|
||||
}
|
||||
|
||||
public InetSocketAddress getSncpAddress() {
|
||||
return sncpAddress;
|
||||
}
|
||||
|
||||
public void setSncpAddress(InetSocketAddress sncpAddress) {
|
||||
this.sncpAddress = sncpAddress;
|
||||
}
|
||||
|
||||
public Collection<InetSocketAddress> getSncpAddresses() {
|
||||
return sncpAddresses;
|
||||
}
|
||||
|
||||
public void setSncpAddresses(Collection<InetSocketAddress> sncpAddresses) {
|
||||
this.sncpAddresses = sncpAddresses;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JsonConvert.root().convertTo(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -332,9 +332,10 @@ public final class SncpClient {
|
||||
return bsonConvert.convertFrom(action.handlerFuncParamIndex >= 0 ? Object.class : action.resultTypes, reader);
|
||||
} catch (RpcRemoteException re) {
|
||||
throw re;
|
||||
} catch (InterruptedException | ExecutionException | TimeoutException e) {
|
||||
//logger.log(Level.SEVERE, actions[index].method + " sncp (params: " + jsonConvert.convertTo(params) + ") remote error", e);
|
||||
throw new RpcRemoteException(actions[index].method + " sncp remote error", e);
|
||||
} catch (TimeoutException e) {
|
||||
throw new RpcRemoteException(actions[index].method + " sncp remote timeout, params=" + JsonConvert.root().convertTo(params));
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
throw new RpcRemoteException(actions[index].method + " sncp remote error, params=" + JsonConvert.root().convertTo(params), e);
|
||||
} finally {
|
||||
bsonConvert.offerBsonReader(reader);
|
||||
}
|
||||
@@ -348,7 +349,12 @@ public final class SncpClient {
|
||||
final BsonWriter writer = bsonConvert.pollBsonWriter(transport.getBufferSupplier()); // 将head写入
|
||||
writer.writeTo(DEFAULT_HEADER);
|
||||
for (int i = 0; i < params.length; i++) { //params 可能包含: 3 个 boolean
|
||||
bsonConvert.convertTo(writer, CompletionHandler.class.isAssignableFrom(myparamclass[i]) ? CompletionHandler.class : myparamtypes[i], params[i]);
|
||||
BsonConvert bcc = bsonConvert;
|
||||
if (params[i] instanceof org.redkale.service.RetResult) {
|
||||
org.redkale.convert.Convert cc = ((org.redkale.service.RetResult) params[i]).convert();
|
||||
if (cc instanceof BsonConvert) bcc = (BsonConvert) cc;
|
||||
}
|
||||
bcc.convertTo(writer, CompletionHandler.class.isAssignableFrom(myparamclass[i]) ? CompletionHandler.class : myparamtypes[i], params[i]);
|
||||
}
|
||||
final int reqBodyLength = writer.count() - HEADER_SIZE; //body总长度
|
||||
final long seqid = System.nanoTime();
|
||||
@@ -358,12 +364,12 @@ public final class SncpClient {
|
||||
return connFuture.thenCompose(conn0 -> {
|
||||
final CompletableFuture<byte[]> future = new CompletableFuture();
|
||||
if (conn0 == null) {
|
||||
future.completeExceptionally(new RuntimeException("sncp " + (conn0 == null ? addr : conn0.getRemoteAddress()) + " cannot connect"));
|
||||
future.completeExceptionally(new RpcRemoteException("sncp " + (conn0 == null ? addr : conn0.getRemoteAddress()) + " cannot connect, params=" + JsonConvert.root().convertTo(params)));
|
||||
return future;
|
||||
}
|
||||
if (!conn0.isOpen()) {
|
||||
conn0.dispose();
|
||||
future.completeExceptionally(new RuntimeException("sncp " + (conn0 == null ? addr : conn0.getRemoteAddress()) + " cannot connect"));
|
||||
future.completeExceptionally(new RpcRemoteException("sncp " + (conn0 == null ? addr : conn0.getRemoteAddress()) + " cannot connect, params=" + JsonConvert.root().convertTo(params)));
|
||||
return future;
|
||||
}
|
||||
final AsyncConnection conn = conn0;
|
||||
@@ -403,7 +409,7 @@ public final class SncpClient {
|
||||
public void completed(Integer count, ByteBuffer buffer) {
|
||||
try {
|
||||
if (count < 1 && buffer.remaining() == buffer.limit()) { //没有数据可读
|
||||
future.completeExceptionally(new RpcRemoteException(action.method + " sncp[" + conn.getRemoteAddress() + "] remote no response data"));
|
||||
future.completeExceptionally(new RpcRemoteException(action.method + " sncp[" + conn.getRemoteAddress() + "] remote no response data, params=" + JsonConvert.root().convertTo(params)));
|
||||
conn.offerBuffer(buffer);
|
||||
transport.offerConnection(true, conn);
|
||||
return;
|
||||
@@ -433,7 +439,7 @@ public final class SncpClient {
|
||||
final int respBodyLength = buffer.getInt();
|
||||
final int retcode = buffer.getInt();
|
||||
if (retcode != 0) {
|
||||
logger.log(Level.SEVERE, action.method + " sncp (params: " + convert.convertTo(params) + ") deal error (retcode=" + retcode + ", retinfo=" + SncpResponse.getRetCodeInfo(retcode) + ")");
|
||||
logger.log(Level.SEVERE, action.method + " sncp (params: " + convert.convertTo(params) + ") deal error (retcode=" + retcode + ", retinfo=" + SncpResponse.getRetCodeInfo(retcode) + "), params=" + JsonConvert.root().convertTo(params));
|
||||
throw new RuntimeException("remote service(" + action.method + ") deal error (retcode=" + retcode + ", retinfo=" + SncpResponse.getRetCodeInfo(retcode) + ")");
|
||||
}
|
||||
|
||||
@@ -451,7 +457,7 @@ public final class SncpClient {
|
||||
success();
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
future.completeExceptionally(new RuntimeException(action.method + " sncp[" + conn.getRemoteAddress() + "] remote response error"));
|
||||
future.completeExceptionally(new RpcRemoteException(action.method + " sncp[" + conn.getRemoteAddress() + "] remote response error, params=" + JsonConvert.root().convertTo(params)));
|
||||
transport.offerConnection(true, conn);
|
||||
if (handler != null) {
|
||||
final Object handlerAttach = action.handlerAttachParamIndex >= 0 ? params[action.handlerAttachParamIndex] : null;
|
||||
@@ -487,27 +493,27 @@ public final class SncpClient {
|
||||
|
||||
@Override
|
||||
public void failed(Throwable exc, ByteBuffer attachment2) {
|
||||
future.completeExceptionally(new RuntimeException(action.method + " sncp remote exec failed"));
|
||||
future.completeExceptionally(new RpcRemoteException(action.method + " sncp remote exec failed, params=" + JsonConvert.root().convertTo(params)));
|
||||
conn.offerBuffer(attachment2);
|
||||
transport.offerConnection(true, conn);
|
||||
if (handler != null) {
|
||||
final Object handlerAttach = action.handlerAttachParamIndex >= 0 ? params[action.handlerAttachParamIndex] : null;
|
||||
handler.failed(exc, handlerAttach);
|
||||
}
|
||||
logger.log(Level.SEVERE, action.method + " sncp (params: " + convert.convertTo(params) + ") remote read exec failed", exc);
|
||||
logger.log(Level.SEVERE, action.method + " sncp (params: " + convert.convertTo(params) + ") remote read exec failed, params=" + JsonConvert.root().convertTo(params), exc);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void failed(Throwable exc, ByteBuffer[] attachment) {
|
||||
future.completeExceptionally(new RuntimeException(action.method + " sncp remote exec failed"));
|
||||
future.completeExceptionally(new RpcRemoteException(action.method + " sncp remote exec failed, params=" + JsonConvert.root().convertTo(params)));
|
||||
transport.offerConnection(true, conn);
|
||||
if (handler != null) {
|
||||
final Object handlerAttach = action.handlerAttachParamIndex >= 0 ? params[action.handlerAttachParamIndex] : null;
|
||||
handler.failed(exc, handlerAttach);
|
||||
}
|
||||
logger.log(Level.SEVERE, action.method + " sncp (params: " + convert.convertTo(params) + ") remote write exec failed", exc);
|
||||
logger.log(Level.SEVERE, action.method + " sncp (params: " + convert.convertTo(params) + ") remote write exec failed, params=" + JsonConvert.root().convertTo(params), exc);
|
||||
}
|
||||
});
|
||||
return future;
|
||||
|
||||
@@ -59,7 +59,7 @@ public final class SncpResponse extends Response<SncpContext, SncpRequest> {
|
||||
|
||||
public void finish(final int retcode, final BsonWriter out) {
|
||||
if (out == null) {
|
||||
final ByteBuffer buffer = pollWriteReadBuffer();
|
||||
final ByteBuffer buffer = channel.pollWriteBuffer();
|
||||
fillHeader(buffer, 0, retcode);
|
||||
finish(buffer);
|
||||
return;
|
||||
|
||||
@@ -49,8 +49,8 @@ public @interface RetLabel {
|
||||
for (Field field : clazz.getFields()) {
|
||||
if (!Modifier.isStatic(field.getModifiers())) continue;
|
||||
if (field.getType() != int.class) continue;
|
||||
RetLabel info = field.getAnnotation(RetLabel.class);
|
||||
if (info == null) continue;
|
||||
RetLabel[] infos = field.getAnnotationsByType(RetLabel.class);
|
||||
if (infos == null || infos.length == 0) continue;
|
||||
int value;
|
||||
try {
|
||||
value = field.getInt(null);
|
||||
@@ -58,7 +58,9 @@ public @interface RetLabel {
|
||||
ex.printStackTrace();
|
||||
continue;
|
||||
}
|
||||
rets.computeIfAbsent(info.locale(), (k) -> new HashMap<>()).put(value, info.value());
|
||||
for (RetLabel info : infos) {
|
||||
rets.computeIfAbsent(info.locale(), (k) -> new HashMap<>()).put(value, info.value());
|
||||
}
|
||||
}
|
||||
return rets;
|
||||
}
|
||||
|
||||
@@ -7,7 +7,9 @@ package org.redkale.service;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import org.redkale.convert.Convert;
|
||||
import org.redkale.convert.json.*;
|
||||
import org.redkale.util.Utility;
|
||||
|
||||
/**
|
||||
* 通用的结果对象,在常见的HTTP+JSON接口中返回的结果需要含结果码,错误信息,和实体对象。 <br>
|
||||
@@ -32,6 +34,8 @@ public class RetResult<T> {
|
||||
|
||||
protected Map<String, String> attach;
|
||||
|
||||
protected Convert convert;
|
||||
|
||||
public RetResult() {
|
||||
}
|
||||
|
||||
@@ -39,6 +43,11 @@ public class RetResult<T> {
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
public RetResult(Convert convert, T result) {
|
||||
this.convert = convert;
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
public RetResult(int retcode) {
|
||||
this.retcode = retcode;
|
||||
}
|
||||
@@ -54,6 +63,14 @@ public class RetResult<T> {
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
public Convert convert() {
|
||||
return convert;
|
||||
}
|
||||
|
||||
public void convert(Convert convert) {
|
||||
this.convert = convert;
|
||||
}
|
||||
|
||||
public static RetResult success() {
|
||||
return new RetResult();
|
||||
}
|
||||
@@ -66,6 +83,14 @@ public class RetResult<T> {
|
||||
return CompletableFuture.completedFuture(new RetResult());
|
||||
}
|
||||
|
||||
public static RetResult<Map<String, String>> map(String... items) {
|
||||
return new RetResult(Utility.ofMap(items));
|
||||
}
|
||||
|
||||
public static <K, V> RetResult<Map<K, V>> map(Object... items) {
|
||||
return new RetResult(Utility.ofMap(items));
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断结果是否成功返回, retcode = 0 视为成功, 否则视为错误码
|
||||
*
|
||||
|
||||
@@ -15,17 +15,25 @@ package org.redkale.source;
|
||||
*/
|
||||
public enum ColumnExpress {
|
||||
/**
|
||||
* 直接赋值 col = val
|
||||
* 赋值 col = val
|
||||
*/
|
||||
MOV,
|
||||
/**
|
||||
* 追加值 col = col + val
|
||||
* 加值 col = col + val
|
||||
*/
|
||||
INC,
|
||||
/**
|
||||
* 乘值 col = col * val
|
||||
*/
|
||||
MUL,
|
||||
/**
|
||||
* 除值 col = col / val
|
||||
*/
|
||||
DIV,
|
||||
/**
|
||||
* 取模 col = col % val
|
||||
*/
|
||||
MOD,
|
||||
/**
|
||||
* 与值 col = col & val
|
||||
*/
|
||||
|
||||
124
src/org/redkale/source/ColumnNodeValue.java
Normal file
124
src/org/redkale/source/ColumnNodeValue.java
Normal file
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.source;
|
||||
|
||||
import java.io.Serializable;
|
||||
import static org.redkale.source.ColumnExpress.*;
|
||||
|
||||
/**
|
||||
* 作为ColumnValue的value字段值,用于复杂的字段表达式 。
|
||||
* String 视为 字段名
|
||||
* Number 视为 数值
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public class ColumnNodeValue implements Serializable {
|
||||
|
||||
private Serializable left;//类型只能是String、Number、ColumnNode
|
||||
|
||||
private ColumnExpress express; //不能是MOV
|
||||
|
||||
private Serializable right;//类型只能是String、Number、ColumnNode
|
||||
|
||||
public ColumnNodeValue() {
|
||||
}
|
||||
|
||||
public ColumnNodeValue(Serializable left, ColumnExpress express, Serializable right) {
|
||||
if (express == null || express == ColumnExpress.MOV) throw new IllegalArgumentException("express cannot be null or MOV");
|
||||
this.left = left;
|
||||
this.express = express;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
public static ColumnNodeValue create(Serializable left, ColumnExpress express, Serializable right) {
|
||||
return new ColumnNodeValue(left, express, right);
|
||||
}
|
||||
|
||||
public static ColumnNodeValue inc(Serializable left, Serializable right) {
|
||||
return new ColumnNodeValue(left, INC, right);
|
||||
}
|
||||
|
||||
public static ColumnNodeValue mul(Serializable left, Serializable right) {
|
||||
return new ColumnNodeValue(left, MUL, right);
|
||||
}
|
||||
|
||||
public static ColumnNodeValue div(Serializable left, Serializable right) {
|
||||
return new ColumnNodeValue(left, DIV, right);
|
||||
}
|
||||
|
||||
public static ColumnNodeValue mod(Serializable left, Serializable right) {
|
||||
return new ColumnNodeValue(left, MOD, right);
|
||||
}
|
||||
|
||||
public static ColumnNodeValue and(Serializable left, Serializable right) {
|
||||
return new ColumnNodeValue(left, AND, right);
|
||||
}
|
||||
|
||||
public static ColumnNodeValue orr(Serializable left, Serializable right) {
|
||||
return new ColumnNodeValue(left, ORR, right);
|
||||
}
|
||||
|
||||
public ColumnNodeValue inc(Serializable right) {
|
||||
return any(INC, right);
|
||||
}
|
||||
|
||||
public ColumnNodeValue mul(Serializable right) {
|
||||
return any(MUL, right);
|
||||
}
|
||||
|
||||
public ColumnNodeValue div(Serializable right) {
|
||||
return any(DIV, right);
|
||||
}
|
||||
|
||||
public ColumnNodeValue mod(Serializable right) {
|
||||
return any(MOD, right);
|
||||
}
|
||||
|
||||
public ColumnNodeValue and(Serializable right) {
|
||||
return any(AND, right);
|
||||
}
|
||||
|
||||
public ColumnNodeValue orr(Serializable right) {
|
||||
return any(ORR, right);
|
||||
}
|
||||
|
||||
protected ColumnNodeValue any(ColumnExpress express, Serializable right) {
|
||||
ColumnNodeValue one = new ColumnNodeValue(this.left, this.express, this.right);
|
||||
this.left = one;
|
||||
this.express = express;
|
||||
this.right = right;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Serializable getLeft() {
|
||||
return left;
|
||||
}
|
||||
|
||||
public void setLeft(Serializable left) {
|
||||
this.left = left;
|
||||
}
|
||||
|
||||
public ColumnExpress getExpress() {
|
||||
return express;
|
||||
}
|
||||
|
||||
public void setExpress(ColumnExpress express) {
|
||||
this.express = express;
|
||||
}
|
||||
|
||||
public Serializable getRight() {
|
||||
return right;
|
||||
}
|
||||
|
||||
public void setRight(Serializable right) {
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "{\"column\":" + ((left instanceof CharSequence) ? ("\"" + left + "\"") : left) + ", \"express\":" + express + ", \"value\":" + ((right instanceof CharSequence) ? ("\"" + right + "\"") : right) + "}";
|
||||
}
|
||||
}
|
||||
@@ -86,6 +86,31 @@ public class ColumnValue {
|
||||
return new ColumnValue(column, MUL, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回 {column} = {column} / {value} 操作
|
||||
*
|
||||
* @param column 字段名
|
||||
* @param value 字段值
|
||||
*
|
||||
* @return ColumnValue
|
||||
*/
|
||||
public static ColumnValue div(String column, Serializable value) {
|
||||
return new ColumnValue(column, DIV, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回 {column} = {column} % {value} 操作
|
||||
*
|
||||
* @param column 字段名
|
||||
* @param value 字段值
|
||||
*
|
||||
* @return ColumnValue
|
||||
*/
|
||||
//不常用,防止开发者容易在mov时误输入mod
|
||||
// public static ColumnValue mod(String column, Serializable value) {
|
||||
// return new ColumnValue(column, MOD, value);
|
||||
// }
|
||||
|
||||
/**
|
||||
* 返回 {column} = {column} & {value} 操作
|
||||
*
|
||||
|
||||
@@ -30,7 +30,6 @@ public class DataCallAttribute implements Attribute<Object, Serializable> {
|
||||
Class cltmp = clazz;
|
||||
do {
|
||||
for (Field field : cltmp.getDeclaredFields()) {
|
||||
if (field.getAnnotation(javax.persistence.GeneratedValue.class) == null) continue;
|
||||
try {
|
||||
rs = Attribute.create(cltmp, field);
|
||||
attributes.put(clazz, rs);
|
||||
|
||||
@@ -70,16 +70,17 @@ public class DataJdbcSource extends DataSqlSource<Connection> {
|
||||
}
|
||||
c = c1;
|
||||
} catch (SQLException se) {
|
||||
if (info.tableStrategy == null || !info.isTableNotExist(se)) throw se;
|
||||
synchronized (info.tables) {
|
||||
final String oldTable = info.table;
|
||||
if (info.getTableStrategy() == null || !info.isTableNotExist(se)) throw se;
|
||||
synchronized (info.disTableLock()) {
|
||||
final String catalog = conn.getCatalog();
|
||||
final String newTable = info.getTable(entitys[0]);
|
||||
if (!info.tables.contains(newTable)) {
|
||||
final String tablekey = newTable.indexOf('.') > 0 ? newTable : (catalog + '.' + newTable);
|
||||
if (!info.containsDisTable(tablekey)) {
|
||||
try {
|
||||
Statement st = conn.createStatement();
|
||||
st.execute(info.tablecopySQL.replace("${newtable}", newTable).replace("${oldtable}", oldTable));
|
||||
st.execute(info.getTableCopySQL(newTable));
|
||||
st.close();
|
||||
info.tables.add(newTable);
|
||||
info.addDisTable(tablekey);
|
||||
} catch (SQLException sqle) { //多进程并发时可能会出现重复建表
|
||||
if (newTable.indexOf('.') > 0 && info.isTableNotExist(se)) {
|
||||
Statement st;
|
||||
@@ -92,14 +93,14 @@ public class DataJdbcSource extends DataSqlSource<Connection> {
|
||||
}
|
||||
try {
|
||||
st = conn.createStatement();
|
||||
st.execute(info.tablecopySQL.replace("${newtable}", newTable).replace("${oldtable}", oldTable));
|
||||
st.execute(info.getTableCopySQL(newTable));
|
||||
st.close();
|
||||
info.tables.add(newTable);
|
||||
info.addDisTable(tablekey);
|
||||
} catch (SQLException sqle2) {
|
||||
logger.log(Level.SEVERE, "create table2(" + info.tablecopySQL.replace("${newtable}", newTable).replace("${oldtable}", oldTable) + ") error", sqle2);
|
||||
logger.log(Level.SEVERE, "create table2(" + info.getTableCopySQL(newTable) + ") error", sqle2);
|
||||
}
|
||||
} else {
|
||||
logger.log(Level.SEVERE, "create table(" + info.tablecopySQL.replace("${newtable}", newTable).replace("${oldtable}", oldTable) + ") error", sqle);
|
||||
logger.log(Level.SEVERE, "create table(" + info.getTableCopySQL(newTable) + ") error", sqle);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -113,20 +114,6 @@ public class DataJdbcSource extends DataSqlSource<Connection> {
|
||||
}
|
||||
c = c1;
|
||||
}
|
||||
if (info.autoGenerated) { //由数据库自动生成主键值
|
||||
ResultSet set = prestmt.getGeneratedKeys();
|
||||
int i = -1;
|
||||
while (set.next()) {
|
||||
if (primaryType == int.class) {
|
||||
primary.set(entitys[++i], set.getInt(1));
|
||||
} else if (primaryType == long.class) {
|
||||
primary.set(entitys[++i], set.getLong(1));
|
||||
} else {
|
||||
primary.set(entitys[++i], set.getObject(1));
|
||||
}
|
||||
}
|
||||
set.close();
|
||||
}
|
||||
prestmt.close();
|
||||
//------------------------------------------------------------
|
||||
if (info.isLoggable(logger, Level.FINEST)) { //打印调试信息
|
||||
@@ -141,7 +128,7 @@ public class DataJdbcSource extends DataSqlSource<Connection> {
|
||||
if (obj != null && obj.getClass().isArray()) {
|
||||
sb.append("'[length=").append(java.lang.reflect.Array.getLength(obj)).append("]'");
|
||||
} else {
|
||||
sb.append(FilterNode.formatToString(obj));
|
||||
sb.append(info.formatSQLValue(obj, sqlFormatter));
|
||||
}
|
||||
} else {
|
||||
sb.append(ch);
|
||||
@@ -155,7 +142,7 @@ public class DataJdbcSource extends DataSqlSource<Connection> {
|
||||
} catch (SQLException e) {
|
||||
CompletableFuture future = new CompletableFuture();
|
||||
future.completeExceptionally(e);
|
||||
return future;
|
||||
return future;//return CompletableFuture.failedFuture(e);
|
||||
} finally {
|
||||
if (conn != null) writePool.offerConnection(conn);
|
||||
}
|
||||
@@ -164,10 +151,9 @@ public class DataJdbcSource extends DataSqlSource<Connection> {
|
||||
protected <T> PreparedStatement createInsertPreparedStatement(final Connection conn, final String sql,
|
||||
final EntityInfo<T> info, T... entitys) throws SQLException {
|
||||
Attribute<T, Serializable>[] attrs = info.insertAttributes;
|
||||
final PreparedStatement prestmt = info.autoGenerated ? conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS) : conn.prepareStatement(sql);
|
||||
final PreparedStatement prestmt = conn.prepareStatement(sql);
|
||||
|
||||
for (final T value : entitys) {
|
||||
if (info.autouuid) info.createPrimaryValue(value);
|
||||
batchStatementParameters(conn, prestmt, info, attrs, value);
|
||||
prestmt.addBatch();
|
||||
}
|
||||
@@ -214,7 +200,7 @@ public class DataJdbcSource extends DataSqlSource<Connection> {
|
||||
} catch (SQLException e) {
|
||||
CompletableFuture future = new CompletableFuture();
|
||||
future.completeExceptionally(e);
|
||||
return future;
|
||||
return future;//return CompletableFuture.failedFuture(e);
|
||||
} finally {
|
||||
if (conn != null) writePool.offerConnection(conn);
|
||||
}
|
||||
@@ -235,7 +221,7 @@ public class DataJdbcSource extends DataSqlSource<Connection> {
|
||||
if (info.isTableNotExist(e)) return CompletableFuture.completedFuture(-1);
|
||||
CompletableFuture future = new CompletableFuture();
|
||||
future.completeExceptionally(e);
|
||||
return future;
|
||||
return future;//return CompletableFuture.failedFuture(e);
|
||||
} finally {
|
||||
if (conn != null) writePool.offerConnection(conn);
|
||||
}
|
||||
@@ -256,7 +242,7 @@ public class DataJdbcSource extends DataSqlSource<Connection> {
|
||||
if (info.isTableNotExist(e)) return CompletableFuture.completedFuture(-1);
|
||||
CompletableFuture future = new CompletableFuture();
|
||||
future.completeExceptionally(e);
|
||||
return future;
|
||||
return future;//return CompletableFuture.failedFuture(e);
|
||||
} finally {
|
||||
if (conn != null) writePool.offerConnection(conn);
|
||||
}
|
||||
@@ -289,7 +275,7 @@ public class DataJdbcSource extends DataSqlSource<Connection> {
|
||||
if (obj != null && obj.getClass().isArray()) {
|
||||
sb.append("'[length=").append(java.lang.reflect.Array.getLength(obj)).append("]'");
|
||||
} else {
|
||||
sb.append(FilterNode.formatToString(obj));
|
||||
sb.append(info.formatSQLValue(obj, sqlFormatter));
|
||||
}
|
||||
} else {
|
||||
sb.append(ch);
|
||||
@@ -309,7 +295,7 @@ public class DataJdbcSource extends DataSqlSource<Connection> {
|
||||
} catch (SQLException e) {
|
||||
CompletableFuture future = new CompletableFuture();
|
||||
future.completeExceptionally(e);
|
||||
return future;
|
||||
return future;//return CompletableFuture.failedFuture(e);
|
||||
} finally {
|
||||
if (conn != null) writePool.offerConnection(conn);
|
||||
}
|
||||
@@ -343,7 +329,7 @@ public class DataJdbcSource extends DataSqlSource<Connection> {
|
||||
} catch (SQLException e) {
|
||||
CompletableFuture future = new CompletableFuture();
|
||||
future.completeExceptionally(e);
|
||||
return future;
|
||||
return future;//return CompletableFuture.failedFuture(e);
|
||||
} finally {
|
||||
if (conn != null) writePool.offerConnection(conn);
|
||||
}
|
||||
@@ -352,12 +338,12 @@ public class DataJdbcSource extends DataSqlSource<Connection> {
|
||||
@Override
|
||||
protected <T, N extends Number> CompletableFuture<Map<String, N>> getNumberMapDB(EntityInfo<T> info, String sql, FilterFuncColumn... columns) {
|
||||
Connection conn = null;
|
||||
final Map map = new HashMap<>();
|
||||
try {
|
||||
conn = readPool.poll();
|
||||
//conn.setReadOnly(true);
|
||||
final Statement stmt = conn.createStatement();
|
||||
ResultSet set = stmt.executeQuery(sql);
|
||||
final Map map = new HashMap<>();
|
||||
if (set.next()) {
|
||||
int index = 0;
|
||||
for (FilterFuncColumn ffc : columns) {
|
||||
@@ -373,9 +359,10 @@ public class DataJdbcSource extends DataSqlSource<Connection> {
|
||||
stmt.close();
|
||||
return CompletableFuture.completedFuture(map);
|
||||
} catch (SQLException e) {
|
||||
if (info.getTableStrategy() != null && info.isTableNotExist(e)) return CompletableFuture.completedFuture(map);
|
||||
CompletableFuture future = new CompletableFuture();
|
||||
future.completeExceptionally(e);
|
||||
return future;
|
||||
return future;//return CompletableFuture.failedFuture(e);
|
||||
} finally {
|
||||
if (conn != null) readPool.offerConnection(conn);
|
||||
}
|
||||
@@ -398,9 +385,10 @@ public class DataJdbcSource extends DataSqlSource<Connection> {
|
||||
stmt.close();
|
||||
return CompletableFuture.completedFuture(rs);
|
||||
} catch (SQLException e) {
|
||||
if (info.getTableStrategy() != null && info.isTableNotExist(e)) return CompletableFuture.completedFuture(defVal);
|
||||
CompletableFuture future = new CompletableFuture();
|
||||
future.completeExceptionally(e);
|
||||
return future;
|
||||
return future;//return CompletableFuture.failedFuture(e);
|
||||
} finally {
|
||||
if (conn != null) readPool.offerConnection(conn);
|
||||
}
|
||||
@@ -409,11 +397,11 @@ public class DataJdbcSource extends DataSqlSource<Connection> {
|
||||
@Override
|
||||
protected <T, K extends Serializable, N extends Number> CompletableFuture<Map<K, N>> queryColumnMapDB(EntityInfo<T> info, String sql, String keyColumn) {
|
||||
Connection conn = null;
|
||||
Map<K, N> rs = new LinkedHashMap<>();
|
||||
try {
|
||||
conn = readPool.poll();
|
||||
//conn.setReadOnly(true);
|
||||
final Statement stmt = conn.createStatement();
|
||||
Map<K, N> rs = new LinkedHashMap<>();
|
||||
ResultSet set = stmt.executeQuery(sql);
|
||||
ResultSetMetaData rsd = set.getMetaData();
|
||||
boolean smallint = rsd == null ? false : rsd.getColumnType(1) == Types.SMALLINT;
|
||||
@@ -424,9 +412,10 @@ public class DataJdbcSource extends DataSqlSource<Connection> {
|
||||
stmt.close();
|
||||
return CompletableFuture.completedFuture(rs);
|
||||
} catch (SQLException e) {
|
||||
if (info.getTableStrategy() != null && info.isTableNotExist(e)) return CompletableFuture.completedFuture(rs);
|
||||
CompletableFuture future = new CompletableFuture();
|
||||
future.completeExceptionally(e);
|
||||
return future;
|
||||
return future;//return CompletableFuture.failedFuture(e);
|
||||
} finally {
|
||||
if (conn != null) readPool.offerConnection(conn);
|
||||
}
|
||||
@@ -446,10 +435,10 @@ public class DataJdbcSource extends DataSqlSource<Connection> {
|
||||
ps.close();
|
||||
return CompletableFuture.completedFuture(rs);
|
||||
} catch (SQLException e) {
|
||||
if (info.tableStrategy != null && info.isTableNotExist(e)) return CompletableFuture.completedFuture(null);
|
||||
if (info.getTableStrategy() != null && info.isTableNotExist(e)) return CompletableFuture.completedFuture(null);
|
||||
CompletableFuture future = new CompletableFuture();
|
||||
future.completeExceptionally(e);
|
||||
return future;
|
||||
return future;//return CompletableFuture.failedFuture(e);
|
||||
} finally {
|
||||
if (conn != null) readPool.offerConnection(conn);
|
||||
}
|
||||
@@ -473,10 +462,10 @@ public class DataJdbcSource extends DataSqlSource<Connection> {
|
||||
ps.close();
|
||||
return CompletableFuture.completedFuture(val == null ? defValue : val);
|
||||
} catch (SQLException e) {
|
||||
if (info.tableStrategy != null && info.isTableNotExist(e)) return CompletableFuture.completedFuture(defValue);
|
||||
if (info.getTableStrategy() != null && info.isTableNotExist(e)) return CompletableFuture.completedFuture(defValue);
|
||||
CompletableFuture future = new CompletableFuture();
|
||||
future.completeExceptionally(e);
|
||||
return future;
|
||||
return future;//return CompletableFuture.failedFuture(e);
|
||||
} finally {
|
||||
if (conn != null) readPool.offerConnection(conn);
|
||||
}
|
||||
@@ -496,10 +485,10 @@ public class DataJdbcSource extends DataSqlSource<Connection> {
|
||||
if (info.isLoggable(logger, Level.FINEST, sql)) logger.finest(info.getType().getSimpleName() + " exists (" + rs + ") sql=" + sql);
|
||||
return CompletableFuture.completedFuture(rs);
|
||||
} catch (SQLException e) {
|
||||
if (info.tableStrategy != null && info.isTableNotExist(e)) return CompletableFuture.completedFuture(false);
|
||||
if (info.getTableStrategy() != null && info.isTableNotExist(e)) return CompletableFuture.completedFuture(false);
|
||||
CompletableFuture future = new CompletableFuture();
|
||||
future.completeExceptionally(e);
|
||||
return future;
|
||||
return future;//return CompletableFuture.failedFuture(e);
|
||||
} finally {
|
||||
if (conn != null) readPool.offerConnection(conn);
|
||||
}
|
||||
@@ -570,10 +559,10 @@ public class DataJdbcSource extends DataSqlSource<Connection> {
|
||||
ps.close();
|
||||
return CompletableFuture.completedFuture(new Sheet<>(total, list));
|
||||
} catch (SQLException e) {
|
||||
if (info.tableStrategy != null && info.isTableNotExist(e)) return CompletableFuture.completedFuture(new Sheet<>());
|
||||
if (info.getTableStrategy() != null && info.isTableNotExist(e)) return CompletableFuture.completedFuture(new Sheet<>());
|
||||
CompletableFuture future = new CompletableFuture();
|
||||
future.completeExceptionally(e);
|
||||
return future;
|
||||
return future;//return CompletableFuture.failedFuture(e);
|
||||
} finally {
|
||||
if (conn != null) readPool.offerConnection(conn);
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ package org.redkale.source;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.net.URL;
|
||||
import java.net.*;
|
||||
import java.util.*;
|
||||
import javax.xml.stream.*;
|
||||
import org.redkale.util.AnyValue;
|
||||
@@ -139,7 +139,7 @@ public final class DataSources {
|
||||
public static DataSource createDataSource(final String unitName) throws IOException {
|
||||
return createDataSource(unitName, System.getProperty(DATASOURCE_CONFPATH) == null
|
||||
? DataJdbcSource.class.getResource("/META-INF/persistence.xml")
|
||||
: new File(System.getProperty(DATASOURCE_CONFPATH)).toURI().toURL());
|
||||
: (System.getProperty(DATASOURCE_CONFPATH, "").contains("://") ? URI.create(System.getProperty(DATASOURCE_CONFPATH)).toURL() : new File(System.getProperty(DATASOURCE_CONFPATH)).toURI().toURL()));
|
||||
}
|
||||
|
||||
public static DataSource createDataSource(final String unitName, URL persistxml) throws IOException {
|
||||
|
||||
@@ -59,6 +59,8 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
@Resource(name = "$")
|
||||
protected DataCacheListener cacheListener;
|
||||
|
||||
protected final BiFunction<EntityInfo, Object, CharSequence> sqlFormatter;
|
||||
|
||||
protected final BiConsumer futureCompleteConsumer = (r, t) -> {
|
||||
if (t != null) logger.log(Level.SEVERE, "CompletableFuture complete error", (Throwable) t);
|
||||
};
|
||||
@@ -89,7 +91,7 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
} else if (s.length() == 2) {
|
||||
s = "0" + s;
|
||||
}
|
||||
t.setName(cname + "-Thread-" + s);
|
||||
t.setName("Redkale-"+cname + "-Thread-" + s);
|
||||
t.setUncaughtExceptionHandler(ueh);
|
||||
return t;
|
||||
});
|
||||
@@ -107,6 +109,7 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
Semaphore semaphore = maxconns > 0 ? new Semaphore(maxconns) : null;
|
||||
this.readPool = createPoolSource(this, "read", queue, semaphore, readprop);
|
||||
this.writePool = createPoolSource(this, "write", queue, semaphore, writeprop);
|
||||
this.sqlFormatter = (info, val) -> formatValueToString(info, val);
|
||||
}
|
||||
|
||||
@Local
|
||||
@@ -206,6 +209,8 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
@Override
|
||||
public void destroy(AnyValue config) {
|
||||
if (this.executor != null) this.executor.shutdownNow();
|
||||
if (readPool != null) readPool.close();
|
||||
if (writePool != null) writePool.close();
|
||||
}
|
||||
|
||||
@Local
|
||||
@@ -284,7 +289,7 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
return null;
|
||||
}
|
||||
|
||||
protected <T> String formatValueToString(final EntityInfo<T> info, Object value) {
|
||||
protected <T> CharSequence formatValueToString(final EntityInfo<T> info, Object value) {
|
||||
final String dbtype = this.readPool.getDbtype();
|
||||
if ("mysql".equals(dbtype)) {
|
||||
if (value == null) return null;
|
||||
@@ -296,7 +301,7 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
}
|
||||
return String.valueOf(value);
|
||||
}
|
||||
return info.formatToString(value);
|
||||
return info.formatSQLValue(value, null);
|
||||
}
|
||||
|
||||
//----------------------------- insert -----------------------------
|
||||
@@ -313,11 +318,6 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
if (entitys.length == 0) return 0;
|
||||
checkEntity("insert", false, entitys);
|
||||
final EntityInfo<T> info = loadEntityInfo((Class<T>) entitys[0].getClass());
|
||||
if (info.autouuid) {
|
||||
for (T value : entitys) {
|
||||
info.createPrimaryValue(value);
|
||||
}
|
||||
}
|
||||
if (isOnlyCache(info)) return insertCache(info, entitys);
|
||||
return insertDB(info, entitys).whenComplete((rs, t) -> {
|
||||
if (t != null) {
|
||||
@@ -334,11 +334,6 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
CompletableFuture future = checkEntity("insert", true, entitys);
|
||||
if (future != null) return future;
|
||||
final EntityInfo<T> info = loadEntityInfo((Class<T>) entitys[0].getClass());
|
||||
if (info.autouuid) {
|
||||
for (T value : entitys) {
|
||||
info.createPrimaryValue(value);
|
||||
}
|
||||
}
|
||||
if (isOnlyCache(info)) {
|
||||
return CompletableFuture.supplyAsync(() -> insertCache(info, entitys), getExecutor());
|
||||
}
|
||||
@@ -473,7 +468,7 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
public <T> int delete(Class<T> clazz, final Flipper flipper, FilterNode node) {
|
||||
final EntityInfo<T> info = loadEntityInfo(clazz);
|
||||
if (isOnlyCache(info)) return deleteCache(info, -1, flipper, node);
|
||||
return DataSqlSource.this.deleteCompose(info, flipper, node).whenComplete((rs, t) -> {
|
||||
return this.deleteCompose(info, flipper, node).whenComplete((rs, t) -> {
|
||||
if (t != null) {
|
||||
futureCompleteConsumer.accept(rs, t);
|
||||
} else {
|
||||
@@ -488,14 +483,14 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
if (isOnlyCache(info)) {
|
||||
return CompletableFuture.supplyAsync(() -> deleteCache(info, -1, flipper, node), getExecutor());
|
||||
}
|
||||
if (isAsync()) return DataSqlSource.this.deleteCompose(info, flipper, node).whenComplete((rs, t) -> {
|
||||
if (isAsync()) return this.deleteCompose(info, flipper, node).whenComplete((rs, t) -> {
|
||||
if (t != null) {
|
||||
futureCompleteConsumer.accept(rs, t);
|
||||
} else {
|
||||
deleteCache(info, rs, flipper, node);
|
||||
}
|
||||
});
|
||||
return CompletableFuture.supplyAsync(() -> DataSqlSource.this.deleteCompose(info, flipper, node).join(), getExecutor()).whenComplete((rs, t) -> {
|
||||
return CompletableFuture.supplyAsync(() -> this.deleteCompose(info, flipper, node).join(), getExecutor()).whenComplete((rs, t) -> {
|
||||
if (t != null) {
|
||||
futureCompleteConsumer.accept(rs, t);
|
||||
} else {
|
||||
@@ -506,13 +501,13 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
|
||||
protected <T> CompletableFuture<Integer> deleteCompose(final EntityInfo<T> info, final Serializable... pks) {
|
||||
if (pks.length == 1) {
|
||||
String sql = "DELETE FROM " + info.getTable(pks[0]) + " WHERE " + info.getPrimarySQLColumn() + " = " + FilterNode.formatToString(info.getSQLValue(info.getPrimarySQLColumn(), pks[0]));
|
||||
String sql = "DELETE FROM " + info.getTable(pks[0]) + " WHERE " + info.getPrimarySQLColumn() + " = " + info.formatSQLValue(info.getPrimarySQLColumn(), pks[0], sqlFormatter);
|
||||
return deleteDB(info, null, sql);
|
||||
}
|
||||
String sql = "DELETE FROM " + info.getTable(pks[0]) + " WHERE " + info.getPrimarySQLColumn() + " IN (";
|
||||
for (int i = 0; i < pks.length; i++) {
|
||||
if (i > 0) sql += ',';
|
||||
sql += FilterNode.formatToString(info.getSQLValue(info.getPrimarySQLColumn(), pks[i]));
|
||||
sql += info.formatSQLValue(info.getPrimarySQLColumn(), pks[i], sqlFormatter);
|
||||
}
|
||||
sql += ")";
|
||||
if (info.isLoggable(logger, Level.FINEST, sql)) logger.finest(info.getType().getSimpleName() + " delete sql=" + sql);
|
||||
@@ -549,7 +544,7 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
public <T> int clearTable(Class<T> clazz, FilterNode node) {
|
||||
final EntityInfo<T> info = loadEntityInfo(clazz);
|
||||
if (isOnlyCache(info)) return clearTableCache(info, node);
|
||||
return DataSqlSource.this.clearTableCompose(info, node).whenComplete((rs, t) -> {
|
||||
return this.clearTableCompose(info, node).whenComplete((rs, t) -> {
|
||||
if (t != null) {
|
||||
futureCompleteConsumer.accept(rs, t);
|
||||
} else {
|
||||
@@ -569,14 +564,14 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
if (isOnlyCache(info)) {
|
||||
return CompletableFuture.supplyAsync(() -> clearTableCache(info, node), getExecutor());
|
||||
}
|
||||
if (isAsync()) return DataSqlSource.this.clearTableCompose(info, node).whenComplete((rs, t) -> {
|
||||
if (isAsync()) return this.clearTableCompose(info, node).whenComplete((rs, t) -> {
|
||||
if (t != null) {
|
||||
futureCompleteConsumer.accept(rs, t);
|
||||
} else {
|
||||
clearTableCache(info, node);
|
||||
}
|
||||
});
|
||||
return CompletableFuture.supplyAsync(() -> DataSqlSource.this.clearTableCompose(info, node).join(), getExecutor()).whenComplete((rs, t) -> {
|
||||
return CompletableFuture.supplyAsync(() -> this.clearTableCompose(info, node).join(), getExecutor()).whenComplete((rs, t) -> {
|
||||
if (t != null) {
|
||||
futureCompleteConsumer.accept(rs, t);
|
||||
} else {
|
||||
@@ -601,7 +596,7 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
public <T> int dropTable(Class<T> clazz, FilterNode node) {
|
||||
final EntityInfo<T> info = loadEntityInfo(clazz);
|
||||
if (isOnlyCache(info)) return dropTableCache(info, node);
|
||||
return DataSqlSource.this.dropTableCompose(info, node).whenComplete((rs, t) -> {
|
||||
return this.dropTableCompose(info, node).whenComplete((rs, t) -> {
|
||||
if (t != null) {
|
||||
futureCompleteConsumer.accept(rs, t);
|
||||
} else {
|
||||
@@ -621,14 +616,14 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
if (isOnlyCache(info)) {
|
||||
return CompletableFuture.supplyAsync(() -> dropTableCache(info, node), getExecutor());
|
||||
}
|
||||
if (isAsync()) return DataSqlSource.this.dropTableCompose(info, node).whenComplete((rs, t) -> {
|
||||
if (isAsync()) return this.dropTableCompose(info, node).whenComplete((rs, t) -> {
|
||||
if (t != null) {
|
||||
futureCompleteConsumer.accept(rs, t);
|
||||
} else {
|
||||
dropTableCache(info, node);
|
||||
}
|
||||
});
|
||||
return CompletableFuture.supplyAsync(() -> DataSqlSource.this.dropTableCompose(info, node).join(), getExecutor()).whenComplete((rs, t) -> {
|
||||
return CompletableFuture.supplyAsync(() -> this.dropTableCompose(info, node).join(), getExecutor()).whenComplete((rs, t) -> {
|
||||
if (t != null) {
|
||||
futureCompleteConsumer.accept(rs, t);
|
||||
} else {
|
||||
@@ -795,11 +790,11 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
|
||||
protected <T> CompletableFuture<Integer> updateColumnCompose(final EntityInfo<T> info, Serializable pk, String column, final Serializable colval) {
|
||||
if (colval instanceof byte[]) {
|
||||
String sql = "UPDATE " + info.getTable(pk) + " SET " + info.getSQLColumn(null, column) + " = " + prepareParamSign(1) + " WHERE " + info.getPrimarySQLColumn() + " = " + FilterNode.formatToString(info.getSQLValue(info.getPrimarySQLColumn(), pk));
|
||||
String sql = "UPDATE " + info.getTable(pk) + " SET " + info.getSQLColumn(null, column) + " = " + prepareParamSign(1) + " WHERE " + info.getPrimarySQLColumn() + " = " + info.formatSQLValue(info.getPrimarySQLColumn(), pk, sqlFormatter);
|
||||
return updateDB(info, null, sql, true, colval);
|
||||
} else {
|
||||
String sql = "UPDATE " + info.getTable(pk) + " SET " + info.getSQLColumn(null, column) + " = "
|
||||
+ formatValueToString(info, info.getSQLValue(column, colval)) + " WHERE " + info.getPrimarySQLColumn() + " = " + FilterNode.formatToString(info.getSQLValue(info.getPrimarySQLColumn(), pk));
|
||||
+ info.formatSQLValue(column, colval, sqlFormatter) + " WHERE " + info.getPrimarySQLColumn() + " = " + info.formatSQLValue(info.getPrimarySQLColumn(), pk, sqlFormatter);
|
||||
return updateDB(info, null, sql, false);
|
||||
}
|
||||
}
|
||||
@@ -819,7 +814,7 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
public <T> int updateColumn(Class<T> clazz, String column, Serializable colval, FilterNode node) {
|
||||
final EntityInfo<T> info = loadEntityInfo(clazz);
|
||||
if (isOnlyCache(info)) return updateCache(info, -1, column, colval, node);
|
||||
return DataSqlSource.this.updateColumnCompose(info, column, colval, node).whenComplete((rs, t) -> {
|
||||
return this.updateColumnCompose(info, column, colval, node).whenComplete((rs, t) -> {
|
||||
if (t != null) {
|
||||
futureCompleteConsumer.accept(rs, t);
|
||||
} else {
|
||||
@@ -834,14 +829,14 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
if (isOnlyCache(info)) {
|
||||
return CompletableFuture.supplyAsync(() -> updateCache(info, -1, column, colval, node), getExecutor());
|
||||
}
|
||||
if (isAsync()) return DataSqlSource.this.updateColumnCompose(info, column, colval, node).whenComplete((rs, t) -> {
|
||||
if (isAsync()) return this.updateColumnCompose(info, column, colval, node).whenComplete((rs, t) -> {
|
||||
if (t != null) {
|
||||
futureCompleteConsumer.accept(rs, t);
|
||||
} else {
|
||||
updateCache(info, rs, column, colval, node);
|
||||
}
|
||||
});
|
||||
return CompletableFuture.supplyAsync(() -> DataSqlSource.this.updateColumnCompose(info, column, colval, node).join(), getExecutor()).whenComplete((rs, t) -> {
|
||||
return CompletableFuture.supplyAsync(() -> this.updateColumnCompose(info, column, colval, node).join(), getExecutor()).whenComplete((rs, t) -> {
|
||||
if (t != null) {
|
||||
futureCompleteConsumer.accept(rs, t);
|
||||
} else {
|
||||
@@ -871,7 +866,7 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
return updateDB(info, null, sql, true, colval);
|
||||
} else {
|
||||
String sql = "UPDATE " + info.getTable(node) + " a " + (join1 == null ? "" : (", " + join1))
|
||||
+ " SET " + info.getSQLColumn(alias, column) + " = " + formatValueToString(info, colval)
|
||||
+ " SET " + info.getSQLColumn(alias, column) + " = " + info.formatSQLValue(colval, sqlFormatter)
|
||||
+ ((where == null || where.length() == 0) ? (join2 == null ? "" : (" WHERE " + join2))
|
||||
: (" WHERE " + where + (join2 == null ? "" : (" AND " + join2))));
|
||||
return updateDB(info, null, sql, false);
|
||||
@@ -893,7 +888,7 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
if (values == null || values.length < 1) return -1;
|
||||
final EntityInfo<T> info = loadEntityInfo(clazz);
|
||||
if (isOnlyCache(info)) return updateCache(info, -1, pk, values);
|
||||
return DataSqlSource.this.updateColumnCompose(info, pk, values).whenComplete((rs, t) -> {
|
||||
return this.updateColumnCompose(info, pk, values).whenComplete((rs, t) -> {
|
||||
if (t != null) {
|
||||
futureCompleteConsumer.accept(rs, t);
|
||||
} else {
|
||||
@@ -909,14 +904,14 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
if (isOnlyCache(info)) {
|
||||
return CompletableFuture.supplyAsync(() -> updateCache(info, -1, pk, values), getExecutor());
|
||||
}
|
||||
if (isAsync()) return DataSqlSource.this.updateColumnCompose(info, pk, values).whenComplete((rs, t) -> {
|
||||
if (isAsync()) return this.updateColumnCompose(info, pk, values).whenComplete((rs, t) -> {
|
||||
if (t != null) {
|
||||
futureCompleteConsumer.accept(rs, t);
|
||||
} else {
|
||||
updateCache(info, rs, pk, values);
|
||||
}
|
||||
});
|
||||
return CompletableFuture.supplyAsync(() -> DataSqlSource.this.updateColumnCompose(info, pk, values).join(), getExecutor()).whenComplete((rs, t) -> {
|
||||
return CompletableFuture.supplyAsync(() -> this.updateColumnCompose(info, pk, values).join(), getExecutor()).whenComplete((rs, t) -> {
|
||||
if (t != null) {
|
||||
futureCompleteConsumer.accept(rs, t);
|
||||
} else {
|
||||
@@ -934,17 +929,17 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
Attribute<T, Serializable> attr = info.getUpdateAttribute(col.getColumn());
|
||||
if (attr == null) throw new RuntimeException(info.getType() + " cannot found column " + col.getColumn());
|
||||
if (setsql.length() > 0) setsql.append(", ");
|
||||
String c = info.getSQLColumn(null, col.getColumn());
|
||||
String sqlColumn = info.getSQLColumn(null, col.getColumn());
|
||||
if (col.getValue() instanceof byte[]) {
|
||||
if (blobs == null) blobs = new ArrayList<>();
|
||||
blobs.add((byte[]) col.getValue());
|
||||
setsql.append(c).append(" = ").append(prepareParamSign(++index));
|
||||
setsql.append(sqlColumn).append(" = ").append(prepareParamSign(++index));
|
||||
} else {
|
||||
setsql.append(c).append(" = ").append(info.formatSQLValue(c, attr, col));
|
||||
setsql.append(sqlColumn).append(" = ").append(info.formatSQLValue(sqlColumn, attr, col, sqlFormatter));
|
||||
}
|
||||
}
|
||||
if (setsql.length() < 1) return CompletableFuture.completedFuture(0);
|
||||
String sql = "UPDATE " + info.getTable(pk) + " SET " + setsql + " WHERE " + info.getPrimarySQLColumn() + " = " + FilterNode.formatToString(info.getSQLValue(info.getPrimarySQLColumn(), pk));
|
||||
String sql = "UPDATE " + info.getTable(pk) + " SET " + setsql + " WHERE " + info.getPrimarySQLColumn() + " = " + info.formatSQLValue(info.getPrimarySQLColumn(), pk, sqlFormatter);
|
||||
if (blobs == null) return updateDB(info, null, sql, false);
|
||||
return updateDB(info, null, sql, true, blobs.toArray());
|
||||
}
|
||||
@@ -974,7 +969,7 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
if (values == null || values.length < 1) return -1;
|
||||
final EntityInfo<T> info = loadEntityInfo(clazz);
|
||||
if (isOnlyCache(info)) return updateCache(info, -1, node, flipper, values);
|
||||
return DataSqlSource.this.updateColumnCompose(info, node, flipper, values).whenComplete((rs, t) -> {
|
||||
return this.updateColumnCompose(info, node, flipper, values).whenComplete((rs, t) -> {
|
||||
if (t != null) {
|
||||
futureCompleteConsumer.accept(rs, t);
|
||||
} else {
|
||||
@@ -990,14 +985,14 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
if (isOnlyCache(info)) {
|
||||
return CompletableFuture.supplyAsync(() -> updateCache(info, -1, node, flipper, values), getExecutor());
|
||||
}
|
||||
if (isAsync()) return DataSqlSource.this.updateColumnCompose(info, node, flipper, values).whenComplete((rs, t) -> {
|
||||
if (isAsync()) return this.updateColumnCompose(info, node, flipper, values).whenComplete((rs, t) -> {
|
||||
if (t != null) {
|
||||
futureCompleteConsumer.accept(rs, t);
|
||||
} else {
|
||||
updateCache(info, rs, node, flipper, values);
|
||||
}
|
||||
});
|
||||
return CompletableFuture.supplyAsync(() -> DataSqlSource.this.updateColumnCompose(info, node, flipper, values).join(), getExecutor()).whenComplete((rs, t) -> {
|
||||
return CompletableFuture.supplyAsync(() -> this.updateColumnCompose(info, node, flipper, values).join(), getExecutor()).whenComplete((rs, t) -> {
|
||||
if (t != null) {
|
||||
futureCompleteConsumer.accept(rs, t);
|
||||
} else {
|
||||
@@ -1016,13 +1011,13 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
Attribute<T, Serializable> attr = info.getUpdateAttribute(col.getColumn());
|
||||
if (attr == null) continue;
|
||||
if (setsql.length() > 0) setsql.append(", ");
|
||||
String c = info.getSQLColumn(alias, col.getColumn());
|
||||
String sqlColumn = info.getSQLColumn(alias, col.getColumn());
|
||||
if (col.getValue() instanceof byte[]) {
|
||||
if (blobs == null) blobs = new ArrayList<>();
|
||||
blobs.add((byte[]) col.getValue());
|
||||
setsql.append(c).append(" = ").append(prepareParamSign(++index));
|
||||
setsql.append(sqlColumn).append(" = ").append(prepareParamSign(++index));
|
||||
} else {
|
||||
setsql.append(c).append(" = ").append(info.formatSQLValue(c, attr, col));
|
||||
setsql.append(sqlColumn).append(" = ").append(info.formatSQLValue(sqlColumn, attr, col, sqlFormatter));
|
||||
}
|
||||
}
|
||||
if (setsql.length() < 1) return CompletableFuture.completedFuture(0);
|
||||
@@ -1070,7 +1065,7 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
Class<T> clazz = (Class) entity.getClass();
|
||||
final EntityInfo<T> info = loadEntityInfo(clazz);
|
||||
if (isOnlyCache(info)) return updateCache(info, -1, false, entity, null, selects);
|
||||
return DataSqlSource.this.updateColumnCompose(info, false, entity, null, selects).whenComplete((rs, t) -> {
|
||||
return this.updateColumnCompose(info, false, entity, null, selects).whenComplete((rs, t) -> {
|
||||
if (t != null) {
|
||||
futureCompleteConsumer.accept(rs, t);
|
||||
} else {
|
||||
@@ -1087,14 +1082,14 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
if (isOnlyCache(info)) {
|
||||
return CompletableFuture.supplyAsync(() -> updateCache(info, -1, false, entity, null, selects), getExecutor());
|
||||
}
|
||||
if (isAsync()) return DataSqlSource.this.updateColumnCompose(info, false, entity, null, selects).whenComplete((rs, t) -> {
|
||||
if (isAsync()) return this.updateColumnCompose(info, false, entity, null, selects).whenComplete((rs, t) -> {
|
||||
if (t != null) {
|
||||
futureCompleteConsumer.accept(rs, t);
|
||||
} else {
|
||||
updateCache(info, rs, false, entity, null, selects);
|
||||
}
|
||||
});
|
||||
return CompletableFuture.supplyAsync(() -> DataSqlSource.this.updateColumnCompose(info, false, entity, null, selects).join(), getExecutor()).whenComplete((rs, t) -> {
|
||||
return CompletableFuture.supplyAsync(() -> this.updateColumnCompose(info, false, entity, null, selects).join(), getExecutor()).whenComplete((rs, t) -> {
|
||||
if (t != null) {
|
||||
futureCompleteConsumer.accept(rs, t);
|
||||
} else {
|
||||
@@ -1109,7 +1104,7 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
Class<T> clazz = (Class) entity.getClass();
|
||||
final EntityInfo<T> info = loadEntityInfo(clazz);
|
||||
if (isOnlyCache(info)) return updateCache(info, -1, true, entity, node, selects);
|
||||
return DataSqlSource.this.updateColumnCompose(info, true, entity, node, selects).whenComplete((rs, t) -> {
|
||||
return this.updateColumnCompose(info, true, entity, node, selects).whenComplete((rs, t) -> {
|
||||
if (t != null) {
|
||||
futureCompleteConsumer.accept(rs, t);
|
||||
} else {
|
||||
@@ -1126,14 +1121,14 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
if (isOnlyCache(info)) {
|
||||
return CompletableFuture.supplyAsync(() -> updateCache(info, -1, true, entity, node, selects), getExecutor());
|
||||
}
|
||||
if (isAsync()) return DataSqlSource.this.updateColumnCompose(info, true, entity, node, selects).whenComplete((rs, t) -> {
|
||||
if (isAsync()) return this.updateColumnCompose(info, true, entity, node, selects).whenComplete((rs, t) -> {
|
||||
if (t != null) {
|
||||
futureCompleteConsumer.accept(rs, t);
|
||||
} else {
|
||||
updateCache(info, rs, true, entity, node, selects);
|
||||
}
|
||||
});
|
||||
return CompletableFuture.supplyAsync(() -> DataSqlSource.this.updateColumnCompose(info, true, entity, node, selects).join(), getExecutor()).whenComplete((rs, t) -> {
|
||||
return CompletableFuture.supplyAsync(() -> this.updateColumnCompose(info, true, entity, node, selects).join(), getExecutor()).whenComplete((rs, t) -> {
|
||||
if (t != null) {
|
||||
futureCompleteConsumer.accept(rs, t);
|
||||
} else {
|
||||
@@ -1157,7 +1152,7 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
blobs.add((byte[]) val);
|
||||
setsql.append(" = ").append(prepareParamSign(++index));
|
||||
} else {
|
||||
setsql.append(" = ").append(formatValueToString(info, val));
|
||||
setsql.append(" = ").append(info.formatSQLValue(val, sqlFormatter));
|
||||
}
|
||||
}
|
||||
if (neednode) {
|
||||
@@ -1178,7 +1173,7 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
return updateDB(info, null, sql, true, blobs.toArray());
|
||||
} else {
|
||||
final Serializable id = (Serializable) info.getSQLValue(info.getPrimary(), entity);
|
||||
String sql = "UPDATE " + info.getTable(id) + " a SET " + setsql + " WHERE " + info.getPrimarySQLColumn() + " = " + FilterNode.formatToString(id);
|
||||
String sql = "UPDATE " + info.getTable(id) + " a SET " + setsql + " WHERE " + info.getPrimarySQLColumn() + " = " + info.formatSQLValue(id, sqlFormatter);
|
||||
if (blobs == null) return updateDB(info, null, sql, false);
|
||||
return updateDB(info, null, sql, true, blobs.toArray());
|
||||
}
|
||||
@@ -1501,13 +1496,14 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
}
|
||||
|
||||
protected <T, K extends Serializable, N extends Number> CompletableFuture<Map<K, N>> queryColumnMapCompose(final EntityInfo<T> info, final String keyColumn, final FilterFunc func, final String funcColumn, FilterNode node) {
|
||||
final String sqlkey = info.getSQLColumn(null, keyColumn);
|
||||
final String keySqlColumn = info.getSQLColumn(null, keyColumn);
|
||||
final Map<Class, String> joinTabalis = node == null ? null : node.getJoinTabalis();
|
||||
final Set<String> haset = new HashSet<>();
|
||||
final CharSequence join = node == null ? null : node.createSQLJoin(this, false, joinTabalis, haset, info);
|
||||
final CharSequence where = node == null ? null : node.createSQLExpress(info, joinTabalis);
|
||||
final String sql = "SELECT a." + sqlkey + ", " + func.getColumn((funcColumn == null || funcColumn.isEmpty() ? "*" : info.getSQLColumn("a", funcColumn)))
|
||||
+ " FROM " + info.getTable(node) + " a" + (join == null ? "" : join) + ((where == null || where.length() == 0) ? "" : (" WHERE " + where)) + " GROUP BY a." + sqlkey;
|
||||
final String funcSqlColumn = func == null ? info.getSQLColumn("a", funcColumn) : func.getColumn((funcColumn == null || funcColumn.isEmpty() ? "*" : info.getSQLColumn("a", funcColumn)));
|
||||
final String sql = "SELECT a." + keySqlColumn + ", " + funcSqlColumn
|
||||
+ " FROM " + info.getTable(node) + " a" + (join == null ? "" : join) + ((where == null || where.length() == 0) ? "" : (" WHERE " + where)) + " GROUP BY a." + keySqlColumn;
|
||||
if (info.isLoggable(logger, Level.FINEST, sql)) logger.finest(info.getType().getSimpleName() + " querycolumnmap sql=" + sql);
|
||||
return queryColumnMapDB(info, sql, keyColumn);
|
||||
}
|
||||
@@ -1557,7 +1553,7 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
|
||||
protected <T> CompletableFuture<T> findCompose(final EntityInfo<T> info, final SelectColumn selects, Serializable pk) {
|
||||
String column = info.getPrimarySQLColumn();
|
||||
final String sql = "SELECT " + info.getQueryColumns(null, selects) + " FROM " + info.getTable(pk) + " WHERE " + column + " = " + FilterNode.formatToString(info.getSQLValue(column, pk));
|
||||
final String sql = "SELECT " + info.getQueryColumns(null, selects) + " FROM " + info.getTable(pk) + " WHERE " + column + " = " + info.formatSQLValue(column, pk, sqlFormatter);
|
||||
if (info.isLoggable(logger, Level.FINEST, sql)) logger.finest(info.getType().getSimpleName() + " find sql=" + sql);
|
||||
return findDB(info, sql, true, selects);
|
||||
}
|
||||
@@ -1607,7 +1603,7 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
final EntityInfo<T> info = loadEntityInfo(clazz);
|
||||
final EntityCache<T> cache = info.getCache();
|
||||
if (cache != null && cache.isFullLoaded() && (node == null || node.isCacheUseable(this))) return cache.find(selects, node);
|
||||
return DataSqlSource.this.findCompose(info, selects, node).join();
|
||||
return this.findCompose(info, selects, node).join();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -1617,8 +1613,8 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
if (cache != null && cache.isFullLoaded() && (node == null || node.isCacheUseable(this))) {
|
||||
return CompletableFuture.completedFuture(cache.find(selects, node));
|
||||
}
|
||||
if (isAsync()) return DataSqlSource.this.findCompose(info, selects, node);
|
||||
return CompletableFuture.supplyAsync(() -> DataSqlSource.this.findCompose(info, selects, node).join(), getExecutor());
|
||||
if (isAsync()) return this.findCompose(info, selects, node);
|
||||
return CompletableFuture.supplyAsync(() -> this.findCompose(info, selects, node).join(), getExecutor());
|
||||
}
|
||||
|
||||
protected <T> CompletableFuture<T> findCompose(final EntityInfo<T> info, final SelectColumn selects, final FilterNode node) {
|
||||
@@ -1694,7 +1690,7 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
}
|
||||
|
||||
protected <T> CompletableFuture<Serializable> findColumnCompose(final EntityInfo<T> info, String column, final Serializable defValue, final Serializable pk) {
|
||||
final String sql = "SELECT " + info.getSQLColumn(null, column) + " FROM " + info.getTable(pk) + " WHERE " + info.getPrimarySQLColumn() + " = " + FilterNode.formatToString(info.getSQLValue(info.getPrimarySQLColumn(), pk));
|
||||
final String sql = "SELECT " + info.getSQLColumn(null, column) + " FROM " + info.getTable(pk) + " WHERE " + info.getPrimarySQLColumn() + " = " + info.formatSQLValue(info.getPrimarySQLColumn(), pk, sqlFormatter);
|
||||
if (info.isLoggable(logger, Level.FINEST, sql)) logger.finest(info.getType().getSimpleName() + " find sql=" + sql);
|
||||
return findColumnDB(info, sql, true, column, defValue);
|
||||
}
|
||||
@@ -1707,7 +1703,7 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
Serializable val = cache.findColumn(column, defValue, node);
|
||||
if (cache.isFullLoaded() || val != null) return val;
|
||||
}
|
||||
return DataSqlSource.this.findColumnCompose(info, column, defValue, node).join();
|
||||
return this.findColumnCompose(info, column, defValue, node).join();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -1718,8 +1714,8 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
Serializable val = cache.findColumn(column, defValue, node);
|
||||
if (cache.isFullLoaded() || val != null) return CompletableFuture.completedFuture(val);
|
||||
}
|
||||
if (isAsync()) return DataSqlSource.this.findColumnCompose(info, column, defValue, node);
|
||||
return CompletableFuture.supplyAsync(() -> DataSqlSource.this.findColumnCompose(info, column, defValue, node).join(), getExecutor());
|
||||
if (isAsync()) return this.findColumnCompose(info, column, defValue, node);
|
||||
return CompletableFuture.supplyAsync(() -> this.findColumnCompose(info, column, defValue, node).join(), getExecutor());
|
||||
}
|
||||
|
||||
protected <T> CompletableFuture<Serializable> findColumnCompose(final EntityInfo<T> info, String column, final Serializable defValue, final FilterNode node) {
|
||||
@@ -1756,7 +1752,7 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
}
|
||||
|
||||
protected <T> CompletableFuture<Boolean> existsCompose(final EntityInfo<T> info, Serializable pk) {
|
||||
final String sql = "SELECT COUNT(*) FROM " + info.getTable(pk) + " WHERE " + info.getPrimarySQLColumn() + " = " + FilterNode.formatToString(info.getSQLValue(info.getPrimarySQLColumn(), pk));
|
||||
final String sql = "SELECT COUNT(*) FROM " + info.getTable(pk) + " WHERE " + info.getPrimarySQLColumn() + " = " + info.formatSQLValue(info.getPrimarySQLColumn(), pk, sqlFormatter);
|
||||
if (info.isLoggable(logger, Level.FINEST, sql)) logger.finest(info.getType().getSimpleName() + " exists sql=" + sql);
|
||||
return existsDB(info, sql, true);
|
||||
}
|
||||
@@ -1779,7 +1775,7 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
boolean rs = cache.exists(node);
|
||||
if (rs || cache.isFullLoaded()) return rs;
|
||||
}
|
||||
return DataSqlSource.this.existsCompose(info, node).join();
|
||||
return this.existsCompose(info, node).join();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -1790,8 +1786,8 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
boolean rs = cache.exists(node);
|
||||
if (rs || cache.isFullLoaded()) return CompletableFuture.completedFuture(rs);
|
||||
}
|
||||
if (isAsync()) return DataSqlSource.this.existsCompose(info, node);
|
||||
return CompletableFuture.supplyAsync(() -> DataSqlSource.this.existsCompose(info, node).join(), getExecutor());
|
||||
if (isAsync()) return this.existsCompose(info, node);
|
||||
return CompletableFuture.supplyAsync(() -> this.existsCompose(info, node).join(), getExecutor());
|
||||
}
|
||||
|
||||
protected <T> CompletableFuture<Boolean> existsCompose(final EntityInfo<T> info, FilterNode node) {
|
||||
|
||||
@@ -265,34 +265,37 @@ public final class EntityCache<T> {
|
||||
if (filter != null) stream = stream.filter(filter);
|
||||
Collector<T, Map, ?> collector = null;
|
||||
final Class valtype = funcAttr == null ? null : funcAttr.type();
|
||||
switch (func) {
|
||||
case AVG:
|
||||
if (valtype == float.class || valtype == Float.class || valtype == double.class || valtype == Double.class) {
|
||||
collector = (Collector<T, Map, ?>) Collectors.averagingDouble((T t) -> ((Number) funcAttr.get(t)).doubleValue());
|
||||
} else {
|
||||
collector = (Collector<T, Map, ?>) Collectors.averagingLong((T t) -> ((Number) funcAttr.get(t)).longValue());
|
||||
}
|
||||
break;
|
||||
case COUNT:
|
||||
collector = (Collector<T, Map, ?>) Collectors.counting();
|
||||
break;
|
||||
case DISTINCTCOUNT:
|
||||
collector = (Collector<T, Map, ?>) Collectors.mapping((t) -> funcAttr.get(t), Collectors.toSet());
|
||||
break;
|
||||
case MAX:
|
||||
case MIN:
|
||||
Comparator<T> comp = (o1, o2) -> o1 == null ? (o2 == null ? 0 : -1) : ((Comparable) funcAttr.get(o1)).compareTo(funcAttr.get(o2));
|
||||
collector = (Collector<T, Map, ?>) ((func == MAX) ? Collectors.maxBy(comp) : Collectors.minBy(comp));
|
||||
break;
|
||||
case SUM:
|
||||
if (valtype == float.class || valtype == Float.class || valtype == double.class || valtype == Double.class) {
|
||||
collector = (Collector<T, Map, ?>) Collectors.summingDouble((T t) -> ((Number) funcAttr.get(t)).doubleValue());
|
||||
} else {
|
||||
collector = (Collector<T, Map, ?>) Collectors.summingLong((T t) -> ((Number) funcAttr.get(t)).longValue());
|
||||
}
|
||||
break;
|
||||
if (func != null) {
|
||||
switch (func) {
|
||||
case AVG:
|
||||
if (valtype == float.class || valtype == Float.class || valtype == double.class || valtype == Double.class) {
|
||||
collector = (Collector<T, Map, ?>) Collectors.averagingDouble((T t) -> ((Number) funcAttr.get(t)).doubleValue());
|
||||
} else {
|
||||
collector = (Collector<T, Map, ?>) Collectors.averagingLong((T t) -> ((Number) funcAttr.get(t)).longValue());
|
||||
}
|
||||
break;
|
||||
case COUNT:
|
||||
collector = (Collector<T, Map, ?>) Collectors.counting();
|
||||
break;
|
||||
case DISTINCTCOUNT:
|
||||
collector = (Collector<T, Map, ?>) Collectors.mapping((t) -> funcAttr.get(t), Collectors.toSet());
|
||||
break;
|
||||
case MAX:
|
||||
case MIN:
|
||||
Comparator<T> comp = (o1, o2) -> o1 == null ? (o2 == null ? 0 : -1) : ((Comparable) funcAttr.get(o1)).compareTo(funcAttr.get(o2));
|
||||
collector = (Collector<T, Map, ?>) ((func == MAX) ? Collectors.maxBy(comp) : Collectors.minBy(comp));
|
||||
break;
|
||||
case SUM:
|
||||
if (valtype == float.class || valtype == Float.class || valtype == double.class || valtype == Double.class) {
|
||||
collector = (Collector<T, Map, ?>) Collectors.summingDouble((T t) -> ((Number) funcAttr.get(t)).doubleValue());
|
||||
} else {
|
||||
collector = (Collector<T, Map, ?>) Collectors.summingLong((T t) -> ((Number) funcAttr.get(t)).longValue());
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
Map rs = stream.collect(Collectors.groupingBy(t -> keyAttr.get(t), LinkedHashMap::new, collector));
|
||||
Map rs = collector == null ? stream.collect(Collectors.toMap(t -> keyAttr.get(t), t -> funcAttr.get(t), (key1, key2) -> key2))
|
||||
: stream.collect(Collectors.groupingBy(t -> keyAttr.get(t), LinkedHashMap::new, collector));
|
||||
if (func == MAX || func == MIN) {
|
||||
Map rs2 = new LinkedHashMap();
|
||||
rs.forEach((x, y) -> {
|
||||
@@ -599,39 +602,17 @@ public final class EntityCache<T> {
|
||||
Serializable newval = null;
|
||||
switch (express) {
|
||||
case INC:
|
||||
numb = (Number) attr.get(entity);
|
||||
if (numb == null) {
|
||||
numb = (Number) val;
|
||||
} else {
|
||||
numb = numb.longValue() + ((Number) val).longValue();
|
||||
}
|
||||
break;
|
||||
case MUL:
|
||||
numb = (Number) attr.get(entity);
|
||||
if (numb == null) {
|
||||
numb = 0;
|
||||
} else {
|
||||
numb = numb.longValue() * ((Number) val).floatValue();
|
||||
}
|
||||
break;
|
||||
case DIV:
|
||||
case MOD:
|
||||
case AND:
|
||||
numb = (Number) attr.get(entity);
|
||||
if (numb == null) {
|
||||
numb = 0;
|
||||
} else {
|
||||
numb = numb.longValue() & ((Number) val).longValue();
|
||||
}
|
||||
break;
|
||||
case ORR:
|
||||
numb = (Number) attr.get(entity);
|
||||
if (numb == null) {
|
||||
numb = 0;
|
||||
} else {
|
||||
numb = numb.longValue() | ((Number) val).longValue();
|
||||
}
|
||||
numb = getValue((Number) attr.get(entity), express, val);
|
||||
break;
|
||||
case MOV:
|
||||
if (val instanceof ColumnNodeValue) val = updateColumnNodeValue(attr, entity, (ColumnNodeValue) val);
|
||||
newval = val;
|
||||
if (val instanceof Number) numb = (Number) val;
|
||||
break;
|
||||
}
|
||||
if (numb != null) {
|
||||
@@ -663,6 +644,70 @@ public final class EntityCache<T> {
|
||||
return entity;
|
||||
}
|
||||
|
||||
private <V> Serializable updateColumnNodeValue(Attribute<T, V> attr, final T entity, ColumnNodeValue node) {
|
||||
Serializable left = node.getLeft();
|
||||
if (left instanceof CharSequence) {
|
||||
left = info.getUpdateAttribute(left.toString()).get(entity);
|
||||
} else if (left instanceof ColumnNodeValue) {
|
||||
left = updateColumnNodeValue(attr, entity, (ColumnNodeValue) left);
|
||||
}
|
||||
Serializable right = node.getRight();
|
||||
if (left instanceof CharSequence) {
|
||||
right = info.getUpdateAttribute(right.toString()).get(entity);
|
||||
} else if (left instanceof ColumnNodeValue) {
|
||||
right = updateColumnNodeValue(attr, entity, (ColumnNodeValue) right);
|
||||
}
|
||||
return getValue((Number) left, node.getExpress(), right);
|
||||
}
|
||||
|
||||
private <V> Number getValue(Number numb, final ColumnExpress express, Serializable val) {
|
||||
switch (express) {
|
||||
case INC:
|
||||
if (numb == null) {
|
||||
numb = (Number) val;
|
||||
} else {
|
||||
numb = numb.longValue() + ((Number) val).longValue();
|
||||
}
|
||||
break;
|
||||
case MUL:
|
||||
if (numb == null) {
|
||||
numb = 0;
|
||||
} else {
|
||||
numb = numb.longValue() * ((Number) val).floatValue();
|
||||
}
|
||||
break;
|
||||
case DIV:
|
||||
if (numb == null) {
|
||||
numb = 0;
|
||||
} else {
|
||||
numb = numb.longValue() / ((Number) val).floatValue();
|
||||
}
|
||||
break;
|
||||
case MOD:
|
||||
if (numb == null) {
|
||||
numb = 0;
|
||||
} else {
|
||||
numb = numb.longValue() % ((Number) val).intValue();
|
||||
}
|
||||
break;
|
||||
case AND:
|
||||
if (numb == null) {
|
||||
numb = 0;
|
||||
} else {
|
||||
numb = numb.longValue() & ((Number) val).longValue();
|
||||
}
|
||||
break;
|
||||
case ORR:
|
||||
if (numb == null) {
|
||||
numb = 0;
|
||||
} else {
|
||||
numb = numb.longValue() | ((Number) val).longValue();
|
||||
}
|
||||
break;
|
||||
}
|
||||
return numb;
|
||||
}
|
||||
|
||||
public Attribute<T, Serializable> getAttribute(String fieldname) {
|
||||
return info.getAttribute(fieldname);
|
||||
}
|
||||
|
||||
@@ -89,16 +89,16 @@ public final class EntityInfo<T> {
|
||||
final String notcontainSQL;
|
||||
|
||||
//用于判断表不存在的使用, 多个SQLState用;隔开
|
||||
final String tablenotexistSqlstates;
|
||||
private final String tablenotexistSqlstates;
|
||||
|
||||
//用于复制表结构使用
|
||||
final String tablecopySQL;
|
||||
private final String tablecopySQL;
|
||||
|
||||
//用于存在table_20160202类似这种分布式表
|
||||
final Set<String> tables = new HashSet<>();
|
||||
//用于存在database.table_20160202类似这种分布式表
|
||||
private final Set<String> tables = new HashSet<>();
|
||||
|
||||
//分表 策略
|
||||
final DistributeTableStrategy<T> tableStrategy;
|
||||
private final DistributeTableStrategy<T> tableStrategy;
|
||||
|
||||
//根据主键查找单个对象的SQL, 含 ?
|
||||
private final String queryPrepareSQL;
|
||||
@@ -154,12 +154,6 @@ public final class EntityInfo<T> {
|
||||
//Flipper.sort转换成以ORDER BY开头SQL的缓存
|
||||
private final Map<String, String> sortOrderbySqls = new ConcurrentHashMap<>();
|
||||
|
||||
//是否由数据库生成主键值
|
||||
final boolean autoGenerated;
|
||||
|
||||
//是否UUID主键
|
||||
final boolean autouuid;
|
||||
|
||||
//所属的DataSource
|
||||
final DataSource source;
|
||||
|
||||
@@ -245,7 +239,7 @@ public final class EntityInfo<T> {
|
||||
}
|
||||
//---------------------------------------------
|
||||
Table t = type.getAnnotation(Table.class);
|
||||
if (type.getAnnotation(VirtualEntity.class) != null || "memory".equalsIgnoreCase(source.getType())) {
|
||||
if (type.getAnnotation(VirtualEntity.class) != null || (source == null || "memory".equalsIgnoreCase(source.getType()))) {
|
||||
this.table = null;
|
||||
BiFunction<DataSource, Class, List> loader = null;
|
||||
try {
|
||||
@@ -287,8 +281,6 @@ public final class EntityInfo<T> {
|
||||
List<Attribute<T, Serializable>> insertattrs = new ArrayList<>();
|
||||
List<String> updatecols = new ArrayList<>();
|
||||
List<Attribute<T, Serializable>> updateattrs = new ArrayList<>();
|
||||
boolean auto = false;
|
||||
boolean uuid = false;
|
||||
Map<Class, Creator<CryptHandler>> cryptCreatorMap = new HashMap<>();
|
||||
do {
|
||||
for (Field field : cltmp.getDeclaredFields()) {
|
||||
@@ -318,19 +310,8 @@ public final class EntityInfo<T> {
|
||||
}
|
||||
if (field.getAnnotation(javax.persistence.Id.class) != null && idAttr0 == null) {
|
||||
idAttr0 = attr;
|
||||
GeneratedValue gv = field.getAnnotation(GeneratedValue.class);
|
||||
auto = gv != null;
|
||||
// if (gv != null && gv.strategy() != GenerationType.IDENTITY) {
|
||||
// throw new RuntimeException(cltmp.getName() + "'s @ID primary not a GenerationType.IDENTITY");
|
||||
// }
|
||||
if (gv != null && field.getType() == String.class) { //UUID
|
||||
uuid = true;
|
||||
auto = false;
|
||||
}
|
||||
if (!auto) {
|
||||
insertcols.add(sqlfield);
|
||||
insertattrs.add(attr);
|
||||
}
|
||||
insertcols.add(sqlfield);
|
||||
insertattrs.add(attr);
|
||||
} else {
|
||||
if (col == null || col.insertable()) {
|
||||
insertcols.add(sqlfield);
|
||||
@@ -456,8 +437,6 @@ public final class EntityInfo<T> {
|
||||
this.deleteNamesPrepareSQL = null;
|
||||
this.queryNamesPrepareSQL = null;
|
||||
}
|
||||
this.autoGenerated = auto;
|
||||
this.autouuid = uuid;
|
||||
//----------------cache--------------
|
||||
Cacheable c = type.getAnnotation(Cacheable.class);
|
||||
if (this.table == null || (!cacheForbidden && c != null && c.value())) {
|
||||
@@ -482,15 +461,6 @@ public final class EntityInfo<T> {
|
||||
return jsonConvert;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建主键值,目前只支持UUID赋值
|
||||
*
|
||||
* @param src Entity对象
|
||||
*/
|
||||
public void createPrimaryValue(T src) {
|
||||
if (autouuid) getPrimary().set(src, Utility.uuid());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Entity缓存器
|
||||
*
|
||||
@@ -536,18 +506,22 @@ public final class EntityInfo<T> {
|
||||
return table == null;
|
||||
}
|
||||
|
||||
public boolean isAutoGenerated() {
|
||||
return autoGenerated;
|
||||
}
|
||||
|
||||
public boolean isAutouuid() {
|
||||
return autouuid;
|
||||
}
|
||||
|
||||
public DistributeTableStrategy<T> getTableStrategy() {
|
||||
return tableStrategy;
|
||||
}
|
||||
|
||||
public Object disTableLock() {
|
||||
return tables;
|
||||
}
|
||||
|
||||
public boolean containsDisTable(String tablekey) {
|
||||
return tables.contains(tablekey);
|
||||
}
|
||||
|
||||
public void addDisTable(String tablekey) {
|
||||
tables.add(tablekey);
|
||||
}
|
||||
|
||||
public String getTableNotExistSqlStates2() {
|
||||
return tablenotexistSqlstates;
|
||||
}
|
||||
@@ -894,9 +868,36 @@ public final class EntityInfo<T> {
|
||||
return handler.encrypt(fieldvalue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 字段值转换成带转义的数据库的值
|
||||
*
|
||||
* @param fieldname 字段名
|
||||
* @param fieldvalue 字段值
|
||||
* @param sqlFormatter 转义器
|
||||
*
|
||||
* @return CharSequence
|
||||
*/
|
||||
public CharSequence formatSQLValue(String fieldname, Serializable fieldvalue, BiFunction<EntityInfo, Object, CharSequence> sqlFormatter) {
|
||||
Object val = getSQLValue(fieldname, fieldvalue);
|
||||
return sqlFormatter == null ? formatToString(val) : sqlFormatter.apply(this, val);
|
||||
}
|
||||
|
||||
/**
|
||||
* 字段值转换成带转义的数据库的值
|
||||
*
|
||||
* @param value 字段值
|
||||
* @param sqlFormatter 转义器
|
||||
*
|
||||
* @return CharSequence
|
||||
*/
|
||||
public CharSequence formatSQLValue(Object value, BiFunction<EntityInfo, Object, CharSequence> sqlFormatter) {
|
||||
return sqlFormatter == null ? formatToString(value) : sqlFormatter.apply(this, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 字段值转换成数据库的值
|
||||
*
|
||||
* @param <F> 泛型
|
||||
* @param attr Attribute
|
||||
* @param entity 记录对象
|
||||
*
|
||||
@@ -909,6 +910,21 @@ public final class EntityInfo<T> {
|
||||
return val;
|
||||
}
|
||||
|
||||
/**
|
||||
* 字段值转换成带转义的数据库的值
|
||||
*
|
||||
* @param <F> 泛型
|
||||
* @param attr Attribute
|
||||
* @param entity 记录对象
|
||||
* @param sqlFormatter 转义器
|
||||
*
|
||||
* @return CharSequence
|
||||
*/
|
||||
public <F> CharSequence formatSQLValue(Attribute<T, F> attr, T entity, BiFunction<EntityInfo, Object, CharSequence> sqlFormatter) {
|
||||
Object val = getSQLValue(attr, entity);
|
||||
return sqlFormatter == null ? formatToString(val) : sqlFormatter.apply(this, val);
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据库的值转换成数字段值
|
||||
*
|
||||
@@ -947,30 +963,68 @@ public final class EntityInfo<T> {
|
||||
/**
|
||||
* 拼接UPDATE给字段赋值的SQL片段
|
||||
*
|
||||
* @param col 表字段名
|
||||
* @param attr Attribute
|
||||
* @param cv ColumnValue
|
||||
* @param sqlColumn 表字段名
|
||||
* @param attr Attribute
|
||||
* @param cv ColumnValue
|
||||
* @param formatter 转义器
|
||||
*
|
||||
* @return CharSequence
|
||||
*/
|
||||
protected CharSequence formatSQLValue(String col, Attribute<T, Serializable> attr, final ColumnValue cv) {
|
||||
protected CharSequence formatSQLValue(String sqlColumn, Attribute<T, Serializable> attr, final ColumnValue cv, BiFunction<EntityInfo, Object, CharSequence> formatter) {
|
||||
if (cv == null) return null;
|
||||
Object val = cv.getValue();
|
||||
CryptHandler handler = attr.attach();
|
||||
if (handler != null) val = handler.encrypt(val);
|
||||
if (val instanceof ColumnNodeValue && cv.getExpress() == ColumnExpress.MOV) return formatSQLValue(attr, (ColumnNodeValue) val, formatter);
|
||||
switch (cv.getExpress()) {
|
||||
case INC:
|
||||
return new StringBuilder().append(col).append(" + ").append(val);
|
||||
return new StringBuilder().append(sqlColumn).append(" + ").append(val);
|
||||
case MUL:
|
||||
return new StringBuilder().append(col).append(" * ").append(val);
|
||||
return new StringBuilder().append(sqlColumn).append(" * ").append(val);
|
||||
case DIV:
|
||||
return new StringBuilder().append(sqlColumn).append(" / ").append(val);
|
||||
case MOD:
|
||||
return new StringBuilder().append(sqlColumn).append(" % ").append(val);
|
||||
case AND:
|
||||
return new StringBuilder().append(col).append(" & ").append(val);
|
||||
return new StringBuilder().append(sqlColumn).append(" & ").append(val);
|
||||
case ORR:
|
||||
return new StringBuilder().append(col).append(" | ").append(val);
|
||||
return new StringBuilder().append(sqlColumn).append(" | ").append(val);
|
||||
case MOV:
|
||||
return formatToString(val);
|
||||
CryptHandler handler = attr.attach();
|
||||
if (handler != null) val = handler.encrypt(val);
|
||||
return formatter == null ? formatToString(val) : formatter.apply(this, val);
|
||||
}
|
||||
return formatToString(val);
|
||||
CryptHandler handler = attr.attach();
|
||||
if (handler != null) val = handler.encrypt(val);
|
||||
return formatter == null ? formatToString(val) : formatter.apply(this, val);
|
||||
}
|
||||
|
||||
protected CharSequence formatSQLValue(Attribute<T, Serializable> attr, final ColumnNodeValue node, BiFunction<EntityInfo, Object, CharSequence> formatter) {
|
||||
Serializable left = node.getLeft();
|
||||
if (left instanceof CharSequence) {
|
||||
left = this.getSQLColumn(null, left.toString());
|
||||
} else if (left instanceof ColumnNodeValue) {
|
||||
left = "(" + formatSQLValue(attr, (ColumnNodeValue) left, formatter) + ")";
|
||||
}
|
||||
Serializable right = node.getRight();
|
||||
if (right instanceof CharSequence) {
|
||||
right = this.getSQLColumn(null, right.toString());
|
||||
} else if (left instanceof ColumnNodeValue) {
|
||||
right = "(" + formatSQLValue(attr, (ColumnNodeValue) right, formatter) + ")";
|
||||
}
|
||||
switch (node.getExpress()) {
|
||||
case INC:
|
||||
return new StringBuilder().append(left).append(" + ").append(right);
|
||||
case MUL:
|
||||
return new StringBuilder().append(left).append(" * ").append(right);
|
||||
case DIV:
|
||||
return new StringBuilder().append(left).append(" / ").append(right);
|
||||
case MOD:
|
||||
return new StringBuilder().append(left).append(" % ").append(right);
|
||||
case AND:
|
||||
return new StringBuilder().append(left).append(" & ").append(right);
|
||||
case ORR:
|
||||
return new StringBuilder().append(left).append(" | ").append(right);
|
||||
}
|
||||
throw new IllegalArgumentException(node + " express cannot be null or MOV");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1021,7 +1075,7 @@ public final class EntityInfo<T> {
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
protected String formatToString(Object value) {
|
||||
private String formatToString(Object value) {
|
||||
if (value == null) return null;
|
||||
if (value instanceof CharSequence) {
|
||||
return new StringBuilder().append('\'').append(value.toString().replace("'", "\\'")).append('\'').toString();
|
||||
|
||||
@@ -31,6 +31,11 @@ public enum FilterExpress {
|
||||
NOTLIKE("NOT LIKE"),
|
||||
IGNORECASELIKE("LIKE"), //不区分大小写的 LIKE
|
||||
IGNORECASENOTLIKE("NOT LIKE"), //不区分大小写的 NOT LIKE
|
||||
LENGTH_EQUAL("="), //字符串值的长度
|
||||
LENGTH_LESSTHAN("<"), //字符串值的长度 <
|
||||
LENGTH_LESSTHANOREQUALTO("<="), //字符串值的长度 <=
|
||||
LENGTH_GREATERTHAN(">"), //字符串值的长度 >
|
||||
LENGTH_GREATERTHANOREQUALTO(">="), //字符串值的长度 >=
|
||||
|
||||
CONTAIN("CONTAIN"), //包含, 相当于反向LIKE
|
||||
NOTCONTAIN("NOT CONTAIN"), //不包含, 相当于反向LIKE
|
||||
|
||||
@@ -397,7 +397,10 @@ public class FilterNode { //FilterNode 不能实现Serializable接口, 否则
|
||||
if (express == NOTCONTAIN) return info.notcontainSQL.replace("${column}", info.getSQLColumn(talis, column)).replace("${keystr}", val);
|
||||
if (express == IGNORECASENOTCONTAIN) return info.notcontainSQL.replace("${column}", "LOWER(" + info.getSQLColumn(talis, column) + ")").replace("${keystr}", val);
|
||||
|
||||
if (express == IGNORECASEEQUAL || express == IGNORECASENOTEQUAL || express == IGNORECASELIKE || express == IGNORECASENOTLIKE) {
|
||||
if (express == LENGTH_EQUAL || express == LENGTH_LESSTHAN || express == LENGTH_LESSTHANOREQUALTO
|
||||
|| express == LENGTH_GREATERTHAN || express == LENGTH_GREATERTHANOREQUALTO) {
|
||||
sb.append("LENGTH(").append(info.getSQLColumn(talis, column)).append(')');
|
||||
} else if (express == IGNORECASEEQUAL || express == IGNORECASENOTEQUAL || express == IGNORECASELIKE || express == IGNORECASENOTLIKE) {
|
||||
sb.append("LOWER(").append(info.getSQLColumn(talis, column)).append(')');
|
||||
if (fk) val = "LOWER(" + info.getSQLColumn(talis, ((FilterKey) val0).getColumn()) + ')';
|
||||
} else {
|
||||
@@ -1406,6 +1409,81 @@ public class FilterNode { //FilterNode 不能实现Serializable接口, 否则
|
||||
return "LOWER(" + field + ") " + express.value() + ' ' + formatToString(valstr2);
|
||||
}
|
||||
};
|
||||
case LENGTH_EQUAL:
|
||||
final int intval = ((Number) val).intValue();
|
||||
return new Predicate<T>() {
|
||||
|
||||
@Override
|
||||
public boolean test(T t) {
|
||||
Object rs = attr.get(t);
|
||||
return (rs == null && 0 == intval) || (rs != null && rs.toString().length() == intval);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "LENGTH(" + field + ") " + express.value() + ' ' + intval;
|
||||
}
|
||||
};
|
||||
case LENGTH_LESSTHAN:
|
||||
final int intval2 = ((Number) val).intValue();
|
||||
return new Predicate<T>() {
|
||||
|
||||
@Override
|
||||
public boolean test(T t) {
|
||||
Object rs = attr.get(t);
|
||||
return (rs == null && 0 < intval2) || (rs != null && rs.toString().length() < intval2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "LENGTH(" + field + ") " + express.value() + ' ' + intval2;
|
||||
}
|
||||
};
|
||||
case LENGTH_LESSTHANOREQUALTO:
|
||||
final int intval3 = ((Number) val).intValue();
|
||||
return new Predicate<T>() {
|
||||
|
||||
@Override
|
||||
public boolean test(T t) {
|
||||
Object rs = attr.get(t);
|
||||
return (rs == null && 0 <= intval3) || (rs != null && rs.toString().length() <= intval3);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "LENGTH(" + field + ") " + express.value() + ' ' + intval3;
|
||||
}
|
||||
};
|
||||
case LENGTH_GREATERTHAN:
|
||||
final int intval4 = ((Number) val).intValue();
|
||||
return new Predicate<T>() {
|
||||
|
||||
@Override
|
||||
public boolean test(T t) {
|
||||
Object rs = attr.get(t);
|
||||
return (rs == null && 0 > intval4) || (rs != null && rs.toString().length() > intval4);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "LENGTH(" + field + ") " + express.value() + ' ' + intval4;
|
||||
}
|
||||
};
|
||||
case LENGTH_GREATERTHANOREQUALTO:
|
||||
final int intval5 = ((Number) val).intValue();
|
||||
return new Predicate<T>() {
|
||||
|
||||
@Override
|
||||
public boolean test(T t) {
|
||||
Object rs = attr.get(t);
|
||||
return (rs == null && 0 >= intval5) || (rs != null && rs.toString().length() >= intval5);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "LENGTH(" + field + ") " + express.value() + ' ' + intval5;
|
||||
}
|
||||
};
|
||||
case CONTAIN:
|
||||
return fk ? new Predicate<T>() {
|
||||
|
||||
@@ -1811,7 +1889,7 @@ public class FilterNode { //FilterNode 不能实现Serializable接口, 否则
|
||||
return sb;
|
||||
}
|
||||
|
||||
protected static CharSequence formatToString(Object value) {
|
||||
private static CharSequence formatToString(Object value) {
|
||||
CharSequence sb = formatToString(null, value);
|
||||
return sb == null ? null : sb.toString();
|
||||
}
|
||||
|
||||
@@ -363,6 +363,9 @@ public final class FilterNodeBean<T extends FilterBean> implements Comparable<Fi
|
||||
sb.append(col).append(' ').append(express.value());
|
||||
} else if (express == ISEMPTY || express == ISNOTEMPTY) {
|
||||
sb.append(col).append(' ').append(express.value()).append(" ''");
|
||||
} else if (express == LENGTH_EQUAL || express == LENGTH_LESSTHAN || express == LENGTH_LESSTHANOREQUALTO
|
||||
|| express == LENGTH_GREATERTHAN || express == LENGTH_GREATERTHANOREQUALTO) {
|
||||
sb.append("LENGTH(").append(col).append(") ").append(express.value()).append(" ?");
|
||||
} else {
|
||||
boolean lower = (express == IGNORECASEEQUAL || express == IGNORECASENOTEQUAL || express == IGNORECASELIKE
|
||||
|| express == IGNORECASENOTLIKE || express == IGNORECASECONTAIN || express == IGNORECASENOTCONTAIN);
|
||||
|
||||
@@ -38,6 +38,8 @@ public abstract class PoolTcpSource extends PoolSource<AsyncConnection> {
|
||||
//TCP Channel组
|
||||
protected AsynchronousChannelGroup group;
|
||||
|
||||
protected ScheduledThreadPoolExecutor scheduler;
|
||||
|
||||
protected final ArrayBlockingQueue<AsyncConnection> connQueue;
|
||||
|
||||
public PoolTcpSource(String rwtype, ArrayBlockingQueue queue, Semaphore semaphore, Properties prop, Logger logger, ObjectPool<ByteBuffer> bufferPool, ThreadPoolExecutor executor) {
|
||||
@@ -50,6 +52,42 @@ public abstract class PoolTcpSource extends PoolSource<AsyncConnection> {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
this.connQueue = queue == null ? new ArrayBlockingQueue<>(this.maxconns) : queue;
|
||||
this.scheduler = new ScheduledThreadPoolExecutor(1, (Runnable r) -> {
|
||||
final Thread t = new Thread(r, "PoolSource-Scheduled-Thread");
|
||||
t.setDaemon(true);
|
||||
return t;
|
||||
});
|
||||
this.scheduler.scheduleAtFixedRate(() -> {
|
||||
runPingTask();
|
||||
}, 60, 30, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
private void runPingTask() {
|
||||
try {
|
||||
if (connQueue.isEmpty()) return;
|
||||
long time = System.currentTimeMillis() - 30 * 1000;
|
||||
AsyncConnection first = connQueue.peek();
|
||||
if (first == null || first.getLastReadTime() >= time || first.getLastWriteTime() >= time) return;
|
||||
pollAsync().whenComplete((conn, e) -> {
|
||||
if (e != null) return;
|
||||
if (conn.getLastReadTime() >= time || conn.getLastWriteTime() >= time) {//半分钟内已经用过
|
||||
offerConnection(conn);
|
||||
return;
|
||||
}
|
||||
CompletableFuture<AsyncConnection> future = sendPingCommand(conn);
|
||||
if (future == null) { //不支持ping
|
||||
offerConnection(conn);
|
||||
return;
|
||||
}
|
||||
future.whenComplete((conn2, e2) -> {
|
||||
if (e2 != null) return;
|
||||
offerConnection(conn2);
|
||||
runPingTask();
|
||||
});
|
||||
});
|
||||
} catch (Exception e) {
|
||||
logger.log(Level.FINEST, "PoolSource task ping failed", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -216,6 +254,7 @@ public abstract class PoolTcpSource extends PoolSource<AsyncConnection> {
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
this.scheduler.shutdownNow();
|
||||
connQueue.stream().forEach(x -> {
|
||||
CompletableFuture<AsyncConnection> future = null;
|
||||
try {
|
||||
@@ -232,5 +271,7 @@ public abstract class PoolTcpSource extends PoolSource<AsyncConnection> {
|
||||
});
|
||||
}
|
||||
|
||||
protected abstract CompletableFuture<AsyncConnection> sendPingCommand(final AsyncConnection conn);
|
||||
|
||||
protected abstract CompletableFuture<AsyncConnection> sendCloseCommand(final AsyncConnection conn);
|
||||
}
|
||||
|
||||
@@ -142,6 +142,21 @@ public final class ByteArray {
|
||||
return Arrays.copyOf(content, count);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取byte[]
|
||||
*
|
||||
* @param offset 偏移位
|
||||
* @param length 长度
|
||||
*
|
||||
* @return byte[]
|
||||
*/
|
||||
public byte[] getBytes(int offset, int length) {
|
||||
if (length < 1) return new byte[0];
|
||||
byte[] bs = new byte[length];
|
||||
System.arraycopy(this.content, offset, bs, 0, length);
|
||||
return bs;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取byte[]并清空
|
||||
*
|
||||
|
||||
@@ -51,6 +51,14 @@ public class ByteBufferReader {
|
||||
this.bigEndian = this.currBuffer.order() == ByteOrder.BIG_ENDIAN;
|
||||
}
|
||||
|
||||
public ByteBufferReader append(ByteBuffer... buffs) {
|
||||
for (ByteBuffer buf : buffs) {
|
||||
Objects.requireNonNull(buf);
|
||||
}
|
||||
this.buffers = Utility.append(this.buffers, buffs);
|
||||
return this;
|
||||
}
|
||||
|
||||
public static ByteBufferReader create(ByteBuffer buffer) {
|
||||
return new ByteBufferReader(buffer);
|
||||
}
|
||||
@@ -80,7 +88,30 @@ public class ByteBufferReader {
|
||||
}
|
||||
|
||||
public boolean hasRemaining() {
|
||||
return this.currBuffer.hasRemaining();
|
||||
boolean v = this.currBuffer.hasRemaining();
|
||||
if (v) return v;
|
||||
if (this.currIndex == this.buffers.length - 1) return false;
|
||||
for (int i = this.currIndex + 1; i < this.buffers.length; i++) {
|
||||
if (this.buffers[i].hasRemaining()) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public int remaining() {
|
||||
int v = this.currBuffer.remaining();
|
||||
for (int i = this.currIndex + 1; i < this.buffers.length; i++) {
|
||||
v += this.buffers[i].remaining();
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
//提前预读一个字节
|
||||
public byte preget() {
|
||||
ByteBuffer buf = this.currBuffer;
|
||||
if (!buf.hasRemaining()) {
|
||||
buf = this.buffers[this.currIndex + 1];
|
||||
}
|
||||
return buf.get(buf.position());
|
||||
}
|
||||
|
||||
public byte get() {
|
||||
|
||||
@@ -85,6 +85,13 @@ public interface Creator<T> {
|
||||
creatorCacheMap.put(Stream.class, (params) -> new ArrayList<>().stream());
|
||||
creatorCacheMap.put(ConcurrentHashMap.class, (params) -> new ConcurrentHashMap<>());
|
||||
creatorCacheMap.put(CompletableFuture.class, (params) -> new CompletableFuture<>());
|
||||
creatorCacheMap.put(Map.Entry.class, new Creator<Map.Entry>() {
|
||||
@Override
|
||||
@ConstructorParameters({"key", "value"})
|
||||
public Map.Entry create(Object... params) {
|
||||
return new AbstractMap.SimpleEntry(params[0], params[1]);
|
||||
}
|
||||
});
|
||||
creatorCacheMap.put(AbstractMap.SimpleEntry.class, new Creator<AbstractMap.SimpleEntry>() {
|
||||
@Override
|
||||
@ConstructorParameters({"key", "value"})
|
||||
@@ -232,6 +239,8 @@ public interface Creator<T> {
|
||||
clazz = (Class<T>) ConcurrentHashMap.class;
|
||||
} else if (Collection.class.isAssignableFrom(clazz) && clazz.isAssignableFrom(ArrayList.class)) {
|
||||
clazz = (Class<T>) ArrayList.class;
|
||||
} else if (Map.Entry.class.isAssignableFrom(clazz) && (Modifier.isInterface(clazz.getModifiers()) || Modifier.isAbstract(clazz.getModifiers()) || !Modifier.isPublic(clazz.getModifiers()))) {
|
||||
clazz = (Class<T>) AbstractMap.SimpleEntry.class;
|
||||
}
|
||||
Creator creator = CreatorInner.creatorCacheMap.get(clazz);
|
||||
if (creator != null) return creator;
|
||||
|
||||
@@ -19,23 +19,25 @@ import java.util.logging.*;
|
||||
* @author zhangjx
|
||||
* @param <T> 对象池元素的数据类型
|
||||
*/
|
||||
public final class ObjectPool<T> implements Supplier<T>, Consumer<T> {
|
||||
public class ObjectPool<T> implements Supplier<T>, Consumer<T> {
|
||||
|
||||
private static final Logger logger = Logger.getLogger(ObjectPool.class.getSimpleName());
|
||||
protected static final Logger logger = Logger.getLogger(ObjectPool.class.getSimpleName());
|
||||
|
||||
private final boolean debug;
|
||||
protected final boolean debug;
|
||||
|
||||
private final Queue<T> queue;
|
||||
protected Creator<T> creator;
|
||||
|
||||
private Creator<T> creator;
|
||||
protected int max;
|
||||
|
||||
private final Consumer<T> prepare;
|
||||
protected final Consumer<T> prepare;
|
||||
|
||||
private final Predicate<T> recycler;
|
||||
protected final Predicate<T> recycler;
|
||||
|
||||
private final AtomicLong creatCounter;
|
||||
protected final AtomicLong creatCounter;
|
||||
|
||||
private final AtomicLong cycleCounter;
|
||||
protected final AtomicLong cycleCounter;
|
||||
|
||||
protected final Queue<T> queue;
|
||||
|
||||
public ObjectPool(Class<T> clazz, Consumer<T> prepare, Predicate<T> recycler) {
|
||||
this(2, clazz, prepare, recycler);
|
||||
@@ -62,12 +64,18 @@ public final class ObjectPool<T> implements Supplier<T>, Consumer<T> {
|
||||
}
|
||||
|
||||
public ObjectPool(AtomicLong creatCounter, AtomicLong cycleCounter, int max, Creator<T> creator, Consumer<T> prepare, Predicate<T> recycler) {
|
||||
this(creatCounter, cycleCounter, Math.max(Runtime.getRuntime().availableProcessors() * 2, max),
|
||||
creator, prepare, recycler, new LinkedBlockingQueue<>(Math.max(Runtime.getRuntime().availableProcessors() * 2, max)));
|
||||
}
|
||||
|
||||
protected ObjectPool(AtomicLong creatCounter, AtomicLong cycleCounter, int max, Creator<T> creator, Consumer<T> prepare, Predicate<T> recycler, Queue<T> queue) {
|
||||
this.creatCounter = creatCounter;
|
||||
this.cycleCounter = cycleCounter;
|
||||
this.creator = creator;
|
||||
this.prepare = prepare;
|
||||
this.recycler = recycler;
|
||||
this.queue = new LinkedBlockingQueue<>(Math.max(Runtime.getRuntime().availableProcessors() * 2, max));
|
||||
this.queue = queue;
|
||||
this.max = max;
|
||||
this.debug = logger.isLoggable(Level.FINEST);
|
||||
}
|
||||
|
||||
|
||||
@@ -17,14 +17,14 @@ public final class Redkale {
|
||||
}
|
||||
|
||||
public static String getDotedVersion() {
|
||||
return "2.0.0";
|
||||
return "2.0.0-rc3";
|
||||
}
|
||||
|
||||
public static int getMajorVersion() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
public static int getMinorVersion() {
|
||||
public static int getMinorVersion() {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
*/
|
||||
package org.redkale.util;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.lang.reflect.*;
|
||||
import java.util.*;
|
||||
@@ -42,12 +43,22 @@ public final class ResourceFactory {
|
||||
|
||||
private final List<WeakReference<ResourceFactory>> chidren = new CopyOnWriteArrayList<>();
|
||||
|
||||
private final ConcurrentHashMap<Type, ResourceLoader> loadermap = new ConcurrentHashMap();
|
||||
private final ConcurrentHashMap<Type, ResourceInjectLoader> injectLoaderMap = new ConcurrentHashMap();
|
||||
|
||||
private final ConcurrentHashMap<Type, ResourceLoader> resLoaderMap = new ConcurrentHashMap();
|
||||
|
||||
private final ConcurrentHashMap<Type, ConcurrentHashMap<String, ResourceEntry>> store = new ConcurrentHashMap();
|
||||
|
||||
private ResourceFactory(ResourceFactory parent) {
|
||||
this.parent = parent;
|
||||
if (parent == null) {
|
||||
ServiceLoader<ResourceInjectLoader> loaders = ServiceLoader.load(ResourceInjectLoader.class);
|
||||
Iterator<ResourceInjectLoader> it = loaders.iterator();
|
||||
while (it.hasNext()) {
|
||||
ResourceInjectLoader ril = it.next();
|
||||
this.injectLoaderMap.put(ril.annotationType(), ril);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -565,6 +576,7 @@ public final class ResourceFactory {
|
||||
try {
|
||||
list.add(src);
|
||||
Class clazz = src.getClass();
|
||||
final boolean diyloaderflag = !instance.injectLoaderMap.isEmpty();
|
||||
do {
|
||||
if (java.lang.Enum.class.isAssignableFrom(clazz)) break;
|
||||
final String cname = clazz.getName();
|
||||
@@ -586,6 +598,13 @@ public final class ResourceFactory {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (flag && diyloaderflag) {
|
||||
instance.injectLoaderMap.values().stream().forEach(iloader -> {
|
||||
Annotation ann = field.getAnnotation(iloader.annotationType());
|
||||
if (ann == null) return;
|
||||
iloader.load(this, src, ann, field, attachment);
|
||||
});
|
||||
}
|
||||
if (ns == null) continue;
|
||||
final String nsname = ns.getClass().getName();
|
||||
if (ns.getClass().isPrimitive() || ns.getClass().isArray()
|
||||
@@ -685,16 +704,21 @@ public final class ResourceFactory {
|
||||
}
|
||||
}
|
||||
|
||||
public <T extends Annotation> void register(final ResourceInjectLoader<T> loader) {
|
||||
if (loader == null) return;
|
||||
instance.injectLoaderMap.put(loader.annotationType(), loader);
|
||||
}
|
||||
|
||||
public void register(final ResourceLoader rs, final Type... clazzs) {
|
||||
if (clazzs == null || rs == null) return;
|
||||
for (Type clazz : clazzs) {
|
||||
loadermap.put(clazz, rs);
|
||||
resLoaderMap.put(clazz, rs);
|
||||
}
|
||||
}
|
||||
|
||||
private ResourceLoader findMatchLoader(Type ft, Field field) {
|
||||
ResourceLoader it = this.loadermap.get(ft);
|
||||
if (it == null && field != null) it = this.loadermap.get(field.getType());
|
||||
ResourceLoader it = this.resLoaderMap.get(ft);
|
||||
if (it == null && field != null) it = this.resLoaderMap.get(field.getType());
|
||||
if (it != null) return it;
|
||||
return parent == null ? null : parent.findMatchLoader(ft, field);
|
||||
}
|
||||
@@ -702,7 +726,7 @@ public final class ResourceFactory {
|
||||
private ResourceLoader findRegxLoader(Type ft, Field field) {
|
||||
if (field == null) return null;
|
||||
Class c = field.getType();
|
||||
for (Map.Entry<Type, ResourceLoader> en : this.loadermap.entrySet()) {
|
||||
for (Map.Entry<Type, ResourceLoader> en : this.resLoaderMap.entrySet()) {
|
||||
Type t = en.getKey();
|
||||
if (t == ft) return en.getValue();
|
||||
if (t instanceof Class && (((Class) t)).isAssignableFrom(c)) return en.getValue();
|
||||
|
||||
25
src/org/redkale/util/ResourceInjectLoader.java
Normal file
25
src/org/redkale/util/ResourceInjectLoader.java
Normal file
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.util;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
/**
|
||||
* 自定义注入加载器
|
||||
*
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
* @param <T> Annotation
|
||||
*/
|
||||
public interface ResourceInjectLoader<T extends Annotation> {
|
||||
|
||||
public void load(ResourceFactory factory, Object src, T annotation, Field field, Object attachment);
|
||||
|
||||
public Class<T> annotationType();
|
||||
}
|
||||
42
src/org/redkale/util/StringWrapper.java
Normal file
42
src/org/redkale/util/StringWrapper.java
Normal file
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.util;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 主要供 JsonConvert.writeWrapper 使用
|
||||
*
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public class StringWrapper implements Serializable {
|
||||
|
||||
protected String value;
|
||||
|
||||
public StringWrapper() {
|
||||
}
|
||||
|
||||
public StringWrapper(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return value;
|
||||
}
|
||||
|
||||
}
|
||||
70
src/org/redkale/util/ThreadLocalObjectPool.java
Normal file
70
src/org/redkale/util/ThreadLocalObjectPool.java
Normal file
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.util;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.function.*;
|
||||
|
||||
/**
|
||||
* 对象池
|
||||
*
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
* @param <T> 对象池元素的数据类型
|
||||
*/
|
||||
public class ThreadLocalObjectPool<T> extends ObjectPool<T> {
|
||||
|
||||
public ThreadLocalObjectPool(Class<T> clazz, Consumer<T> prepare, Predicate<T> recycler) {
|
||||
this(2, clazz, prepare, recycler);
|
||||
}
|
||||
|
||||
public ThreadLocalObjectPool(int max, Class<T> clazz, Consumer<T> prepare, Predicate<T> recycler) {
|
||||
this(max, Creator.create(clazz), prepare, recycler);
|
||||
}
|
||||
|
||||
public ThreadLocalObjectPool(Creator<T> creator, Consumer<T> prepare, Predicate<T> recycler) {
|
||||
this(2, creator, prepare, recycler);
|
||||
}
|
||||
|
||||
public ThreadLocalObjectPool(int max, Creator<T> creator, Consumer<T> prepare, Predicate<T> recycler) {
|
||||
this(null, null, max, creator, prepare, recycler);
|
||||
}
|
||||
|
||||
public ThreadLocalObjectPool(int max, Supplier<T> creator, Consumer<T> prepare, Predicate<T> recycler) {
|
||||
this(null, null, max, creator, prepare, recycler);
|
||||
}
|
||||
|
||||
public ThreadLocalObjectPool(AtomicLong creatCounter, AtomicLong cycleCounter, int max, Supplier<T> creator, Consumer<T> prepare, Predicate<T> recycler) {
|
||||
this(creatCounter, cycleCounter, max, c -> creator.get(), prepare, recycler);
|
||||
}
|
||||
|
||||
public ThreadLocalObjectPool(AtomicLong creatCounter, AtomicLong cycleCounter, int max, Creator<T> creator, Consumer<T> prepare, Predicate<T> recycler) {
|
||||
super(creatCounter, cycleCounter, max, creator, prepare, recycler, new LinkedList<>());
|
||||
}
|
||||
|
||||
@Override
|
||||
public T get() {
|
||||
T result = queue.poll();
|
||||
if (result == null) {
|
||||
if (creatCounter != null) creatCounter.incrementAndGet();
|
||||
result = this.creator.create();
|
||||
}
|
||||
if (prepare != null) prepare.accept(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(final T e) {
|
||||
if (e != null && recycler.test(e) && this.queue.size() < this.max) {
|
||||
if (cycleCounter != null) cycleCounter.incrementAndGet();
|
||||
queue.offer(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -83,7 +83,8 @@ public abstract class TypeToken<T> {
|
||||
if (type instanceof TypeVariable) return null;
|
||||
if (type instanceof GenericArrayType) return Array.newInstance(typeToClass(((GenericArrayType) type).getGenericComponentType()), 0).getClass();
|
||||
if (!(type instanceof ParameterizedType)) return null; //只能是null了
|
||||
return typeToClass(((ParameterizedType) type).getOwnerType());
|
||||
Type owner = ((ParameterizedType) type).getOwnerType();
|
||||
return typeToClass(owner == null ? ((ParameterizedType) type).getRawType() : owner);
|
||||
}
|
||||
|
||||
public static Type[] getGenericType(final Type[] types, final Type declaringClass) {
|
||||
|
||||
@@ -37,13 +37,13 @@ public final class Utility {
|
||||
|
||||
/**
|
||||
* <blockquote><pre>
|
||||
* public final class AnonymousArrayFunction implements java.util.function.Function<Object, char[]> {
|
||||
* public final class AnonymousCharArrayFunction implements java.util.function.Function<Object, char[]> {
|
||||
*
|
||||
* final sun.misc.Unsafe unsafe;
|
||||
*
|
||||
* final long fd;
|
||||
*
|
||||
* public AnonymousArrayFunction(Object obj, long fd) {
|
||||
* public AnonymousCharArrayFunction(Object obj, long fd) {
|
||||
* this.unsafe = (sun.misc.Unsafe) obj;
|
||||
* this.fd = fd;
|
||||
* }
|
||||
@@ -56,37 +56,80 @@ public final class Utility {
|
||||
* }
|
||||
* </pre></blockquote>
|
||||
*/
|
||||
private static final String functionClassBinary = "cafebabe00000034002f0a00090022070023090008002409000800250a000200260700"
|
||||
+ "270a0008002807002907002a07002b010006756e736166650100114c73756e2f6d6973632f556e736166653b01000266640100014a0100063c69"
|
||||
+ "6e69743e010016284c6a6176612f6c616e672f4f626a6563743b4a2956010004436f646501000f4c696e654e756d6265725461626c650100124c"
|
||||
+ "6f63616c5661726961626c655461626c65010004746869730100294c6f72672f7265646b616c652f7574696c2f416e6f6e796d6f757341727261"
|
||||
+ "7946756e6374696f6e3b0100036f626a0100124c6a6176612f6c616e672f4f626a6563743b0100056170706c79010016284c6a6176612f6c616e"
|
||||
+ "672f4f626a6563743b295b43010001740100236f72672e6e65746265616e732e536f757263654c6576656c416e6e6f746174696f6e730100144c"
|
||||
+ "6a6176612f6c616e672f4f766572726964653b010026284c6a6176612f6c616e672f4f626a6563743b294c6a6176612f6c616e672f4f626a6563"
|
||||
+ "743b0100095369676e61747572650100454c6a6176612f6c616e672f4f626a6563743b4c6a6176612f7574696c2f66756e6374696f6e2f46756e"
|
||||
+ "6374696f6e3c4c6a6176612f6c616e672f4f626a6563743b5b433e3b01000a536f7572636546696c6501001b416e6f6e796d6f75734172726179"
|
||||
+ "46756e6374696f6e2e6a6176610c000f002c01000f73756e2f6d6973632f556e736166650c000b000c0c000d000e0c002d002e0100025b430c00"
|
||||
+ "1800190100276f72672f7265646b616c652f7574696c2f416e6f6e796d6f7573417272617946756e6374696f6e0100106a6176612f6c616e672f"
|
||||
+ "4f626a65637401001b6a6176612f7574696c2f66756e6374696f6e2f46756e6374696f6e0100032829560100096765744f626a65637401002728"
|
||||
+ "4c6a6176612f6c616e672f4f626a6563743b4a294c6a6176612f6c616e672f4f626a6563743b0031000800090001000a00020010000b000c0000"
|
||||
+ "0010000d000e000000030001000f0010000100110000005c00030004000000122ab700012a2bc00002b500032a20b50004b10000000200120000"
|
||||
+ "001200040000000e0004000f000c0010001100110013000000200003000000120014001500000000001200160017000100000012000d000e0002"
|
||||
+ "000100180019000200110000004400040002000000102ab400032b2ab40004b60005c00006b00000000200120000000600010000001500130000"
|
||||
+ "001600020000001000140015000000000010001a00170001001b000000060001001c000010410018001d00020011000000300002000200000006"
|
||||
+ "2a2bb60007b00000000200120000000600010000000800130000000c000100000006001400150000001b000000060001001c00000002001e0000"
|
||||
+ "0002001f0020000000020021";
|
||||
private static final String functionCharClassBinary = "cafebabe00000034002d0a00090020070021090008002209000800230a00020024070025"
|
||||
+ "0a00080026070027070028070029010006756e736166650100114c73756e2f6d6973632f556e736166653b01000266640100014a0100063c696e69743e0"
|
||||
+ "10016284c6a6176612f6c616e672f4f626a6563743b4a2956010004436f646501000f4c696e654e756d6265725461626c650100124c6f63616c56617269"
|
||||
+ "61626c655461626c650100047468697301002d4c6f72672f7265646b616c652f7574696c2f416e6f6e796d6f757343686172417272617946756e6374696"
|
||||
+ "f6e3b0100036f626a0100124c6a6176612f6c616e672f4f626a6563743b0100056170706c79010016284c6a6176612f6c616e672f4f626a6563743b295b"
|
||||
+ "4301000174010026284c6a6176612f6c616e672f4f626a6563743b294c6a6176612f6c616e672f4f626a6563743b0100095369676e61747572650100454"
|
||||
+ "c6a6176612f6c616e672f4f626a6563743b4c6a6176612f7574696c2f66756e6374696f6e2f46756e6374696f6e3c4c6a6176612f6c616e672f4f626a65"
|
||||
+ "63743b5b433e3b01000a536f7572636546696c6501001f416e6f6e796d6f757343686172417272617946756e6374696f6e2e6a6176610c000f002a01000"
|
||||
+ "f73756e2f6d6973632f556e736166650c000b000c0c000d000e0c002b002c0100025b430c0018001901002b6f72672f7265646b616c652f7574696c2f41"
|
||||
+ "6e6f6e796d6f757343686172417272617946756e6374696f6e0100106a6176612f6c616e672f4f626a65637401001b6a6176612f7574696c2f66756e637"
|
||||
+ "4696f6e2f46756e6374696f6e0100032829560100096765744f626a656374010027284c6a6176612f6c616e672f4f626a6563743b4a294c6a6176612f6c"
|
||||
+ "616e672f4f626a6563743b0021000800090001000a00020010000b000c00000010000d000e000000030001000f0010000100110000005c0003000400000"
|
||||
+ "0122ab700012a2bc00002b500032a20b50004b10000000200120000001200040000001200040013000c0014001100150013000000200003000000120014"
|
||||
+ "001500000000001200160017000100000012000d000e0002000100180019000100110000004700040002000000132ab400032b2ab40004b60005c00006c"
|
||||
+ "00006b00000000200120000000600010000001900130000001600020000001300140015000000000013001a0017000110410018001b0001001100000030"
|
||||
+ "00020002000000062a2bb60007b00000000200120000000600010000000c00130000000c0001000000060014001500000002001c00000002001d001e000"
|
||||
+ "00002001f";
|
||||
|
||||
private static final Function<Object, char[]> strFunction;
|
||||
private static final Function<Object, char[]> strCharFunction;
|
||||
|
||||
private static final Function<Object, char[]> sbFunction;
|
||||
private static final Function<Object, char[]> sbCharFunction;
|
||||
|
||||
/**
|
||||
* <blockquote><pre>
|
||||
* public final class AnonymousByteArrayFunction implements java.util.function.Function<Object, byte[]> {
|
||||
*
|
||||
* final sun.misc.Unsafe unsafe;
|
||||
*
|
||||
* final long fd;
|
||||
*
|
||||
* public AnonymousByteArrayFunction(Object obj, long fd) {
|
||||
* this.unsafe = (sun.misc.Unsafe) obj;
|
||||
* this.fd = fd;
|
||||
* }
|
||||
*
|
||||
* @Override
|
||||
* public byte[] apply(Object t) {
|
||||
* return (byte[]) unsafe.getObject(t, fd);
|
||||
* }
|
||||
*
|
||||
* }
|
||||
* </pre></blockquote>
|
||||
*/
|
||||
private static final String functionByteClassBinary = "cafebabe00000034002d0a00090020070021090008002209000800230a00020024070025"
|
||||
+ "0a00080026070027070028070029010006756e736166650100114c73756e2f6d6973632f556e736166653b01000266640100014a0100063c696e69743e0"
|
||||
+ "10016284c6a6176612f6c616e672f4f626a6563743b4a2956010004436f646501000f4c696e654e756d6265725461626c650100124c6f63616c56617269"
|
||||
+ "61626c655461626c650100047468697301002d4c6f72672f7265646b616c652f7574696c2f416e6f6e796d6f757342797465417272617946756e6374696"
|
||||
+ "f6e3b0100036f626a0100124c6a6176612f6c616e672f4f626a6563743b0100056170706c79010016284c6a6176612f6c616e672f4f626a6563743b295b"
|
||||
+ "4201000174010026284c6a6176612f6c616e672f4f626a6563743b294c6a6176612f6c616e672f4f626a6563743b0100095369676e61747572650100454"
|
||||
+ "c6a6176612f6c616e672f4f626a6563743b4c6a6176612f7574696c2f66756e6374696f6e2f46756e6374696f6e3c4c6a6176612f6c616e672f4f626a65"
|
||||
+ "63743b5b423e3b01000a536f7572636546696c6501001f416e6f6e796d6f757342797465417272617946756e6374696f6e2e6a6176610c000f002a01000"
|
||||
+ "f73756e2f6d6973632f556e736166650c000b000c0c000d000e0c002b002c0100025b420c0018001901002b6f72672f7265646b616c652f7574696c2f41"
|
||||
+ "6e6f6e796d6f757342797465417272617946756e6374696f6e0100106a6176612f6c616e672f4f626a65637401001b6a6176612f7574696c2f66756e637"
|
||||
+ "4696f6e2f46756e6374696f6e0100032829560100096765744f626a656374010027284c6a6176612f6c616e672f4f626a6563743b4a294c6a6176612f6c"
|
||||
+ "616e672f4f626a6563743b0021000800090001000a00020010000b000c00000010000d000e000000030001000f0010000100110000005c0003000400000"
|
||||
+ "0122ab700012a2bc00002b500032a20b50004b10000000200120000001200040000001200040013000c0014001100150013000000200003000000120014"
|
||||
+ "001500000000001200160017000100000012000d000e0002000100180019000100110000004700040002000000132ab400032b2ab40004b60005c00006c"
|
||||
+ "00006b00000000200120000000600010000001900130000001600020000001300140015000000000013001a0017000110410018001b0001001100000030"
|
||||
+ "00020002000000062a2bb60007b00000000200120000000600010000000c00130000000c0001000000060014001500000002001c00000002001d001e000"
|
||||
+ "00002001f";
|
||||
|
||||
private static final Function<Object, byte[]> strByteFunction;
|
||||
|
||||
private static final Function<Object, byte[]> sbByteFunction;
|
||||
|
||||
private static final javax.net.ssl.SSLContext DEFAULTSSL_CONTEXT;
|
||||
|
||||
private static final javax.net.ssl.HostnameVerifier defaultVerifier = (s, ss) -> true;
|
||||
|
||||
static {
|
||||
Function<Object, char[]> strFunction0 = null;
|
||||
Function<Object, char[]> sbFunction0 = null;
|
||||
Function<Object, char[]> strCharFunction0 = null;
|
||||
Function<Object, char[]> sbCharFunction0 = null;
|
||||
Function<Object, byte[]> strByteFunction0 = null;
|
||||
Function<Object, byte[]> sbByteFunction0 = null;
|
||||
try {
|
||||
Field f = String.class.getDeclaredField("value");
|
||||
if (f.getType() == char[].class) { //JDK9及以上不再是char[]
|
||||
@@ -97,21 +140,40 @@ public final class Utility {
|
||||
final Method fm = usafe.getClass().getMethod("objectFieldOffset", Field.class);
|
||||
final long fd1 = (Long) fm.invoke(usafe, f);
|
||||
final long fd2 = (Long) fm.invoke(usafe, StringBuilder.class.getSuperclass().getDeclaredField("value"));
|
||||
byte[] bytes = hexToBin(functionClassBinary);
|
||||
byte[] bytes = hexToBin(functionCharClassBinary);
|
||||
Class<Attribute> creatorClazz = (Class<Attribute>) new ClassLoader() {
|
||||
public final Class<?> loadClass(String name, byte[] b) {
|
||||
return defineClass(name, b, 0, b.length);
|
||||
}
|
||||
}.loadClass("org.re" + "dkale.util.AnonymousArrayFunction", bytes);
|
||||
}.loadClass("org.re" + "dkale.util.AnonymousCharArrayFunction", bytes);
|
||||
|
||||
strFunction0 = (Function<Object, char[]>) creatorClazz.getDeclaredConstructor(Object.class, long.class).newInstance(usafe, fd1);
|
||||
sbFunction0 = (Function<Object, char[]>) creatorClazz.getDeclaredConstructor(Object.class, long.class).newInstance(usafe, fd2);
|
||||
strCharFunction0 = (Function<Object, char[]>) creatorClazz.getDeclaredConstructor(Object.class, long.class).newInstance(usafe, fd1);
|
||||
sbCharFunction0 = (Function<Object, char[]>) creatorClazz.getDeclaredConstructor(Object.class, long.class).newInstance(usafe, fd2);
|
||||
} else {
|
||||
Class unsafeClass = Class.forName("sun.misc.Unsafe");
|
||||
Field safeField = unsafeClass.getDeclaredField("theUnsafe");
|
||||
safeField.setAccessible(true);
|
||||
final Object usafe = safeField.get(null);
|
||||
final Method fm = usafe.getClass().getMethod("objectFieldOffset", Field.class);
|
||||
final long fd1 = (Long) fm.invoke(usafe, f);
|
||||
final long fd2 = (Long) fm.invoke(usafe, StringBuilder.class.getSuperclass().getDeclaredField("value"));
|
||||
byte[] bytes = hexToBin(functionByteClassBinary);
|
||||
Class<Attribute> creatorClazz = (Class<Attribute>) new ClassLoader() {
|
||||
public final Class<?> loadClass(String name, byte[] b) {
|
||||
return defineClass(name, b, 0, b.length);
|
||||
}
|
||||
}.loadClass("org.re" + "dkale.util.AnonymousByteArrayFunction", bytes);
|
||||
|
||||
strByteFunction0 = (Function<Object, byte[]>) creatorClazz.getDeclaredConstructor(Object.class, long.class).newInstance(usafe, fd1);
|
||||
sbByteFunction0 = (Function<Object, byte[]>) creatorClazz.getDeclaredConstructor(Object.class, long.class).newInstance(usafe, fd2);
|
||||
}
|
||||
} catch (Throwable e) { //不会发生
|
||||
//e.printStackTrace();
|
||||
}
|
||||
strFunction = strFunction0;
|
||||
sbFunction = sbFunction0;
|
||||
strCharFunction = strCharFunction0;
|
||||
sbCharFunction = sbCharFunction0;
|
||||
strByteFunction = strByteFunction0;
|
||||
sbByteFunction = sbByteFunction0;
|
||||
|
||||
try {
|
||||
DEFAULTSSL_CONTEXT = javax.net.ssl.SSLContext.getInstance("SSL");
|
||||
@@ -610,6 +672,30 @@ public final class Utility {
|
||||
return news;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将int数组倒序
|
||||
*
|
||||
* @param array 原数组
|
||||
*
|
||||
* @return 新数组
|
||||
*/
|
||||
public static int[] reverseSort(final int[] array) {
|
||||
if (array == null || array.length == 0) return array;
|
||||
return Arrays.stream(array).boxed().sorted(Collections.reverseOrder()).mapToInt(x -> x).toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将long数组倒序
|
||||
*
|
||||
* @param array 原数组
|
||||
*
|
||||
* @return 新数组
|
||||
*/
|
||||
public static long[] reverseSort(final long[] array) {
|
||||
if (array == null || array.length == 0) return array;
|
||||
return Arrays.stream(array).boxed().sorted(Collections.reverseOrder()).mapToLong(x -> x).toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将元素从数组中删除
|
||||
*
|
||||
@@ -647,6 +733,180 @@ public final class Utility {
|
||||
return rs;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将指定的long元素从数组中删除, 相同的元素会根据items里重复次数来执行删除 <br>
|
||||
* 例如: <br>
|
||||
* remove(new short[]{1, 1, 1, 2, 2, 3, 3, 3}, false, 1, 1, 2, 3, 3) = [1,2,3]<br>
|
||||
*
|
||||
* @param array 原数组
|
||||
* @param items short[]
|
||||
*
|
||||
* @return 新数组
|
||||
*/
|
||||
public static short[] removeMatch(final short[] array, final short... items) {
|
||||
return remove(array, false, items);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将指定的int元素从数组中删除, repeat=true时相同的元素会根据items里重复次数来执行删除 <br>
|
||||
* 例如: <br>
|
||||
* remove(new short[]{1, 1, 1, 2, 2, 3, 3, 3}, true, 1, 1, 2, 3, 3) = [] <br>
|
||||
* remove(new short[]{1, 1, 1, 2, 2, 3, 3, 3}, false, 1, 1, 2, 3, 3) = [1,2,3]
|
||||
*
|
||||
* @param array 原数组
|
||||
* @param repeat 是否重复删除相同的元素
|
||||
* @param items short[]
|
||||
*
|
||||
* @return 新数组
|
||||
*/
|
||||
public static short[] remove(final short[] array, boolean repeat, final short... items) {
|
||||
if (array == null || array.length == 0 || items == null || items.length == 0) return array;
|
||||
final short[] news = new short[array.length];
|
||||
short[] subs = items;
|
||||
int index = 0;
|
||||
for (int i = 0; i < news.length; i++) {
|
||||
if (subs.length > 0 && contains(subs, array[i])) {
|
||||
if (!repeat) {
|
||||
short[] newsubs = new short[subs.length - 1];
|
||||
int k = 0;
|
||||
boolean done = false;
|
||||
for (short v : subs) {
|
||||
if (done) {
|
||||
newsubs[k++] = v;
|
||||
} else if (v == array[i]) {
|
||||
done = true;
|
||||
} else {
|
||||
newsubs[k++] = v;
|
||||
}
|
||||
}
|
||||
subs = newsubs;
|
||||
}
|
||||
} else {
|
||||
news[index++] = array[i];
|
||||
}
|
||||
}
|
||||
if (index == array.length) return array;
|
||||
final short[] rs = new short[index];
|
||||
System.arraycopy(news, 0, rs, 0, index);
|
||||
return rs;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将指定的long元素从数组中删除, 相同的元素会根据items里重复次数来执行删除 <br>
|
||||
* 例如: <br>
|
||||
* remove(new int[]{1, 1, 1, 2, 2, 3, 3, 3}, false, 1, 1, 2, 3, 3) = [1,2,3]<br>
|
||||
*
|
||||
* @param array 原数组
|
||||
* @param items int[]
|
||||
*
|
||||
* @return 新数组
|
||||
*/
|
||||
public static int[] removeMatch(final int[] array, final int... items) {
|
||||
return remove(array, false, items);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将指定的int元素从数组中删除, repeat=false时相同的元素会根据items里重复次数来执行删除 <br>
|
||||
* 例如: <br>
|
||||
* remove(new int[]{1, 1, 1, 2, 2, 3, 3, 3}, true, 1, 1, 2, 3, 3) = [] <br>
|
||||
* remove(new int[]{1, 1, 1, 2, 2, 3, 3, 3}, false, 1, 1, 2, 3, 3) = [1,2,3]
|
||||
*
|
||||
* @param array 原数组
|
||||
* @param repeat 是否重复删除相同的元素
|
||||
* @param items int[]
|
||||
*
|
||||
* @return 新数组
|
||||
*/
|
||||
public static int[] remove(final int[] array, boolean repeat, final int... items) {
|
||||
if (array == null || array.length == 0 || items == null || items.length == 0) return array;
|
||||
final int[] news = new int[array.length];
|
||||
int[] subs = items;
|
||||
int index = 0;
|
||||
for (int i = 0; i < news.length; i++) {
|
||||
if (subs.length > 0 && contains(subs, array[i])) {
|
||||
if (!repeat) {
|
||||
int[] newsubs = new int[subs.length - 1];
|
||||
int k = 0;
|
||||
boolean done = false;
|
||||
for (int v : subs) {
|
||||
if (done) {
|
||||
newsubs[k++] = v;
|
||||
} else if (v == array[i]) {
|
||||
done = true;
|
||||
} else {
|
||||
newsubs[k++] = v;
|
||||
}
|
||||
}
|
||||
subs = newsubs;
|
||||
}
|
||||
} else {
|
||||
news[index++] = array[i];
|
||||
}
|
||||
}
|
||||
if (index == array.length) return array;
|
||||
final int[] rs = new int[index];
|
||||
System.arraycopy(news, 0, rs, 0, index);
|
||||
return rs;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将指定的long元素从数组中删除, 相同的元素会根据items里重复次数来执行删除 <br>
|
||||
* 例如: <br>
|
||||
* remove(new long[]{1, 1, 1, 2, 2, 3, 3, 3}, false, 1, 1, 2, 3, 3) = [1,2,3]<br>
|
||||
*
|
||||
* @param array 原数组
|
||||
* @param items long[]
|
||||
*
|
||||
* @return 新数组
|
||||
*/
|
||||
public static long[] removeMatch(final long[] array, final long... items) {
|
||||
return remove(array, false, items);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将指定的long元素从数组中删除, repeat=false时相同的元素会根据items里重复次数来执行删除 <br>
|
||||
* 例如: <br>
|
||||
* remove(new long[]{1, 1, 1, 2, 2, 3, 3, 3}, true, 1, 1, 2, 3, 3) = [] <br>
|
||||
* remove(new long[]{1, 1, 1, 2, 2, 3, 3, 3}, false, 1, 1, 2, 3, 3) = [1,2,3]<br>
|
||||
*
|
||||
* @param array 原数组
|
||||
* @param repeat 是否重复删除相同的元素
|
||||
* @param items long[]
|
||||
*
|
||||
* @return 新数组
|
||||
*/
|
||||
public static long[] remove(final long[] array, boolean repeat, final long... items) {
|
||||
if (array == null || array.length == 0 || items == null || items.length == 0) return array;
|
||||
final long[] news = new long[array.length];
|
||||
long[] subs = items;
|
||||
int index = 0;
|
||||
for (int i = 0; i < news.length; i++) {
|
||||
if (subs.length > 0 && contains(subs, array[i])) {
|
||||
if (!repeat) {
|
||||
long[] newsubs = new long[subs.length - 1];
|
||||
int k = 0;
|
||||
boolean done = false;
|
||||
for (long v : subs) {
|
||||
if (done) {
|
||||
newsubs[k++] = v;
|
||||
} else if (v == array[i]) {
|
||||
done = true;
|
||||
} else {
|
||||
newsubs[k++] = v;
|
||||
}
|
||||
}
|
||||
subs = newsubs;
|
||||
}
|
||||
} else {
|
||||
news[index++] = array[i];
|
||||
}
|
||||
}
|
||||
if (index == array.length) return array;
|
||||
final long[] rs = new long[index];
|
||||
System.arraycopy(news, 0, rs, 0, index);
|
||||
return rs;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断字符串是否包含指定的字符,包含返回true
|
||||
*
|
||||
@@ -697,6 +957,22 @@ public final class Utility {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断指定值(不要包含相同的元素)是否包含指定的数组中,包含返回true
|
||||
*
|
||||
* @param values 集合
|
||||
* @param items 多值
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public static boolean contains(short[] values, short... items) {
|
||||
if (values == null) return false;
|
||||
for (short item : items) {
|
||||
if (!contains(values, item)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断指定值是否包含指定的数组中,包含返回true
|
||||
*
|
||||
@@ -713,6 +989,22 @@ public final class Utility {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断指定值(不要包含相同的元素)是否包含指定的数组中,包含返回true
|
||||
*
|
||||
* @param values 集合
|
||||
* @param items 多值
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public static boolean contains(int[] values, int... items) {
|
||||
if (values == null) return false;
|
||||
for (int item : items) {
|
||||
if (!contains(values, item)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断指定值是否包含指定的数组中,包含返回true
|
||||
*
|
||||
@@ -729,6 +1021,22 @@ public final class Utility {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断指定值(不要包含相同的元素)是否包含指定的数组中,包含返回true
|
||||
*
|
||||
* @param values 集合
|
||||
* @param items 多值
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public static boolean contains(long[] values, long... items) {
|
||||
if (values == null) return false;
|
||||
for (long item : items) {
|
||||
if (!contains(values, item)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断指定值是否包含指定的数组中,包含返回true
|
||||
*
|
||||
@@ -746,6 +1054,117 @@ public final class Utility {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将指定的short元素是否数组中完全包含,重复元素的次数也要相同 <br>
|
||||
* 例如: <br>
|
||||
* containsMatch(new short[]{1, 2, 2, 3, 3, 3}, 1, 2, 3, 3) = true <br>
|
||||
* containsMatch(new short[]{1, 2, 2, 3, 3, 3}, 1, 1, 2, 3, 3) = false <br>
|
||||
*
|
||||
* @param array 原数组
|
||||
* @param items short[]
|
||||
*
|
||||
* @return 是否完全包含
|
||||
*/
|
||||
public static boolean containsMatch(final short[] array, final short... items) {
|
||||
if (array == null) return false;
|
||||
if (items == null || items.length == 0) return true;
|
||||
if (array.length == 0 && items.length == 0) return true;
|
||||
if (array.length < items.length) return false;
|
||||
|
||||
short[] subs = array;
|
||||
for (short item : items) {
|
||||
if (!contains(subs, item)) return false;
|
||||
short[] newsubs = new short[subs.length - 1];
|
||||
int k = 0;
|
||||
boolean done = false;
|
||||
for (short v : subs) {
|
||||
if (done) {
|
||||
newsubs[k++] = v;
|
||||
} else if (v == item) {
|
||||
done = true;
|
||||
} else {
|
||||
newsubs[k++] = v;
|
||||
}
|
||||
}
|
||||
subs = newsubs;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将指定的int元素是否数组中完全包含,重复元素的次数也要相同 <br>
|
||||
* 例如: <br>
|
||||
* containsMatch(new int[]{1, 2, 2, 3, 3, 3}, 1, 2, 3, 3) = true <br>
|
||||
* containsMatch(new int[]{1, 2, 2, 3, 3, 3}, 1, 1, 2, 3, 3) = false <br>
|
||||
*
|
||||
* @param array 原数组
|
||||
* @param items int[]
|
||||
*
|
||||
* @return 是否完全包含
|
||||
*/
|
||||
public static boolean containsMatch(final int[] array, final int... items) {
|
||||
if (array == null) return false;
|
||||
if (items == null || items.length == 0) return true;
|
||||
if (array.length == 0 && items.length == 0) return true;
|
||||
if (array.length < items.length) return false;
|
||||
|
||||
int[] subs = array;
|
||||
for (int item : items) {
|
||||
if (!contains(subs, item)) return false;
|
||||
int[] newsubs = new int[subs.length - 1];
|
||||
int k = 0;
|
||||
boolean done = false;
|
||||
for (int v : subs) {
|
||||
if (done) {
|
||||
newsubs[k++] = v;
|
||||
} else if (v == item) {
|
||||
done = true;
|
||||
} else {
|
||||
newsubs[k++] = v;
|
||||
}
|
||||
}
|
||||
subs = newsubs;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将指定的long元素是否数组中完全包含,重复元素的次数也要相同 <br>
|
||||
* 例如: <br>
|
||||
* containsMatch(new long[]{1, 2, 2, 3, 3, 3}, 1, 2, 3, 3) = true <br>
|
||||
* containsMatch(new long[]{1, 2, 2, 3, 3, 3}, 1, 1, 2, 3, 3) = false <br>
|
||||
*
|
||||
* @param array 原数组
|
||||
* @param items long[]
|
||||
*
|
||||
* @return 是否完全包含
|
||||
*/
|
||||
public static boolean containsMatch(final long[] array, final long... items) {
|
||||
if (array == null) return false;
|
||||
if (items == null || items.length == 0) return true;
|
||||
if (array.length == 0 && items.length == 0) return true;
|
||||
if (array.length < items.length) return false;
|
||||
|
||||
long[] subs = array;
|
||||
for (long item : items) {
|
||||
if (!contains(subs, item)) return false;
|
||||
long[] newsubs = new long[subs.length - 1];
|
||||
int k = 0;
|
||||
boolean done = false;
|
||||
for (long v : subs) {
|
||||
if (done) {
|
||||
newsubs[k++] = v;
|
||||
} else if (v == item) {
|
||||
done = true;
|
||||
} else {
|
||||
newsubs[k++] = v;
|
||||
}
|
||||
}
|
||||
subs = newsubs;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除掉字符串数组中包含指定的字符串
|
||||
*
|
||||
@@ -1504,8 +1923,8 @@ public final class Utility {
|
||||
|
||||
public static byte[] encodeUTF8(final String value) {
|
||||
if (value == null) return new byte[0];
|
||||
if (strFunction == null) return encodeUTF8(value.toCharArray());
|
||||
return encodeUTF8((char[]) strFunction.apply(value));
|
||||
if (strCharFunction == null) return encodeUTF8(value.toCharArray());
|
||||
return encodeUTF8((char[]) strCharFunction.apply(value));
|
||||
}
|
||||
|
||||
public static byte[] encodeUTF8(final char[] array) {
|
||||
@@ -1515,10 +1934,10 @@ public final class Utility {
|
||||
public static byte[] encodeUTF8(final char[] text, final int start, final int len) {
|
||||
char c;
|
||||
int size = 0;
|
||||
final char[] chars = text;
|
||||
final char[] chs = text;
|
||||
final int limit = start + len;
|
||||
for (int i = start; i < limit; i++) {
|
||||
c = chars[i];
|
||||
c = chs[i];
|
||||
if (c < 0x80) {
|
||||
size++;
|
||||
} else if (c < 0x800) {
|
||||
@@ -1532,14 +1951,14 @@ public final class Utility {
|
||||
final byte[] bytes = new byte[size];
|
||||
size = 0;
|
||||
for (int i = start; i < limit; i++) {
|
||||
c = chars[i];
|
||||
c = chs[i];
|
||||
if (c < 0x80) {
|
||||
bytes[size++] = (byte) c;
|
||||
} else if (c < 0x800) {
|
||||
bytes[size++] = (byte) (0xc0 | (c >> 6));
|
||||
bytes[size++] = (byte) (0x80 | (c & 0x3f));
|
||||
} else if (Character.isSurrogate(c)) { //连取两个
|
||||
int uc = Character.toCodePoint(c, chars[i + 1]);
|
||||
int uc = Character.toCodePoint(c, chs[i + 1]);
|
||||
bytes[size++] = (byte) (0xf0 | ((uc >> 18)));
|
||||
bytes[size++] = (byte) (0x80 | ((uc >> 12) & 0x3f));
|
||||
bytes[size++] = (byte) (0x80 | ((uc >> 6) & 0x3f));
|
||||
@@ -1556,14 +1975,28 @@ public final class Utility {
|
||||
|
||||
public static char[] charArray(String value) {
|
||||
if (value == null) return null;
|
||||
if (strFunction == null) return value.toCharArray();
|
||||
return strFunction.apply(value);
|
||||
if (strCharFunction == null) return value.toCharArray();
|
||||
return strCharFunction.apply(value);
|
||||
}
|
||||
|
||||
public static char[] charArray(StringBuilder value) {
|
||||
if (value == null) return null;
|
||||
if (sbFunction == null) return value.toString().toCharArray();
|
||||
return sbFunction.apply(value);
|
||||
if (sbCharFunction == null) return value.toString().toCharArray();
|
||||
return sbCharFunction.apply(value);
|
||||
}
|
||||
|
||||
//只能是单字节字符串
|
||||
public static byte[] byteArray(String latin1Value) {
|
||||
if (latin1Value == null) return null;
|
||||
if (strByteFunction == null) return latin1Value.getBytes();
|
||||
return strByteFunction.apply(latin1Value);
|
||||
}
|
||||
|
||||
//只能是单字节字符串
|
||||
public static byte[] byteArray(StringBuilder latin1Value) {
|
||||
if (latin1Value == null) return null;
|
||||
if (sbByteFunction == null) return latin1Value.toString().getBytes();
|
||||
return sbByteFunction.apply(latin1Value);
|
||||
}
|
||||
|
||||
public static ByteBuffer encodeUTF8(final ByteBuffer buffer, final char[] array) {
|
||||
@@ -1576,8 +2009,8 @@ public final class Utility {
|
||||
|
||||
public static int encodeUTF8Length(String value) {
|
||||
if (value == null) return -1;
|
||||
if (strFunction == null) return encodeUTF8Length(value.toCharArray());
|
||||
return encodeUTF8Length(strFunction.apply(value));
|
||||
if (strCharFunction == null) return encodeUTF8Length(value.toCharArray());
|
||||
return encodeUTF8Length(strCharFunction.apply(value));
|
||||
}
|
||||
|
||||
public static int encodeUTF8Length(final char[] text) {
|
||||
@@ -1587,11 +2020,19 @@ public final class Utility {
|
||||
public static int encodeUTF8Length(final char[] text, final int start, final int len) {
|
||||
char c;
|
||||
int size = 0;
|
||||
final char[] chars = text;
|
||||
final char[] chs = text;
|
||||
final int limit = start + len;
|
||||
for (int i = start; i < limit; i++) {
|
||||
c = chars[i];
|
||||
size += (c < 0x80 ? 1 : (c < 0x800 ? 2 : 3));
|
||||
c = chs[i];
|
||||
if (c < 0x80) {
|
||||
size++;
|
||||
} else if (c < 0x800) {
|
||||
size += 2;
|
||||
} else if (Character.isSurrogate(c)) {
|
||||
size += 2;
|
||||
} else {
|
||||
size += 3;
|
||||
}
|
||||
}
|
||||
return size;
|
||||
}
|
||||
@@ -1612,15 +2053,16 @@ public final class Utility {
|
||||
return encodeUTF8(buffer, encodeUTF8Length(text, start, len), text, start, len);
|
||||
}
|
||||
|
||||
//返回的ByteBuffer为扩展buffer,为null表示参数中的buffer足够存储数据
|
||||
public static ByteBuffer encodeUTF8(final ByteBuffer buffer, int bytesLength, final char[] text, final int start, final int len) {
|
||||
char c;
|
||||
char[] chars = text;
|
||||
char[] chs = text;
|
||||
final int limit = start + len;
|
||||
int remain = buffer.remaining();
|
||||
final ByteBuffer buffer2 = remain >= bytesLength ? null : ByteBuffer.allocate(bytesLength - remain + 3); //最差情况buffer最后两byte没有填充
|
||||
final ByteBuffer buffer2 = remain >= bytesLength ? null : ByteBuffer.allocate(bytesLength - remain + 4); //最差情况buffer最后两byte没有填充
|
||||
ByteBuffer buf = buffer;
|
||||
for (int i = start; i < limit; i++) {
|
||||
c = chars[i];
|
||||
c = chs[i];
|
||||
if (c < 0x80) {
|
||||
if (buf.remaining() < 1) buf = buffer2;
|
||||
buf.put((byte) c);
|
||||
@@ -1628,6 +2070,14 @@ public final class Utility {
|
||||
if (buf.remaining() < 2) buf = buffer2;
|
||||
buf.put((byte) (0xc0 | (c >> 6)));
|
||||
buf.put((byte) (0x80 | (c & 0x3f)));
|
||||
} else if (Character.isSurrogate(c)) { //连取两个
|
||||
if (buf.remaining() < 4) buf = buffer2;
|
||||
int uc = Character.toCodePoint(c, chs[i + 1]);
|
||||
buf.put((byte) (0xf0 | ((uc >> 18))));
|
||||
buf.put((byte) (0x80 | ((uc >> 12) & 0x3f)));
|
||||
buf.put((byte) (0x80 | ((uc >> 6) & 0x3f)));
|
||||
buf.put((byte) (0x80 | (uc & 0x3f)));
|
||||
i++;
|
||||
} else {
|
||||
if (buf.remaining() < 3) buf = buffer2;
|
||||
buf.put((byte) (0xe0 | ((c >> 12))));
|
||||
@@ -1636,7 +2086,7 @@ public final class Utility {
|
||||
}
|
||||
}
|
||||
if (buffer2 != null) buffer2.flip();
|
||||
return buffer2;
|
||||
return buffer2; //返回扩展buffer
|
||||
}
|
||||
|
||||
public static String getTypeDescriptor(java.lang.reflect.Type type) {
|
||||
|
||||
68
test/org/redkale/test/convert/BiFunctionConvertMain.java
Normal file
68
test/org/redkale/test/convert/BiFunctionConvertMain.java
Normal file
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.test.convert;
|
||||
|
||||
import org.redkale.convert.ConvertField;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
import org.redkale.util.Attribute;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public class BiFunctionConvertMain {
|
||||
|
||||
public static class GamePlayer {
|
||||
|
||||
public int userid;
|
||||
|
||||
public String username;
|
||||
|
||||
public int[] cards;
|
||||
}
|
||||
|
||||
public static class GameTable {
|
||||
|
||||
public int tableid;
|
||||
|
||||
public GamePlayer[] players;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Throwable {
|
||||
GamePlayer player1 = new GamePlayer();
|
||||
player1.userid = 1;
|
||||
player1.username = "玩家1";
|
||||
player1.cards = new int[]{11, 12, 13, 14, 15};
|
||||
GamePlayer player2 = new GamePlayer();
|
||||
player2.userid = 2;
|
||||
player2.username = "玩家2";
|
||||
player2.cards = new int[]{21, 22, 23, 24, 25};
|
||||
GamePlayer player3 = new GamePlayer();
|
||||
player3.userid = 3;
|
||||
player3.username = "玩家3";
|
||||
player3.cards = new int[]{31, 32, 33, 34, 35};
|
||||
GameTable table = new GameTable();
|
||||
table.tableid = 100;
|
||||
table.players = new GamePlayer[]{player1, player2, player3};
|
||||
JsonConvert convert1 = JsonConvert.root();
|
||||
System.out.println(convert1.convertTo(table));
|
||||
JsonConvert convert2 = convert1.newConvert((Attribute t, Object u) -> {
|
||||
if (t.field().equals("cards") && u instanceof GamePlayer) {
|
||||
int userid = ((GamePlayer) u).userid;
|
||||
if (userid == 3) return null; //玩家3的cards不输出
|
||||
return t.get(u);
|
||||
}
|
||||
return t.get(u);
|
||||
}, (Object u) -> {
|
||||
if (table != u) return null;
|
||||
//return new ConvertField[]{new ConvertField("extcol1", 30), new ConvertField("extcol2", "扩展字段值")};
|
||||
return ConvertField.ofArray("extcol1", 30, "extcol2", "扩展字段值");
|
||||
});
|
||||
System.out.println(convert2.convertTo(table));
|
||||
//{"players":[{"cards":[11,12,13,14,15],"userid":1,"username":"玩家1"},{"cards":[21,22,23,24,25],"userid":2,"username":"玩家2"},{"cards":[31,32,33,34,35],"userid":3,"username":"玩家3"}],"tableid":100}
|
||||
//{"players":[{"cards":[11,12,13,14,15],"userid":1,"username":"玩家1"},{"cards":[21,22,23,24,25],"userid":2,"username":"玩家2"},{"userid":3,"username":"玩家3"}],"tableid":100,"extcol1":30,"extcol2":"扩展字段值"}
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,6 @@ import org.redkale.convert.json.*;
|
||||
public class SncpTestBean implements FilterBean {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private long id;
|
||||
|
||||
private String content;
|
||||
|
||||
@@ -21,12 +21,9 @@ import org.redkale.convert.json.*;
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
@Entity
|
||||
public class LoginTestRecord {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
//@SequenceGenerator(name = "SEQ", initialValue = 100001, allocationSize = 1000)
|
||||
private String sessionid;
|
||||
|
||||
private int userid;
|
||||
|
||||
67
test/org/redkale/test/util/ResourceInjectMain.java
Normal file
67
test/org/redkale/test/util/ResourceInjectMain.java
Normal file
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.test.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.annotation.*;
|
||||
import static java.lang.annotation.ElementType.FIELD;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
import java.lang.reflect.Field;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
import org.redkale.util.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public class ResourceInjectMain {
|
||||
|
||||
public static void main(String[] args) throws Throwable {
|
||||
ResourceFactory factory = ResourceFactory.root();
|
||||
factory.register(new CustomConfLoader());
|
||||
InjectBean bean = new InjectBean();
|
||||
factory.inject(bean);
|
||||
}
|
||||
|
||||
public static class CustomConfLoader implements ResourceInjectLoader<CustomConf> {
|
||||
|
||||
@Override
|
||||
public void load(ResourceFactory factory, Object src, CustomConf annotation, Field field, Object attachment) {
|
||||
try {
|
||||
field.set(src, new File(annotation.path()));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println("对象是 src =" + src + ", path=" + annotation.path());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<CustomConf> annotationType() {
|
||||
return CustomConf.class;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class InjectBean {
|
||||
|
||||
@CustomConf(path = "conf/test.xml")
|
||||
public File conf;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JsonConvert.root().convertTo(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Documented
|
||||
@Target({FIELD})
|
||||
@Retention(RUNTIME)
|
||||
public static @interface CustomConf {
|
||||
|
||||
String path();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user