org.springframework.osgi.context.support
Class AbstractOsgiBundleApplicationContext

java.lang.Object
  extended by org.springframework.core.io.DefaultResourceLoader
      extended by org.springframework.context.support.AbstractApplicationContext
          extended by org.springframework.context.support.AbstractRefreshableApplicationContext
              extended by org.springframework.osgi.context.support.AbstractOsgiBundleApplicationContext
All Implemented Interfaces:
BeanFactory, DisposableBean, HierarchicalBeanFactory, ListableBeanFactory, ApplicationContext, ApplicationEventPublisher, ConfigurableApplicationContext, Lifecycle, MessageSource, ResourceLoader, ResourcePatternResolver, ConfigurableOsgiBundleApplicationContext
Direct Known Subclasses:
AbstractDelegatedExecutionApplicationContext

public abstract class AbstractOsgiBundleApplicationContext
extends AbstractRefreshableApplicationContext
implements ConfigurableOsgiBundleApplicationContext

AbstractRefreshableApplicationContext subclass that implements the ConfigurableOsgiBundleApplicationContext interface for OSGi environments. Pre-implements a configLocation property, to be populated through the ConfigurableOsgiApplicationContext interface after OSGi bundle startup.

This class is as easy to subclass as AbstractRefreshableApplicationContext(see the javadoc for details): all you need to implement is the loadBeanDefinitions method Note that implementations are supposed to load bean definitions from the files specified by the locations returned by getConfigLocations method.

In addition to the special beans detected by AbstractApplicationContext, this class registers the BundleContextAwareProcessor for processing beans that implement the BundleContextAware interface. Also it interprets resource paths as OSGi bundle resources (either from the bundle class space, bundle space or jar space).

This application context implementation offers the OSGi-specific, bundle scope.

Note: OsgiApplicationContext implementations are generally supposed to configure themselves based on the configuration received through the ConfigurableOsgiBundleApplicationContext interface. In contrast, a stand-alone application context might allow for configuration in custom startup code (for example, GenericApplicationContext).

Author:
Costin Leau, Adrian Colyer, Hal Hildebrand

Field Summary
 
Fields inherited from class org.springframework.context.support.AbstractApplicationContext
APPLICATION_EVENT_MULTICASTER_BEAN_NAME, LIFECYCLE_PROCESSOR_BEAN_NAME, logger, MESSAGE_SOURCE_BEAN_NAME
 
Fields inherited from interface org.springframework.osgi.context.ConfigurableOsgiBundleApplicationContext
APPLICATION_CONTEXT_SERVICE_PROPERTY_NAME, BUNDLE_BEAN_NAME, BUNDLE_CONTEXT_BEAN_NAME
 
Fields inherited from interface org.springframework.context.ConfigurableApplicationContext
CONFIG_LOCATION_DELIMITERS, CONVERSION_SERVICE_BEAN_NAME, LOAD_TIME_WEAVER_BEAN_NAME, SYSTEM_ENVIRONMENT_BEAN_NAME, SYSTEM_PROPERTIES_BEAN_NAME
 
Fields inherited from interface org.springframework.beans.factory.BeanFactory
FACTORY_BEAN_PREFIX
 
Fields inherited from interface org.springframework.core.io.support.ResourcePatternResolver
CLASSPATH_ALL_URL_PREFIX
 
Fields inherited from interface org.springframework.core.io.ResourceLoader
CLASSPATH_URL_PREFIX
 
Constructor Summary
AbstractOsgiBundleApplicationContext()
          Creates a new AbstractOsgiBundleApplicationContext with no parent.
AbstractOsgiBundleApplicationContext(ApplicationContext parent)
          Creates a new AbstractOsgiBundleApplicationContext with the given parent context.
 
Method Summary
protected  ResourcePatternResolver createResourcePatternResolver()
          Creates an OSGi specific resource pattern resolver.
protected  void customizeApplicationContextServiceProperties(Map<Object,Object> serviceProperties)
          Customizes the properties of the application context OSGi service.
protected  void destroyBeans()
           
protected  void doClose()
          Unregister the ApplicationContext OSGi service (in case there is any).
protected  void finishRefresh()
           
 Bundle getBundle()
          Returns the OSGi Bundle for this application context.
 BundleContext getBundleContext()
          Return the BundleContext for this application context.
 ClassLoader getClassLoader()
           
 String[] getConfigLocations()
          Returns this application context configuration locations.
protected  String[] getDefaultConfigLocations()
          Returns the default configuration locations to use, for the case where no explicit configuration locations have been specified.
 Resource getResource(String location)
           
protected  Resource getResourceByPath(String path)
           
