View Javadoc

1   /*
2    * Copyright 2006-2008 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.osgi.extender.support;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  import org.osgi.framework.Bundle;
22  import org.osgi.framework.BundleContext;
23  import org.springframework.osgi.context.DelegatedExecutionOsgiBundleApplicationContext;
24  import org.springframework.osgi.context.support.OsgiBundleXmlApplicationContext;
25  import org.springframework.osgi.extender.OsgiApplicationContextCreator;
26  import org.springframework.osgi.extender.support.scanning.ConfigurationScanner;
27  import org.springframework.osgi.extender.support.scanning.DefaultConfigurationScanner;
28  import org.springframework.osgi.util.OsgiStringUtils;
29  import org.springframework.util.Assert;
30  import org.springframework.util.ObjectUtils;
31  
32  /**
33   * Default {@link OsgiApplicationContextCreator} implementation.
34   * 
35   * <p/> Creates an {@link OsgiBundleXmlApplicationContext} instance using the
36   * default locations (<tt>Spring-Context</tt> manifest header or
37   * <tt>META-INF/spring/*.xml</tt>) if available, null otherwise.
38   * 
39   * <p/> Additionally, this implementation allows custom locations to be
40   * specified through {@link ConfigurationScanner} interface.
41   * 
42   * @see ConfigurationScanner
43   * @author Costin Leau
44   * 
45   */
46  public class DefaultOsgiApplicationContextCreator implements OsgiApplicationContextCreator {
47  
48  	/** logger */
49  	private static final Log log = LogFactory.getLog(DefaultOsgiApplicationContextCreator.class);
50  
51  	private ConfigurationScanner configurationScanner = new DefaultConfigurationScanner();
52  
53  
54  	public DelegatedExecutionOsgiBundleApplicationContext createApplicationContext(BundleContext bundleContext)
55  			throws Exception {
56  		Bundle bundle = bundleContext.getBundle();
57  		ApplicationContextConfiguration config = new ApplicationContextConfiguration(bundle, configurationScanner);
58  		if (log.isTraceEnabled())
59  			log.trace("Created configuration " + config + " for bundle "
60  					+ OsgiStringUtils.nullSafeNameAndSymName(bundle));
61  
62  		// it's not a spring bundle, ignore it
63  		if (!config.isSpringPoweredBundle()) {
64  			return null;
65  		}
66  
67  		log.info("Discovered configurations " + ObjectUtils.nullSafeToString(config.getConfigurationLocations())
68  				+ " in bundle [" + OsgiStringUtils.nullSafeNameAndSymName(bundle) + "]");
69  
70  		DelegatedExecutionOsgiBundleApplicationContext sdoac = new OsgiBundleXmlApplicationContext(
71  			config.getConfigurationLocations());
72  		sdoac.setBundleContext(bundleContext);
73  		sdoac.setPublishContextAsService(config.isPublishContextAsService());
74  
75  		return sdoac;
76  	}
77  
78  	/**
79  	 * Sets the configurationScanner used by this creator.
80  	 * 
81  	 * @param configurationScanner The configurationScanner to set.
82  	 */
83  	public void setConfigurationScanner(ConfigurationScanner configurationScanner) {
84  		Assert.notNull(configurationScanner);
85  		this.configurationScanner = configurationScanner;
86  	}
87  }