Java Code Examples for java.util.ServiceLoader#loadInstalled()

The following examples show how to use java.util.ServiceLoader#loadInstalled() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IIORegistry.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private void registerInstalledProviders() {
    /*
      We need to load installed providers
      in the privileged mode in order to
      be able read corresponding jar files even if
      file read capability is restricted (like the
      applet context case).
     */
    PrivilegedAction<Object> doRegistration =
        new PrivilegedAction<Object>() {
            public Object run() {
                Iterator<Class<?>> categories = getCategories();
                while (categories.hasNext()) {
                    @SuppressWarnings("unchecked")
                    Class<IIOServiceProvider> c = (Class<IIOServiceProvider>)categories.next();
                    for (IIOServiceProvider p : ServiceLoader.loadInstalled(c)) {
                        registerServiceProvider(p);
                    }
                }
                return this;
            }
        };

    AccessController.doPrivileged(doRegistration);
}
 
Example 2
Source File: IIORegistry.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
private void registerInstalledProviders() {
    /*
      We need to load installed providers from the
      system classpath (typically the <code>lib/ext</code>
      directory in in the Java installation directory)
      in the privileged mode in order to
      be able read corresponding jar files even if
      file read capability is restricted (like the
      applet context case).
     */
    PrivilegedAction doRegistration =
        new PrivilegedAction() {
            public Object run() {
                Iterator categories = getCategories();
                while (categories.hasNext()) {
                    Class<IIOServiceProvider> c = (Class)categories.next();
                    for (IIOServiceProvider p : ServiceLoader.loadInstalled(c)) {
                        registerServiceProvider(p);
                    }
                }
                return this;
            }
        };

    AccessController.doPrivileged(doRegistration);
}
 
Example 3
Source File: IIORegistry.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private void registerInstalledProviders() {
    /*
      We need to load installed providers from the
      system classpath (typically the <code>lib/ext</code>
      directory in in the Java installation directory)
      in the privileged mode in order to
      be able read corresponding jar files even if
      file read capability is restricted (like the
      applet context case).
     */
    PrivilegedAction doRegistration =
        new PrivilegedAction() {
            public Object run() {
                Iterator categories = getCategories();
                while (categories.hasNext()) {
                    Class<IIOServiceProvider> c = (Class)categories.next();
                    for (IIOServiceProvider p : ServiceLoader.loadInstalled(c)) {
                        registerServiceProvider(p);
                    }
                }
                return this;
            }
        };

    AccessController.doPrivileged(doRegistration);
}
 
Example 4
Source File: IIORegistry.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private void registerInstalledProviders() {
    /*
      We need to load installed providers from the
      system classpath (typically the <code>lib/ext</code>
      directory in in the Java installation directory)
      in the privileged mode in order to
      be able read corresponding jar files even if
      file read capability is restricted (like the
      applet context case).
     */
    PrivilegedAction doRegistration =
        new PrivilegedAction() {
            public Object run() {
                Iterator categories = getCategories();
                while (categories.hasNext()) {
                    Class<IIOServiceProvider> c = (Class)categories.next();
                    for (IIOServiceProvider p : ServiceLoader.loadInstalled(c)) {
                        registerServiceProvider(p);
                    }
                }
                return this;
            }
        };

    AccessController.doPrivileged(doRegistration);
}
 
Example 5
Source File: SRE.java    From sarl with Apache License 2.0 6 votes vote down vote up
/** Replies all the installed SRE into the class path.
 *
 * @param onlyInstalledInJRE indicates if the services will be considered only into the libraries that are
 *     installed into the JRE. If {@code true}, only the libraries into the JRE will be considered and
 *     the application libraries will be ignored. If {@code false}, the application libraries will be
 *     considered as well.
 * @return the installed SRE.
 */
@Pure
public static ServiceLoader<SREBootstrap> getServiceLoader(boolean onlyInstalledInJRE) {
	synchronized (SRE.class) {
		ServiceLoader<SREBootstrap> sl = loader == null ? null : loader.get();
		if (sl == null) {
		    if (onlyInstalledInJRE) {
				sl = ServiceLoader.loadInstalled(SREBootstrap.class);
			} else {
				sl = ServiceLoader.load(SREBootstrap.class);
			}
			loader = new SoftReference<>(sl);
		}
		return sl;
	}
}
 
Example 6
Source File: IIORegistry.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
private void registerInstalledProviders() {
    /*
      We need to load installed providers
      in the privileged mode in order to
      be able read corresponding jar files even if
      file read capability is restricted (like the
      applet context case).
     */
    PrivilegedAction<Object> doRegistration =
        new PrivilegedAction<Object>() {
            public Object run() {
                Iterator<Class<?>> categories = getCategories();
                while (categories.hasNext()) {
                    @SuppressWarnings("unchecked")
                    Class<IIOServiceProvider> c = (Class<IIOServiceProvider>)categories.next();
                    for (IIOServiceProvider p : ServiceLoader.loadInstalled(c)) {
                        registerServiceProvider(p);
                    }
                }
                return this;
            }
        };

    AccessController.doPrivileged(doRegistration);
}
 