protected  ResourcePatternResolver getResourcePatternResolver()
          This implementation supports pattern matching inside the OSGi bundle.
 Resource[] getResources(String locationPattern)
           
protected  void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
           
protected  void prepareRefresh()
           
 void setBundleContext(BundleContext bundleContext)
          Sets the BundleContext used by this OSGi bundle application context.
 void setClassLoader(ClassLoader classLoader)
           
 void setConfigLocations(String[] configLocations)
          Sets the config locations for this OSGi bundle application context.
 void setPublishContextAsService(boolean publishContextAsService)
          Indicates whether this application context should be publish as an OSGi service if successfully started.
 
Methods inherited from class org.springframework.context.support.AbstractRefreshableApplicationContext
cancelRefresh, closeBeanFactory, createBeanFactory, customizeBeanFactory, getBeanFactory, hasBeanFactory, loadBeanDefinitions, refreshBeanFactory, setAllowBeanDefinitionOverriding, setAllowCircularReferences
 
Methods inherited from class org.springframework.context.support.AbstractApplicationContext
addApplicationListener, addBeanFactoryPostProcessor, addListener, close, containsBean, containsBeanDefinition, containsLocalBean, destroy, findAnnotationOnBean, finishBeanFactoryInitialization, getAliases, getApplicationListeners, getAutowireCapableBeanFactory, getBean, getBean, getBean, getBean, getBeanDefinitionCount, getBeanDefinitionNames, getBeanFactoryPostProcessors, getBeanNamesForType, getBeanNamesForType, getBeansOfType, getBeansOfType, getBeansWithAnnotation, getDisplayName, getId, getInternalParentBeanFactory, getInternalParentMessageSource, getMessage, getMessage, getMessage, getParent, getParentBeanFactory, getStartupDate, getType, initApplicationEventMulticaster, initLifecycleProcessor, initMessageSource, invokeBeanFactoryPostProcessors, isActive, isPrototype, isRunning, isSingleton, isTypeMatch, obtainFreshBeanFactory, onClose, onRefresh, prepareBeanFactory, publishEvent, refresh, registerBeanPostProcessors, registerListeners, registerShutdownHook, setDisplayName, setId, setParent, start, stop, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.springframework.context.ConfigurableApplicationContext
addApplicationListener, addBeanFactoryPostProcessor, close, getBeanFactory, isActive, refresh, registerShutdownHook, setId, setParent
 
Methods inherited from interface org.springframework.context.ApplicationContext
getAutowireCapableBeanFactory, getDisplayName, getId, getParent, getStartupDate
 
Methods inherited from interface org.springframework.beans.factory.ListableBeanFactory
containsBeanDefinition, findAnnotationOnBean, getBeanDefinitionCount, getBeanDefinitionNames, getBeanNamesForType, getBeanNamesForType, getBeansOfType, getBeansOfType, getBeansWithAnnotation
 
Methods inherited from interface org.springframework.beans.factory.HierarchicalBeanFactory
containsLocalBean, getParentBeanFactory
 
Methods inherited from interface org.springframework.beans.factory.BeanFactory
containsBean, getAliases, getBean, getBean, getBean, getBean, getType, isPrototype, isSingleton, isTypeMatch
 
Methods inherited from interface org.springframework.context.MessageSource
getMessage, getMessage, getMessage
 
Methods inherited from interface org.springframework.context.ApplicationEventPublisher
publishEvent
 
Methods inherited from interface org.springframework.context.Lifecycle
isRunning, start, stop
 

Constructor Detail

AbstractOsgiBundleApplicationContext

public AbstractOsgiBundleApplicationContext()
Creates a new AbstractOsgiBundleApplicationContext with no parent.


AbstractOsgiBundleApplicationContext

public AbstractOsgiBundleApplicationContext(ApplicationContext parent)
Creates a new AbstractOsgiBundleApplicationContext with the given parent context.

Parameters:
parent - the parent context
Method Detail

setBundleContext

public void setBundleContext(BundleContext bundleContext)
Sets the BundleContext used by this OSGi bundle application context. Normally it's the BundleContext in which the context runs.

Does not cause an initialization of the context: ConfigurableApplicationContext.refresh() needs to be called after the setting of all configuration properties.

Will automatically determine the bundle, create a new ResourceLoader (and set its ClassLoader (if none is set already) to a custom implementation that will delegate the calls to the bundle).

Specified by:
setBundleContext in interface ConfigurableOsgiBundleApplicationContext
Parameters:
bundleContext - the BundleContext used by this application context.
See Also:
ConfigurableApplicationContext.refresh()

getBundleContext

