jdk/src/java.base/share/classes/java/util/ServiceLoader.java
changeset 43712 5dfd0950317c
parent 42338 a60f280f803c
child 44003 dc26a57d2570
equal deleted inserted replaced
43619:dc9102c475f3 43712:5dfd0950317c
   117  *
   117  *
   118  * <p> An application or library using this loading facility and developed
   118  * <p> An application or library using this loading facility and developed
   119  * and deployed as an explicit module must have an appropriate <i>uses</i>
   119  * and deployed as an explicit module must have an appropriate <i>uses</i>
   120  * clause in its <i>module descriptor</i> to declare that the module uses
   120  * clause in its <i>module descriptor</i> to declare that the module uses
   121  * implementations of the service. A corresponding requirement is that a
   121  * implementations of the service. A corresponding requirement is that a
   122  * provider deployed as a named module must have an appropriate
   122  * provider deployed as an explicit module must have an appropriate
   123  * <i>provides</i> clause in its module descriptor to declare that the module
   123  * <i>provides</i> clause in its module descriptor to declare that the module
   124  * provides an implementation of the service. The <i>uses</i> and
   124  * provides an implementation of the service. The <i>uses</i> and
   125  * <i>provides</i> allow consumers of a service to be <i>linked</i> to modules
   125  * <i>provides</i> allow consumers of a service to be <i>linked</i> to modules
   126  * containing providers of the service.
   126  * containing providers of the service.
   127  *
   127  *
   201  *     all providers in that layer are located, irrespective of their class
   201  *     all providers in that layer are located, irrespective of their class
   202  *     loader. The ordering of modules defined to the same class loader, or the
   202  *     loader. The ordering of modules defined to the same class loader, or the
   203  *     ordering of modules in a layer, is not defined. </li>
   203  *     ordering of modules in a layer, is not defined. </li>
   204  *
   204  *
   205  *     <li> If a named module declares more than one provider then the providers
   205  *     <li> If a named module declares more than one provider then the providers
   206  *     are located in the order that they appear in the {@code provides} table of
   206  *     are located in the iteration order of the {@link
   207  *     the {@code Module} class file attribute ({@code module-info.class}). </li>
   207  *     java.lang.module.ModuleDescriptor.Provides#providers() providers} list.
       
   208  *     Providers added dynamically by instrumentation agents ({@link
       
   209  *     java.lang.instrument.Instrumentation#redefineModule redefineModule})
       
   210  *     are always located after providers declared by the module. </li>
   208  *
   211  *
   209  *     <li> When locating providers in unnamed modules then the ordering is
   212  *     <li> When locating providers in unnamed modules then the ordering is
   210  *     based on the order that the class loader's {@link
   213  *     based on the order that the class loader's {@link
   211  *     ClassLoader#getResources(String) ClassLoader.getResources(String)}
   214  *     ClassLoader#getResources(String) ClassLoader.getResources(String)}
   212  *     method finds the service configuration files. </li>
   215  *     method finds the service configuration files. </li>
   333  * @param  <S>
   336  * @param  <S>
   334  *         The type of the service to be loaded by this loader
   337  *         The type of the service to be loaded by this loader
   335  *
   338  *
   336  * @author Mark Reinhold
   339  * @author Mark Reinhold
   337  * @since 1.6
   340  * @since 1.6
       
   341  * @revised 9
       
   342  * @spec JPMS
   338  */
   343  */
   339 
   344 
   340 public final class ServiceLoader<S>
   345 public final class ServiceLoader<S>
   341     implements Iterable<S>
   346     implements Iterable<S>
   342 {
   347 {
   384      * to select or filter on the provider class without instantiating the
   389      * to select or filter on the provider class without instantiating the
   385      * provider. </p>
   390      * provider. </p>
   386      *
   391      *
   387      * @param  <S> The service type
   392      * @param  <S> The service type
   388      * @since 9
   393      * @since 9
       
   394      * @spec JPMS
   389      */
   395      */
   390     public static interface Provider<S> extends Supplier<S> {
   396     public static interface Provider<S> extends Supplier<S> {
   391         /**
   397         /**
   392          * Returns the provider type. There is no guarantee that this type is
   398          * Returns the provider type. There is no guarantee that this type is
   393          * accessible or that it has a public no-args constructor. The {@link
   399          * accessible or that it has a public no-args constructor. The {@link
   925             if (loader == null) {
   931             if (loader == null) {
   926                 catalog = BootLoader.getServicesCatalog();
   932                 catalog = BootLoader.getServicesCatalog();
   927             } else {
   933             } else {
   928                 catalog = ServicesCatalog.getServicesCatalogOrNull(loader);
   934                 catalog = ServicesCatalog.getServicesCatalogOrNull(loader);
   929             }
   935             }
   930             Stream<ServiceProvider> stream1;
   936             List<ServiceProvider> providers;
   931             if (catalog == null) {
   937             if (catalog == null) {
   932                 stream1 = Stream.empty();
   938                 providers = List.of();
   933             } else {
   939             } else {
   934                 stream1 = catalog.findServices(serviceName).stream();
   940                 providers = catalog.findServices(serviceName);
   935             }
   941             }
   936 
   942 
   937             // modules in custom layers that define modules to the class loader
   943             // modules in custom layers that define modules to the class loader
   938             Stream<ServiceProvider> stream2;
       
   939             if (loader == null) {
   944             if (loader == null) {
   940                 stream2 = Stream.empty();
   945                 return providers.iterator();
   941             } else {
   946             } else {
       
   947                 List<ServiceProvider> allProviders = new ArrayList<>(providers);
   942                 Layer bootLayer = Layer.boot();
   948                 Layer bootLayer = Layer.boot();
   943                 stream2 = JLRM_ACCESS.layers(loader)
   949                 Iterator<Layer> iterator = JLRM_ACCESS.layers(loader).iterator();
   944                         .filter(l -> (l != bootLayer))
   950                 while (iterator.hasNext()) {
   945                         .map(l -> providers(l))
   951                     Layer layer = iterator.next();
   946                         .flatMap(List::stream);
   952                     if (layer != bootLayer) {
   947             }
   953                         allProviders.addAll(providers(layer));
   948 
   954                     }
   949             return Stream.concat(stream1, stream2).iterator();
   955                 }
       
   956                 return allProviders.iterator();
       
   957             }
   950         }
   958         }
   951 
   959 
   952         @Override
   960         @Override
   953         public boolean hasNext() {
   961         public boolean hasNext() {
   954             // already have the next provider cached
   962             // already have the next provider cached
  1212      * Invoking its {@link java.util.Iterator#remove() remove} method will
  1220      * Invoking its {@link java.util.Iterator#remove() remove} method will
  1213      * cause an {@link UnsupportedOperationException} to be thrown.
  1221      * cause an {@link UnsupportedOperationException} to be thrown.
  1214      *
  1222      *
  1215      * @return  An iterator that lazily loads providers for this loader's
  1223      * @return  An iterator that lazily loads providers for this loader's
  1216      *          service
  1224      *          service
       
  1225      *
       
  1226      * @revised 9
       
  1227      * @spec JPMS
  1217      */
  1228      */
  1218     public Iterator<S> iterator() {
  1229     public Iterator<S> iterator() {
  1219 
  1230 
  1220         // create lookup iterator if needed
  1231         // create lookup iterator if needed
  1221         if (lookupIterator1 == null) {
  1232         if (lookupIterator1 == null) {
  1277      * thrown when locating the provider then it is wrapped with a {@code
  1288      * thrown when locating the provider then it is wrapped with a {@code
  1278      * ServiceConfigurationError} and thrown by whatever method caused the
  1289      * ServiceConfigurationError} and thrown by whatever method caused the
  1279      * provider to be loaded. </p>
  1290      * provider to be loaded. </p>
  1280      *
  1291      *
  1281      * <p> If this loader's provider caches are cleared by invoking the {@link
  1292      * <p> If this loader's provider caches are cleared by invoking the {@link
  1282      * #reload() reload} method then existing streams for this service
  1293      * #reload() reload} method then existing streams for this service loader
  1283      * loader should be discarded. </p>
  1294      * should be discarded. The returned stream's source {@code Spliterator} is
       
  1295      * <em>fail-fast</em> and will throw {@link ConcurrentModificationException}
       
  1296      * if the provider cache has been cleared. </p>
  1284      *
  1297      *
  1285      * <p> The following examples demonstrate usage. The first example
  1298      * <p> The following examples demonstrate usage. The first example
  1286      * creates a stream of providers, the second example is the same except
  1299      * creates a stream of providers, the second example is the same except
  1287      * that it sorts the providers by provider class name (and so locate all
  1300      * that it sorts the providers by provider class name (and so locate all
  1288      * providers).
  1301      * providers).
  1298      * }</pre>
  1311      * }</pre>
  1299      *
  1312      *
  1300      * @return  A stream that lazily loads providers for this loader's service
  1313      * @return  A stream that lazily loads providers for this loader's service
  1301      *
  1314      *
  1302      * @since 9
  1315      * @since 9
       
  1316      * @spec JPMS
  1303      */
  1317      */
  1304     public Stream<Provider<S>> stream() {
  1318     public Stream<Provider<S>> stream() {
  1305         // use cached providers as the source when all providers loaded
  1319         // use cached providers as the source when all providers loaded
  1306         if (loadedAllProviders) {
  1320         if (loadedAllProviders) {
  1307             return loadedProviders.stream();
  1321             return loadedProviders.stream();
  1412      *
  1426      *
  1413      * @throws ServiceConfigurationError
  1427      * @throws ServiceConfigurationError
  1414      *         if the service type is not accessible to the caller or the
  1428      *         if the service type is not accessible to the caller or the
  1415      *         caller is in an explicit module and its module descriptor does
  1429      *         caller is in an explicit module and its module descriptor does
  1416      *         not declare that it uses {@code service}
  1430      *         not declare that it uses {@code service}
       
  1431      *
       
  1432      * @revised 9
       
  1433      * @spec JPMS
  1417      */
  1434      */
  1418     @CallerSensitive
  1435     @CallerSensitive
  1419     public static <S> ServiceLoader<S> load(Class<S> service,
  1436     public static <S> ServiceLoader<S> load(Class<S> service,
  1420                                             ClassLoader loader)
  1437                                             ClassLoader loader)
  1421     {
  1438     {
  1455      *
  1472      *
  1456      * @throws ServiceConfigurationError
  1473      * @throws ServiceConfigurationError
  1457      *         if the service type is not accessible to the caller or the
  1474      *         if the service type is not accessible to the caller or the
  1458      *         caller is in an explicit module and its module descriptor does
  1475      *         caller is in an explicit module and its module descriptor does
  1459      *         not declare that it uses {@code service}
  1476      *         not declare that it uses {@code service}
       
  1477      *
       
  1478      * @revised 9
       
  1479      * @spec JPMS
  1460      */
  1480      */
  1461     @CallerSensitive
  1481     @CallerSensitive
  1462     public static <S> ServiceLoader<S> load(Class<S> service) {
  1482     public static <S> ServiceLoader<S> load(Class<S> service) {
  1463         ClassLoader cl = Thread.currentThread().getContextClassLoader();
  1483         ClassLoader cl = Thread.currentThread().getContextClassLoader();
  1464         return new ServiceLoader<>(Reflection.getCallerClass(), service, cl);
  1484         return new ServiceLoader<>(Reflection.getCallerClass(), service, cl);
  1488      *
  1508      *
  1489      * @throws ServiceConfigurationError
  1509      * @throws ServiceConfigurationError
  1490      *         if the service type is not accessible to the caller or the
  1510      *         if the service type is not accessible to the caller or the
  1491      *         caller is in an explicit module and its module descriptor does
  1511      *         caller is in an explicit module and its module descriptor does
  1492      *         not declare that it uses {@code service}
  1512      *         not declare that it uses {@code service}
       
  1513      *
       
  1514      * @revised 9
       
  1515      * @spec JPMS
  1493      */
  1516      */
  1494     @CallerSensitive
  1517     @CallerSensitive
  1495     public static <S> ServiceLoader<S> loadInstalled(Class<S> service) {
  1518     public static <S> ServiceLoader<S> loadInstalled(Class<S> service) {
  1496         ClassLoader cl = ClassLoader.getPlatformClassLoader();
  1519         ClassLoader cl = ClassLoader.getPlatformClassLoader();
  1497         return new ServiceLoader<>(Reflection.getCallerClass(), service, cl);
  1520         return new ServiceLoader<>(Reflection.getCallerClass(), service, cl);
  1520      *         if the service type is not accessible to the caller or the
  1543      *         if the service type is not accessible to the caller or the
  1521      *         caller is in an explicit module and its module descriptor does
  1544      *         caller is in an explicit module and its module descriptor does
  1522      *         not declare that it uses {@code service}
  1545      *         not declare that it uses {@code service}
  1523      *
  1546      *
  1524      * @since 9
  1547      * @since 9
       
  1548      * @spec JPMS
  1525      */
  1549      */
  1526     @CallerSensitive
  1550     @CallerSensitive
  1527     public static <S> ServiceLoader<S> load(Layer layer, Class<S> service) {
  1551     public static <S> ServiceLoader<S> load(Layer layer, Class<S> service) {
  1528         return new ServiceLoader<>(Reflection.getCallerClass(), layer, service);
  1552         return new ServiceLoader<>(Reflection.getCallerClass(), layer, service);
  1529     }
  1553     }
  1549      *         appropriate static factory method or constructor, can't be
  1573      *         appropriate static factory method or constructor, can't be
  1550      *         assigned to the service type, or if any other kind of exception
  1574      *         assigned to the service type, or if any other kind of exception
  1551      *         or error is thrown when locating or instantiating the provider.
  1575      *         or error is thrown when locating or instantiating the provider.
  1552      *
  1576      *
  1553      * @since 9
  1577      * @since 9
       
  1578      * @spec JPMS
  1554      */
  1579      */
  1555     public Optional<S> findFirst() {
  1580     public Optional<S> findFirst() {
  1556         Iterator<S> iterator = iterator();
  1581         Iterator<S> iterator = iterator();
  1557         if (iterator.hasNext()) {
  1582         if (iterator.hasNext()) {
  1558             return Optional.of(iterator.next());
  1583             return Optional.of(iterator.next());