org.springframework.osgi.service.importer.support
Class OsgiServiceCollectionProxyFactoryBean

java.lang.Object
  extended by org.springframework.osgi.service.importer.support.AbstractDependableServiceImporter
      extended by org.springframework.osgi.service.importer.support.AbstractOsgiServiceImportFactoryBean
          extended by org.springframework.osgi.service.importer.support.OsgiServiceCollectionProxyFactoryBean
All Implemented Interfaces:
BeanClassLoaderAware, DisposableBean, FactoryBean, InitializingBean, SmartFactoryBean, BundleContextAware, DependableServiceImporter, ServiceDependency

public class OsgiServiceCollectionProxyFactoryBean
extends AbstractOsgiServiceImportFactoryBean

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 Iterators 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.

Author:
Costin Leau
See Also:
Iterator, Collection, List, Set, SortedSet

Constructor Summary
OsgiServiceCollectionProxyFactoryBean()
           
 
Method Summary
 void afterPropertiesSet()
           
 Class getObjectType()
           
 boolean isSatisfied()
          Indicates if the dependency is satisified or not.
 void setCardinality(Cardinality cardinality)
          Sets the importer cardinality (0..1, 1..1, 0..N, or 1..N).
 void setCollectionType(CollectionType collectionType)
          Sets the collection type this FactoryBean will produce.
 void setComparator(Comparator comparator)
          Sets the (optional) comparator for ordering the resulting collection.
 
Methods inherited from class org.springframework.osgi.service.importer.support.AbstractOsgiServiceImportFactoryBean
destroy, getBeanClassLoader, getBundleContext, getContextClassLoader, getFilter, getInterfaces, getListeners, getObject, getUnifiedFilter, isEagerInit, isPrototype, isSingleton, setBeanClassLoader, setBundleContext, setContextClassLoader, setFilter, setInterfaces, setListeners, setServiceBeanName
 
Methods inherited from class org.springframework.osgi.service.importer.support.AbstractDependableServiceImporter
getCardinality, isMandatory, registerListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

OsgiServiceCollectionProxyFactoryBean

public OsgiServiceCollectionProxyFactoryBean()
Method Detail

afterPropertiesSet

public void afterPropertiesSet()
Specified by:
afterPropertiesSet in interface InitializingBean
Overrides:
afterPropertiesSet in class AbstractOsgiServiceImportFactoryBean

getObjectType

public Class getObjectType()

isSatisfied

public boolean isSatisfied()
Description copied from interface: ServiceDependency
Indicates if the dependency is satisified or not. For optional service imports, this should always return true while for mandatory service imports, should return true if at least one service is available or false otherwise.

Returns:
true if the dependency is satisfied, false otherwise.

setComparator

public void setComparator(Comparator comparator)
Sets the (optional) comparator for ordering the resulting collection. The presence of a comparator will force the FactoryBean to use a sorted collection even though, the specified collection type does not imply ordering.

Thus, instead of list a sorted list will be created and instead of a set, a sorted set.

Parameters:
comparator - Comparator (can be null) used for ordering the resulting collection.
See Also:
setCollectionType(CollectionType)

setCollectionType

public void setCollectionType(CollectionType collectionType)
Sets the collection type this FactoryBean will produce. Note that if a comparator is set, a sorted collection will be created even if the specified type is does not imply ordering. If no comparator is set but the collection type implies ordering, the natural order of the elements will be used.

Parameters:
collectionType - the collection type as string using one of the values above.
See Also:
setComparator(Comparator), Comparable, Comparator, CollectionType

setCardinality

public void setCardinality(Cardinality cardinality)
Sets the importer cardinality (0..1, 1..1, 0..N, or 1..N). Default is 1..X.

Since this implementation creates a managed collection, only multiple cardinalities are accepted.

Overrides:
setCardinality in class AbstractDependableServiceImporter
Parameters:
cardinality - importer cardinality.


Copyright 2006-2008 Spring Framework. All Rights Reserved.