Skip to content

Commit

Permalink
Refactor AgentServiceLoader (apache#23000)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Dec 20, 2022
1 parent 5683895 commit df8ca57
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.shardingsphere.agent.core.spi;

import com.google.common.base.Preconditions;
import lombok.Getter;

import java.util.Collection;
import java.util.LinkedList;
Expand All @@ -28,17 +29,29 @@
/**
* Agent service loader.
*/
@Getter
public final class AgentServiceLoader<T> {

private static final Map<Class<?>, AgentServiceLoader<?>> LOADERS = new ConcurrentHashMap<>();

private final Map<Class<?>, Collection<T>> serviceMap = new ConcurrentHashMap<>();

private final Class<T> service;
private final Collection<T> services;

private AgentServiceLoader(final Class<T> service) {
this.service = service;
register(service);
validate(service);
this.services = register(service);
}

private void validate(final Class<T> service) {
Preconditions.checkNotNull(service, "Extension clazz is null.");
Preconditions.checkArgument(service.isInterface(), "Extension clazz `%s` is not interface.", service);
}

private Collection<T> register(final Class<T> service) {
Collection<T> result = new LinkedList<>();
for (T each : ServiceLoader.load(service)) {
result.add(each);
}
return result;
}

/**
Expand All @@ -50,29 +63,6 @@ private AgentServiceLoader(final Class<T> service) {
*/
@SuppressWarnings("unchecked")
public static <T> AgentServiceLoader<T> getServiceLoader(final Class<T> service) {
Preconditions.checkNotNull(service, "Extension clazz is null.");
Preconditions.checkArgument(service.isInterface(), "Extension clazz `%s` is not interface.", service);
AgentServiceLoader<T> agentServiceLoader = (AgentServiceLoader<T>) LOADERS.get(service);
if (null != agentServiceLoader) {
return agentServiceLoader;
}
return (AgentServiceLoader<T>) LOADERS.computeIfAbsent(service, AgentServiceLoader::new);
}

/**
* New service instances.
*
* @return service instances
*/
public Collection<T> newServiceInstances() {
return serviceMap.get(service);
}

private void register(final Class<T> service) {
if (serviceMap.containsKey(service)) {
return;
}
serviceMap.put(service, new LinkedList<>());
ServiceLoader.load(service).forEach(each -> serviceMap.get(service).add(each));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public final class PluginBootServiceRegistry {
* @return registered service
*/
public static Optional<PluginBootService> getRegisteredService(final String type) {
return AgentServiceLoader.getServiceLoader(PluginBootService.class).newServiceInstances().stream().filter(each -> each.getType().equalsIgnoreCase(type)).findFirst();
return AgentServiceLoader.getServiceLoader(PluginBootService.class).getServices().stream().filter(each -> each.getType().equalsIgnoreCase(type)).findFirst();
}

/**
Expand All @@ -48,7 +48,7 @@ public static Optional<PluginBootService> getRegisteredService(final String type
* @return registered services
*/
public static Collection<PluginBootService> getAllRegisteredServices() {
return AgentServiceLoader.getServiceLoader(PluginBootService.class).newServiceInstances();
return AgentServiceLoader.getServiceLoader(PluginBootService.class).getServices();
}

/**
Expand Down

0 comments on commit df8ca57

Please sign in to comment.