1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
29
30
31
32
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 public ServiceLoaderIterator(final Class<E> service) {
42 this(service, ClassLoader.getSystemClassLoader());
43 }
44
45 public ServiceLoaderIterator(final Class<E> service, final ClassLoader classLoader) {
46 this.service = service;
47 this.serviceLoaderIterator = ServiceLoader.load(service, classLoader).iterator();
48 }
49
50 @Override
51 public boolean hasNext() {
52 while (nextServiceLoader == null) {
53 try {
54 if (!serviceLoaderIterator.hasNext()) {
55 return false;
56 }
57 nextServiceLoader = serviceLoaderIterator.next();
58 } catch (final ServiceConfigurationError e) {
59 if (e.getCause() instanceof SecurityException) {
60
61
62 continue;
63 }
64 throw e;
65 }
66 }
67 return true;
68 }
69
70 @Override
71 public E next() {
72 if (!hasNext()) {
73 throw new NoSuchElementException("No more elements for service " + service.getName());
74 }
75 final E tempNext = nextServiceLoader;
76 nextServiceLoader = null;
77 return tempNext;
78 }
79
80 @Override
81 public void remove() {
82 throw new UnsupportedOperationException("service=" + service.getName());
83 }
84
85 }