java.util.ServiceLoader.Provider#java.util.ServiceConfigurationError源码实例Demo

下面列出了java.util.ServiceLoader.Provider#java.util.ServiceConfigurationError 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: openjdk-jdk8u   文件: FactoryFinder.java
private static <T> T findServiceProvider(final Class<T> type)
        throws DatatypeConfigurationException
{
    try {
        return AccessController.doPrivileged(new PrivilegedAction<T>() {
            public T run() {
                final ServiceLoader<T> serviceLoader = ServiceLoader.load(type);
                final Iterator<T> iterator = serviceLoader.iterator();
                if (iterator.hasNext()) {
                    return iterator.next();
                } else {
                    return null;
                }
            }
        });
    } catch(ServiceConfigurationError e) {
        final DatatypeConfigurationException error =
                new DatatypeConfigurationException(
                    "Provider for " + type + " cannot be found", e);
        throw error;
    }
}
 
源代码2 项目: allure1   文件: ServiceLoaderUtils.java
/**
 * Invoke to find all services for given service type using specified class loader
 *
 * @param classLoader specified class loader
 * @param serviceType given service type
 * @return List of found services
 */
public static <T> List<T> load(ClassLoader classLoader, Class<T> serviceType) {
    List<T> foundServices = new ArrayList<>();
    Iterator<T> iterator = ServiceLoader.load(serviceType, classLoader).iterator();

    while (checkHasNextSafely(iterator)) {
        try {
            T item = iterator.next();
            foundServices.add(item);
            LOGGER.debug(String.format("Found %s [%s]", serviceType.getSimpleName(), item.toString()));
        } catch (ServiceConfigurationError e) {
            LOGGER.trace("Can't find services using Java SPI", e);
            LOGGER.error(e.getMessage());
        }
    }
    return foundServices;
}
 
源代码3 项目: jdk8u-jdk   文件: AsynchronousChannelProvider.java
private static AsynchronousChannelProvider loadProviderAsService() {
    ServiceLoader<AsynchronousChannelProvider> sl =
        ServiceLoader.load(AsynchronousChannelProvider.class,
                           ClassLoader.getSystemClassLoader());
    Iterator<AsynchronousChannelProvider> i = sl.iterator();
    for (;;) {
        try {
            return (i.hasNext()) ? i.next() : null;
        } catch (ServiceConfigurationError sce) {
            if (sce.getCause() instanceof SecurityException) {
                // Ignore the security exception, try the next provider
                continue;
            }
            throw sce;
        }
    }
}
 
源代码4 项目: j2objc   文件: AsynchronousChannelProvider.java
private static AsynchronousChannelProvider loadProviderAsService() {
    ServiceLoader<AsynchronousChannelProvider> sl =
        ServiceLoader.load(AsynchronousChannelProvider.class,
                           ClassLoader.getSystemClassLoader());
    Iterator<AsynchronousChannelProvider> i = sl.iterator();
    for (;;) {
        try {
            return (i.hasNext()) ? i.next() : null;
        } catch (ServiceConfigurationError sce) {
            if (sce.getCause() instanceof SecurityException) {
                // Ignore the security exception, try the next provider
                continue;
            }
            throw sce;
        }
    }
}
 
源代码5 项目: Java8CN   文件: FactoryFinder.java
private static <T> T findServiceProvider(final Class<T> type)
        throws DatatypeConfigurationException
{
    try {
        return AccessController.doPrivileged(new PrivilegedAction<T>() {
            public T run() {
                final ServiceLoader<T> serviceLoader = ServiceLoader.load(type);
                final Iterator<T> iterator = serviceLoader.iterator();
                if (iterator.hasNext()) {
                    return iterator.next();
                } else {
                    return null;
                }
            }
        });
    } catch(ServiceConfigurationError e) {
        final DatatypeConfigurationException error =
                new DatatypeConfigurationException(
                    "Provider for " + type + " cannot be found", e);
        throw error;
    }
}
 
