Search in sources :

Example 31 with ServiceConfigurationError

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();
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) Iterator(java.util.Iterator) ServiceConfigurationError(java.util.ServiceConfigurationError) Test(org.junit.Test)

Example 32 with ServiceConfigurationError

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);
}
Also used : ServiceConfigurationError(java.util.ServiceConfigurationError) LinkedHashMap(java.util.LinkedHashMap)

Example 33 with ServiceConfigurationError

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);
    }
}
Also used : ServiceConfigurationError(java.util.ServiceConfigurationError)

Example 34 with ServiceConfigurationError

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;
}
Also used : SQLException(java.sql.SQLException) ServiceConfigurationError(java.util.ServiceConfigurationError)

Example 35 with ServiceConfigurationError

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);
}
Also used : ServiceConfigurationError(java.util.ServiceConfigurationError) IOException(java.io.IOException) Properties(java.util.Properties) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) File(java.io.File)

Aggregations

ServiceConfigurationError (java.util.ServiceConfigurationError)95 IOException (java.io.IOException)22 ArrayList (java.util.ArrayList)16 Iterator (java.util.Iterator)15 File (java.io.File)10 URL (java.net.URL)10 URLClassLoader (java.net.URLClassLoader)7 ServiceLoader (java.util.ServiceLoader)7 Test (org.junit.Test)7 BufferedReader (java.io.BufferedReader)6 InputStream (java.io.InputStream)6 InputStreamReader (java.io.InputStreamReader)6 NoSuchElementException (java.util.NoSuchElementException)6 CharsetProvider (java.nio.charset.spi.CharsetProvider)5 HashMap (java.util.HashMap)5 FileInputStream (java.io.FileInputStream)4 Path (java.nio.file.Path)4 SQLException (java.sql.SQLException)4 LinkedList (java.util.LinkedList)4 List (java.util.List)4