ServiceLoaderIterator.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  *   https://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing,
  13.  * software distributed under the License is distributed on an
  14.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15.  * KIND, either express or implied.  See the License for the
  16.  * specific language governing permissions and limitations
  17.  * under the License.
  18.  */

  19. package org.apache.commons.compress.utils;

  20. import java.util.Iterator;
  21. import java.util.NoSuchElementException;
  22. import java.util.ServiceConfigurationError;
  23. import java.util.ServiceLoader;

  24. /**
  25.  * Iterates all services for a given class through the standard {@link ServiceLoader} mechanism.
  26.  *
  27.  * @param <E> The service to load
  28.  * @since 1.13
  29.  * @deprecated No longer needed.
  30.  */
  31. @Deprecated
  32. public class ServiceLoaderIterator<E> implements Iterator<E> {

  33.     private E nextServiceLoader;
  34.     private final Class<E> service;
  35.     private final Iterator<E> serviceLoaderIterator;

  36.     /**
  37.      * Constructs a new instance.
  38.      *
  39.      * @param service The interface or abstract class representing the service.
  40.      */
  41.     public ServiceLoaderIterator(final Class<E> service) {
  42.         this(service, ClassLoader.getSystemClassLoader());
  43.     }

  44.     /**
  45.      * Constructs a new instance.
  46.      *
  47.      * @param service     The interface or abstract class representing the service.
  48.      * @param classLoader The class loader to be used to load provider-configuration files and provider classes, or {@code null} if the system class loader (or,
  49.      *                    failing that, the bootstrap class loader) is to be used
  50.      */
  51.     public ServiceLoaderIterator(final Class<E> service, final ClassLoader classLoader) {
  52.         this.service = service;
  53.         this.serviceLoaderIterator = ServiceLoader.load(service, classLoader).iterator();
  54.     }

  55.     @Override
  56.     public boolean hasNext() {
  57.         while (nextServiceLoader == null) {
  58.             try {
  59.                 if (!serviceLoaderIterator.hasNext()) {
  60.                     return false;
  61.                 }
  62.                 nextServiceLoader = serviceLoaderIterator.next();
  63.             } catch (final ServiceConfigurationError e) {
  64.                 if (e.getCause() instanceof SecurityException) {
  65.                     // Ignore security exceptions
  66.                     // TODO Log?
  67.                     continue;
  68.                 }
  69.                 throw e;
  70.             }
  71.         }
  72.         return true;
  73.     }

  74.     @Override
  75.     public E next() {
  76.         if (!hasNext()) {
  77.             throw new NoSuchElementException("No more elements for service " + service.getName());
  78.         }
  79.         final E tempNext = nextServiceLoader;
  80.         nextServiceLoader = null;
  81.         return tempNext;
  82.     }

  83.     @Override
  84.     public void remove() {
  85.         throw new UnsupportedOperationException("service=" + service.getName());
  86.     }

  87. }