private static AsynchronousChannelProvider loadProviderAsService() {
    ServiceLoader<AsynchronousChannelProvider> sl =
        ServiceLoader.load(AsynchronousChannelProvider.class,
                           ClassLoader.getSystemClassLoader());
    Iterator<AsynchronousChannelProvider> i = sl.iterator();
    for (;;) {
        try {
            return (i.hasNext()) ? i.next() : null;
        } catch (ServiceConfigurationError sce) {
            if (sce.getCause() instanceof SecurityException) {
                // Ignore the security exception, try the next provider
                continue;
            }
            throw sce;
        }
    }
}
 
源代码7 项目: openjdk-jdk8u   文件: RowSetProvider.java
/**
 * Use the ServiceLoader mechanism to load  the default RowSetFactory
 * @return default RowSetFactory Implementation
 */
static private 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;

}
 
源代码8 项目: CLIFF   文件: EntityExtractorService.java
@SuppressWarnings("rawtypes")
public ExtractedEntities extractEntitiesFromSentences(Map[] sentences, boolean manuallyReplaceDemonyms, String langauge){
    ExtractedEntities e = new ExtractedEntities();
    try {
        Iterator<EntityExtractor> extractors = loader.iterator();
        while (extractors != null && extractors.hasNext()) {
            EntityExtractor currentExtractor = extractors.next();
            ExtractedEntities e2 = currentExtractor.extractEntitiesFromSentences(sentences, manuallyReplaceDemonyms, langauge);
            e.merge(e2);
        }
    } catch (ServiceConfigurationError serviceError) {
        e = null;
        serviceError.printStackTrace();
    }
    return e;
}
 
源代码9 项目: openjdk-jdk8u   文件: HttpServerProvider.java
private static boolean loadProviderFromProperty() {
    String cn = System.getProperty("com.sun.net.httpserver.HttpServerProvider");
    if (cn == null)
        return false;
    try {
        Class<?> c = Class.forName(cn, true,
                                   ClassLoader.getSystemClassLoader());
        provider = (HttpServerProvider)c.newInstance();
        return true;
    } catch (ClassNotFoundException |
             IllegalAccessException |
             InstantiationException |
             SecurityException x) {
        throw new ServiceConfigurationError(null, x);
    }
}
 
源代码10 项目: flink   文件: ReporterSetup.java
private static Map<String, MetricReporterFactory> loadAvailableReporterFactories(@Nullable PluginManager pluginManager) {
	final Map<String, MetricReporterFactory> reporterFactories = new HashMap<>(2);
	final Iterator<MetricReporterFactory> factoryIterator = getAllReporterFactories(pluginManager);
	// do not use streams or for-each loops here because they do not allow catching individual ServiceConfigurationErrors
	// such an error might be caused if the META-INF/services contains an entry to a non-existing factory class
	while (factoryIterator.hasNext()) {
		try {
			MetricReporterFactory factory = factoryIterator.next();
			String factoryClassName = factory.getClass().getName();
			MetricReporterFactory existingFactory = reporterFactories.get(factoryClassName);
			if (existingFactory == null) {
				reporterFactories.put(factoryClassName, factory);
				LOG.debug("Found reporter factory {} at {} ",
					factoryClassName,
					new File(factory.getClass().getProtectionDomain().getCodeSource().getLocation().toURI()).getCanonicalPath());
			} else {
				LOG.warn("Multiple implementations of the same reporter were found in 'lib' and/or 'plugins' directories for {}. It is recommended to remove redundant reporter JARs to resolve used versions' ambiguity.", factoryClassName);
			}
		} catch (Exception | ServiceConfigurationError e) {
			LOG.warn("Error while loading reporter factory.", e);
		}
	}

	return Collections.unmodifiableMap(reporterFactories);
}
 
