165 lines
6.4 KiB
Java
165 lines
6.4 KiB
Java
|
package io.grpc;
|
||
|
|
||
|
import com.google.common.base.Preconditions;
|
||
|
import com.google.common.collect.ImmutableMap;
|
||
|
import io.grpc.NameResolver;
|
||
|
import io.grpc.ServiceProviders;
|
||
|
import java.net.URI;
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.Collections;
|
||
|
import java.util.HashMap;
|
||
|
import java.util.Iterator;
|
||
|
import java.util.LinkedHashSet;
|
||
|
import java.util.List;
|
||
|
import java.util.Map;
|
||
|
import java.util.logging.Level;
|
||
|
import java.util.logging.Logger;
|
||
|
import org.bouncycastle.asn1.cmp.PKIFailureInfo;
|
||
|
|
||
|
/* loaded from: classes6.dex */
|
||
|
public final class NameResolverRegistry {
|
||
|
private static final String UNKNOWN_SCHEME = "unknown";
|
||
|
private static NameResolverRegistry instance;
|
||
|
private static final Logger logger = Logger.getLogger(NameResolverRegistry.class.getName());
|
||
|
private final NameResolver.Factory factory = new NameResolverFactory();
|
||
|
private String defaultScheme = "unknown";
|
||
|
private final LinkedHashSet<NameResolverProvider> allProviders = new LinkedHashSet<>();
|
||
|
private ImmutableMap<String, NameResolverProvider> effectiveProviders = ImmutableMap.of();
|
||
|
|
||
|
public final void register(NameResolverProvider nameResolverProvider) {
|
||
|
synchronized (this) {
|
||
|
addProvider(nameResolverProvider);
|
||
|
refreshProviders();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void addProvider(NameResolverProvider nameResolverProvider) {
|
||
|
synchronized (this) {
|
||
|
Preconditions.checkArgument(nameResolverProvider.isAvailable(), "isAvailable() returned false");
|
||
|
this.allProviders.add(nameResolverProvider);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public final void deregister(NameResolverProvider nameResolverProvider) {
|
||
|
synchronized (this) {
|
||
|
this.allProviders.remove(nameResolverProvider);
|
||
|
refreshProviders();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void refreshProviders() {
|
||
|
synchronized (this) {
|
||
|
HashMap hashMap = new HashMap();
|
||
|
String str = "unknown";
|
||
|
Iterator<NameResolverProvider> it = this.allProviders.iterator();
|
||
|
int i = PKIFailureInfo.systemUnavail;
|
||
|
while (it.hasNext()) {
|
||
|
NameResolverProvider next = it.next();
|
||
|
String scheme = next.getScheme();
|
||
|
NameResolverProvider nameResolverProvider = (NameResolverProvider) hashMap.get(scheme);
|
||
|
if (nameResolverProvider == null || nameResolverProvider.priority() < next.priority()) {
|
||
|
hashMap.put(scheme, next);
|
||
|
}
|
||
|
if (i < next.priority()) {
|
||
|
i = next.priority();
|
||
|
str = next.getScheme();
|
||
|
}
|
||
|
}
|
||
|
this.effectiveProviders = ImmutableMap.copyOf((Map) hashMap);
|
||
|
this.defaultScheme = str;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static NameResolverRegistry getDefaultRegistry() {
|
||
|
NameResolverRegistry nameResolverRegistry;
|
||
|
synchronized (NameResolverRegistry.class) {
|
||
|
if (instance == null) {
|
||
|
List<NameResolverProvider> loadAll = ServiceProviders.loadAll(NameResolverProvider.class, getHardCodedClasses(), NameResolverProvider.class.getClassLoader(), new NameResolverPriorityAccessor());
|
||
|
if (loadAll.isEmpty()) {
|
||
|
logger.warning("No NameResolverProviders found via ServiceLoader, including for DNS. This is probably due to a broken build. If using ProGuard, check your configuration");
|
||
|
}
|
||
|
instance = new NameResolverRegistry();
|
||
|
for (NameResolverProvider nameResolverProvider : loadAll) {
|
||
|
Logger logger2 = logger;
|
||
|
StringBuilder sb = new StringBuilder();
|
||
|
sb.append("Service loader found ");
|
||
|
sb.append(nameResolverProvider);
|
||
|
logger2.fine(sb.toString());
|
||
|
if (nameResolverProvider.isAvailable()) {
|
||
|
instance.addProvider(nameResolverProvider);
|
||
|
}
|
||
|
}
|
||
|
instance.refreshProviders();
|
||
|
}
|
||
|
nameResolverRegistry = instance;
|
||
|
}
|
||
|
return nameResolverRegistry;
|
||
|
}
|
||
|
|
||
|
final Map<String, NameResolverProvider> providers() {
|
||
|
ImmutableMap<String, NameResolverProvider> immutableMap;
|
||
|
synchronized (this) {
|
||
|
immutableMap = this.effectiveProviders;
|
||
|
}
|
||
|
return immutableMap;
|
||
|
}
|
||
|
|
||
|
static List<Class<?>> getHardCodedClasses() {
|
||
|
ArrayList arrayList = new ArrayList();
|
||
|
try {
|
||
|
arrayList.add(Class.forName("io.grpc.internal.DnsNameResolverProvider"));
|
||
|
} catch (ClassNotFoundException e) {
|
||
|
logger.log(Level.FINE, "Unable to find DNS NameResolver", (Throwable) e);
|
||
|
}
|
||
|
return Collections.unmodifiableList(arrayList);
|
||
|
}
|
||
|
|
||
|
/* loaded from: classes6.dex */
|
||
|
final class NameResolverFactory extends NameResolver.Factory {
|
||
|
final NameResolverRegistry this$0;
|
||
|
|
||
|
private NameResolverFactory(NameResolverRegistry nameResolverRegistry) {
|
||
|
this.this$0 = nameResolverRegistry;
|
||
|
}
|
||
|
|
||
|
@Override // io.grpc.NameResolver.Factory
|
||
|
public final NameResolver newNameResolver(URI uri, NameResolver.Args args) {
|
||
|
NameResolverProvider nameResolverProvider = this.this$0.providers().get(uri.getScheme());
|
||
|
if (nameResolverProvider == null) {
|
||
|
return null;
|
||
|
}
|
||
|
return nameResolverProvider.newNameResolver(uri, args);
|
||
|
}
|
||
|
|
||
|
@Override // io.grpc.NameResolver.Factory
|
||
|
public final String getDefaultScheme() {
|
||
|
String str;
|
||
|
synchronized (this.this$0) {
|
||
|
str = this.this$0.defaultScheme;
|
||
|
}
|
||
|
return str;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
/* loaded from: classes6.dex */
|
||
|
public static final class NameResolverPriorityAccessor implements ServiceProviders.PriorityAccessor<NameResolverProvider> {
|
||
|
private NameResolverPriorityAccessor() {
|
||
|
}
|
||
|
|
||
|
@Override // io.grpc.ServiceProviders.PriorityAccessor
|
||
|
public final boolean isAvailable(NameResolverProvider nameResolverProvider) {
|
||
|
return nameResolverProvider.isAvailable();
|
||
|
}
|
||
|
|
||
|
@Override // io.grpc.ServiceProviders.PriorityAccessor
|
||
|
public final int getPriority(NameResolverProvider nameResolverProvider) {
|
||
|
return nameResolverProvider.priority();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public final NameResolver.Factory asFactory() {
|
||
|
return this.factory;
|
||
|
}
|
||
|
}
|