Skip to content

Commit

Permalink
3.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
youtongluan committed Jun 18, 2022
1 parent 54c2c14 commit 704ead1
Show file tree
Hide file tree
Showing 24 changed files with 175 additions and 99 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -322,3 +322,9 @@ v3.1.1

v3.1.2
1、增加BeanProvider接口,IOC通过BeanProvider获取bean。方便与Spring等第三方IOC对接

v3.2.0
1、异常码从int类型改为String类型(int方式入参仍保留)。旧用户在客户端升级前,可通过sumk.http.interrorcode=1让返回值使用int方式
2、允许自定义数据库事务的实现
3、BizException的堆栈信息改为默认打印,可以通过sumk.bizexception.fullstack=0来关闭

4 changes: 3 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.youtongluan</groupId>
<artifactId>sumk</artifactId>
<version>3.1.2</version>
<version>3.2.0</version>

<name>com.github.youtongluan:sumk</name>
<description>A quick developing framewort for internet company</description>
Expand Down Expand Up @@ -201,6 +201,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<inherited>true</inherited>
<configuration>
<source>1.8</source>
Expand All @@ -211,6 +212,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
Expand Down
42 changes: 13 additions & 29 deletions src/main/java/org/yx/asm/ProxyMethodWritor.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,7 @@
*/
package org.yx.asm;