Example 7
Source File: IIORegistry.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
private void registerInstalledProviders() {
    /*
      We need to load installed providers from the
      system classpath (typically the <code>lib/ext</code>
      directory in in the Java installation directory)
      in the privileged mode in order to
      be able read corresponding jar files even if
      file read capability is restricted (like the
      applet context case).
     */
    PrivilegedAction doRegistration =
        new PrivilegedAction() {
            public Object run() {
                Iterator categories = getCategories();
                while (categories.hasNext()) {
                    Class<IIOServiceProvider> c = (Class)categories.next();
                    for (IIOServiceProvider p : ServiceLoader.loadInstalled(c)) {
                        registerServiceProvider(p);
                    }
                }
                return this;
            }
        };

    AccessController.doPrivileged(doRegistration);
}
 
Example 8
Source File: IIORegistry.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
private void registerInstalledProviders() {
    /*
      We need to load installed providers from the
      system classpath (typically the <code>lib/ext</code>
      directory in in the Java installation directory)
      in the privileged mode in order to
      be able read corresponding jar files even if
      file read capability is restricted (like the
      applet context case).
     */
    PrivilegedAction doRegistration =
        new PrivilegedAction() {
            public Object run() {
                Iterator categories = getCategories();
                while (categories.hasNext()) {
                    Class<IIOServiceProvider> c = (Class)categories.next();
                    for (IIOServiceProvider p : ServiceLoader.loadInstalled(c)) {
                        registerServiceProvider(p);
                    }
                }
                return this;
            }
        };

    AccessController.doPrivileged(doRegistration);
}
 
Example 9
Source File: ScriptEngineManager.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private ServiceLoader<ScriptEngineFactory> getServiceLoader(final ClassLoader loader) {
    if (loader != null) {
        return ServiceLoader.load(ScriptEngineFactory.class, loader);
    } else {
        return ServiceLoader.loadInstalled(ScriptEngineFactory.class);
    }
}
 
Example 10
Source File: ScriptEngineManager.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private ServiceLoader<ScriptEngineFactory> getServiceLoader(final ClassLoader loader) {
    if (loader != null) {
        return ServiceLoader.load(ScriptEngineFactory.class, loader);
    } else {
        return ServiceLoader.loadInstalled(ScriptEngineFactory.class);
    }
}
 
Example 11
Source File: ScriptEngineManager.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private ServiceLoader<ScriptEngineFactory> getServiceLoader(final ClassLoader loader) {
    if (loader != null) {
        return ServiceLoader.load(ScriptEngineFactory.class, loader);
    } else {
        return ServiceLoader.loadInstalled(ScriptEngineFactory.class);
    }
}
 
Example 12
Source File: ScriptEngineManager.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private ServiceLoader<ScriptEngineFactory> getServiceLoader(final ClassLoader loader) {
    if (loader != null) {
        return ServiceLoader.load(ScriptEngineFactory.class, loader);
    } else {
        return ServiceLoader.loadInstalled(ScriptEngineFactory.class);
    }
}
 
Example 13
Source File: ScriptEngineManager.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
private ServiceLoader<ScriptEngineFactory> getServiceLoader(final ClassLoader loader) {
    if (loader != null) {
        return ServiceLoader.load(ScriptEngineFactory.class, loader);
    } else {
        return ServiceLoader.loadInstalled(ScriptEngineFactory.class);
    }
}
 
Example 14
Source File: Bundles.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
private static ResourceBundle loadBundleOf(String baseName,
                                           Locale targetLocale,
                                           Strategy strategy) {
    Objects.requireNonNull(baseName);
    Objects.requireNonNull(targetLocale);
    Objects.requireNonNull(strategy);

    CacheKey cacheKey = new CacheKey(baseName, targetLocale);

    ResourceBundle bundle = null;

    // Quick lookup of the cache.
    BundleReference bundleRef = cacheList.get(cacheKey);
    if (bundleRef != null) {
        bundle = bundleRef.get();
    }

    // If this bundle and all of its parents are valid,
    // then return this bundle.
    if (isValidBundle(bundle)) {
        return bundle;
    }

    // Get the providers for loading the "leaf" bundle (i.e., bundle for
    // targetLocale). If no providers are required for the bundle,
    // none of its parents will require providers.
    Class<? extends ResourceBundleProvider> type
            = strategy.getResourceBundleProviderType(baseName, targetLocale);
    if (type != null) {
        @SuppressWarnings("unchecked")
        ServiceLoader<ResourceBundleProvider> providers
            = (ServiceLoader<ResourceBundleProvider>) ServiceLoader.loadInstalled(type);
        cacheKey.setProviders(providers);
    }

    List<Locale> candidateLocales = strategy.getCandidateLocales(baseName, targetLocale);
    bundle = findBundleOf(cacheKey, strategy, baseName, candidateLocales, 0);
    if (bundle == null) {
        throwMissingResourceException(baseName, targetLocale, cacheKey.getCause());
    }
    return bundle;
}
 
