    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 * http://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
    029 * {@link ServiceLoader} mechanism.
    030 *
    031 * @param <E>
    032 *            The service to load
    033 * @since 1.13
    034 * @deprecated No longer needed.
    035 */
    036@Deprecated
    037public class ServiceLoaderIterator<E> implements Iterator<E> {
    038
    039    private E nextServiceLoader;
    040    private final Class<E> service;
    041    private final Iterator<E> serviceLoaderIterator;
    042
    043    public ServiceLoaderIterator(final Class<E> service) {
    044        this(service, ClassLoader.getSystemClassLoader());
    045    }
    046
    047    public ServiceLoaderIterator(final Class<E> service, final ClassLoader classLoader) {
    048        this.service = service;
    049        this.serviceLoaderIterator = ServiceLoader.load(service, classLoader).iterator();
    050    }
    051
    052    @Override
    053    public boolean hasNext() {
    054        while (nextServiceLoader == null) {
    055            try {
    056                if (!serviceLoaderIterator.hasNext()) {
    057                    return false;
    058                }
    059                nextServiceLoader = serviceLoaderIterator.next();
    060            } catch (final ServiceConfigurationError e) {
    061                if (e.getCause() instanceof SecurityException) {
    062                    // Ignore security exceptions
    063                    // TODO Log?
    064                    continue;
    065                }
    066                throw e;
    067            }
    068        }
    069        return true;
    070    }
    071
    072    @Override
    073    public E next() {
    074        if (!hasNext()) {
    075            throw new NoSuchElementException("No more elements for service " + service.getName());
    076        }
    077        final E tempNext = nextServiceLoader;
    078        nextServiceLoader = null;
    079        return tempNext;
    080    }
    081
    082    @Override
    083    public void remove() {
    084        throw new UnsupportedOperationException("service=" + service.getName());
    085    }
    086
    087}