Skip to content

Commit

Permalink
Don't let certain errors fail SPI loading (Azure#41992)
Browse files Browse the repository at this point in the history
  • Loading branch information
alzimmermsft authored and mssfang committed Oct 21, 2024
1 parent 45adb94 commit ae70d1d
Showing 1 changed file with 30 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;
import java.util.function.Function;
import java.util.function.Supplier;

import static io.clientcore.core.implementation.util.ImplUtils.isNullOrEmpty;

/**
* Helper class that unifies SPI instances creation.
*
Expand Down Expand Up @@ -45,37 +44,40 @@ public Providers(Class<TProvider> providerClass, String defaultImplementationNam
// classloading differently (OSGi, Spring and others) and don't depend on the
// System classloader to load TProvider classes.
ServiceLoader<TProvider> serviceLoader = ServiceLoader.load(providerClass, Providers.class.getClassLoader());
availableProviders = new HashMap<>();
// Use the first provider found in the service loader iterator.
Iterator<TProvider> it = serviceLoader.iterator();

if (it.hasNext()) {
defaultProvider = it.next();
defaultProviderName = defaultProvider.getClass().getName();
availableProviders.put(defaultProviderName, defaultProvider);

LOGGER.atVerbose()
.addKeyValue("providerName", defaultProviderName)
.addKeyValue("providerClass", providerClass.getName())
.log("Found provider.");
} else {
defaultProvider = null;
defaultProviderName = null;
}
TProvider defaultProvider = null;
String defaultProviderName = null;
this.availableProviders = new HashMap<>();

// Load all provider instances.
Iterator<TProvider> it = serviceLoader.iterator();
while (it.hasNext()) {
TProvider additionalProvider = it.next();
String additionalProviderName = additionalProvider.getClass().getName();

availableProviders.put(additionalProviderName, additionalProvider);
LOGGER.atVerbose()
.addKeyValue("providerName", additionalProviderName)
.log("Additional provider found on the classpath");
try {
TProvider provider = it.next();
String providerName = provider.getClass().getName();
availableProviders.put(providerName, provider);
if (defaultProvider == null) {
defaultProvider = provider;
defaultProviderName = providerName;
LOGGER.atVerbose()
.addKeyValue("providerName", providerName)
.addKeyValue("providerClass", providerClass.getName())
.log("Loaded default provider.");
} else {
LOGGER.atVerbose()
.addKeyValue("providerName", providerName)
.log("Additional provider found on the classpath");
}
} catch (LinkageError | ServiceConfigurationError error) {
LOGGER.atWarning().log("Failed to load a provider instance.", error);
}
}

defaultImplementation = defaultImplementationName;
noDefaultImplementation = isNullOrEmpty(defaultImplementation);
noProviderMessage = noProviderErrorMessage;
this.defaultProvider = defaultProvider;
this.defaultProviderName = defaultProviderName;
this.defaultImplementation = defaultImplementationName;
this.noDefaultImplementation = ImplUtils.isNullOrEmpty(defaultImplementation);
this.noProviderMessage = noProviderErrorMessage;
}

private String formatNoSpecificProviderErrorMessage(String selectedImplementation) {
Expand Down

0 comments on commit ae70d1d

Please sign in to comment.