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
20 package org.apache.commons.compress.utils;
21
22 import java.util.Iterator;
23 import java.util.NoSuchElementException;
24 import java.util.ServiceConfigurationError;
25 import java.util.ServiceLoader;
26
27 /**
28 * Iterates all services for a given class through the standard {@link ServiceLoader} mechanism.
29 *
30 * @param <E> The service to load
31 * @since 1.13
32 * @deprecated No longer needed.
33 */
34 @Deprecated
35 public class ServiceLoaderIterator<E> implements Iterator<E> {
36
37 private E nextServiceLoader;
38 private final Class<E> service;
39 private final Iterator<E> serviceLoaderIterator;
40
41 /**
42 * Constructs a new instance.
43 *
44 * @param service The interface or abstract class representing the service.
45 */
46 public ServiceLoaderIterator(final Class<E> service) {
47 this(service, ClassLoader.getSystemClassLoader());
48 }
49
50 /**
51 * Constructs a new instance.
52 *
53 * @param service The interface or abstract class representing the service.
54 * @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,
55 * failing that, the bootstrap class loader) is to be used
56 */
57 public ServiceLoaderIterator(final Class<E> service, final ClassLoader classLoader) {
58 this.service = service;
59 this.serviceLoaderIterator = ServiceLoader.load(service, classLoader).iterator();
60 }
61
62 @Override
63 public boolean hasNext() {
64 while (nextServiceLoader == null) {
65 try {
66 if (!serviceLoaderIterator.hasNext()) {
67 return false;
68 }
69 nextServiceLoader = serviceLoaderIterator.next();
70 } catch (final ServiceConfigurationError e) {
71 if (e.getCause() instanceof SecurityException) {
72 // Ignore security exceptions
73 // TODO Log?
74 continue;
75 }
76 throw e;
77 }
78 }
79 return true;
80 }
81
82 @Override
83 public E next() {
84 if (!hasNext()) {
85 throw new NoSuchElementException("No more elements for service " + service.getName());
86 }
87 final E tempNext = nextServiceLoader;
88 nextServiceLoader = null;
89 return tempNext;
90 }
91
92 @Override
93 public void remove() {
94 throw new UnsupportedOperationException("service=" + service.getName());
95 }
96
97 }