This is an automated email from the ASF dual-hosted git repository.
sunnianjun 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 f372dc5d0b0 Refactor AgentTypedSPIRegistry (#22748)
f372dc5d0b0 is described below
commit f372dc5d0b02849c8ebfda23229508ef28bd1546
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Dec 8 23:49:03 2022 +0800
Refactor AgentTypedSPIRegistry (#22748)
* Merge 2 DatabaseOperateBackendHandlerFactoryTest
* Refactor AgentTypedSPIRegistry
---
.../core/plugin/PluginBootServiceManager.java | 16 +++++-----
.../agent/core/spi/AgentTypedSPIRegistry.java | 34 ++++++----------------
2 files changed, 17 insertions(+), 33 deletions(-)
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 71b02929aa6..d471fd90c32 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
@@ -39,23 +39,23 @@ public final class PluginBootServiceManager {
/**
* Start all services.
*
- * @param pluginConfigurationMap plugin configuration map
+ * @param pluginConfigMap plugin configuration map
* @param classLoader classLoader
* @param isEnhancedForProxy is enhanced for proxy
*/
- public static void startAllServices(final Map<String, PluginConfiguration>
pluginConfigurationMap, final ClassLoader classLoader, final boolean
isEnhancedForProxy) {
+ public static void startAllServices(final Map<String, PluginConfiguration>
pluginConfigMap, final ClassLoader classLoader, final boolean
isEnhancedForProxy) {
ClassLoader originalClassLoader =
Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(classLoader);
- for (Entry<String, PluginConfiguration> entry :
pluginConfigurationMap.entrySet()) {
-
AgentTypedSPIRegistry.getRegisteredServiceOptional(PluginBootService.class,
entry.getKey()).ifPresent(optional -> {
+ for (Entry<String, PluginConfiguration> entry :
pluginConfigMap.entrySet()) {
+
AgentTypedSPIRegistry.getRegisteredService(PluginBootService.class,
entry.getKey()).ifPresent(optional -> {
try {
LOGGER.info("Start plugin: {}", optional.getType());
optional.start(entry.getValue(), isEnhancedForProxy);
// CHECKSTYLE:OFF
} catch (final Throwable ex) {
// CHECKSTYLE:ON
- LOGGER.error("Failed to start service", ex);
+ LOGGER.error("Failed to start service.", ex);
}
});
}
@@ -68,20 +68,20 @@ public final class PluginBootServiceManager {
* Close all services.
*/
public static void closeAllServices() {
-
AgentTypedSPIRegistry.getAllRegisteredService(PluginBootService.class).forEach(each
-> {
+
AgentTypedSPIRegistry.getAllRegisteredServices(PluginBootService.class).forEach(each
-> {
try {
each.close();
// CHECKSTYLE:OFF
} catch (final Throwable ex) {
// CHECKSTYLE:ON
- LOGGER.error("Failed to close service", ex);
+ LOGGER.error("Failed to close service.", ex);
}
});
PluginJarHolder.getPluginJars().forEach(each -> {
try {
each.getJarFile().close();
} catch (final IOException ex) {
- LOGGER.error("Failed to close jar file", ex);
+ LOGGER.error("Failed to close jar file.", ex);
}
});
}
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/AgentTypedSPIRegistry.java
index 41e84648519..81db0a5c2d7 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/AgentTypedSPIRegistry.java
@@ -17,55 +17,39 @@
package org.apache.shardingsphere.agent.core.spi;
-import java.util.Collection;
-import java.util.Optional;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
-import
org.apache.shardingsphere.agent.core.exception.AgentServiceProviderNotFoundException;
import org.apache.shardingsphere.agent.spi.type.AgentTypedSPI;
+import java.util.Collection;
+import java.util.Optional;
+
/**
* Agent typed SPI registry.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class AgentTypedSPIRegistry {
- /**
- * Get registered service.
- *
- * @param typedSPIClass typed SPI class
- * @param type type
- * @param <T> type
- * @return registered service
- */
- public static <T extends AgentTypedSPI> T getRegisteredService(final
Class<T> typedSPIClass, final String type) {
- Optional<T> serviceInstance =
AgentServiceLoader.getServiceLoader(typedSPIClass).newServiceInstances().stream().filter(each
-> each.getType().equalsIgnoreCase(type)).findFirst();
- if (serviceInstance.isPresent()) {
- return serviceInstance.get();
- }
- throw new AgentServiceProviderNotFoundException(typedSPIClass, type);
- }
-
/**
* Get registered service.
*
* @param typedSPIClass typed SPI class
* @param type type
- * @param <T> type
+ * @param <T> type of agent typed SPI
* @return registered service
*/
- public static <T extends AgentTypedSPI> Optional<T>
getRegisteredServiceOptional(final Class<T> typedSPIClass, final String type) {
+ public static <T extends AgentTypedSPI> Optional<T>
getRegisteredService(final Class<T> typedSPIClass, final String type) {
return
AgentServiceLoader.getServiceLoader(typedSPIClass).newServiceInstances().stream().filter(each
-> each.getType().equalsIgnoreCase(type)).findFirst();
}
/**
- * Get all registered service.
+ * Get all registered services.
*
* @param typedSPIClass typed SPI class
- * @param <T> type
- * @return registered service
+ * @param <T> type of agent typed SPI
+ * @return registered services
*/
- public static <T extends AgentTypedSPI> Collection<T>
getAllRegisteredService(final Class<T> typedSPIClass) {
+ public static <T extends AgentTypedSPI> Collection<T>
getAllRegisteredServices(final Class<T> typedSPIClass) {
return
AgentServiceLoader.getServiceLoader(typedSPIClass).newServiceInstances();
}
}