public BundleContext getBundleContext()
Description copied from interface: ConfigurableOsgiBundleApplicationContext
Return the BundleContext for this application context. This method is offered as a helper since as of OSGi 4.1, the bundle context can be discovered directly from the given bundle.

Specified by:
getBundleContext in interface ConfigurableOsgiBundleApplicationContext
Returns:
the BundleContext in which this application context runs
See Also:
ConfigurableOsgiBundleApplicationContext.getBundle()

getBundle

public Bundle getBundle()
Description copied from interface: ConfigurableOsgiBundleApplicationContext
Returns the OSGi Bundle for this application context.

Specified by:
getBundle in interface ConfigurableOsgiBundleApplicationContext
Returns:
the Bundle for this OSGi bundle application context.

setConfigLocations

public void setConfigLocations(String[] configLocations)
Description copied from interface: ConfigurableOsgiBundleApplicationContext
Sets the config locations for this OSGi bundle application context. If not set, the implementation is supposed to use a default for the given bundle.

Specified by:
setConfigLocations in interface ConfigurableOsgiBundleApplicationContext
Parameters:
configLocations - array of configuration locations

getConfigLocations

public String[] getConfigLocations()
Returns this application context configuration locations. The default implementation will check whether there are any locations configured and, if not, will return the default locations.

Returns:
application context configuration locations.
See Also:
getDefaultConfigLocations()

doClose

protected void doClose()
Unregister the ApplicationContext OSGi service (in case there is any).

Overrides:
doClose in class AbstractApplicationContext

destroyBeans

protected void destroyBeans()
Overrides:
destroyBeans in class AbstractApplicationContext

getDefaultConfigLocations

protected String[] getDefaultConfigLocations()
Returns the default configuration locations to use, for the case where no explicit configuration locations have been specified.

Default implementation returns null, requiring explicit configuration locations.

Returns:
application context default configuration locations
See Also:
setConfigLocations(java.lang.String[])

prepareRefresh

protected void prepareRefresh()
Overrides:
prepareRefresh in class AbstractApplicationContext

finishRefresh

protected void finishRefresh()
Overrides:
finishRefresh in class AbstractApplicationContext

postProcessBeanFactory

protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
                               throws BeansException
Overrides:
postProcessBeanFactory in class AbstractApplicationContext
Throws:
BeansException

customizeApplicationContextServiceProperties

protected void customizeApplicationContextServiceProperties(Map<Object,Object> serviceProperties)
Customizes the properties of the application context OSGi service. This method is called only if the application context will be published as an OSGi service.

The default implementation stores the bundle symbolic name under Constants.BUNDLE_SYMBOLICNAME and ConfigurableOsgiBundleApplicationContext.APPLICATION_CONTEXT_SERVICE_PROPERTY_NAME and the bundle version under Constants.BUNDLE_VERSION property. Can be overridden by subclasses to add more properties if needed (for example for web applications where multiple application contexts are available inside the same bundle).

Parameters:
serviceProperties - service properties map (can be casted to Dictionary)

createResourcePatternResolver

protected ResourcePatternResolver createResourcePatternResolver()
Creates an OSGi specific resource pattern resolver.

Returns:
returns an OSGi specific pattern resolver.

getResourcePatternResolver

protected ResourcePatternResolver getResourcePatternResolver()
This implementation supports pattern matching inside the OSGi bundle.

Overrides:
getResourcePatternResolver in class AbstractApplicationContext
See Also:
OsgiBundleResourcePatternResolver

getClassLoader

public ClassLoader getClassLoader()
Specified by:
getClassLoader in interface ResourceLoader
Overrides:
getClassLoader in class DefaultResourceLoader

getResource

public Resource getResource(String location)
Specified by:
getResource in interface ResourceLoader
Overrides:
getResource in class DefaultResourceLoader

getResources

public Resource[] getResources(String locationPattern)
                        throws IOException
Specified by:
getResources in interface ResourcePatternResolver
Overrides:
getResources in class AbstractApplicationContext
Throws:
IOException

setClassLoader

public void setClassLoader(ClassLoader classLoader)
Overrides:
setClassLoader in class DefaultResourceLoader

getResourceByPath

protected Resource getResourceByPath(String path)
Overrides:
getResourceByPath in class DefaultResourceLoader

setPublishContextAsService

public void setPublishContextAsService(boolean publishContextAsService)
Description copied from interface: ConfigurableOsgiBundleApplicationContext
Indicates whether this application context should be publish as an OSGi service if successfully started. By default, this is set to true.

Specified by:
setPublishContextAsService in interface ConfigurableOsgiBundleApplicationContext
Parameters:
publishContextAsService - true if the application context should be published as a service, false otherwise


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