1 package org.apache.onami.spi.core;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.util.Iterator;
23 import java.util.StringTokenizer;
24
25 /**
26 * Lazy-loading iterator over the discovered classes defined in the Java System Properties.
27 *
28 * @param <S> The service type being loaded.
29 */
30 final class PropertyServiceClassIterator<S>
31 extends AbstractServiceClassIterator<S>
32 {
33
34 /**
35 * The service names separator.
36 */
37 private static final String DEFAULT_SEPARATOR = ",";
38
39 /**
40 * The Provider names defined in the Service as Java System Property.
41 */
42 private final String systemServiceNames;
43
44 /**
45 * Flag to mark that iterator has already been consumed.
46 */
47 private boolean consumed = false;
48
49 /**
50 * Creates a new Provider classes Iterator.
51 *
52 * @param service The Service being loaded.
53 * @param classLoader The ClassLoader used to load Provider classes.
54 * @param systemServiceNames the Provider names defined in the Service as Java System Property.
55 */
56 public PropertyServiceClassIterator( Class<S> service, ClassLoader classLoader, String systemServiceNames )
57 {
58 super( service, classLoader );
59 this.systemServiceNames = systemServiceNames;
60 }
61
62 /**
63 * {@inheritDoc}
64 */
65 @Override
66 protected boolean hasMorePendingNames()
67 {
68 return !consumed;
69 }
70
71 /**
72 * {@inheritDoc}
73 */
74 @Override
75 protected Iterator<String> getPendingNames()
76 {
77 consumed = true;
78 return new Iterator<String>()
79 {
80
81 private final StringTokenizer tokenizer = new StringTokenizer( systemServiceNames, DEFAULT_SEPARATOR );
82
83 public boolean hasNext()
84 {
85 return tokenizer.hasMoreTokens();
86 }
87
88 public String next()
89 {
90 return tokenizer.nextToken();
91 }
92
93 public void remove()
94 {
95 throw new UnsupportedOperationException();
96 }
97
98 };
99 }
100
101 }