Skip to content

Commit

Permalink
Move the judgment of whether to enable the plugin to each advice. (#2…
Browse files Browse the repository at this point in the history
…9754)

* Move the judgment of whether to enable the plugin to each advice.

* Improve agent plugins

* Update doc of AgentPluginEnable

* Update doc of AgentPluginEnable

* Update doc of AgentPluginEnable

* Update doc of AgentPluginEnable

* Update doc of AgentPluginEnable

* Refactor AgentPluginEnable
  • Loading branch information
jiangML authored Jan 18, 2024
1 parent 5767529 commit f715ca3
Show file tree
Hide file tree
Showing 31 changed files with 214 additions and 158 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.shardingsphere.agent.api.plugin;

public interface AgentPluginEnable {

/**
* Is the plugin enabled.
*
* @return true or false
*/
default boolean isPluginEnabled() {
return true;
}
}
13 changes: 0 additions & 13 deletions agent/core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,6 @@
<artifactId>shardingsphere-agent-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-proxy-backend-core</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc-core</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.apache.shardingsphere.agent.core.advisor.config.AdvisorConfigurationLoader;
import org.apache.shardingsphere.agent.core.builder.AgentBuilderFactory;
import org.apache.shardingsphere.agent.core.path.AgentPath;
import org.apache.shardingsphere.agent.core.plugin.PluginContext;
import org.apache.shardingsphere.agent.core.plugin.config.PluginConfigurationLoader;
import org.apache.shardingsphere.agent.core.plugin.jar.PluginJarLoader;

