org.springframework.osgi.service.exporter.support
Class OsgiServiceFactoryBean

java.lang.Object
  extended by org.springframework.osgi.service.exporter.support.AbstractDependentServiceExporter
      extended by org.springframework.osgi.service.exporter.support.AbstractOsgiServiceExporter
          extended by org.springframework.osgi.service.exporter.support.OsgiServiceFactoryBean
All Implemented Interfaces:
BeanClassLoaderAware, BeanFactoryAware, BeanNameAware, DisposableBean, FactoryBean, InitializingBean, Ordered, BundleContextAware, DependentServiceExporter

public class OsgiServiceFactoryBean
extends AbstractOsgiServiceExporter
implements BeanFactoryAware, DisposableBean, BundleContextAware, FactoryBean, Ordered, BeanClassLoaderAware, BeanNameAware

FactoryBean that transparently publishes other beans in the same application context as OSGi services returning the ServiceRegistration for the given object. Also known as an exporter this class handle the registration and unregistration of an OSGi service for the backing/target object.

The service properties used when publishing the service are determined by the OsgiServicePropertiesResolver. The default implementation uses

Note:If thread context class loader management is used (setContextClassLoader(ExportContextClassLoader), since proxying is required, the target class has to meet certain criterias described in the Spring AOP documentation. In short, final classes are not supported when class enhancement is used.

Author:
Adrian Colyer, Costin Leau, Hal Hildebrand, Andy Piper

Field Summary
 
Fields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
 
Constructor Summary
OsgiServiceFactoryBean()
           
 
Method Summary
 void afterPropertiesSet()
           
 void destroy()
           
 String getBeanName()
          Returns the bean name of this class when configured inside a Spring container.
 Class[] getInterfaces()
          Returns the interfaces that will be considered when exporting the target as an OSGi service.
 Object getObject()
          

Returns a ServiceRegistration to the OSGi service for the target object.

 Class getObjectType()
           
 int getOrder()
           
 int getRanking()
          Returns the OSGi ranking used when publishing the service.
 OsgiServicePropertiesResolver getResolver()
          Returns the property resolver used for publishing the service.
 Map getServiceProperties()
          Returns the properties used when exporting the target as an OSGi service.
 Object getTarget()
          Returns the object exported as an OSGi service.
 String getTargetBeanName()
          Returns the target bean name.
 boolean isSingleton()
           
 void registerService()
          Publishes the given object as an OSGi service.
 void setAutoExport(AutoExport classExporter)
          Sets the strategy used for automatically publishing classes.
 void setBeanClassLoader(ClassLoader classLoader)
           
 void setBeanFactory(BeanFactory beanFactory)
           
 void setBeanName(String name)
           
 void setBundleContext(BundleContext context)
          Set the BundleContext that this bean runs in.
 void setContextClassLoader(ExportContextClassLoader ccl)
          Sets the context class loader management strategy to use when invoking operations on the exposed target bean.
 void setInterfaces(Class[] interfaces)
          Sets the interfaces advertised by the service.These will be advertised in the OSGi space and are considered when looking for a service.
 void setOrder(int order)
          Set the ordering which will apply to this class's implementation of Ordered, used when applying multiple BeanPostProcessors.
 void setRanking(int ranking)
          Shortcut for setting the ranking property of the published service.
 void setResolver(OsgiServicePropertiesResolver resolver)
          Sets the property resolver used when publishing the bean as an OSGi service.
 void setServiceProperties(Map serviceProperties)
          Sets the properties used when exposing the target as an OSGi service.
 void setTarget(Object target)
          Sets the given object to be export as an OSGi service.
 void setTargetBeanName(String name)
          Sets the name of the bean managed by the Spring container, which will be exported as an OSGi service.
 void unregisterService()
          Unregisters/de-exports the OSGi service.
 
Methods inherited from class org.springframework.osgi.service.exporter.support.AbstractOsgiServiceExporter
setListeners
 
Methods inherited from class org.springframework.osgi.service.exporter.support.AbstractDependentServiceExporter
isRunning, setPublishAtStartup, shouldPublishAtStartup, start, stop
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

OsgiServiceFactoryBean

public OsgiServiceFactoryBean()
Method Detail

afterPropertiesSet

public void afterPropertiesSet()
                        throws Exception
Specified by:
afterPropertiesSet in interface InitializingBean
Overrides:
afterPropertiesSet in class AbstractDependentServiceExporter
Throws:
Exception

registerService

public void registerService()
Publishes the given object as an OSGi service. It simply assembles the classes required for publishing and then delegates the actual registration to a dedicated method.


setBeanClassLoader

public void setBeanClassLoader(ClassLoader classLoader)
Specified by:
setBeanClassLoader in interface BeanClassLoaderAware

getObject

public Object getObject()
                 throws Exception

Returns a ServiceRegistration to the OSGi service for the target object.

Specified by:
getObject in interface FactoryBean
Throws:
Exception

getObjectType

public Class getObjectType()
Specified by:
getObjectType in interface FactoryBean

isSingleton

public boolean isSingleton()
Specified by:
isSingleton in interface FactoryBean

destroy

public void destroy()
Specified by:
destroy in interface DisposableBean

unregisterService

public void unregisterService()
Description copied from class: AbstractDependentServiceExporter
Unregisters/de-exports the OSGi service.


setContextClassLoader

public void setContextClassLoader(ExportContextClassLoader ccl)
Sets the context class loader management strategy to use when invoking operations on the exposed target bean. By default, ExportContextClassLoader.UNMANAGED is used.

Note: Since proxying is required for context class loader manager, the target class has to meet certain criterias described in the Spring AOP documentation. In short, final classes are not supported when class enhancement is used.

Parameters:
ccl - context class loader strategy to use
See Also:
ExportContextClassLoader

getTarget

public Object getTarget()
Returns the object exported as an OSGi service.

Returns:
the object exported as an OSGi service

setTarget

public void setTarget(Object target)
Sets the given object to be export as an OSGi service. Normally used when the exported service is a nested bean or an object not managed by the Spring container. Note that the passed target instance is ignored if setTargetBeanName(String) is used.

Parameters:
target - the object to be exported as an OSGi service

getTargetBeanName

public String getTargetBeanName()
Returns the target bean name.

Returns:
the target object bean name

setTargetBeanName

public void setTargetBeanName(String name)
Sets the name of the bean managed by the Spring container, which will be exported as an OSGi service. This method is normally what most use-cases need, rather then setTarget(Object).

Parameters:
name - target bean name

setAutoExport

public void setAutoExport(AutoExport classExporter)
Sets the strategy used for automatically publishing classes. This allows the exporter to use the target class hierarchy and/or interfaces for registering the OSGi service. By default, autoExport is disabled AutoExport.DISABLED.

Parameters:
classExporter - class exporter used for automatically publishing service classes.
See Also:
AutoExport

getServiceProperties

public Map getServiceProperties()
Returns the properties used when exporting the target as an OSGi service.

Returns:
properties used for exporting the target

setServiceProperties

public void setServiceProperties(Map serviceProperties)
Sets the properties used when exposing the target as an OSGi service.

Parameters:
serviceProperties - properties used for exporting the target as an OSGi service

getRanking

public int getRanking()
Returns the OSGi ranking used when publishing the service.

Returns:
service ranking used when publishing the service

setRanking

public void setRanking(int ranking)
Shortcut for setting the ranking property of the published service.

Parameters:
ranking - service ranking
See Also:
Constants.SERVICE_RANKING

setBeanFactory

public void setBeanFactory(BeanFactory beanFactory)
                    throws BeansException
Specified by:
setBeanFactory in interface BeanFactoryAware
Throws:
BeansException

setBundleContext

public void setBundleContext(BundleContext context)
Description copied from interface: BundleContextAware
Set the BundleContext that this bean runs in. Normally this can be used to initialize an object.

Specified by:
setBundleContext in interface BundleContextAware
Parameters:
context - the BundleContext object to be used by this object

getResolver

public OsgiServicePropertiesResolver getResolver()
Returns the property resolver used for publishing the service.

Returns:
service property resolver

setResolver

public void setResolver(OsgiServicePropertiesResolver resolver)
Sets the property resolver used when publishing the bean as an OSGi service.

Parameters:
resolver - service property resolver

getInterfaces

public Class[] getInterfaces()
Returns the interfaces that will be considered when exporting the target as an OSGi service.

Returns:
interfaces under which the target will be published as an OSGi service

setInterfaces

public void setInterfaces(Class[] interfaces)
Sets the interfaces advertised by the service.These will be advertised in the OSGi space and are considered when looking for a service.

Parameters:
interfaces - array of classes to advertise

getOrder

public int getOrder()
Specified by:
getOrder in interface Ordered

setOrder

public void setOrder(int order)
Set the ordering which will apply to this class's implementation of Ordered, used when applying multiple BeanPostProcessors.

Default value is Integer.MAX_VALUE, meaning that it's non-ordered.

Parameters:
order - ordering value

getBeanName

public String getBeanName()
Returns the bean name of this class when configured inside a Spring container.

Returns:
the bean name for this class

setBeanName

public void setBeanName(String name)
Specified by:
setBeanName in interface BeanNameAware


Copyright 2006-2008 Spring Framework. All Rights Reserved.