View Javadoc
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   * http://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      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                      // Ignore security exceptions
61                      // TODO Log?
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  }