Example usage for java.util ServiceLoader load

List of usage examples for java.util ServiceLoader load

Introduction

In this page you can find the example usage for java.util ServiceLoader load.

Prototype

@CallerSensitive
public static <S> ServiceLoader<S> load(ModuleLayer layer, Class<S> service) 

Source Link

Document

Creates a new service loader for the given service type to load service providers from modules in the given module layer and its ancestors.

Usage

From source file:org.apache.geode.internal.cache.xmlcache.CacheXmlParser.java

/**
 * Get delegate {@link XmlParser} for the given <code>namespaceUri</code>
 * /*from  www . j a  va2s .co  m*/
 * @param namespaceUri to find {@link XmlParser} for.
 * @return {@link XmlParser} if found, otherwise null.
 * @since GemFire 8.1
 */
// UnitTest CacheXmlParser.testGetDelegate()
private XmlParser getDelegate(final String namespaceUri) {
    XmlParser delegate = delegates.get(namespaceUri);
    if (null == delegate) {
        try {
            final ServiceLoader<XmlParser> serviceLoader = ServiceLoader.load(XmlParser.class,
                    ClassPathLoader.getLatestAsClassLoader());
            for (final XmlParser xmlParser : serviceLoader) {
                if (xmlParser.getNamespaceUri().equals(namespaceUri)) {
                    delegate = xmlParser;
                    delegate.setStack(stack);
                    delegate.setDocumentLocator(documentLocator);
                    delegates.put(xmlParser.getNamespaceUri(), xmlParser);
                    break;
                }
            }
        } catch (final Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
    return delegate;
}