源代码11 项目: jdk8u-jdk   文件: SelectorProvider.java
private static boolean loadProviderAsService() {

        ServiceLoader<SelectorProvider> sl =
            ServiceLoader.load(SelectorProvider.class,
                               ClassLoader.getSystemClassLoader());
        Iterator<SelectorProvider> i = sl.iterator();
        for (;;) {
            try {
                if (!i.hasNext())
                    return false;
                provider = i.next();
                return true;
            } catch (ServiceConfigurationError sce) {
                if (sce.getCause() instanceof SecurityException) {
                    // Ignore the security exception, try the next provider
                    continue;
                }
                throw sce;
            }
        }
    }
 
private static AsynchronousChannelProvider loadProviderAsService() {
    ServiceLoader<AsynchronousChannelProvider> sl =
        ServiceLoader.load(AsynchronousChannelProvider.class,
                           ClassLoader.getSystemClassLoader());
    Iterator<AsynchronousChannelProvider> i = sl.iterator();
    for (;;) {
        try {
            return (i.hasNext()) ? i.next() : null;
        } catch (ServiceConfigurationError sce) {
            if (sce.getCause() instanceof SecurityException) {
                // Ignore the security exception, try the next provider
                continue;
            }
            throw sce;
        }
    }
}
 
private static AsynchronousChannelProvider loadProviderAsService() {
    ServiceLoader<AsynchronousChannelProvider> sl =
        ServiceLoader.load(AsynchronousChannelProvider.class,
                           ClassLoader.getSystemClassLoader());
    Iterator<AsynchronousChannelProvider> i = sl.iterator();
    for (;;) {
        try {
            return (i.hasNext()) ? i.next() : null;
        } catch (ServiceConfigurationError sce) {
            if (sce.getCause() instanceof SecurityException) {
                // Ignore the security exception, try the next provider
                continue;
            }
            throw sce;
        }
    }
}
 
源代码14 项目: dragonwell8_jdk   文件: FtpClientProvider.java
private static boolean loadProviderFromProperty() {
    String cm = System.getProperty("sun.net.ftpClientProvider");
    if (cm == null) {
        return false;
    }
    try {
        Class<?> c = Class.forName(cm, true, null);
        provider = (FtpClientProvider) c.newInstance();
        return true;
    } catch (ClassNotFoundException |
             IllegalAccessException |
             InstantiationException |
             SecurityException x) {
        throw new ServiceConfigurationError(x.toString());
    }
}
 
源代码15 项目: dragonwell8_jdk   文件: HttpServerProvider.java
private static boolean loadProviderFromProperty() {
    String cn = System.getProperty("com.sun.net.httpserver.HttpServerProvider");
    if (cn == null)
        return false;
    try {
        Class<?> c = Class.forName(cn, true,
                                   ClassLoader.getSystemClassLoader());
        provider = (HttpServerProvider)c.newInstance();
        return true;
    } catch (ClassNotFoundException |
             IllegalAccessException |
             InstantiationException |
             SecurityException x) {
        throw new ServiceConfigurationError(null, x);
    }
}
 
源代码16 项目: jdk8u_jdk   文件: HttpServerProvider.java
private static boolean loadProviderFromProperty() {
    String cn = System.getProperty("com.sun.net.httpserver.HttpServerProvider");
    if (cn == null)
        return false;
    try {
        Class<?> c = Class.forName(cn, true,
                                   ClassLoader.getSystemClassLoader());
        provider = (HttpServerProvider)c.newInstance();
        return true;
    } catch (ClassNotFoundException |
             IllegalAccessException |
             InstantiationException |
             SecurityException x) {
        throw new ServiceConfigurationError(null, x);
    }
}
 
private boolean needClassLoader(String procNames, Iterable<? extends File> workingpath) {
    if (procNames != null)
        return true;

    URL[] urls = new URL[1];
    for(File pathElement : workingpath) {
        try {
            urls[0] = pathElement.toURI().toURL();
            if (ServiceProxy.hasService(Processor.class, urls))
                return true;
        } catch (MalformedURLException ex) {
            throw new AssertionError(ex);
        }
        catch (ServiceProxy.ServiceConfigurationError e) {
            log.error(Errors.ProcBadConfigFile(e.getLocalizedMessage()));
            return true;
        }
    }

    return false;
}
 