import static org.objectweb.asm.Opcodes.ACONST_NULL;
import static org.objectweb.asm.Opcodes.ALOAD;
import static org.objectweb.asm.Opcodes.ARETURN;
import static org.objectweb.asm.Opcodes.ASTORE;
import static org.objectweb.asm.Opcodes.ATHROW;
import static org.objectweb.asm.Opcodes.DRETURN;
import static org.objectweb.asm.Opcodes.DUP;
import static org.objectweb.asm.Opcodes.FRETURN;
import static org.objectweb.asm.Opcodes.GOTO;
import static org.objectweb.asm.Opcodes.INVOKESPECIAL;
import static org.objectweb.asm.Opcodes.INVOKESTATIC;
import static org.objectweb.asm.Opcodes.INVOKEVIRTUAL;
import static org.objectweb.asm.Opcodes.IRETURN;
import static org.objectweb.asm.Opcodes.LRETURN;
import static org.objectweb.asm.Opcodes.NEW;
import static org.objectweb.asm.Opcodes.RETURN;
import static org.objectweb.asm.Opcodes.SIPUSH;
import static org.objectweb.asm.Opcodes.*;
import static org.yx.asm.WriterHelper.SINGLE;
import static org.yx.asm.WriterHelper.WIDTH;
import static org.yx.asm.WriterHelper.buildParamArray;
Expand Down Expand Up @@ -131,31 +115,31 @@ private void writeVoidMethod(int key) {
mv.visitLabel(l0);
mv.visitVarInsn(ALOAD, this.aopExcutorIndex);
buildParamArray(mv, params);
mv.visitMethodInsn(INVOKEVIRTUAL, AOP_EXCUTOR, "before", "([Ljava/lang/Object;)V", false);
mv.visitMethodInsn(INVOKEINTERFACE, AOP_EXCUTOR, "before", "([Ljava/lang/Object;)V", true);
this.callSuperMethod();
mv.visitVarInsn(ALOAD, this.aopExcutorIndex);
mv.visitInsn(ACONST_NULL);
mv.visitMethodInsn(INVOKEVIRTUAL, AOP_EXCUTOR, "after", "(Ljava/lang/Object;)V", false);
mv.visitMethodInsn(INVOKEINTERFACE, AOP_EXCUTOR, "after", "(Ljava/lang/Object;)V", true);
mv.visitLabel(l1);
mv.visitVarInsn(ALOAD, this.aopExcutorIndex);
mv.visitMethodInsn(INVOKEVIRTUAL, AOP_EXCUTOR, "close", "()V", false);
mv.visitMethodInsn(INVOKEINTERFACE, AOP_EXCUTOR, "close", "()V", true);
mv.visitInsn(RETURN);
mv.visitLabel(l2);
this.visitFullFrame();
mv.visitVarInsn(ASTORE, this.aopExcutorIndex + 1);
mv.visitVarInsn(ALOAD, this.aopExcutorIndex);
mv.visitVarInsn(ALOAD, this.aopExcutorIndex + 1);
mv.visitMethodInsn(INVOKEVIRTUAL, AOP_EXCUTOR, "onError", "(Ljava/lang/Throwable;)V", false);
mv.visitMethodInsn(INVOKEINTERFACE, AOP_EXCUTOR, "onError", "(Ljava/lang/Throwable;)V", true);
mv.visitLabel(l4);
mv.visitVarInsn(ALOAD, this.aopExcutorIndex);
mv.visitMethodInsn(INVOKEVIRTUAL, AOP_EXCUTOR, "close", "()V", false);
mv.visitMethodInsn(INVOKEINTERFACE, AOP_EXCUTOR, "close", "()V", true);
Label l5 = new Label();
mv.visitJumpInsn(GOTO, l5);
mv.visitLabel(l3);
mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Throwable" });
mv.visitVarInsn(ASTORE, this.aopExcutorIndex + 2);
mv.visitVarInsn(ALOAD, this.aopExcutorIndex);
mv.visitMethodInsn(INVOKEVIRTUAL, AOP_EXCUTOR, "close", "()V", false);
mv.visitMethodInsn(INVOKEINTERFACE, AOP_EXCUTOR, "close", "()V", true);
mv.visitVarInsn(ALOAD, this.aopExcutorIndex + 2);
mv.visitInsn(ATHROW);
mv.visitLabel(l5);
Expand Down Expand Up @@ -201,33 +185,33 @@ private void writeWithReturn(int key) {
mv.visitLabel(l0);
mv.visitVarInsn(ALOAD, this.aopExcutorIndex);
buildParamArray(mv, params);
mv.visitMethodInsn(INVOKEVIRTUAL, AOP_EXCUTOR, "before", "([Ljava/lang/Object;)V", false);
mv.visitMethodInsn(INVOKEINTERFACE, AOP_EXCUTOR, "before", "([Ljava/lang/Object;)V", true);
this.callSuperMethod();
int returnWidth = this.storeReuturnToLocalVariable(this.aopExcutorIndex + 1);
mv.visitVarInsn(ALOAD, this.aopExcutorIndex);
loadFromLocalVariable(mv, this.returnType, this.aopExcutorIndex + 1, true);
mv.visitMethodInsn(INVOKEVIRTUAL, AOP_EXCUTOR, "after", "(Ljava/lang/Object;)V", false);
mv.visitMethodInsn(INVOKEINTERFACE, AOP_EXCUTOR, "after", "(Ljava/lang/Object;)V", true);
mv.visitLabel(l1);
mv.visitVarInsn(ALOAD, this.aopExcutorIndex);
mv.visitMethodInsn(INVOKEVIRTUAL, AOP_EXCUTOR, "close", "()V", false);
mv.visitMethodInsn(INVOKEINTERFACE, AOP_EXCUTOR, "close", "()V", true);
loadFromLocalVariable(mv, this.returnType, this.aopExcutorIndex + 1, false);
this.jReturn();
mv.visitLabel(l2);
this.visitFullFrame();
mv.visitVarInsn(ASTORE, this.aopExcutorIndex + 1);
mv.visitVarInsn(ALOAD, this.aopExcutorIndex);
mv.visitVarInsn(ALOAD, this.aopExcutorIndex + 1);
mv.visitMethodInsn(INVOKEVIRTUAL, AOP_EXCUTOR, "onError", "(Ljava/lang/Throwable;)V", false);
mv.visitMethodInsn(INVOKEINTERFACE, AOP_EXCUTOR, "onError", "(Ljava/lang/Throwable;)V", true);
mv.visitLabel(l4);
mv.visitVarInsn(ALOAD, this.aopExcutorIndex);
mv.visitMethodInsn(INVOKEVIRTUAL, AOP_EXCUTOR, "close", "()V", false);
mv.visitMethodInsn(INVOKEINTERFACE, AOP_EXCUTOR, "close", "()V", true);
Label l5 = new Label();
mv.visitJumpInsn(GOTO, l5);
mv.visitLabel(l3);
mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Throwable" });
mv.visitVarInsn(ASTORE, this.aopExcutorIndex + 1 + returnWidth);
mv.visitVarInsn(ALOAD, this.aopExcutorIndex);
mv.visitMethodInsn(INVOKEVIRTUAL, AOP_EXCUTOR, "close", "()V", false);
mv.visitMethodInsn(INVOKEINTERFACE, AOP_EXCUTOR, "close", "()V", true);
mv.visitVarInsn(ALOAD, this.aopExcutorIndex + 1 + returnWidth);
mv.visitInsn(ATHROW);
mv.visitLabel(l5);
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/org/yx/bean/AopExcutorFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,26 @@
package org.yx.bean;

import java.util.Objects;
import java.util.function.Function;

import org.yx.annotation.spec.BoxSpec;
import org.yx.common.AopExcutor;
import org.yx.common.AopExcutorImpl;
import org.yx.db.exec.DBSource;
import org.yx.db.exec.DBTransaction;
import org.yx.db.exec.DefaultDBSource;

public class AopExcutorFactory {
private static DBSource[] boxs = new DBSource[0];
private static Function<DBSource, AopExcutor> factory = ds -> new AopExcutorImpl(new DBTransaction(ds));

public static Function<DBSource, AopExcutor> getFactory() {
return factory;
}

public static void setFactory(Function<DBSource, AopExcutor> innerFactory) {
AopExcutorFactory.factory = Objects.requireNonNull(innerFactory);
}

public static synchronized int add(BoxSpec box) {
Objects.requireNonNull(box);
Expand Down Expand Up @@ -57,6 +68,7 @@ public static int length() {
}

public static AopExcutor create(int key) {
return new AopExcutor(new DBTransaction(boxs[key]));
DBSource ds = boxs[key];
return factory.apply(ds);
}
}
31 changes: 5 additions & 26 deletions src/main/java/org/yx/common/AopExcutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,13 @@
*/
package org.yx.common;

import org.yx.annotation.doc.NotNull;
import org.yx.db.exec.DBTransaction;
import org.yx.util.ExceptionUtil;
public interface AopExcutor {

public class AopExcutor {
public void before(Object[] params);

@NotNull
private final DBTransaction transaction;
public void onError(Throwable e);

public AopExcutor(@NotNull DBTransaction transaction) {
this.transaction = transaction;
}

public void before(Object[] params) {
transaction.begin();
}

public void onError(Throwable e) {
transaction.rollback(e);
throw ExceptionUtil.toRuntimeException(e);
}

public void after(Object result) {
transaction.commit();
}

public void close() {
transaction.close();
}
public void after(Object result);

public void close();
}
48 changes: 48 additions & 0 deletions src/main/java/org/yx/common/AopExcutorImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Copyright (C) 2016 - 2030 youtongluan.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.yx.common;

import org.yx.annotation.doc.NotNull;
import org.yx.db.exec.DBTransaction;
import org.yx.util.ExceptionUtil;

public class AopExcutorImpl implements AopExcutor {

@NotNull
private final DBTransaction transaction;

public AopExcutorImpl(@NotNull DBTransaction transaction) {
this.transaction = transaction;
}

public void before(Object[] params) {
transaction.begin();
}

public void onError(Throwable e) {
transaction.rollback(e);
throw ExceptionUtil.toRuntimeException(e);
}

public void after(Object result) {
transaction.commit();
}

public void close() {
transaction.close();
}

}
2 changes: 1 addition & 1 deletion src/main/java/org/yx/conf/Const.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public final class Const {

public static final String DEFAULT_DB_NAME = "sumk";

public static final int SUMK_VERSION = 0x312;
public static final int SUMK_VERSION = 0x320;

public static String sumkVersion() {
return new StringBuilder(10).append((Const.SUMK_VERSION >> 8) & 0x0F).append('.')
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/org/yx/exception/BizException.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public class BizException extends CodeException {
private static final long serialVersionUID = 453453454L;

public BizException(int code, String msg) {
super(String.valueOf(code), msg);
}

public BizException(String code, String msg) {
super(code, msg);
}

Expand All @@ -36,9 +40,17 @@ public static void throwException(int code, String msg) throws BizException {
throw new BizException(code, msg);
}

public static BizException create(String code, String msg) {
return new BizException(code, msg);
}

public static void throwException(String code, String msg) throws BizException {
throw new BizException(code, msg);
}

@Override
public Throwable fillInStackTrace() {
if (AppInfo.getBoolean("sumk.bizexception.fullstack", false)) {
if (AppInfo.getBoolean("sumk.bizexception.fullstack", true)) {
return super.fillInStackTrace();
}
return this;
Expand Down
18 changes: 12 additions & 6 deletions src/main/java/org/yx/exception/CodeException.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,32 @@
*/
package org.yx.exception;

import java.util.Objects;

public abstract class CodeException extends RuntimeException {

private static final long serialVersionUID = 1L;

protected final int code;
protected final String code;

public CodeException(int code, String msg) {
public CodeException(String code, String msg) {
super(msg);
this.code = code;
this.code = Objects.requireNonNull(code);
}

public CodeException(int code, String msg, Throwable exception) {
public CodeException(String code, String msg, Throwable exception) {
super(msg, exception);
this.code = code;
this.code = Objects.requireNonNull(code);
}

public int getCode() {
public String getCode() {
return code;
}

public boolean isSameCode(String expect) {
return this.code.equals(expect);
}

@Override
public String getLocalizedMessage() {
return new StringBuilder().append(this.getMessage()).append(" (").append(code).append(")").toString();
Expand Down
20 changes: 13 additions & 7 deletions src/main/java/org/yx/exception/SoaException.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,21 @@ public String getExceptionClz() {
return exceptionClz;
}

public SoaException(Throwable e, int code, String msg) {
super(e instanceof BizException ? ((BizException) e).getCode() : code,
e instanceof BizException ? e.getMessage() : msg);
this.exceptionClz = e == null ? null : e.getClass().getName();
this.detailError = getException(e);
this.bizException = e instanceof BizException;
public static SoaException create(String code, String msg, Throwable cause) {
String detailError = getException(cause);
SoaException ex;
if (cause instanceof BizException) {
BizException bizEx = (BizException) cause;
ex = new SoaException(bizEx.getCode(), bizEx.getMessage(), detailError);
ex.bizException = true;
} else {
ex = new SoaException(code, msg, detailError);
}
ex.exceptionClz = cause == null ? null : cause.getClass().getName();
return ex;
}

public SoaException(int code, String msg, String detail) {
public SoaException(String code, String msg, String detail) {
super(code, msg);
this.exceptionClz = null;
this.detailError = detail;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/yx/exception/SumkException.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public SumkException(int code, String msg, Throwable exception) {
super(negative(code), msg, exception);
}

private static int negative(int code) {
return code > 0 ? -code : code;
private static String negative(int code) {
return code > 0 ? String.valueOf(-code) : String.valueOf(code);
}

public static SumkException wrap(Throwable e) {
Expand Down
Loading

0 comments on commit 704ead1

Please sign in to comment.