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.  * http://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.     public ServiceLoaderIterator(final Class<E> service) {
  37.         this(service, ClassLoader.getSystemClassLoader());
  38.     }

  39.     public ServiceLoaderIterator(final Class<E> service, final ClassLoader classLoader) {
  40.         this.service = service;
  41.         this.serviceLoaderIterator = ServiceLoader.load(service, classLoader).iterator();
  42.     }

  43.     @Override
  44.     public boolean hasNext() {
  45.         while (nextServiceLoader == null) {
  46.             try {
  47.                 if (!serviceLoaderIterator.hasNext()) {
  48.                     return false;
  49.                 }
  50.                 nextServiceLoader = serviceLoaderIterator.next();
  51.             } catch (final ServiceConfigurationError e) {
  52.                 if (e.getCause() instanceof SecurityException) {
  53.                     // Ignore security exceptions
  54.                     // TODO Log?
  55.                     continue;
  56.                 }
  57.                 throw e;
  58.             }
  59.         }
  60.         return true;
  61.     }

  62.     @Override
  63.     public E next() {
  64.         if (!hasNext()) {
  65.             throw new NoSuchElementException("No more elements for service " + service.getName());
  66.         }
  67.         final E tempNext = nextServiceLoader;
  68.         nextServiceLoader = null;
  69.         return tempNext;
  70.     }

  71.     @Override
  72.     public void remove() {
  73.         throw new UnsupportedOperationException("service=" + service.getName());
  74.     }

  75. }