This commit is contained in:
Redkale
2017-05-25 23:06:27 +08:00
parent d9bf5c8412
commit c326b7ed05
4 changed files with 97 additions and 34 deletions

View File

@@ -13,17 +13,12 @@
</resources> </resources>
<server protocol="HTTP" host="0.0.0.0" port="6060" root="root"> <server protocol="HTTP" host="0.0.0.0" port="6060" root="root">
<!--
<request>
<remoteaddr value="request.headers.X-RemoteAddress"/>
</request>
-->
<!-- base指定的自定义HttpServlet子类必须标记@HttpUserType, 不设置base则视为没有当前用户信息设置 -->
<rest base="org.redkale.net.http.HttpServlet"/>
<services autoload="true"/> <services autoload="true"/>
<!-- base指定的自定义HttpServlet子类必须标记@HttpUserType, 不设置base则视为没有当前用户信息设置 -->
<rest path="/pipes" base="org.redkale.net.http.HttpServlet"/>
<servlets path="/pipes" autoload="true" /> <servlets path="/pipes" autoload="true" />
</server> </server>

View File

@@ -3,7 +3,7 @@
handlers = java.util.logging.ConsoleHandler handlers = java.util.logging.ConsoleHandler
############################################################ ############################################################
.level = FINE .level = FINER
java.level = INFO java.level = INFO
javax.level = INFO javax.level = INFO
@@ -12,7 +12,7 @@ sun.level = INFO
jdk.level = INFO jdk.level = INFO
java.util.logging.FileHandler.level = FINE java.util.logging.FileHandler.level = FINER
#10M #10M
java.util.logging.FileHandler.limit = 10485760 java.util.logging.FileHandler.limit = 10485760
java.util.logging.FileHandler.count = 10000 java.util.logging.FileHandler.count = 10000
@@ -21,4 +21,4 @@ java.util.logging.FileHandler.pattern = ${APP_HOME}/logs-%m/log-%d.log
java.util.logging.FileHandler.unusual = ${APP_HOME}/logs-%m/log-warnerr-%u.log java.util.logging.FileHandler.unusual = ${APP_HOME}/logs-%m/log-warnerr-%u.log
java.util.logging.FileHandler.append = true java.util.logging.FileHandler.append = true
java.util.logging.ConsoleHandler.level = FINE java.util.logging.ConsoleHandler.level = FINER

View File

