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

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

public final 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()
           
 void destroy()
           
 Object getObject()
          Returns a managed object for accessing OSGi service(s). Returns a managed collection that automatically handles the dynamics of matching OSGi services.
 Class getObjectType()
           Returns the managed collection type.
 boolean isEagerInit()
           The object created by this factory bean is eagerly initialized.
 boolean isPrototype()
           The object returned by this FactoryBean is a not a prototype.
 boolean isSingleton()
           The object managed by this factory is a singleton.
 void setBeanClassLoader(ClassLoader classLoader)
           This method is called automatically by the container. The class will automatically chain this classloader with the AOP infrastructure classes (even if these are not visible to the user) so that the proxy creation can be completed successfully.
 void setCardinality(Cardinality cardinality)
          Sets the importer cardinality (0..1, 1..1, 0..N, or 1..N).

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

 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.
 void setGreedyProxying(boolean greedyProxying)
          Dictates whether greedy proxies are created or not (default).
 
Methods inherited from class org.springframework.osgi.service.importer.support.AbstractOsgiServiceImportFactoryBean
getBeanClassLoader, getBeanName, getBundleContext, getCardinality, getContextClassLoader, getFilter, getInterfaces, getListeners, getUnifiedFilter, setBeanName, setBundleContext, setContextClassLoader, setFilter, setInterfaces, setListeners, setServiceBeanName
 
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

getObjectType

public Class getObjectType()
Returns the managed collection type.


getObject

public Object getObject()
Returns a managed object for accessing OSGi service(s). Returns a managed collection that automatically handles the dynamics of matching OSGi services.

Specified by:
getObject in interface FactoryBean
Returns:
managed OSGi service(s)

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 AbstractOsgiServiceImportFactoryBean
Parameters:
cardinality - importer cardinality.

setGreedyProxying

public void setGreedyProxying(boolean greedyProxying)
Dictates whether greedy proxies are created or not (default).

Greedy proxies will proxy all the (visible) classes published by the imported OSGi services. This means that the individual service proxy, might implement/extend additional classes.

By default, greedy proxies are disabled (false) meaning that only the specified classes are used for generating the the imported OSGi service proxies.

Note:Greedy proxying will use the proxy mechanism dictated by this factory configuration. This means that if JDK proxies are used, greedy proxing will consider only additional interfaces exposed by the OSGi service and none of the extra classes. When CGLIB is used, all extra published classes (whether interfaces or non-final concrete classes) will be considered.

Parameters:
greedyProxying - true if greedy proxying should be enabled, false otherwise.

destroy

public void destroy()
             throws Exception
Specified by:
destroy in interface DisposableBean
Throws:
Exception

isSingleton

public boolean isSingleton()
The object managed by this factory is a singleton.

Specified by:
isSingleton in interface FactoryBean
Returns:
true (i.e. the FactoryBean returns singletons)

isEagerInit

public boolean isEagerInit()
The object created by this factory bean is eagerly initialized.

Specified by:
isEagerInit in interface SmartFactoryBean
Returns:
true (this factory bean should be eagerly initialized)

isPrototype

public boolean isPrototype()
The object returned by this FactoryBean is a not a prototype.

Specified by:
isPrototype in interface SmartFactoryBean
Returns:
false (the managed object is not a prototype)

setBeanClassLoader

public void setBeanClassLoader(ClassLoader classLoader)
This method is called automatically by the container. The class will automatically chain this classloader with the AOP infrastructure classes (even if these are not visible to the user) so that the proxy creation can be completed successfully.

Specified by:
setBeanClassLoader in interface BeanClassLoaderAware
Overrides:
setBeanClassLoader in class AbstractOsgiServiceImportFactoryBean


Copyright © 2006-2009 Spring Framework. All Rights Reserved.