-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: support after event processing of method
- Loading branch information
1 parent
df0c8a2
commit 49a4366
Showing
20 changed files
with
494 additions
and
39 deletions.
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
...ox/src/main/java/com/alibaba/chaosblade/exec/bootstrap/jvmsandbox/AfterEventListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Copyright 1999-2019 Alibaba Group Holding Ltd. | ||
* | ||
* 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 com.alibaba.chaosblade.exec.bootstrap.jvmsandbox; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Arrays; | ||
|
||
import com.alibaba.chaosblade.exec.common.aop.Plugin; | ||
import com.alibaba.chaosblade.exec.common.exception.InterruptProcessException; | ||
import com.alibaba.chaosblade.exec.common.exception.InterruptProcessException.State; | ||
import com.alibaba.chaosblade.exec.common.util.ReflectUtil; | ||
import com.alibaba.jvm.sandbox.api.ProcessControlException; | ||
import com.alibaba.jvm.sandbox.api.event.BeforeEvent; | ||
import com.alibaba.jvm.sandbox.api.event.Event; | ||
import com.alibaba.jvm.sandbox.api.event.ReturnEvent; | ||
import com.alibaba.jvm.sandbox.api.listener.EventListener; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* @author Changjun Xiao | ||
*/ | ||
public class AfterEventListener implements EventListener { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(AfterEventListener.class); | ||
|
||
private Plugin plugin; | ||
private ThreadLocal<BeforeEventBean> beforeEventCache; | ||
|
||
public AfterEventListener(Plugin plugin) { | ||
this.plugin = plugin; | ||
this.beforeEventCache = new ThreadLocal<BeforeEventBean>(); | ||
} | ||
|
||
@Override | ||
public void onEvent(Event event) throws Throwable { | ||
if (event instanceof BeforeEvent) { | ||
BeforeEvent beforeEvent = (BeforeEvent)event; | ||
BeforeEventBean beforeEventBean = new BeforeEventBean( | ||
beforeEvent.javaClassLoader, | ||
beforeEvent.target, | ||
beforeEvent.javaClassName, | ||
beforeEvent.javaMethodName, | ||
beforeEvent.javaMethodDesc, | ||
beforeEvent.argumentArray); | ||
beforeEventCache.set(beforeEventBean); | ||
return; | ||
} | ||
if (!(event instanceof ReturnEvent)) { | ||
beforeEventCache.remove(); | ||
return; | ||
} | ||
BeforeEventBean beforeEventBean = beforeEventCache.get(); | ||
if (beforeEventBean == null) { | ||
return; | ||
} | ||
ReturnEvent returnEvent = (ReturnEvent)event; | ||
// get the method class | ||
Class clazz; | ||
if (beforeEventBean.getObject() == null) { | ||
clazz = beforeEventBean.getClassLoader().loadClass(beforeEventBean.getClassName()); | ||
} else { | ||
clazz = beforeEventBean.getObject().getClass(); | ||
} | ||
Method method; | ||
try { | ||
method = ReflectUtil.getMethod(clazz, beforeEventBean.getMethodDesc(), beforeEventBean.getMethodName()); | ||
} catch (NoSuchMethodException e) { | ||
LOGGER.warn("get method by reflection exception for after event. " | ||
+ "class: {}, method: {}, arguments: {}, desc: {}", | ||
clazz, beforeEventBean.getMethodName(), Arrays.toString(beforeEventBean.getArgumentArray()), | ||
beforeEventBean.getMethodDesc(), e); | ||
beforeEventCache.remove(); | ||
return; | ||
} | ||
|
||
try { | ||
// do enhancer | ||
plugin.getEnhancer().afterAdvice(plugin.getModelSpec().getTarget(), beforeEventBean.getClassLoader(), | ||
beforeEventBean.getClassName(), beforeEventBean.getObject(), method, beforeEventBean.getArgumentArray(), | ||
returnEvent.object); | ||
} catch (Exception e) { | ||
// handle return or throw exception | ||
if (e instanceof InterruptProcessException) { | ||
InterruptProcessException exception = (InterruptProcessException)e; | ||
if (exception.getState() == State.RETURN_IMMEDIATELY) { | ||
ProcessControlException.throwReturnImmediately(exception.getResponse()); | ||
} else if (exception.getState() == State.THROWS_IMMEDIATELY) { | ||
ProcessControlException.throwThrowsImmediately((Throwable)exception.getResponse()); | ||
} | ||
} else { | ||
throw e; | ||
} | ||
} finally { | ||
beforeEventCache.remove(); | ||
} | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...ndbox/src/main/java/com/alibaba/chaosblade/exec/bootstrap/jvmsandbox/BeforeEventBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 1999-2019 Alibaba Group Holding Ltd. | ||
* | ||
* 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 com.alibaba.chaosblade.exec.bootstrap.jvmsandbox; | ||
|
||
/** | ||
* @author Changjun Xiao | ||
*/ | ||
public class BeforeEventBean { | ||
|
||
private ClassLoader classLoader; | ||
private Object object; | ||
private String className; | ||
private String methodName; | ||
private String methodDesc; | ||
private Object[] argumentArray; | ||
|
||
public BeforeEventBean(ClassLoader classLoader, Object object, String className, String methodName, | ||
String methodDesc, Object[] argumentArray) { | ||
this.classLoader = classLoader; | ||
this.object = object; | ||
this.className = className; | ||
this.methodName = methodName; | ||
this.methodDesc = methodDesc; | ||
this.argumentArray = argumentArray; | ||
} | ||
|
||
public ClassLoader getClassLoader() { | ||
return classLoader; | ||
} | ||
|
||
public Object getObject() { | ||
return object; | ||
} | ||
|
||
public String getClassName() { | ||
return className; | ||
} | ||
|
||
public String getMethodName() { | ||
return methodName; | ||
} | ||
|
||
public String getMethodDesc() { | ||
return methodDesc; | ||
} | ||
|
||
public Object[] getArgumentArray() { | ||
return argumentArray; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...blade-exec-common/src/main/java/com/alibaba/chaosblade/exec/common/aop/AfterEnhancer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright 1999-2019 Alibaba Group Holding Ltd. | ||
* | ||
* 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 com.alibaba.chaosblade.exec.common.aop; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
import com.alibaba.chaosblade.exec.common.center.ManagerFactory; | ||
import com.alibaba.chaosblade.exec.common.injection.Injector; | ||
|
||
/** | ||
* @author Changjun Xiao | ||
*/ | ||
public abstract class AfterEnhancer implements Enhancer { | ||
|
||
@Override | ||
public void beforeAdvice(String targetName, ClassLoader classLoader, String className, Object object, Method method, | ||
Object[] methodArguments) throws Exception { | ||
return; | ||
} | ||
|
||
@Override | ||
public void afterAdvice(String targetName, ClassLoader classLoader, String className, Object object, | ||
Method method, Object[] methodArguments, Object returnObject) throws Exception { | ||
if (!ManagerFactory.getStatusManager().expExists(targetName)) { | ||
return; | ||
} | ||
EnhancerModel model = doAfterAdvice(classLoader, className, object, method, methodArguments, returnObject); | ||
if (model == null) { | ||
return; | ||
} | ||
model.setTarget(targetName).setMethod(method).setObject(object).setMethodArguments(methodArguments); | ||
Injector.inject(model); | ||
} | ||
|
||
public abstract EnhancerModel doAfterAdvice(ClassLoader classLoader, String className, | ||
Object object, Method method, Object[] methodArguments, | ||
Object returnObject) throws Exception; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.