This is an automated email from the ASF dual-hosted git repository.

panjuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new dd82a4b79d1 Add InterceptorPointRegistry (#22788)
dd82a4b79d1 is described below

commit dd82a4b79d13c5de84aac328ea7344ac9780294e
Author: Liang Zhang <[email protected]>
AuthorDate: Sat Dec 10 18:59:43 2022 +0800

    Add InterceptorPointRegistry (#22788)
---
 .../agent/api/point/PluginInterceptorPoint.java    |  8 ++---
 .../AbstractPluginDefinitionService.java           | 14 ++------
 ...nService.java => InterceptorPointRegistry.java} | 42 ++++++++++------------
 .../core/plugin/PluginBootServiceManager.java      |  6 ++--
 ...TypedSPIRegistry.java => AgentSPIRegistry.java} |  4 +--
 5 files changed, 31 insertions(+), 43 deletions(-)

diff --git 
a/agent/api/src/main/java/org/apache/shardingsphere/agent/api/point/PluginInterceptorPoint.java
 
b/agent/api/src/main/java/org/apache/shardingsphere/agent/api/point/PluginInterceptorPoint.java
index 046243c19f0..68944440aae 100644
--- 
a/agent/api/src/main/java/org/apache/shardingsphere/agent/api/point/PluginInterceptorPoint.java
+++ 
b/agent/api/src/main/java/org/apache/shardingsphere/agent/api/point/PluginInterceptorPoint.java
@@ -75,14 +75,14 @@ public final class PluginInterceptorPoint {
     @RequiredArgsConstructor
     public static final class Builder {
         
+        private final String targetClassName;
+        
         private final List<ConstructorPoint> constructorPoints = new 
ArrayList<>();
         
         private final List<InstanceMethodPoint> instanceMethodPoints = new 
ArrayList<>();
         
         private final List<ClassStaticMethodPoint> classStaticMethodPoints = 
new ArrayList<>();
         
-        private final String targetClassName;
-        
         /**
          * Configure the intercepting point on constructor.
          *
@@ -129,7 +129,7 @@ public final class PluginInterceptorPoint {
         public static final class InstanceMethodPointBuilder {
             
             private final Builder builder;
-    
+            
             private final ElementMatcher<? super MethodDescription> matcher;
             
             private String adviceClassName;
@@ -175,7 +175,7 @@ public final class PluginInterceptorPoint {
         public static final class StaticMethodPointBuilder {
             
             private final Builder builder;
-    
+            
             private final ElementMatcher<? super MethodDescription> matcher;
             
             private String adviceClassName;
diff --git 
a/agent/api/src/main/java/org/apache/shardingsphere/agent/spi/definition/AbstractPluginDefinitionService.java
 
b/agent/api/src/main/java/org/apache/shardingsphere/agent/spi/definition/AbstractPluginDefinitionService.java
index aff9f63da97..47cc9ff8391 100644
--- 
a/agent/api/src/main/java/org/apache/shardingsphere/agent/spi/definition/AbstractPluginDefinitionService.java
+++ 
b/agent/api/src/main/java/org/apache/shardingsphere/agent/spi/definition/AbstractPluginDefinitionService.java
@@ -21,16 +21,13 @@ import 
org.apache.shardingsphere.agent.api.point.PluginInterceptorPoint;
 import 
org.apache.shardingsphere.agent.api.point.PluginInterceptorPoint.Builder;
 
 import java.util.Collection;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.stream.Collectors;
 
 /**
  * Abstract plugin definition service.
  */
 public abstract class AbstractPluginDefinitionService implements 
PluginDefinitionService {
     
-    private final Map<String, Builder> interceptorPoints = new HashMap<>();
+    private final InterceptorPointRegistry interceptorPointRegistry = new 
InterceptorPointRegistry();
     
     @Override
     public final Collection<PluginInterceptorPoint> install(final boolean 
isEnhancedForProxy) {
@@ -39,7 +36,7 @@ public abstract class AbstractPluginDefinitionService 
implements PluginDefinitio
         } else {
             defineJdbcInterceptors();
         }
-        return 
interceptorPoints.values().stream().map(Builder::install).collect(Collectors.toList());
+        return interceptorPointRegistry.getAllInterceptorPoints();
     }
     
     protected abstract void defineProxyInterceptors();
@@ -47,11 +44,6 @@ public abstract class AbstractPluginDefinitionService 
implements PluginDefinitio
     protected abstract void defineJdbcInterceptors();
     
     protected final Builder defineInterceptor(final String targetClassName) {
-        if (interceptorPoints.containsKey(targetClassName)) {
-            return interceptorPoints.get(targetClassName);
-        }
-        Builder result = PluginInterceptorPoint.intercept(targetClassName);
-        interceptorPoints.put(targetClassName, result);
-        return result;
+        return 
interceptorPointRegistry.getInterceptorPointBuilder(targetClassName);
     }
 }
diff --git 
a/agent/api/src/main/java/org/apache/shardingsphere/agent/spi/definition/AbstractPluginDefinitionService.java
 
b/agent/api/src/main/java/org/apache/shardingsphere/agent/spi/definition/InterceptorPointRegistry.java
similarity index 52%
copy from 
agent/api/src/main/java/org/apache/shardingsphere/agent/spi/definition/AbstractPluginDefinitionService.java
copy to 
agent/api/src/main/java/org/apache/shardingsphere/agent/spi/definition/InterceptorPointRegistry.java
index aff9f63da97..9da66f39f09 100644
--- 
a/agent/api/src/main/java/org/apache/shardingsphere/agent/spi/definition/AbstractPluginDefinitionService.java
+++ 
b/agent/api/src/main/java/org/apache/shardingsphere/agent/spi/definition/InterceptorPointRegistry.java
@@ -21,37 +21,33 @@ import 
org.apache.shardingsphere.agent.api.point.PluginInterceptorPoint;
 import 
org.apache.shardingsphere.agent.api.point.PluginInterceptorPoint.Builder;
 
 import java.util.Collection;
-import java.util.HashMap;
 import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.stream.Collectors;
 
 /**
- * Abstract plugin definition service.
+ * Interceptor point registry.
  */
-public abstract class AbstractPluginDefinitionService implements 
PluginDefinitionService {
+public final class InterceptorPointRegistry {
     
-    private final Map<String, Builder> interceptorPoints = new HashMap<>();
+    private final Map<String, Builder> builders = new ConcurrentHashMap<>();
     
-    @Override
-    public final Collection<PluginInterceptorPoint> install(final boolean 
isEnhancedForProxy) {
-        if (isEnhancedForProxy) {
-            defineProxyInterceptors();
-        } else {
-            defineJdbcInterceptors();
-        }
-        return 
interceptorPoints.values().stream().map(Builder::install).collect(Collectors.toList());
+    /**
+     * Get interceptor point builder.
+     * 
+     * @param targetClassName target class name to be intercepted
+     * @return interceptor point builder
+     */
+    public Builder getInterceptorPointBuilder(final String targetClassName) {
+        return builders.computeIfAbsent(targetClassName, 
PluginInterceptorPoint::intercept);
     }
     
-    protected abstract void defineProxyInterceptors();
-    
-    protected abstract void defineJdbcInterceptors();
-    
-    protected final Builder defineInterceptor(final String targetClassName) {
-        if (interceptorPoints.containsKey(targetClassName)) {
-            return interceptorPoints.get(targetClassName);
-        }
-        Builder result = PluginInterceptorPoint.intercept(targetClassName);
-        interceptorPoints.put(targetClassName, result);
-        return result;
+    /**
+     * Get all interceptor points.
+     * 
+     * @return all interceptor points
+     */
+    public Collection<PluginInterceptorPoint> getAllInterceptorPoints() {
+        return 
builders.values().stream().map(Builder::install).collect(Collectors.toList());
     }
 }
diff --git 
a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/PluginBootServiceManager.java
 
b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/PluginBootServiceManager.java
index d471fd90c32..3f50c989d75 100644
--- 
a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/PluginBootServiceManager.java
+++ 
b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/PluginBootServiceManager.java
@@ -21,7 +21,7 @@ import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
 import org.apache.shardingsphere.agent.config.PluginConfiguration;
 import org.apache.shardingsphere.agent.core.logging.LoggerFactory;
-import org.apache.shardingsphere.agent.core.spi.AgentTypedSPIRegistry;
+import org.apache.shardingsphere.agent.core.spi.AgentSPIRegistry;
 import org.apache.shardingsphere.agent.spi.boot.PluginBootService;
 
 import java.io.IOException;
@@ -48,7 +48,7 @@ public final class PluginBootServiceManager {
         try {
             Thread.currentThread().setContextClassLoader(classLoader);
             for (Entry<String, PluginConfiguration> entry : 
pluginConfigMap.entrySet()) {
-                
AgentTypedSPIRegistry.getRegisteredService(PluginBootService.class, 
entry.getKey()).ifPresent(optional -> {
+                AgentSPIRegistry.getRegisteredService(PluginBootService.class, 
entry.getKey()).ifPresent(optional -> {
                     try {
                         LOGGER.info("Start plugin: {}", optional.getType());
                         optional.start(entry.getValue(), isEnhancedForProxy);
@@ -68,7 +68,7 @@ public final class PluginBootServiceManager {
      * Close all services.
      */
     public static void closeAllServices() {
-        
AgentTypedSPIRegistry.getAllRegisteredServices(PluginBootService.class).forEach(each
 -> {
+        
AgentSPIRegistry.getAllRegisteredServices(PluginBootService.class).forEach(each 
-> {
             try {
                 each.close();
                 // CHECKSTYLE:OFF
diff --git 
a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/spi/AgentTypedSPIRegistry.java
 
b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/spi/AgentSPIRegistry.java
similarity index 96%
rename from 
agent/core/src/main/java/org/apache/shardingsphere/agent/core/spi/AgentTypedSPIRegistry.java
rename to 
agent/core/src/main/java/org/apache/shardingsphere/agent/core/spi/AgentSPIRegistry.java
index e67b40af0db..829a1178fc3 100644
--- 
a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/spi/AgentTypedSPIRegistry.java
+++ 
b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/spi/AgentSPIRegistry.java
@@ -25,10 +25,10 @@ import java.util.Collection;
 import java.util.Optional;
 
 /**
- *  Agent typed SPI registry.
+ *  Agent SPI registry.
  */
 @NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class AgentTypedSPIRegistry {
+public final class AgentSPIRegistry {
     
     /**
      * Get registered service.

Reply via email to