Interface OsgiApplicationContextCreator

All Known Implementing Classes:
ConditionalApplicationContextCreator, DefaultOsgiApplicationContextCreator

public interface OsgiApplicationContextCreator

Extender hook for customizing the OSGi application context creation. Each bundle started by the OSGi platform can create (or customize) an application context that becomes managed by the Spring-DM extender. For example, to create an application context based on the presence of a manifest header, one could use the following code:

 class HeaderBasedAppCtxCreator implements OsgiApplicationContextCreator {
        /** location header */
        private static final String HEADER = "Context-Locations";
        public DelegatedExecutionOsgiBundleApplicationContext createApplicationContext(BundleContext bundleContext) {
                Bundle owningBundle = bundleContext.getBundle();
                Object value = owningBundle.getHeaders().get(HEADER);
                String[] locations = null;
                if (value != null && value instanceof String) {
                        locations = StringUtils.commaDelimitedListToStringArray((String) value);
                else {
                        locations = <default values>
                // create application context from 'locations'  
                return applicationContext;      

Note: The application contexts should be only created and initialized but not started (i.e. refresh() method should not be called).

The recommended way of configuring the extender is to attach any relevant OsgiApplicationContextCreator implementation as fragments to extender bundle. Please see the OSGi specification and Spring-DM reference documentation for more information on how to do that.

Note the extender also supports OsgiBeanFactoryPostProcessor for application context customization.

The creation of an application context doesn't guarantee that a bundle becomes Spring-DM managed. The Spring-DM extender can do additional post filtering that can discard the bundle (and associated context).

Costin Leau

Method Summary
 DelegatedExecutionOsgiBundleApplicationContext createApplicationContext(BundleContext bundleContext)
          Creates an application context for the given bundle context.

Method Detail


DelegatedExecutionOsgiBundleApplicationContext createApplicationContext(BundleContext bundleContext)
                                                                        throws Exception
Creates an application context for the given bundle context. If no application context needs to be created, then null should be returned. Exceptions will be caught and logged but will not prevent the creation of other application contexts.

bundleContext - OSGi bundle context determining the context creation
null if no context should be created, non-null otherwise
Exception - if something goes wrong

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