144 lines
5.4 KiB
Java
144 lines
5.4 KiB
Java
|
package io.grpc;
|
||
|
|
||
|
import com.google.common.base.Preconditions;
|
||
|
import io.grpc.ServerProvider;
|
||
|
import io.grpc.ServiceProviders;
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.Collections;
|
||
|
import java.util.Comparator;
|
||
|
import java.util.LinkedHashSet;
|
||
|
import java.util.List;
|
||
|
import java.util.logging.Logger;
|
||
|
|
||
|
/* loaded from: classes6.dex */
|
||
|
public final class ServerRegistry {
|
||
|
private static ServerRegistry instance;
|
||
|
private static final Logger logger = Logger.getLogger(ServerRegistry.class.getName());
|
||
|
private final LinkedHashSet<ServerProvider> allProviders = new LinkedHashSet<>();
|
||
|
private List<ServerProvider> effectiveProviders = Collections.emptyList();
|
||
|
|
||
|
public final void register(ServerProvider serverProvider) {
|
||
|
synchronized (this) {
|
||
|
addProvider(serverProvider);
|
||
|
refreshProviders();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void addProvider(ServerProvider serverProvider) {
|
||
|
synchronized (this) {
|
||
|
Preconditions.checkArgument(serverProvider.isAvailable(), "isAvailable() returned false");
|
||
|
this.allProviders.add(serverProvider);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public final void deregister(ServerProvider serverProvider) {
|
||
|
synchronized (this) {
|
||
|
this.allProviders.remove(serverProvider);
|
||
|
refreshProviders();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void refreshProviders() {
|
||
|
synchronized (this) {
|
||
|
ArrayList arrayList = new ArrayList(this.allProviders);
|
||
|
Collections.sort(arrayList, Collections.reverseOrder(new Comparator<ServerProvider>(this) { // from class: io.grpc.ServerRegistry.1
|
||
|
final ServerRegistry this$0;
|
||
|
|
||
|
{
|
||
|
this.this$0 = this;
|
||
|
}
|
||
|
|
||
|
@Override // java.util.Comparator
|
||
|
public int compare(ServerProvider serverProvider, ServerProvider serverProvider2) {
|
||
|
return serverProvider.priority() - serverProvider2.priority();
|
||
|
}
|
||
|
}));
|
||
|
this.effectiveProviders = Collections.unmodifiableList(arrayList);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static ServerRegistry getDefaultRegistry() {
|
||
|
ServerRegistry serverRegistry;
|
||
|
synchronized (ServerRegistry.class) {
|
||
|
if (instance == null) {
|
||
|
List<ServerProvider> loadAll = ServiceProviders.loadAll(ServerProvider.class, Collections.emptyList(), ServerProvider.class.getClassLoader(), new ServerPriorityAccessor());
|
||
|
instance = new ServerRegistry();
|
||
|
for (ServerProvider serverProvider : loadAll) {
|
||
|
Logger logger2 = logger;
|
||
|
StringBuilder sb = new StringBuilder();
|
||
|
sb.append("Service loader found ");
|
||
|
sb.append(serverProvider);
|
||
|
logger2.fine(sb.toString());
|
||
|
if (serverProvider.isAvailable()) {
|
||
|
instance.addProvider(serverProvider);
|
||
|
}
|
||
|
}
|
||
|
instance.refreshProviders();
|
||
|
}
|
||
|
serverRegistry = instance;
|
||
|
}
|
||
|
return serverRegistry;
|
||
|
}
|
||
|
|
||
|
final List<ServerProvider> providers() {
|
||
|
List<ServerProvider> list;
|
||
|
synchronized (this) {
|
||
|
list = this.effectiveProviders;
|
||
|
}
|
||
|
return list;
|
||
|
}
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
public final ServerProvider provider() {
|
||
|
List<ServerProvider> providers = providers();
|
||
|
if (providers.isEmpty()) {
|
||
|
return null;
|
||
|
}
|
||
|
return providers.get(0);
|
||
|
}
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
public final ServerBuilder<?> newServerBuilderForPort(int i, ServerCredentials serverCredentials) {
|
||
|
if (providers().isEmpty()) {
|
||
|
throw new ProviderNotFoundException("No functional server found. Try adding a dependency on the grpc-netty or grpc-netty-shaded artifact");
|
||
|
}
|
||
|
StringBuilder sb = new StringBuilder();
|
||
|
for (ServerProvider serverProvider : providers()) {
|
||
|
ServerProvider.NewServerBuilderResult newServerBuilderForPort = serverProvider.newServerBuilderForPort(i, serverCredentials);
|
||
|
if (newServerBuilderForPort.getServerBuilder() != null) {
|
||
|
return newServerBuilderForPort.getServerBuilder();
|
||
|
}
|
||
|
sb.append("; ");
|
||
|
sb.append(serverProvider.getClass().getName());
|
||
|
sb.append(": ");
|
||
|
sb.append(newServerBuilderForPort.getError());
|
||
|
}
|
||
|
throw new ProviderNotFoundException(sb.substring(2));
|
||
|
}
|
||
|
|
||
|
/* loaded from: classes6.dex */
|
||
|
static final class ServerPriorityAccessor implements ServiceProviders.PriorityAccessor<ServerProvider> {
|
||
|
private ServerPriorityAccessor() {
|
||
|
}
|
||
|
|
||
|
@Override // io.grpc.ServiceProviders.PriorityAccessor
|
||
|
public final boolean isAvailable(ServerProvider serverProvider) {
|
||
|
return serverProvider.isAvailable();
|
||
|
}
|
||
|
|
||
|
@Override // io.grpc.ServiceProviders.PriorityAccessor
|
||
|
public final int getPriority(ServerProvider serverProvider) {
|
||
|
return serverProvider.priority();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* loaded from: classes6.dex */
|
||
|
public static final class ProviderNotFoundException extends RuntimeException {
|
||
|
private static final long serialVersionUID = 1;
|
||
|
|
||
|
public ProviderNotFoundException(String str) {
|
||
|
super(str);
|
||
|
}
|
||
|
}
|
||
|
}
|