@@ -184,7 +184,7 @@ public final class Rest {
cw.visit(V1_8, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, newDynName, null, supDynName, null); cw.visit(V1_8, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, newDynName, null, supDynName, null);
{ //注入 @WebServlet 注解 { //注入 @WebServlet 注解
String urlpath = (rws.catalog().isEmpty() ? "/" : ("/" + rws.catalog() + "/")) + rws.name() + "/*"; String urlpath = (rws.catalog().isEmpty() ? "/" : ("/" + rws.catalog() + "/")) + rws.name();
av0 = cw.visitAnnotation(webServletDesc, true); av0 = cw.visitAnnotation(webServletDesc, true);
{ {
AnnotationVisitor av1 = av0.visitArray("value"); AnnotationVisitor av1 = av0.visitArray("value");
@@ -209,7 +209,7 @@ public final class Rest {
cw.visitInnerClass(newDynMessageFullName + endfix, newDynName, newDynMessageSimpleName + endfix, ACC_PUBLIC + ACC_STATIC); cw.visitInnerClass(newDynMessageFullName + endfix, newDynName, newDynMessageSimpleName + endfix, ACC_PUBLIC + ACC_STATIC);
} }
} }
{//@Resource { //@Resource
for (int i = 0; i < resourcesFields.size(); i++) { for (int i = 0; i < resourcesFields.size(); i++) {
Field field = resourcesFields.get(i); Field field = resourcesFields.get(i);
Resource res = field.getAnnotation(Resource.class); Resource res = field.getAnnotation(Resource.class);
@@ -242,12 +242,21 @@ public final class Rest {
mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, newDynName, "_redkale_resource_" + i, Type.getDescriptor(resourcesFields.get(i).getType())); mv.visitFieldInsn(GETFIELD, newDynName, "_redkale_resource_" + i, Type.getDescriptor(resourcesFields.get(i).getType()));
} }
mv.visitMethodInsn(INVOKESPECIAL, newDynName + "$" + newDynWebSokcetSimpleName, "<init>", "(" + resourceDescriptor + ")V", false); mv.visitMethodInsn(INVOKESPECIAL, newDynWebSokcetFullName, "<init>", "(" + resourceDescriptor + ")V", false);
mv.visitInsn(ARETURN); mv.visitInsn(ARETURN);
mv.visitMaxs(2 + resourcesFields.size(), 1); mv.visitMaxs(2 + resourcesFields.size(), 1);
mv.visitEnd(); mv.visitEnd();
} }
cw.visitEnd(); { //createRestOnMessageConsumer
mv = new AsmMethodVisitor(cw.visitMethod(ACC_PROTECTED, "createRestOnMessageConsumer", "()Ljava/util/function/BiConsumer;", "()Ljava/util/function/BiConsumer<Lorg/redkale/net/http/WebSocket;Ljava/lang/Object;>;", null));
mv.visitTypeInsn(NEW, newDynConsumerFullName);
mv.visitInsn(DUP);
mv.visitMethodInsn(INVOKESPECIAL, newDynConsumerFullName, "<init>", "()V", false);
mv.visitInsn(ARETURN);
mv.visitMaxs(2, 1);
mv.visitEnd();
}
RestClassLoader newLoader = new RestClassLoader(loader); RestClassLoader newLoader = new RestClassLoader(loader);
for (int i = 0; i < messageMethods.size(); i++) { // _DyncXXXWebSocketMessage List for (int i = 0; i < messageMethods.size(); i++) { // _DyncXXXWebSocketMessage List
@@ -285,8 +294,17 @@ public final class Rest {
mv.visitMaxs(1, 1); mv.visitMaxs(1, 1);
mv.visitEnd(); mv.visitEnd();
} }
{ //toString
mv = new AsmMethodVisitor(cw2.visitMethod(ACC_PUBLIC, "toString", "()Ljava/lang/String;", null, null));
mv.visitMethodInsn(INVOKESTATIC, "org/redkale/convert/json/JsonConvert", "root", "()Lorg/redkale/convert/json/JsonConvert;", false);
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKEVIRTUAL, "org/redkale/convert/json/JsonConvert", "convertTo", "(Ljava/lang/Object;)Ljava/lang/String;", false);
mv.visitInsn(ARETURN);
mv.visitMaxs(2, 1);
mv.visitEnd();
}
cw2.visitEnd(); cw2.visitEnd();
newLoader.loadClass(newDynMessageFullName.replace('/', '.'), cw2.toByteArray()); newLoader.loadClass((newDynMessageFullName + endfix).replace('/', '.'), cw2.toByteArray());
} }
{ //_DynXXXWebSocketMessage class { //_DynXXXWebSocketMessage class
@@ -299,18 +317,27 @@ public final class Rest {
Method method = messageMethods.get(i); Method method = messageMethods.get(i);
String endfix = "_" + method.getName() + "_" + (i > 9 ? i : ("0" + i)); String endfix = "_" + method.getName() + "_" + (i > 9 ? i : ("0" + i));
cw2.visitInnerClass(newDynMessageFullName + endfix, newDynName, newDynMessageSimpleName + endfix, ACC_PUBLIC + ACC_STATIC); cw2.visitInnerClass(newDynMessageFullName + endfix, newDynName, newDynMessageSimpleName + endfix, ACC_PUBLIC + ACC_STATIC);
}
{ fv = cw2.visitField(ACC_PUBLIC, method.getAnnotation(RestOnMessage.class).name(), "L" + newDynMessageFullName + endfix + ";", null, null);
mv = new AsmMethodVisitor(cw2.visitMethod(ACC_PUBLIC, "<init>", "(" + resourceDescriptor + ")V", resourceGenericDescriptor == null ? null : ("(" + resourceGenericDescriptor + ")V"), null)); fv.visitEnd();
mv.visitVarInsn(ALOAD, 0); }
mv.visitMethodInsn(INVOKESPECIAL, webSocketInternalName, "<init>", "()V", false); { //构造函数
mv = new AsmMethodVisitor(cw2.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null));
mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
mv.visitInsn(RETURN); mv.visitInsn(RETURN);
mv.visitMaxs(1, 1); mv.visitMaxs(1, 1);
mv.visitEnd(); mv.visitEnd();
} }
{ //toString
mv = new AsmMethodVisitor(cw2.visitMethod(ACC_PUBLIC, "toString", "()Ljava/lang/String;", null, null));
mv.visitMethodInsn(INVOKESTATIC, "org/redkale/convert/json/JsonConvert", "root", "()Lorg/redkale/convert/json/JsonConvert;", false);
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKEVIRTUAL, "org/redkale/convert/json/JsonConvert", "convertTo", "(Ljava/lang/Object;)Ljava/lang/String;", false);
mv.visitInsn(ARETURN);
mv.visitMaxs(2, 1);
mv.visitEnd();
}
cw2.visitEnd(); cw2.visitEnd();
newLoader.loadClass(newDynMessageFullName.replace('/', '.'), cw2.toByteArray()); newLoader.loadClass(newDynMessageFullName.replace('/', '.'), cw2.toByteArray());
} }
@@ -329,7 +356,7 @@ public final class Rest {
Field field = resourcesFields.get(i); Field field = resourcesFields.get(i);
mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, i + 1); mv.visitVarInsn(ALOAD, i + 1);
mv.visitFieldInsn(PUTFIELD, newDynWebSokcetFullName, "_redkale_resource_" + i, Type.getDescriptor(field.getType())); mv.visitFieldInsn(PUTFIELD, newDynWebSokcetFullName, field.getName(), Type.getDescriptor(field.getType()));
} }
mv.visitInsn(RETURN); mv.visitInsn(RETURN);
mv.visitMaxs(2, 1 + resourcesFields.size()); mv.visitMaxs(2, 1 + resourcesFields.size());
@@ -370,9 +397,41 @@ public final class Rest {
mv.visitTypeInsn(CHECKCAST, newDynMessageFullName); mv.visitTypeInsn(CHECKCAST, newDynMessageFullName);
mv.visitVarInsn(ASTORE, 4); mv.visitVarInsn(ASTORE, 4);
//待开发 for (int i = 0; i < messageMethods.size(); i++) {
final Method method = messageMethods.get(i);
mv.visitMaxs(3, 5); String endfix = "_" + method.getName() + "_" + (i > 9 ? i : ("0" + i));
final String messagename = method.getAnnotation(RestOnMessage.class).name();
String methodesc = method.getName() + ":" + Type.getMethodDescriptor(method);
List<String> names = asmParamMap.get(methodesc);
Parameter[] params = method.getParameters();
mv.visitVarInsn(ALOAD, 4);
mv.visitFieldInsn(GETFIELD, newDynMessageFullName, messagename, "L" + (newDynMessageFullName + endfix) + ";");
Label ifLabel = new Label();
mv.visitJumpInsn(IFNULL, ifLabel);
mv.visitVarInsn(ALOAD, 3);
for (int j = 0; j < params.length; j++) {
Parameter param = params[j];
String paramname = param.getName();
RestParam rp = param.getAnnotation(RestParam.class);
if (rp != null && !rp.name().isEmpty()) {
paramname = rp.name();
} else if (names != null && names.size() > j) {
paramname = names.get(j);
}
mv.visitVarInsn(ALOAD, 4);
mv.visitFieldInsn(GETFIELD, newDynMessageFullName, messagename, "L" + (newDynMessageFullName + endfix) + ";");
mv.visitFieldInsn(GETFIELD, (newDynMessageFullName + endfix), paramname, Type.getDescriptor(param.getType()));
}
mv.visitMethodInsn(INVOKEVIRTUAL, newDynWebSokcetFullName, method.getName(), Type.getMethodDescriptor(method), false);
mv.visitInsn(RETURN);
mv.visitLabel(ifLabel);
}
mv.visitInsn(RETURN);
mv.visitMaxs(3, 3 + messageMethods.size());
mv.visitEnd(); mv.visitEnd();
} }
{//虚拟accept函数 {//虚拟accept函数
@@ -388,9 +447,9 @@ public final class Rest {
mv.visitEnd(); mv.visitEnd();
} }
cw2.visitEnd(); cw2.visitEnd();
newLoader.loadClass(newDynWebSokcetFullName.replace('/', '.'), cw2.toByteArray()); newLoader.loadClass(newDynConsumerFullName.replace('/', '.'), cw2.toByteArray());
} }
cw.visitEnd();
Class<?> newClazz = newLoader.loadClass(newDynName.replace('/', '.'), cw.toByteArray()); Class<?> newClazz = newLoader.loadClass(newDynName.replace('/', '.'), cw.toByteArray());
try { try {
return (T) newClazz.newInstance(); return (T) newClazz.newInstance();
@@ -399,10 +458,6 @@ public final class Rest {
} }
} }
public static void main(String[] args) throws Throwable {
System.out.println(createRestWebSocketServlet((Class<WebSocket>) Class.forName("org.redkale.test.ws.ChatWebSocket")));
}
static <T extends HttpServlet> T createRestServlet(final Class userType0, final Class<T> baseServletType, final Class<? extends Service> serviceType) { static <T extends HttpServlet> T createRestServlet(final Class userType0, final Class<T> baseServletType, final Class<? extends Service> serviceType) {
if (baseServletType == null || serviceType == null) throw new RuntimeException(" Servlet or Service is null Class on createRestServlet"); if (baseServletType == null || serviceType == null) throw new RuntimeException(" Servlet or Service is null Class on createRestServlet");
if (!HttpServlet.class.isAssignableFrom(baseServletType)) throw new RuntimeException(baseServletType + " is not HttpServlet Class on createRestServlet"); if (!HttpServlet.class.isAssignableFrom(baseServletType)) throw new RuntimeException(baseServletType + " is not HttpServlet Class on createRestServlet");

View File

@@ -9,6 +9,7 @@ import java.io.Serializable;
import java.util.Map; import java.util.Map;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import javax.annotation.Resource; import javax.annotation.Resource;
import org.redkale.convert.json.JsonConvert;
import org.redkale.net.http.*; import org.redkale.net.http.*;
import org.redkale.test.ws.ChatMessage; import org.redkale.test.ws.ChatMessage;
import org.redkale.test.ws.ChatService; import org.redkale.test.ws.ChatService;
@@ -22,7 +23,7 @@ import org.redkale.test.ws.ChatWebSocket;
public final class _DyncChatWebSocketServlet extends WebSocketServlet { public final class _DyncChatWebSocketServlet extends WebSocketServlet {
@Resource @Resource
private ChatService service; private ChatService _redkale_resource_0;
public _DyncChatWebSocketServlet() { public _DyncChatWebSocketServlet() {
super(); super();
@@ -31,7 +32,7 @@ public final class _DyncChatWebSocketServlet extends WebSocketServlet {
@Override @Override
protected <G extends Serializable, T> WebSocket<G, T> createWebSocket() { protected <G extends Serializable, T> WebSocket<G, T> createWebSocket() {
return (WebSocket) new _DyncChatWebSocket(service); return (WebSocket) new _DyncChatWebSocket(_redkale_resource_0);
} }
@Override @Override
@@ -53,6 +54,10 @@ public final class _DyncChatWebSocketServlet extends WebSocketServlet {
public _DyncChatWebSocketMessage_joinroom joinroom; public _DyncChatWebSocketMessage_joinroom joinroom;
@Override
public String toString() {
return JsonConvert.root().convertTo(this);
}
} }
public static class _DyncChatWebSocketMessage_sendmessagee { public static class _DyncChatWebSocketMessage_sendmessagee {
@@ -61,12 +66,20 @@ public final class _DyncChatWebSocketServlet extends WebSocketServlet {
public Map<String, String> extmap; public Map<String, String> extmap;
@Override
public String toString() {
return JsonConvert.root().convertTo(this);
}
} }
public static class _DyncChatWebSocketMessage_joinroom { public static class _DyncChatWebSocketMessage_joinroom {
public int roomid; public int roomid;
@Override
public String toString() {
return JsonConvert.root().convertTo(this);
}
} }
public static class _DynRestOnMessageConsumer implements BiConsumer<WebSocket, Object> { public static class _DynRestOnMessageConsumer implements BiConsumer<WebSocket, Object> {