OSGi service (collection) importer. This implementation creates a managed (read-only) collection of OSGi services. The returned collection automatically handles the OSGi services dynamics. If a new service that matches the configuration criteria appears, it will be automatically added to the collection. If a service that matches the criteria disappears (is unregistered), it will be automatically removed from the collection.
Due to the dynamic nature of OSGi services, the collection content can change at runtime, even during iteration. This implementation will correctly update all the collection
Iterator
s so they reflect the collection content. This approach (as opposed to the 'snapshot' strategy) prevents dealing with
dead services which can appear when imported services go down while iterating. This means that iterating while the collection is being changed is safe.
Note that the
Iterator
still has to be fulfilled meaning the
next()
method always obey the result of the previous
hasNext()
invocation:
hasNext() returned value | next() behaviour |
true | Always return a non-null value, even when the collection has shrunk as services when away. This means returning a proxy that will throw an exception on an invocation that requires the backing service to be present. |
false | per Iterator contract, NoSuchElementException is thrown. This applies even if other services are added to the collection. |
Due to the dynamic nature of OSGi,
hasNext()
invocation made on the same
Iterator
can return different values based on the services availability. However, as explained above,
next()
will always obey the result of the last
hasNext()
method.
Note: Even though the collection and its iterators communicate in a thread-safe manner, iterators themselves are not thread-safe. Concurrent access on the iterators should be properly synchronized. Due to the light nature of the iterators, consider creating a new one rather then reusing or sharing.
@see java.util.Iterator
@see java.util.Collection
@see java.util.List
@see java.util.Set
@see java.util.SortedSet
@author Costin Leau