package io.grpc; import com.google.common.base.Preconditions; import io.grpc.ManagedChannelProvider; 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.Level; import java.util.logging.Logger; /* loaded from: classes6.dex */ public final class ManagedChannelRegistry { private static ManagedChannelRegistry instance; private static final Logger logger = Logger.getLogger(ManagedChannelRegistry.class.getName()); private final LinkedHashSet allProviders = new LinkedHashSet<>(); private List effectiveProviders = Collections.emptyList(); public final void register(ManagedChannelProvider managedChannelProvider) { synchronized (this) { addProvider(managedChannelProvider); refreshProviders(); } } private void addProvider(ManagedChannelProvider managedChannelProvider) { synchronized (this) { Preconditions.checkArgument(managedChannelProvider.isAvailable(), "isAvailable() returned false"); this.allProviders.add(managedChannelProvider); } } public final void deregister(ManagedChannelProvider managedChannelProvider) { synchronized (this) { this.allProviders.remove(managedChannelProvider); refreshProviders(); } } private void refreshProviders() { synchronized (this) { ArrayList arrayList = new ArrayList(this.allProviders); Collections.sort(arrayList, Collections.reverseOrder(new Comparator(this) { // from class: io.grpc.ManagedChannelRegistry.1 final ManagedChannelRegistry this$0; { this.this$0 = this; } @Override // java.util.Comparator public int compare(ManagedChannelProvider managedChannelProvider, ManagedChannelProvider managedChannelProvider2) { return managedChannelProvider.priority() - managedChannelProvider2.priority(); } })); this.effectiveProviders = Collections.unmodifiableList(arrayList); } } public static ManagedChannelRegistry getDefaultRegistry() { ManagedChannelRegistry managedChannelRegistry; synchronized (ManagedChannelRegistry.class) { if (instance == null) { List loadAll = ServiceProviders.loadAll(ManagedChannelProvider.class, getHardCodedClasses(), ManagedChannelProvider.class.getClassLoader(), new ManagedChannelPriorityAccessor()); instance = new ManagedChannelRegistry(); for (ManagedChannelProvider managedChannelProvider : loadAll) { Logger logger2 = logger; StringBuilder sb = new StringBuilder(); sb.append("Service loader found "); sb.append(managedChannelProvider); logger2.fine(sb.toString()); if (managedChannelProvider.isAvailable()) { instance.addProvider(managedChannelProvider); } } instance.refreshProviders(); } managedChannelRegistry = instance; } return managedChannelRegistry; } final List providers() { List list; synchronized (this) { list = this.effectiveProviders; } return list; } /* JADX INFO: Access modifiers changed from: package-private */ public final ManagedChannelProvider provider() { List providers = providers(); if (providers.isEmpty()) { return null; } return providers.get(0); } static List> getHardCodedClasses() { ArrayList arrayList = new ArrayList(); try { arrayList.add(Class.forName("io.grpc.okhttp.OkHttpChannelProvider")); } catch (ClassNotFoundException e) { logger.log(Level.FINE, "Unable to find OkHttpChannelProvider", (Throwable) e); } try { arrayList.add(Class.forName("io.grpc.netty.NettyChannelProvider")); } catch (ClassNotFoundException e2) { logger.log(Level.FINE, "Unable to find NettyChannelProvider", (Throwable) e2); } return Collections.unmodifiableList(arrayList); } /* JADX INFO: Access modifiers changed from: package-private */ public final ManagedChannelBuilder newChannelBuilder(String str, ChannelCredentials channelCredentials) { if (providers().isEmpty()) { throw new ProviderNotFoundException("No functional channel service provider found. Try adding a dependency on the grpc-okhttp, grpc-netty, or grpc-netty-shaded artifact"); } StringBuilder sb = new StringBuilder(); for (ManagedChannelProvider managedChannelProvider : providers()) { ManagedChannelProvider.NewChannelBuilderResult newChannelBuilder = managedChannelProvider.newChannelBuilder(str, channelCredentials); if (newChannelBuilder.getChannelBuilder() != null) { return newChannelBuilder.getChannelBuilder(); } sb.append("; "); sb.append(managedChannelProvider.getClass().getName()); sb.append(": "); sb.append(newChannelBuilder.getError()); } throw new ProviderNotFoundException(sb.substring(2)); } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public static final class ManagedChannelPriorityAccessor implements ServiceProviders.PriorityAccessor { private ManagedChannelPriorityAccessor() { } @Override // io.grpc.ServiceProviders.PriorityAccessor public final boolean isAvailable(ManagedChannelProvider managedChannelProvider) { return managedChannelProvider.isAvailable(); } @Override // io.grpc.ServiceProviders.PriorityAccessor public final int getPriority(ManagedChannelProvider managedChannelProvider) { return managedChannelProvider.priority(); } } /* loaded from: classes6.dex */ public static final class ProviderNotFoundException extends RuntimeException { private static final long serialVersionUID = 1; public ProviderNotFoundException(String str) { super(str); } } }