源代码18 项目: openjdk-jdk8u-backup   文件: RowSetProvider.java
/**
 * Use the ServiceLoader mechanism to load  the default RowSetFactory
 * @return default RowSetFactory Implementation
 */
static private 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;

}
 
源代码19 项目: openjdk-jdk9   文件: BadProvidersTest.java
@Test(dataProvider = "badfactories",
      expectedExceptions = ServiceConfigurationError.class)
public void testBadFactory(String testName, String ignore) throws Exception {
    Path mods = compileTest(TEST1_MODULE);

    // compile the bad factory
    Path source = BADFACTORIES_DIR.resolve(testName);
    Path output = Files.createTempDirectory(USER_DIR, "tmp");
    boolean compiled = CompilerUtils.compile(source, output);
    assertTrue(compiled);

    // copy the compiled class into the module
    Path classFile = Paths.get("p", "ProviderFactory.class");
    Files.copy(output.resolve(classFile),
               mods.resolve(TEST1_MODULE).resolve(classFile),
               StandardCopyOption.REPLACE_EXISTING);

    // load providers and instantiate each one
    loadProviders(mods, TEST1_MODULE).forEach(Provider::get);
}
 
源代码20 项目: TencentKona-8   文件: SelectorProvider.java
private static boolean loadProviderAsService() {

        ServiceLoader<SelectorProvider> sl =
            ServiceLoader.load(SelectorProvider.class,
                               ClassLoader.getSystemClassLoader());
        Iterator<SelectorProvider> i = sl.iterator();
        for (;;) {
            try {
                if (!i.hasNext())
                    return false;
                provider = i.next();
                return true;
            } catch (ServiceConfigurationError sce) {
                if (sce.getCause() instanceof SecurityException) {
                    // Ignore the security exception, try the next provider
                    continue;
                }
                throw sce;
            }
        }
    }
 
private static AsynchronousChannelProvider loadProviderAsService() {
    ServiceLoader<AsynchronousChannelProvider> sl =
        ServiceLoader.load(AsynchronousChannelProvider.class,
                           ClassLoader.getSystemClassLoader());
    Iterator<AsynchronousChannelProvider> i = sl.iterator();
    for (;;) {
        try {
            return (i.hasNext()) ? i.next() : null;
        } catch (ServiceConfigurationError sce) {
            if (sce.getCause() instanceof SecurityException) {
                // Ignore the security exception, try the next provider
                continue;
            }
            throw sce;
        }
    }
}
 
源代码22 项目: allure1   文件: ServiceLoaderUtils.java
/**
 * Check {@link java.util.Iterator#hasNext()} safely.
 *
 * @param iterator specified Iterator to check hasNext
 * @return true if {@link java.util.Iterator#hasNext()} checked successfully, false otherwise.
 */
public static boolean checkHasNextSafely(Iterator iterator) {
    try {
        /* Throw a ServiceConfigurationError if a provider-configuration file violates the specified format,
        or if it names a provider class that cannot be found and instantiated, or if the result of
        instantiating the class is not assignable to the service type, or if any other kind of exception
        or error is thrown as the next provider is located and instantiated.
        @see http://docs.oracle.com/javase/7/docs/api/java/util/ServiceLoader.html#iterator()
        */
        return iterator.hasNext();
    } catch (Exception | ServiceConfigurationError e) {
        LOGGER.trace("Can't load some service using Java SPI", e);
        LOGGER.error(e.getMessage());
        return false;
    }
}
 
源代码23 项目: hottub   文件: AsynchronousChannelProvider.java
private static AsynchronousChannelProvider loadProviderAsService() {
    ServiceLoader<AsynchronousChannelProvider> sl =
        ServiceLoader.load(AsynchronousChannelProvider.class,
                           ClassLoader.getSystemClassLoader());
    Iterator<AsynchronousChannelProvider> i = sl.iterator();
    for (;;) {
        try {
            return (i.hasNext()) ? i.next() : null;
        } catch (ServiceConfigurationError sce) {
            if (sce.getCause() instanceof SecurityException) {
                // Ignore the security exception, try the next provider
                continue;
            }
            throw sce;
        }
    }
}
 
