This is an automated email from the ASF dual-hosted git repository.
jianglongtao 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 07f0641e59f Add RegisteredShardingSphereSPI (#27928)
07f0641e59f is described below
commit 07f0641e59f8d1e92200d37b80016e38d7fd10f4
Author: Liang Zhang <[email protected]>
AuthorDate: Sat Aug 5 01:09:32 2023 +0800
Add RegisteredShardingSphereSPI (#27928)
* Add RegisteredShardingSphereSPI
* Add RegisteredShardingSphereSPI
* Add RegisteredShardingSphereSPI
* Add RegisteredShardingSphereSPI
---
...oader.java => RegisteredShardingSphereSPI.java} | 60 +++++------------
.../infra/spi/ShardingSphereServiceLoader.java | 75 +++++-----------------
2 files changed, 32 insertions(+), 103 deletions(-)
diff --git
a/infra/spi/src/main/java/org/apache/shardingsphere/infra/spi/ShardingSphereServiceLoader.java
b/infra/spi/src/main/java/org/apache/shardingsphere/infra/spi/RegisteredShardingSphereSPI.java
similarity index 53%
copy from
infra/spi/src/main/java/org/apache/shardingsphere/infra/spi/ShardingSphereServiceLoader.java
copy to
infra/spi/src/main/java/org/apache/shardingsphere/infra/spi/RegisteredShardingSphereSPI.java
index 196738a0ee3..5f30ee6c478 100644
---
a/infra/spi/src/main/java/org/apache/shardingsphere/infra/spi/ShardingSphereServiceLoader.java
+++
b/infra/spi/src/main/java/org/apache/shardingsphere/infra/spi/RegisteredShardingSphereSPI.java
@@ -23,37 +23,31 @@ import
org.apache.shardingsphere.infra.spi.annotation.SingletonSPI;
import java.util.Collection;
import java.util.LinkedList;
-import java.util.Map;
import java.util.ServiceLoader;
-import java.util.concurrent.ConcurrentHashMap;
/**
- * ShardingSphere service loader.
+ * Registered ShardingSphere SPI.
*
* @param <T> type of service
*/
-public final class ShardingSphereServiceLoader<T> {
+class RegisteredShardingSphereSPI<T> {
- private static final Map<Class<?>, ShardingSphereServiceLoader<?>> LOADERS
= new ConcurrentHashMap<>();
-
- private static final Object LOAD_LOCK = new Object();
+ private final Class<T> serviceInterface;
private final Collection<T> services;
- private final boolean isSingletonSPI;
-
- private ShardingSphereServiceLoader(final Class<T> serviceInterface) {
- validate(serviceInterface);
- services = load(serviceInterface);
- isSingletonSPI = null !=
serviceInterface.getAnnotation(SingletonSPI.class);
+ RegisteredShardingSphereSPI(final Class<T> serviceInterface) {
+ this.serviceInterface = serviceInterface;
+ validate();
+ services = load();
}
- private void validate(final Class<T> serviceInterface) {
+ private void validate() {
Preconditions.checkNotNull(serviceInterface, "SPI interface is null.");
Preconditions.checkArgument(serviceInterface.isInterface(), "SPI `%s`
is not an interface.", serviceInterface);
}
- private Collection<T> load(final Class<T> serviceInterface) {
+ private Collection<T> load() {
Collection<T> result = new LinkedList<>();
for (T each : ServiceLoader.load(serviceInterface)) {
result.add(each);
@@ -61,36 +55,8 @@ public final class ShardingSphereServiceLoader<T> {
return result;
}
- /**
- * Get service instances.
- *
- * @param serviceInterface service interface
- * @param <T> type of service interface
- * @return service instances
- * @see <a
href="https://bugs.openjdk.java.net/browse/JDK-8161372">JDK-8161372</a>
- */
- @SuppressWarnings("unchecked")
- public static <T> Collection<T> getServiceInstances(final Class<T>
serviceInterface) {
- ShardingSphereServiceLoader<?> result = LOADERS.get(serviceInterface);
- if (null != result) {
- return (Collection<T>) result.getServiceInstances();
- }
- synchronized (LOAD_LOCK) {
- result = LOADERS.get(serviceInterface);
- if (null == result) {
- result = new ShardingSphereServiceLoader<>(serviceInterface);
- LOADERS.put(serviceInterface, result);
- }
- }
- return (Collection<T>) result.getServiceInstances();
- }
-
- private Collection<T> getServiceInstances() {
- return isSingletonSPI ? getSingletonServiceInstances() :
createNewServiceInstances();
- }
-
- private Collection<T> getSingletonServiceInstances() {
- return services;
+ Collection<T> getServiceInstances() {
+ return null == serviceInterface.getAnnotation(SingletonSPI.class) ?
createNewServiceInstances() : getSingletonServiceInstances();
}
@SneakyThrows(ReflectiveOperationException.class)
@@ -102,4 +68,8 @@ public final class ShardingSphereServiceLoader<T> {
}
return result;
}
+
+ private Collection<T> getSingletonServiceInstances() {
+ return services;
+ }
}
diff --git
a/infra/spi/src/main/java/org/apache/shardingsphere/infra/spi/ShardingSphereServiceLoader.java
b/infra/spi/src/main/java/org/apache/shardingsphere/infra/spi/ShardingSphereServiceLoader.java
index 196738a0ee3..6ca779c1b9f 100644
---
a/infra/spi/src/main/java/org/apache/shardingsphere/infra/spi/ShardingSphereServiceLoader.java
+++
b/infra/spi/src/main/java/org/apache/shardingsphere/infra/spi/ShardingSphereServiceLoader.java
@@ -17,89 +17,48 @@
package org.apache.shardingsphere.infra.spi;
-import com.google.common.base.Preconditions;
-import lombok.SneakyThrows;
-import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI;
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
import java.util.Collection;
-import java.util.LinkedList;
import java.util.Map;
-import java.util.ServiceLoader;
import java.util.concurrent.ConcurrentHashMap;
/**
* ShardingSphere service loader.
- *
- * @param <T> type of service
*/
-public final class ShardingSphereServiceLoader<T> {
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class ShardingSphereServiceLoader {
- private static final Map<Class<?>, ShardingSphereServiceLoader<?>> LOADERS
= new ConcurrentHashMap<>();
+ private static final Map<Class<?>, RegisteredShardingSphereSPI<?>>
REGISTERED_SERVICES = new ConcurrentHashMap<>();
private static final Object LOAD_LOCK = new Object();
- private final Collection<T> services;
-
- private final boolean isSingletonSPI;
-
- private ShardingSphereServiceLoader(final Class<T> serviceInterface) {
- validate(serviceInterface);
- services = load(serviceInterface);
- isSingletonSPI = null !=
serviceInterface.getAnnotation(SingletonSPI.class);
- }
-
- private void validate(final Class<T> serviceInterface) {
- Preconditions.checkNotNull(serviceInterface, "SPI interface is null.");
- Preconditions.checkArgument(serviceInterface.isInterface(), "SPI `%s`
is not an interface.", serviceInterface);
- }
-
- private Collection<T> load(final Class<T> serviceInterface) {
- Collection<T> result = new LinkedList<>();
- for (T each : ServiceLoader.load(serviceInterface)) {
- result.add(each);
- }
- return result;
- }
-
/**
* Get service instances.
*
* @param serviceInterface service interface
* @param <T> type of service interface
* @return service instances
- * @see <a
href="https://bugs.openjdk.java.net/browse/JDK-8161372">JDK-8161372</a>
*/
@SuppressWarnings("unchecked")
public static <T> Collection<T> getServiceInstances(final Class<T>
serviceInterface) {
- ShardingSphereServiceLoader<?> result = LOADERS.get(serviceInterface);
+ return (Collection<T>)
getRegisteredSPI(serviceInterface).getServiceInstances();
+ }
+
+ /*
+ * @see <a
href="https://bugs.openjdk.java.net/browse/JDK-8161372">JDK-8161372</a>
+ */
+ private static <T> RegisteredShardingSphereSPI<?> getRegisteredSPI(final
Class<T> serviceInterface) {
+ RegisteredShardingSphereSPI<?> result =
REGISTERED_SERVICES.get(serviceInterface);
if (null != result) {
- return (Collection<T>) result.getServiceInstances();
+ return result;
}
synchronized (LOAD_LOCK) {
- result = LOADERS.get(serviceInterface);
- if (null == result) {
- result = new ShardingSphereServiceLoader<>(serviceInterface);
- LOADERS.put(serviceInterface, result);
+ if (!REGISTERED_SERVICES.containsKey(serviceInterface)) {
+ REGISTERED_SERVICES.put(serviceInterface, new
RegisteredShardingSphereSPI<>(serviceInterface));
}
}
- return (Collection<T>) result.getServiceInstances();
- }
-
- private Collection<T> getServiceInstances() {
- return isSingletonSPI ? getSingletonServiceInstances() :
createNewServiceInstances();
- }
-
- private Collection<T> getSingletonServiceInstances() {
- return services;
- }
-
- @SneakyThrows(ReflectiveOperationException.class)
- @SuppressWarnings("unchecked")
- private Collection<T> createNewServiceInstances() {
- Collection<T> result = new LinkedList<>();
- for (Object each : services) {
- result.add((T)
each.getClass().getDeclaredConstructor().newInstance());
- }
- return result;
+ return REGISTERED_SERVICES.get(serviceInterface);
}
}