Skip to content

Commit

Permalink
Support get alibaba GenericService from spring context (#12585)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbumenJ authored Jun 22, 2023
1 parent e79e021 commit 27d9ce3
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 161 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,46 +17,28 @@

package com.alibaba.dubbo.rpc.service;

import org.apache.dubbo.common.utils.StringUtils;

@Deprecated
public class GenericException extends RuntimeException {
public class GenericException extends org.apache.dubbo.rpc.service.GenericException {

private static final long serialVersionUID = -1182299763306599962L;

private String exceptionClass;

private String exceptionMessage;

public GenericException() {
}

public GenericException(String exceptionClass, String exceptionMessage) {
public GenericException(String exceptionMessage) {
super(exceptionMessage);
this.exceptionClass = exceptionClass;
this.exceptionMessage = exceptionMessage;
}

public GenericException(Throwable cause) {
super(StringUtils.toString(cause));
this.exceptionClass = cause.getClass().getName();
this.exceptionMessage = cause.getMessage();
}

public String getExceptionClass() {
return exceptionClass;
}

public void setExceptionClass(String exceptionClass) {
this.exceptionClass = exceptionClass;
public GenericException(String exceptionClass, String exceptionMessage) {
super(exceptionClass, exceptionMessage);
}

public String getExceptionMessage() {
return exceptionMessage;
public GenericException(Throwable cause) {
super(cause);
}

public void setExceptionMessage(String exceptionMessage) {
this.exceptionMessage = exceptionMessage;
public GenericException(String message, Throwable cause, String exceptionClass, String exceptionMessage) {
super(message, cause, exceptionClass, exceptionMessage);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ public interface GenericService extends org.apache.dubbo.rpc.service.GenericServ

@Override
Object $invoke(String method, String[] parameterTypes, Object[] args)
throws com.alibaba.dubbo.rpc.service.GenericException;
throws GenericException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public static Class<?> determineInterfaceClass(String generic, String interfaceN

public static Class<?> determineInterfaceClass(String generic, String interfaceName, ClassLoader classLoader) {
if (ProtocolUtils.isGeneric(generic)) {
return GenericService.class;
return com.alibaba.dubbo.rpc.service.GenericService.class;
}
try {
if (StringUtils.isNotEmpty(interfaceName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@
*/
package org.apache.dubbo.rpc.service;

import org.apache.dubbo.common.utils.JsonUtils;
import org.apache.dubbo.common.utils.StringUtils;

import java.beans.Transient;
import java.io.Serializable;

/**
* GenericException
*
Expand All @@ -31,169 +27,50 @@ public class GenericException extends RuntimeException {

private static final long serialVersionUID = -1182299763306599962L;

private boolean useCause;

private String exceptionClass;

private String exceptionMessage;

private final GenericExceptionInfo genericExceptionInfo;

public GenericException() {
this(null, null);
}

public GenericException(String exceptionMessage) {
super(exceptionMessage);
this.exceptionMessage = exceptionMessage;
}

public GenericException(String exceptionClass, String exceptionMessage) {
super(exceptionMessage);
this.useCause = false;
this.exceptionClass = exceptionClass;
this.exceptionMessage = exceptionMessage;
this.genericExceptionInfo = new GenericExceptionInfo(exceptionClass, exceptionMessage, exceptionMessage, getStackTrace());
}

public GenericException(Throwable cause) {
super(StringUtils.toString(cause));
this.useCause = false;
this.exceptionClass = cause.getClass().getName();
this.exceptionMessage = cause.getMessage();
this.genericExceptionInfo = new GenericExceptionInfo(this.exceptionClass, this.exceptionMessage, super.getMessage(), getStackTrace());
}

protected GenericException(GenericExceptionInfo info) {
super(info.getMsg(), null, true, false);
setStackTrace(info.getStackTrace());
this.useCause = false;
this.exceptionClass = info.getExClass();
this.exceptionMessage = info.getExMsg();
this.genericExceptionInfo = info;
public GenericException(String message, Throwable cause, String exceptionClass, String exceptionMessage) {
super(message, cause);
this.exceptionClass = exceptionClass;
this.exceptionMessage = exceptionMessage;
}

@Transient
public String getExceptionClass() {
if(this.useCause) {
return ((GenericException)getCause()).getExceptionClass();
}
return exceptionClass;
}


public void setExceptionClass(String exceptionClass) {
if(this.useCause) {
((GenericException)getCause()).setExceptionClass(exceptionClass);
return;
}
this.exceptionClass = exceptionClass;
}

@Transient
public String getExceptionMessage() {
if(this.useCause) {
return ((GenericException)getCause()).getExceptionMessage();
}
return exceptionMessage;
}

public void setExceptionMessage(String exceptionMessage) {
if(this.useCause) {
((GenericException)getCause()).setExceptionMessage(exceptionMessage);
return;
}
this.exceptionMessage = exceptionMessage;
}

@Override
@Transient
public StackTraceElement[] getStackTrace() {
if(this.useCause) {
return ((GenericException)getCause()).getStackTrace();
}
return super.getStackTrace();
}

@Override
@Transient
public String getMessage() {
if(this.useCause) {
return getCause().getMessage();
}
return JsonUtils.toJson(GenericExceptionInfo.createNoStackTrace(genericExceptionInfo));
}

public String getGenericException() {
if(this.useCause) {
return ((GenericException)getCause()).getGenericException();
}
return JsonUtils.toJson(genericExceptionInfo);
}

public void setGenericException(String json) {
GenericExceptionInfo info = JsonUtils.toJavaObject(json, GenericExceptionInfo.class);
if(info == null) {
return;
}
this.useCause = true;
initCause(new GenericException(info));
}

@Override
@Transient
public String getLocalizedMessage() {
return getMessage();
}

/**
* create generic exception info
*/
public static class GenericExceptionInfo implements Serializable {
private String exClass;
private String exMsg;
private String msg;
private StackTraceElement[] stackTrace;

public GenericExceptionInfo() {
}

public GenericExceptionInfo(String exceptionClass, String exceptionMessage, String message, StackTraceElement[] stackTrace) {
this.exClass = exceptionClass;
this.exMsg = exceptionMessage;
this.msg = message;
this.stackTrace = stackTrace;
}

public static GenericExceptionInfo createNoStackTrace(GenericExceptionInfo info) {
return new GenericExceptionInfo(info.getExClass(), info.getExMsg(), info.getMsg(), null);
}

public String getMsg() {
return msg;
}

public String getExClass() {
return exClass;
}

public String getExMsg() {
return exMsg;
}

public void setExClass(String exClass) {
this.exClass = exClass;
}

public void setExMsg(String exMsg) {
this.exMsg = exMsg;
}

public void setMsg(String msg) {
this.msg = msg;
}

public StackTraceElement[] getStackTrace() {
return stackTrace;
}

public void setStackTrace(StackTraceElement[] stackTrace) {
this.stackTrace = stackTrace;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.List;
import java.util.Map;

@Deprecated
public class RpcInvocation implements Invocation, Serializable {

private static final long serialVersionUID = -4355285085441097045L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ public Object recreate() throws Throwable {
} catch (Exception e) {
// ignore
}
if ((exception instanceof RpcException) && !(exception instanceof com.alibaba.dubbo.rpc.RpcException)) {
com.alibaba.dubbo.rpc.RpcException recreated =
new com.alibaba.dubbo.rpc.RpcException(((RpcException) exception).getCode(),
exception.getMessage(), exception.getCause());
recreated.setStackTrace(exception.getStackTrace());
throw recreated;
}
throw exception;
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,9 @@ public void onResponse(Result appResponse, Invoker<?> invoker, Invocation inv) {
Throwable appException = appResponse.getException();
if (appException instanceof GenericException) {
GenericException tmp = (GenericException) appException;
appException = new com.alibaba.dubbo.rpc.service.GenericException(tmp.getExceptionClass(), tmp.getExceptionMessage());
appException = new com.alibaba.dubbo.rpc.service.GenericException(tmp.getMessage(), tmp.getCause(),
tmp.getExceptionClass(), tmp.getExceptionMessage());
appException.setStackTrace(tmp.getStackTrace());
}
if (!(appException instanceof com.alibaba.dubbo.rpc.service.GenericException)) {
appException = new com.alibaba.dubbo.rpc.service.GenericException(appException);
Expand Down

0 comments on commit 27d9ce3

Please sign in to comment.