Expand Down Expand Up @@ -54,7 +53,6 @@ public static void premain(final String args, final Instrumentation instrumentat
Map<String, PluginConfiguration> pluginConfigs = PluginConfigurationLoader.load(rootPath);
Collection<JarFile> pluginJars = PluginJarLoader.load(rootPath);
boolean isEnhancedForProxy = isEnhancedForProxy();
PluginContext.getInstance().setEnhancedForProxy(isEnhancedForProxy);
Map<String, AdvisorConfiguration> advisorConfigs = AdvisorConfigurationLoader.load(pluginJars, pluginConfigs.keySet());
AgentBuilderFactory.create(pluginConfigs, pluginJars, advisorConfigs, isEnhancedForProxy).installOn(instrumentation);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import net.bytebuddy.matcher.ElementMatchers;
import org.apache.shardingsphere.agent.api.advice.TargetAdviceObject;
import org.apache.shardingsphere.agent.api.advice.type.ConstructorAdvice;
import org.apache.shardingsphere.agent.api.plugin.AgentPluginEnable;
import org.apache.shardingsphere.agent.core.advisor.executor.AdviceExecutor;
import org.apache.shardingsphere.agent.core.plugin.PluginContext;

import java.util.Collection;
import java.util.Map;
Expand All @@ -54,14 +54,12 @@ public final class ConstructorAdviceExecutor implements AdviceExecutor {
*/
@RuntimeType
public void advice(@This final TargetAdviceObject target, @AllArguments final Object[] args) {
boolean adviceEnabled = PluginContext.getInstance().isPluginEnabled();
if (!adviceEnabled) {
return;
}
try {
for (Entry<String, Collection<ConstructorAdvice>> entry : advices.entrySet()) {
for (ConstructorAdvice each : entry.getValue()) {
each.onConstructor(target, args, entry.getKey());
if (isPluginEnabled(each)) {
each.onConstructor(target, args, entry.getKey());
}
}
}
// CHECKSTYLE:OFF
Expand All @@ -71,6 +69,10 @@ public void advice(@This final TargetAdviceObject target, @AllArguments final Ob
}
}

private boolean isPluginEnabled(final ConstructorAdvice advice) {
return !(advice instanceof AgentPluginEnable) || ((AgentPluginEnable) advice).isPluginEnabled();
}

@Override
public Builder<?> intercept(final Builder<?> builder, final MethodDescription pointcut) {
return builder.constructor(ElementMatchers.is(pointcut)).intercept(SuperMethodCall.INSTANCE.andThen(MethodDelegation.withDefaultConfiguration().to(this)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
import net.bytebuddy.implementation.bind.annotation.SuperCall;
import net.bytebuddy.implementation.bind.annotation.This;
import net.bytebuddy.matcher.ElementMatchers;
import org.apache.shardingsphere.agent.api.plugin.AgentPluginEnable;
import org.apache.shardingsphere.agent.api.advice.TargetAdviceObject;
import org.apache.shardingsphere.agent.api.advice.type.InstanceMethodAdvice;
import org.apache.shardingsphere.agent.core.advisor.executor.AdviceExecutor;
import org.apache.shardingsphere.agent.core.plugin.PluginContext;

import java.lang.reflect.Method;
import java.util.Collection;
Expand Down Expand Up @@ -62,24 +62,17 @@ public final class InstanceMethodAdviceExecutor implements AdviceExecutor {
@RuntimeType
@SneakyThrows
public Object advice(@This final TargetAdviceObject target, @Origin final Method method, @AllArguments final Object[] args, @SuperCall final Callable<?> callable) {
boolean adviceEnabled = PluginContext.getInstance().isPluginEnabled();
if (adviceEnabled) {
adviceBefore(target, method, args);
}
adviceBefore(target, method, args);
Object result = null;
try {
result = callable.call();
// CHECKSTYLE:OFF
} catch (final Throwable ex) {
// CHECKSTYLE:ON
if (adviceEnabled) {
adviceThrow(target, method, args, ex);
}
adviceThrow(target, method, args, ex);
throw ex;
} finally {
if (adviceEnabled) {
adviceAfter(target, method, args, result);
}
adviceAfter(target, method, args, result);
}
return result;
}
Expand All @@ -88,7 +81,9 @@ private void adviceBefore(final TargetAdviceObject target, final Method method,
try {
for (Entry<String, Collection<InstanceMethodAdvice>> entry : advices.entrySet()) {
for (InstanceMethodAdvice each : entry.getValue()) {
each.beforeMethod(target, method, args, entry.getKey());
if (isPluginEnabled(each)) {
each.beforeMethod(target, method, args, entry.getKey());
}
}
}
// CHECKSTYLE:OFF
Expand All @@ -102,7 +97,9 @@ private void adviceThrow(final TargetAdviceObject target, final Method method, f
try {
for (Entry<String, Collection<InstanceMethodAdvice>> entry : advices.entrySet()) {
for (InstanceMethodAdvice each : entry.getValue()) {
each.onThrowing(target, method, args, ex, entry.getKey());
if (isPluginEnabled(each)) {
each.onThrowing(target, method, args, ex, entry.getKey());
}
}
}
// CHECKSTYLE:OFF
Expand All @@ -116,9 +113,10 @@ private void adviceAfter(final TargetAdviceObject target, final Method method, f
try {
for (Entry<String, Collection<InstanceMethodAdvice>> entry : advices.entrySet()) {
for (InstanceMethodAdvice each : entry.getValue()) {
each.afterMethod(target, method, args, result, entry.getKey());
if (isPluginEnabled(each)) {
each.afterMethod(target, method, args, result, entry.getKey());
}
}

}
// CHECKSTYLE:OFF
} catch (final Throwable ex) {
Expand All @@ -127,6 +125,10 @@ private void adviceAfter(final TargetAdviceObject target, final Method method, f
}
}

private boolean isPluginEnabled(final InstanceMethodAdvice advice) {
return !(advice instanceof AgentPluginEnable) || ((AgentPluginEnable) advice).isPluginEnabled();
}

@Override
public Builder<?> intercept(final Builder<?> builder, final MethodDescription pointcut) {
return builder.method(ElementMatchers.is(pointcut)).intercept(MethodDelegation.withDefaultConfiguration().to(this));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
import net.bytebuddy.implementation.bind.annotation.SuperCall;
import net.bytebuddy.matcher.ElementMatchers;
import org.apache.shardingsphere.agent.api.plugin.AgentPluginEnable;
import org.apache.shardingsphere.agent.api.advice.type.StaticMethodAdvice;
import org.apache.shardingsphere.agent.core.advisor.executor.AdviceExecutor;
import org.apache.shardingsphere.agent.core.plugin.PluginContext;

import java.lang.reflect.Method;
import java.util.Collection;
Expand Down Expand Up @@ -60,24 +60,17 @@ public final class StaticMethodAdviceExecutor implements AdviceExecutor {
@RuntimeType
@SneakyThrows
public Object advice(@Origin final Class<?> klass, @Origin final Method method, @AllArguments final Object[] args, @SuperCall final Callable<?> callable) {
boolean adviceEnabled = PluginContext.getInstance().isPluginEnabled();
if (adviceEnabled) {
adviceBefore(klass, method, args);
}
adviceBefore(klass, method, args);
Object result = null;
try {
result = callable.call();
// CHECKSTYLE:OFF
} catch (final Throwable ex) {
// CHECKSTYLE:ON
if (adviceEnabled) {
adviceThrow(klass, method, args, ex);
}
adviceThrow(klass, method, args, ex);
throw ex;
} finally {
if (adviceEnabled) {
adviceAfter(klass, method, args, result);
}
adviceAfter(klass, method, args, result);
}
return result;
}
Expand All @@ -86,7 +79,9 @@ private void adviceBefore(final Class<?> klass, final Method method, final Objec
try {
for (Entry<String, Collection<StaticMethodAdvice>> entry : advices.entrySet()) {
for (StaticMethodAdvice each : entry.getValue()) {
each.beforeMethod(klass, method, args, entry.getKey());
if (isPluginEnabled(each)) {
each.beforeMethod(klass, method, args, entry.getKey());
}
}
}
// CHECKSTYLE:OFF
Expand All @@ -100,7 +95,9 @@ private void adviceThrow(final Class<?> klass, final Method method, final Object
try {
for (Entry<String, Collection<StaticMethodAdvice>> entry : advices.entrySet()) {
for (StaticMethodAdvice each : entry.getValue()) {
each.onThrowing(klass, method, args, ex, entry.getKey());
if (isPluginEnabled(each)) {
each.onThrowing(klass, method, args, ex, entry.getKey());
}
}
}
// CHECKSTYLE:OFF
Expand All @@ -114,7 +111,9 @@ private void adviceAfter(final Class<?> klass, final Method method, final Object
try {
for (Entry<String, Collection<StaticMethodAdvice>> entry : advices.entrySet()) {
for (StaticMethodAdvice each : entry.getValue()) {
each.afterMethod(klass, method, args, result, entry.getKey());
if (isPluginEnabled(each)) {
each.afterMethod(klass, method, args, result, entry.getKey());
}
}
}
// CHECKSTYLE:OFF
Expand All @@ -124,6 +123,10 @@ private void adviceAfter(final Class<?> klass, final Method method, final Object
}
}

private boolean isPluginEnabled(final StaticMethodAdvice advice) {
return !(advice instanceof AgentPluginEnable) || ((AgentPluginEnable) advice).isPluginEnabled();
}

@Override
public Builder<?> intercept(final Builder<?> builder, final MethodDescription pointcut) {
return builder.method(ElementMatchers.is(pointcut)).intercept(MethodDelegation.withDefaultConfiguration().to(this));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.shardingsphere.agent.plugin.core.advice;

import org.apache.shardingsphere.agent.api.plugin.AgentPluginEnable;
import org.apache.shardingsphere.agent.api.advice.type.InstanceMethodAdvice;
import org.apache.shardingsphere.agent.plugin.core.context.PluginContext;

public abstract class AbstractInstanceMethodAdvice implements InstanceMethodAdvice, AgentPluginEnable {

@Override
public boolean isPluginEnabled() {
return PluginContext.getInstance().isPluginEnabled();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.shardingsphere.agent.plugin.core.advice;

import org.apache.shardingsphere.agent.api.plugin.AgentPluginEnable;
import org.apache.shardingsphere.agent.api.advice.type.StaticMethodAdvice;
import org.apache.shardingsphere.agent.plugin.core.context.PluginContext;

public abstract class AbstractStaticMethodAdvice implements StaticMethodAdvice, AgentPluginEnable {

@Override
public boolean isPluginEnabled() {
return PluginContext.getInstance().isPluginEnabled();
}
}
Loading

0 comments on commit f715ca3

Please sign in to comment.