This commit is contained in:
@@ -5,6 +5,8 @@
|
|||||||
*/
|
*/
|
||||||
package com.wentch.redkale.net;
|
package com.wentch.redkale.net;
|
||||||
|
|
||||||
|
import com.wentch.redkale.convert.bson.*;
|
||||||
|
import com.wentch.redkale.convert.json.*;
|
||||||
import java.nio.*;
|
import java.nio.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
@@ -16,6 +18,10 @@ public abstract class Request {
|
|||||||
|
|
||||||
protected final Context context;
|
protected final Context context;
|
||||||
|
|
||||||
|
protected final BsonConvert bsonConvert;
|
||||||
|
|
||||||
|
protected final JsonConvert jsonConvert;
|
||||||
|
|
||||||
protected long createtime;
|
protected long createtime;
|
||||||
|
|
||||||
protected boolean keepAlive;
|
protected boolean keepAlive;
|
||||||
@@ -32,6 +38,8 @@ public abstract class Request {
|
|||||||
|
|
||||||
protected Request(Context context) {
|
protected Request(Context context) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
|
this.bsonConvert = context.getBsonConvert();
|
||||||
|
this.jsonConvert = context.getJsonConvert();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -44,8 +52,9 @@ public abstract class Request {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 读取buffer,并返回读取的有效数据长度
|
* 读取buffer,并返回读取的有效数据长度
|
||||||
@param buffer
|
*
|
||||||
@return
|
* @param buffer
|
||||||
|
* @return
|
||||||
*/
|
*/
|
||||||
protected abstract int readBody(ByteBuffer buffer);
|
protected abstract int readBody(ByteBuffer buffer);
|
||||||
|
|
||||||
|
|||||||
@@ -5,8 +5,6 @@
|
|||||||
*/
|
*/
|
||||||
package com.wentch.redkale.net.http;
|
package com.wentch.redkale.net.http;
|
||||||
|
|
||||||
import com.wentch.redkale.convert.bson.*;
|
|
||||||
import com.wentch.redkale.convert.json.*;
|
|
||||||
import com.wentch.redkale.net.*;
|
import com.wentch.redkale.net.*;
|
||||||
import com.wentch.redkale.util.*;
|
import com.wentch.redkale.util.*;
|
||||||
import com.wentch.redkale.watch.*;
|
import com.wentch.redkale.watch.*;
|
||||||
@@ -58,11 +56,4 @@ public final class HttpContext extends Context {
|
|||||||
return responsePool;
|
return responsePool;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected JsonFactory getJsonFactory() {
|
|
||||||
return jsonFactory;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected BsonFactory getBsonFactory() {
|
|
||||||
return bsonFactory;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,8 +5,8 @@
|
|||||||
*/
|
*/
|
||||||
package com.wentch.redkale.net.http;
|
package com.wentch.redkale.net.http;
|
||||||
|
|
||||||
import com.wentch.redkale.util.ByteArray;
|
|
||||||
import com.wentch.redkale.convert.json.*;
|
import com.wentch.redkale.convert.json.*;
|
||||||
|
import com.wentch.redkale.util.ByteArray;
|
||||||
import com.wentch.redkale.net.*;
|
import com.wentch.redkale.net.*;
|
||||||
import com.wentch.redkale.util.AnyValue.DefaultAnyValue;
|
import com.wentch.redkale.util.AnyValue.DefaultAnyValue;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
@@ -49,8 +49,6 @@ public class HttpRequest extends Request {
|
|||||||
|
|
||||||
protected String newsessionid;
|
protected String newsessionid;
|
||||||
|
|
||||||
protected final JsonConvert convert;
|
|
||||||
|
|
||||||
protected final DefaultAnyValue header = new DefaultAnyValue();
|
protected final DefaultAnyValue header = new DefaultAnyValue();
|
||||||
|
|
||||||
protected final DefaultAnyValue params = new DefaultAnyValue();
|
protected final DefaultAnyValue params = new DefaultAnyValue();
|
||||||
@@ -65,9 +63,8 @@ public class HttpRequest extends Request {
|
|||||||
|
|
||||||
private final String remoteAddrHeader;
|
private final String remoteAddrHeader;
|
||||||
|
|
||||||
public HttpRequest(Context context, JsonFactory factory, String remoteAddrHeader) {
|
public HttpRequest(Context context, String remoteAddrHeader) {
|
||||||
super(context);
|
super(context);
|
||||||
this.convert = factory.getConvert();
|
|
||||||
this.remoteAddrHeader = remoteAddrHeader;
|
this.remoteAddrHeader = remoteAddrHeader;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,6 +79,10 @@ public class HttpRequest extends Request {
|
|||||||
protected AsyncConnection getChannel() {
|
protected AsyncConnection getChannel() {
|
||||||
return this.channel;
|
return this.channel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected JsonConvert getJsonConvert(){
|
||||||
|
return this.jsonConvert;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected int readHeader(final ByteBuffer buffer) {
|
protected int readHeader(final ByteBuffer buffer) {
|
||||||
@@ -270,11 +271,11 @@ public class HttpRequest extends Request {
|
|||||||
public final MultiContext getMultiContext() {
|
public final MultiContext getMultiContext() {
|
||||||
return new MultiContext(context.getCharset(), this.getContentType(),
|
return new MultiContext(context.getCharset(), this.getContentType(),
|
||||||
new BufferedInputStream(Channels.newInputStream(this.channel), Math.max(array.count(), 8192)) {
|
new BufferedInputStream(Channels.newInputStream(this.channel), Math.max(array.count(), 8192)) {
|
||||||
{
|
{
|
||||||
array.write(this.buf);
|
array.write(this.buf);
|
||||||
this.count = array.count();
|
this.count = array.count();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -446,7 +447,7 @@ public class HttpRequest extends Request {
|
|||||||
|
|
||||||
public <T> T getJsonHeader(Class<T> clazz, String name) {
|
public <T> T getJsonHeader(Class<T> clazz, String name) {
|
||||||
String v = getHeader(name);
|
String v = getHeader(name);
|
||||||
return v == null || v.isEmpty() ? null : convert.convertFrom(clazz, v);
|
return v == null || v.isEmpty() ? null : jsonConvert.convertFrom(clazz, v);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean getBooleanHeader(String name, boolean defaultValue) {
|
public boolean getBooleanHeader(String name, boolean defaultValue) {
|
||||||
@@ -490,7 +491,7 @@ public class HttpRequest extends Request {
|
|||||||
|
|
||||||
public <T> T getJsonParameter(Class<T> clazz, String name) {
|
public <T> T getJsonParameter(Class<T> clazz, String name) {
|
||||||
String v = getParameter(name);
|
String v = getParameter(name);
|
||||||
return v == null || v.isEmpty() ? null : convert.convertFrom(clazz, v);
|
return v == null || v.isEmpty() ? null : jsonConvert.convertFrom(clazz, v);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean getBooleanParameter(String name, boolean defaultValue) {
|
public boolean getBooleanParameter(String name, boolean defaultValue) {
|
||||||
|
|||||||
@@ -182,17 +182,17 @@ public class HttpResponse<R extends HttpRequest> extends Response<R> {
|
|||||||
|
|
||||||
public void finishJson(Object obj) {
|
public void finishJson(Object obj) {
|
||||||
this.contentType = "text/plain; charset=utf-8";
|
this.contentType = "text/plain; charset=utf-8";
|
||||||
finish(request.convert.convertTo(context.getCharset(), context.getBufferSupplier(), obj));
|
finish(request.getJsonConvert().convertTo(context.getCharset(), context.getBufferSupplier(), obj));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void finishJson(Type type, Object obj) {
|
public void finishJson(Type type, Object obj) {
|
||||||
this.contentType = "text/plain; charset=utf-8";
|
this.contentType = "text/plain; charset=utf-8";
|
||||||
finish(request.convert.convertTo(context.getCharset(), context.getBufferSupplier(), type, obj));
|
finish(request.getJsonConvert().convertTo(context.getCharset(), context.getBufferSupplier(), type, obj));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void finishJson(Object... objs) {
|
public void finishJson(Object... objs) {
|
||||||
this.contentType = "text/plain; charset=utf-8";
|
this.contentType = "text/plain; charset=utf-8";
|
||||||
finish(request.convert.convertTo(context.getCharset(), context.getBufferSupplier(), objs));
|
finish(request.getJsonConvert().convertTo(context.getCharset(), context.getBufferSupplier(), objs));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void finish(String obj) {
|
public void finish(String obj) {
|
||||||
|
|||||||
@@ -120,7 +120,7 @@ public final class HttpServer extends Server {
|
|||||||
ObjectPool<Response> responsePool = HttpResponse.createPool(createResponseCounter, cycleResponseCounter, this.responsePoolSize, null);
|
ObjectPool<Response> responsePool = HttpResponse.createPool(createResponseCounter, cycleResponseCounter, this.responsePoolSize, null);
|
||||||
HttpContext httpcontext = new HttpContext(this.serverStartTime, this.logger, executor, rcapacity, bufferPool, responsePool,
|
HttpContext httpcontext = new HttpContext(this.serverStartTime, this.logger, executor, rcapacity, bufferPool, responsePool,
|
||||||
this.maxbody, this.charset, this.address, this.prepare, this.watch, this.readTimeoutSecond, this.writeTimeoutSecond, contextPath);
|
this.maxbody, this.charset, this.address, this.prepare, this.watch, this.readTimeoutSecond, this.writeTimeoutSecond, contextPath);
|
||||||
responsePool.setCreator((Object... params) -> new HttpResponse(httpcontext, new HttpRequest(httpcontext, httpcontext.getJsonFactory(), addrHeader), addHeaders, setHeaders, defCookie));
|
responsePool.setCreator((Object... params) -> new HttpResponse(httpcontext, new HttpRequest(httpcontext, addrHeader), addHeaders, setHeaders, defCookie));
|
||||||
return httpcontext;
|
return httpcontext;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
24
src/com/wentch/redkale/net/sncp/SncpCall.java
Normal file
24
src/com/wentch/redkale/net/sncp/SncpCall.java
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
/*
|
||||||
|
* 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 com.wentch.redkale.net.sncp;
|
||||||
|
|
||||||
|
import com.wentch.redkale.util.*;
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 参数回写
|
||||||
|
*
|
||||||
|
* @author zhangjx
|
||||||
|
*/
|
||||||
|
@Inherited
|
||||||
|
@Documented
|
||||||
|
@Target({ElementType.PARAMETER})
|
||||||
|
@Retention(RUNTIME)
|
||||||
|
public @interface SncpCall {
|
||||||
|
|
||||||
|
Class<? extends Attribute> value();
|
||||||
|
}
|
||||||
@@ -1,52 +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 com.wentch.redkale.net.sncp;
|
|
||||||
|
|
||||||
import com.wentch.redkale.convert.bson.*;
|
|
||||||
import com.wentch.redkale.convert.json.*;
|
|
||||||
import com.wentch.redkale.net.*;
|
|
||||||
import com.wentch.redkale.util.*;
|
|
||||||
import com.wentch.redkale.watch.*;
|
|
||||||
import java.net.*;
|
|
||||||
import java.nio.*;
|
|
||||||
import java.nio.charset.*;
|
|
||||||
import java.util.concurrent.*;
|
|
||||||
import java.util.logging.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @author zhangjx
|
|
||||||
*/
|
|
||||||
public final class SncpContext extends Context {
|
|
||||||
|
|
||||||
public SncpContext(long serverStartTime, Logger logger, ExecutorService executor, int bufferCapacity, ObjectPool<ByteBuffer> bufferPool,
|
|
||||||
ObjectPool<Response> responsePool, int maxbody, Charset charset, InetSocketAddress address, PrepareServlet prepare,
|
|
||||||
WatchFactory watch, int readTimeoutSecond, int writeTimeoutSecond) {
|
|
||||||
super(serverStartTime, logger, executor, bufferCapacity, bufferPool, responsePool, maxbody, charset,
|
|
||||||
address, prepare, watch, readTimeoutSecond, writeTimeoutSecond);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
protected WatchFactory getWatchFactory() {
|
|
||||||
return watch;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected ExecutorService getExecutor() {
|
|
||||||
return executor;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected ObjectPool<Response> getResponsePool() {
|
|
||||||
return responsePool;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected JsonFactory getJsonFactory() {
|
|
||||||
return jsonFactory;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected BsonFactory getBsonFactory() {
|
|
||||||
return bsonFactory;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -43,9 +43,9 @@ public final class SncpRequest extends Request {
|
|||||||
|
|
||||||
private byte[] bufferbytes = new byte[6];
|
private byte[] bufferbytes = new byte[6];
|
||||||
|
|
||||||
protected SncpRequest(SncpContext context, BsonFactory factory) {
|
protected SncpRequest(Context context) {
|
||||||
super(context);
|
super(context);
|
||||||
this.convert = factory.getConvert();
|
this.convert = context.getBsonConvert();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -57,9 +57,9 @@ public final class SncpServer extends Server {
|
|||||||
AtomicLong createResponseCounter = watch == null ? new AtomicLong() : watch.createWatchNumber("SNCP_" + port + ".Response.creatCounter");
|
AtomicLong createResponseCounter = watch == null ? new AtomicLong() : watch.createWatchNumber("SNCP_" + port + ".Response.creatCounter");
|
||||||
AtomicLong cycleResponseCounter = watch == null ? new AtomicLong() : watch.createWatchNumber("SNCP_" + port + ".Response.cycleCounter");
|
AtomicLong cycleResponseCounter = watch == null ? new AtomicLong() : watch.createWatchNumber("SNCP_" + port + ".Response.cycleCounter");
|
||||||
ObjectPool<Response> responsePool = SncpResponse.createPool(createResponseCounter, cycleResponseCounter, this.responsePoolSize, null);
|
ObjectPool<Response> responsePool = SncpResponse.createPool(createResponseCounter, cycleResponseCounter, this.responsePoolSize, null);
|
||||||
SncpContext sncpcontext = new SncpContext(this.serverStartTime, this.logger, executor, rcapacity, bufferPool, responsePool,
|
Context sncpcontext = new Context(this.serverStartTime, this.logger, executor, rcapacity, bufferPool, responsePool,
|
||||||
this.maxbody, this.charset, this.address, this.prepare, this.watch, this.readTimeoutSecond, this.writeTimeoutSecond);
|
this.maxbody, this.charset, this.address, this.prepare, this.watch, this.readTimeoutSecond, this.writeTimeoutSecond);
|
||||||
responsePool.setCreator((Object... params) -> new SncpResponse(sncpcontext, new SncpRequest(sncpcontext, sncpcontext.getBsonFactory())));
|
responsePool.setCreator((Object... params) -> new SncpResponse(sncpcontext, new SncpRequest(sncpcontext)));
|
||||||
return sncpcontext;
|
return sncpcontext;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
*/
|
*/
|
||||||
package com.wentch.redkale.service;
|
package com.wentch.redkale.service;
|
||||||
|
|
||||||
|
import com.wentch.redkale.net.sncp.*;
|
||||||
import com.wentch.redkale.source.*;
|
import com.wentch.redkale.source.*;
|
||||||
import com.wentch.redkale.util.*;
|
import com.wentch.redkale.util.*;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
@@ -32,12 +33,12 @@ public class DataSourceService implements DataSource, Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> void insert(T... values) {
|
public <T> void insert(@SncpCall(EntityCallAttribute.class) T... values) {
|
||||||
source.insert(values);
|
source.insert(values);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> void insert(DataConnection conn, T... values) {
|
public <T> void insert(DataConnection conn, @SncpCall(EntityCallAttribute.class) T... values) {
|
||||||
source.insert(conn, values);
|
source.insert(conn, values);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
82
src/com/wentch/redkale/source/EntityCallAttribute.java
Normal file
82
src/com/wentch/redkale/source/EntityCallAttribute.java
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
/*
|
||||||
|
* 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 com.wentch.redkale.source;
|
||||||
|
|
||||||
|
import com.wentch.redkale.util.*;
|
||||||
|
import java.lang.reflect.*;
|
||||||
|
import java.util.concurrent.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author zhangjx
|
||||||
|
* @param <T>
|
||||||
|
* @param <F>
|
||||||
|
*/
|
||||||
|
public final class EntityCallAttribute<T, F> implements Attribute<T[], F[]> {
|
||||||
|
|
||||||
|
public static final EntityCallAttribute instance = new EntityCallAttribute();
|
||||||
|
|
||||||
|
private static final ConcurrentHashMap<Class, Attribute> attributes = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
private static Attribute load(final Class clazz) {
|
||||||
|
Attribute rs = attributes.get(clazz);
|
||||||
|
if (rs != null) return rs;
|
||||||
|
synchronized (attributes) {
|
||||||
|
rs = attributes.get(clazz);
|
||||||
|
if (rs == null) {
|
||||||
|
Class cltmp = clazz;
|
||||||
|
do {
|
||||||
|
for (Field field : cltmp.getDeclaredFields()) {
|
||||||
|
if (field.getAnnotation(javax.persistence.Id.class) == null) continue;
|
||||||
|
try {
|
||||||
|
rs = Attribute.create(cltmp, field);
|
||||||
|
attributes.put(clazz, rs);
|
||||||
|
return rs;
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} while ((cltmp = cltmp.getSuperclass()) != Object.class);
|
||||||
|
}
|
||||||
|
return rs;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<? extends F[]> type() {
|
||||||
|
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<T[]> declaringClass() {
|
||||||
|
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String field() {
|
||||||
|
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public F[] get(final T[] objs) {
|
||||||
|
if (objs == null || objs.length == 0) return null;
|
||||||
|
final Attribute<T, F> attr = (Attribute<T, F>) load(objs[0].getClass());
|
||||||
|
final F[] keys = (F[]) Array.newInstance(attr.type(), objs.length);
|
||||||
|
for (int i = 0; i < objs.length; i++) {
|
||||||
|
keys[i] = attr.get(objs[i]);
|
||||||
|
}
|
||||||
|
return keys;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void set(final T[] objs, final F[] keys) {
|
||||||
|
if (objs == null || objs.length == 0) return;
|
||||||
|
final Attribute<T, F> attr = (Attribute<T, F>) load(objs[0].getClass());
|
||||||
|
for (int i = 0; i < objs.length; i++) {
|
||||||
|
attr.set(objs[i], (F) keys[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -108,7 +108,7 @@ public final class EntityInfo<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> EntityInfo<T> get(Class<T> clazz) {
|
static <T> EntityInfo<T> get(Class<T> clazz) {
|
||||||
return entityInfos.get(clazz);
|
return entityInfos.get(clazz);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user