    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one
    003 * or more contributor license agreements.  See the NOTICE file
    004 * distributed with this work for additional information
    005 * regarding copyright ownership.  The ASF licenses this file
    006 * to you under the Apache License, Version 2.0 (the
    007 * "License"); you may not use this file except in compliance
    008 * with the License.  You may obtain a copy of the License at
    009 *
    010 *   https://www.apache.org/licenses/LICENSE-2.0
    011 *
    012 * Unless required by applicable law or agreed to in writing,
    013 * software distributed under the License is distributed on an
    014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    015 * KIND, either express or implied.  See the License for the
    016 * specific language governing permissions and limitations
    017 * under the License.
    018 */
    019
    020package org.apache.commons.compress.utils;
    021
    022import java.util.Iterator;
    023import java.util.NoSuchElementException;
    024import java.util.ServiceConfigurationError;
    025import java.util.ServiceLoader;
    026
    027/**
    028 * Iterates all services for a given class through the standard {@link ServiceLoader} mechanism.
    029 *
    030 * @param <E> The service to load
    031 * @since 1.13
    032 * @deprecated No longer needed.
    033 */
    034@Deprecated
    035public class ServiceLoaderIterator<E> implements Iterator<E> {
    036
    037    private E nextServiceLoader;
    038    private final Class<E> service;
    039    private final Iterator<E> serviceLoaderIterator;
    040
    041    /**
    042     * Constructs a new instance.
    043     *
    044     * @param service The interface or abstract class representing the service.
    045     */
    046    public ServiceLoaderIterator(final Class<E> service) {
    047        this(service, ClassLoader.getSystemClassLoader());
    048    }
    049
    050    /**
    051     * Constructs a new instance.
    052     *
    053     * @param service     The interface or abstract class representing the service.
    054     * @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,
    055     *                    failing that, the bootstrap class loader) is to be used
    056     */
    057    public ServiceLoaderIterator(final Class<E> service, final ClassLoader classLoader) {
    058        this.service = service;
    059        this.serviceLoaderIterator = ServiceLoader.load(service, classLoader).iterator();
    060    }
    061
    062    @Override
    063    public boolean hasNext() {
    064        while (nextServiceLoader == null) {
    065            try {
    066                if (!serviceLoaderIterator.hasNext()) {
    067                    return false;
    068                }
    069                nextServiceLoader = serviceLoaderIterator.next();
    070            } catch (final ServiceConfigurationError e) {
    071                if (e.getCause() instanceof SecurityException) {
    072                    // Ignore security exceptions
    073                    // TODO Log?
    074                    continue;
    075                }
    076                throw e;
    077            }
    078        }
    079        return true;
    080    }
    081
    082    @Override
    083    public E next() {
    084        if (!hasNext()) {
    085            throw new NoSuchElementException("No more elements for service " + service.getName());
    086        }
    087        final E tempNext = nextServiceLoader;
    088        nextServiceLoader = null;
    089        return tempNext;
    090    }
    091
    092    @Override
    093    public void remove() {
    094        throw new UnsupportedOperationException("service=" + service.getName());
    095    }
    096
    097}