Example 15
Source File: Bundles.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private static ResourceBundle loadBundleOf(String baseName,
                                           Locale targetLocale,
                                           Strategy strategy) {
    Objects.requireNonNull(baseName);
    Objects.requireNonNull(targetLocale);
    Objects.requireNonNull(strategy);

    CacheKey cacheKey = new CacheKey(baseName, targetLocale);

    ResourceBundle bundle = null;

    // Quick lookup of the cache.
    BundleReference bundleRef = cacheList.get(cacheKey);
    if (bundleRef != null) {
        bundle = bundleRef.get();
    }

    // If this bundle and all of its parents are valid,
    // then return this bundle.
    if (isValidBundle(bundle)) {
        return bundle;
    }

    // Get the providers for loading the "leaf" bundle (i.e., bundle for
    // targetLocale). If no providers are required for the bundle,
    // none of its parents will require providers.
    Class<? extends ResourceBundleProvider> type
            = strategy.getResourceBundleProviderType(baseName, targetLocale);
    if (type != null) {
        @SuppressWarnings("unchecked")
        ServiceLoader<ResourceBundleProvider> providers
            = (ServiceLoader<ResourceBundleProvider>) ServiceLoader.loadInstalled(type);
        cacheKey.setProviders(providers);
    }

    List<Locale> candidateLocales = strategy.getCandidateLocales(baseName, targetLocale);
    bundle = findBundleOf(cacheKey, strategy, baseName, candidateLocales, 0);
    if (bundle == null) {
        throwMissingResourceException(baseName, targetLocale, cacheKey.getCause());
    }
    return bundle;
}
 
Example 16
Source File: ModulesTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = { NullPointerException.class })
public void testLoadNull5() {
    ServiceLoader.loadInstalled(null);
}
 
Example 17
Source File: JMXConnectorFactory.java    From openjdk-jdk9 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates a connector from a provider loaded from the ServiceLoader.
 * <p>
 * The pair (P,C) will be either one of: <br>
 * a. (JMXConnectorProvider, JMXConnector) or <br>
 * b. (JMXConnectorServerProvider, JMXConnectorServer)
 *
 * @param providerClass The service type for which an implementation
 *        should be looked up from the {@code ServiceLoader}. This will
 *        be either {@code JMXConnectorProvider.class} or
 *        {@code JMXConnectorServerProvider.class}
 *
 * @param loader The ClassLoader to use when looking up an implementation
 *        of the service. If null, then only installed services will be
 *        considered.
 *
 * @param url The JMXServiceURL of the connector for which a provider is
 *        requested.
 *
 * @param filter A filter used to exclude or return provider
 *        implementations. Typically the filter will either exclude
 *        system services (system default implementations) or only
 *        retain those.
 *        This can allow to first look for custom implementations (e.g.
 *        deployed on the CLASSPATH with META-INF/services) and
 *        then only default to system implementations.
 *
 * @param factory A functional factory that can attempt to create an
 *        instance of connector {@code C} from a provider {@code P}.
 *        Typically, this is a simple wrapper over {@code
 *        JMXConnectorProvider::newJMXConnector} or {@code
 *        JMXConnectorProviderServer::newJMXConnectorServer}.
 *
 * @throws IOException if {@code C} could not be instantiated, and
 *         at least one provider {@code P} threw an exception that wasn't a
 *         {@code MalformedURLException} or a {@code JMProviderException}.
 *
 * @throws JMXProviderException if a provider {@code P} for the protocol in
 *         <code>url</code> was found, but couldn't create the connector
 *         {@code C} for some reason.
 *
 * @return an instance of {@code C} if a provider {@code P} was found from
 *         which one could be instantiated, {@code null} otherwise.
 */
static <P,C> C getConnectorAsService(Class<P> providerClass,
                                     ClassLoader loader,
                                     JMXServiceURL url,
                                     Predicate<Provider<?>> filter,
                                     ConnectorFactory<P,C> factory)
    throws IOException {

    // sanity check
    if (JMXConnectorProvider.class != providerClass
        && JMXConnectorServerProvider.class != providerClass) {
        // should never happen
        throw new InternalError("Unsupported service interface: "
                                + providerClass.getName());
    }

    ServiceLoader<P> serviceLoader = loader == null
            ? ServiceLoader.loadInstalled(providerClass)
            : ServiceLoader.load(providerClass, loader);
    Stream<Provider<P>> stream = serviceLoader.stream().filter(filter);
    ProviderFinder<P,C> finder = new ProviderFinder<>(factory, url);

    try {
        stream.filter(finder).findFirst();
        return finder.get();
    } catch (UncheckedIOException e) {
        if (e.getCause() instanceof JMXProviderException) {
            throw (JMXProviderException) e.getCause();
        } else {
            throw e;
        }
    }
}
 
Example 18
Source File: NPE.java    From dragonwell8_jdk with GNU General Public License v2.0 votes vote down vote up
void run() { ServiceLoader.loadInstalled(null); } 
Example 19
Source File: NPE.java    From openjdk-jdk9 with GNU General Public License v2.0 votes vote down vote up
void run() { ServiceLoader.loadInstalled(null); } 
Example 20
Source File: NPE.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 votes vote down vote up
void run() { ServiceLoader.loadInstalled(null); }