源代码24 项目: TencentKona-8   文件: RowSetProvider.java
/**
 * Use the ServiceLoader mechanism to load  the default RowSetFactory
 * @return default RowSetFactory Implementation
 */
static private 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;

}
 
源代码25 项目: jdk8u-dev-jdk   文件: HttpServerProvider.java
private static boolean loadProviderAsService() {
    Iterator<HttpServerProvider> i =
        ServiceLoader.load(HttpServerProvider.class,
                           ClassLoader.getSystemClassLoader())
            .iterator();
    for (;;) {
        try {
            if (!i.hasNext())
                return false;
            provider = i.next();
            return true;
        } catch (ServiceConfigurationError sce) {
            if (sce.getCause() instanceof SecurityException) {
                // Ignore the security exception, try the next provider
                continue;
            }
            throw sce;
        }
    }
}
 
源代码26 项目: jdk8u-jdk   文件: HttpServerProvider.java
private static boolean loadProviderAsService() {
    Iterator<HttpServerProvider> i =
        ServiceLoader.load(HttpServerProvider.class,
                           ClassLoader.getSystemClassLoader())
            .iterator();
    for (;;) {
        try {
            if (!i.hasNext())
                return false;
            provider = i.next();
            return true;
        } catch (ServiceConfigurationError sce) {
            if (sce.getCause() instanceof SecurityException) {
                // Ignore the security exception, try the next provider
                continue;
            }
            throw sce;
        }
    }
}
 
源代码27 项目: openjdk-8   文件: RowSetProvider.java
/**
 * Use the ServiceLoader mechanism to load  the default RowSetFactory
 * @return default RowSetFactory Implementation
 */
static private 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;

}
 
源代码28 项目: jdk8u-dev-jdk   文件: SelectorProvider.java
private static boolean loadProviderAsService() {

        ServiceLoader<SelectorProvider> sl =
            ServiceLoader.load(SelectorProvider.class,
                               ClassLoader.getSystemClassLoader());
        Iterator<SelectorProvider> i = sl.iterator();
        for (;;) {
            try {
                if (!i.hasNext())
                    return false;
                provider = i.next();
                return true;
            } catch (ServiceConfigurationError sce) {
                if (sce.getCause() instanceof SecurityException) {
                    // Ignore the security exception, try the next provider
                    continue;
                }
                throw sce;
            }
        }
    }
 
源代码29 项目: Java8CN   文件: SelectorProvider.java
private static boolean loadProviderAsService() {

        ServiceLoader<SelectorProvider> sl =
            ServiceLoader.load(SelectorProvider.class,
                               ClassLoader.getSystemClassLoader());
        Iterator<SelectorProvider> i = sl.iterator();
        for (;;) {
            try {
                if (!i.hasNext())
                    return false;
                provider = i.next();
                return true;
            } catch (ServiceConfigurationError sce) {
                if (sce.getCause() instanceof SecurityException) {
                    // Ignore the security exception, try the next provider
                    continue;
                }
                throw sce;
            }
        }
    }
 
源代码30 项目: jdk8u60   文件: SelectorProvider.java
private static boolean loadProviderAsService() {

        ServiceLoader<SelectorProvider> sl =
            ServiceLoader.load(SelectorProvider.class,
                               ClassLoader.getSystemClassLoader());
        Iterator<SelectorProvider> i = sl.iterator();
        for (;;) {
            try {
                if (!i.hasNext())
                    return false;
                provider = i.next();
                return true;
            } catch (ServiceConfigurationError sce) {
                if (sce.getCause() instanceof SecurityException) {
                    // Ignore the security exception, try the next provider
                    continue;
                }
                throw sce;
            }
        }
    }