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
42
43
44
45
46 public ServiceLoaderIterator(final Class<E> service) {
47 this(service, ClassLoader.getSystemClassLoader());
48 }
49
50
51
52
53
54
55
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
73
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 }