use of java.util.ServiceConfigurationError in project hadoop by apache.
the class TestCluster method testProtocolProviderCreation.
@Test
@SuppressWarnings("unchecked")
public void testProtocolProviderCreation() throws Exception {
Iterator iterator = mock(Iterator.class);
when(iterator.hasNext()).thenReturn(true, true, true, true);
when(iterator.next()).thenReturn(getClientProtocolProvider()).thenThrow(new ServiceConfigurationError("Test error")).thenReturn(getClientProtocolProvider());
Iterable frameworkLoader = mock(Iterable.class);
when(frameworkLoader.iterator()).thenReturn(iterator);
Cluster.frameworkLoader = frameworkLoader;
Cluster testCluster = new Cluster(new Configuration());
// Check that we get the acceptable client, even after
// failure in instantiation.
assertNotNull("ClientProtocol is expected", testCluster.getClient());
// Check if we do not try to load the providers after a failure.
verify(iterator, times(2)).next();
}
use of java.util.ServiceConfigurationError in project lucene-solr by apache.
the class NamedSPILoader method reload.
/**
* Reloads the internal SPI list from the given {@link ClassLoader}.
* Changes to the service list are visible after the method ends, all
* iterators ({@link #iterator()},...) stay consistent.
*
* <p><b>NOTE:</b> Only new service providers are added, existing ones are
* never removed or replaced.
*
* <p><em>This method is expensive and should only be called for discovery
* of new service providers on the given classpath/classloader!</em>
*/
public void reload(ClassLoader classloader) {
Objects.requireNonNull(classloader, "classloader");
final LinkedHashMap<String, S> services = new LinkedHashMap<>(this.services);
final SPIClassIterator<S> loader = SPIClassIterator.get(clazz, classloader);
while (loader.hasNext()) {
final Class<? extends S> c = loader.next();
try {
final S service = c.newInstance();
final String name = service.getName();
// them used instead of others
if (!services.containsKey(name)) {
checkServiceName(name);
services.put(name, service);
}
} catch (Exception e) {
throw new ServiceConfigurationError("Cannot instantiate SPI class: " + c.getName(), e);
}
}
this.services = Collections.unmodifiableMap(services);
}
use of java.util.ServiceConfigurationError in project jdk8u_jdk by JetBrains.
the class SelectorProvider method loadProviderFromProperty.
private static boolean loadProviderFromProperty() {
String cn = System.getProperty("java.nio.channels.spi.SelectorProvider");
if (cn == null)
return false;
try {
Class<?> c = Class.forName(cn, true, ClassLoader.getSystemClassLoader());
provider = (SelectorProvider) c.newInstance();
return true;
} catch (ClassNotFoundException x) {
throw new ServiceConfigurationError(null, x);
} catch (IllegalAccessException x) {
throw new ServiceConfigurationError(null, x);
} catch (InstantiationException x) {
throw new ServiceConfigurationError(null, x);
} catch (SecurityException x) {
throw new ServiceConfigurationError(null, x);
}
}
use of java.util.ServiceConfigurationError in project jdk8u_jdk by JetBrains.
the class RowSetProvider method loadViaServiceLoader.
/**
* Use the ServiceLoader mechanism to load the default RowSetFactory
* @return default RowSetFactory Implementation
*/
private static RowSetFactory loadViaServiceLoader() throws SQLException {
RowSetFactory theFactory = null;
try {
trace("***in loadViaServiceLoader():");
for (RowSetFactory factory : ServiceLoader.load(javax.sql.rowset.RowSetFactory.class)) {
trace(" Loading done by the java.util.ServiceLoader :" + factory.getClass().getName());
theFactory = factory;
break;
}
} catch (ServiceConfigurationError e) {
throw new SQLException("RowSetFactory: Error locating RowSetFactory using Service " + "Loader API: " + e, e);
}
return theFactory;
}
use of java.util.ServiceConfigurationError in project jaxrs-api by eclipse-ee4j.
the class FactoryFinder method find.
/**
* Finds the implementation {@code Class} for the given factory name,
* or if that fails, finds the {@code Class} for the given fallback
* class name and create its instance. The arguments supplied MUST be
* used in order. If using the first argument is successful, the second
* one will not be used.
* <p>
* This method is package private so that this code can be shared.
*
* @param factoryId the name of the factory to find, which is
* a system property.
* @param fallbackClassName the implementation class name, which is
* to be used only if nothing else.
* is found; {@code null} to indicate that
* there is no fallback class name.
* @param service service to be found.
* @param <T> type of the service to be found.
* @return the instance of the specified service; may not be {@code null}.
* @throws ClassNotFoundException if the given class could not be found
* or could not be instantiated.
*/
static <T> Object find(final String factoryId, final String fallbackClassName, Class<T> service) throws ClassNotFoundException {
ClassLoader classLoader = getContextClassLoader();
try {
Iterator<T> iterator = ServiceLoader.load(service, FactoryFinder.getContextClassLoader()).iterator();
if (iterator.hasNext()) {
return iterator.next();
}
} catch (Exception | ServiceConfigurationError ex) {
LOGGER.log(Level.FINER, "Failed to load service " + factoryId + ".", ex);
}
try {
Iterator<T> iterator = ServiceLoader.load(service, FactoryFinder.class.getClassLoader()).iterator();
if (iterator.hasNext()) {
return iterator.next();
}
} catch (Exception | ServiceConfigurationError ex) {
LOGGER.log(Level.FINER, "Failed to load service " + factoryId + ".", ex);
}
// try to read from $java.home/lib/jaxrs.properties
FileInputStream inputStream = null;
String configFile = null;
try {
String javah = System.getProperty("java.home");
configFile = javah + File.separator + "lib" + File.separator + "jaxrs.properties";
File f = new File(configFile);
if (f.exists()) {
Properties props = new Properties();
inputStream = new FileInputStream(f);
props.load(inputStream);
String factoryClassName = props.getProperty(factoryId);
return newInstance(factoryClassName, classLoader);
}
} catch (Exception ex) {
LOGGER.log(Level.FINER, "Failed to load service " + factoryId + " from $java.home/lib/jaxrs.properties", ex);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException ex) {
LOGGER.log(Level.FINER, String.format("Error closing %s file.", configFile), ex);
}
}
}
// Use the system property
try {
String systemProp = System.getProperty(factoryId);
if (systemProp != null) {
return newInstance(systemProp, classLoader);
}
} catch (SecurityException se) {
LOGGER.log(Level.FINER, "Failed to load service " + factoryId + " from a system property", se);
}
if (fallbackClassName == null) {
throw new ClassNotFoundException("Provider for " + factoryId + " cannot be found", null);
}
return newInstance(fallbackClassName, classLoader);
}
Aggregations