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.internal.support;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  import org.osgi.framework.BundleContext;
22  import org.springframework.beans.BeanUtils;
23  import org.springframework.beans.BeansException;
24  import org.springframework.beans.factory.BeanClassLoaderAware;
25  import org.springframework.beans.factory.BeanFactoryAware;
26  import org.springframework.beans.factory.config.BeanPostProcessor;
27  import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
28  import org.springframework.osgi.OsgiException;
29  import org.springframework.osgi.context.BundleContextAware;
30  import org.springframework.osgi.extender.OsgiBeanFactoryPostProcessor;
31  
32  /**
33   * Post processor used for processing Spring-DM annotations.
34   * 
35   * @author Costin Leau
36   * 
37   */
38  public class OsgiAnnotationPostProcessor implements OsgiBeanFactoryPostProcessor {
39  
40  	/** logger */
41  	private static final Log log = LogFactory.getLog(OsgiAnnotationPostProcessor.class);
42  
43  	/** service reference bpp */
44  	private static final String ANNOTATION_BPP_CLASS = "org.springframework.osgi.extensions.annotation.ServiceReferenceInjectionBeanPostProcessor";
45  
46  
47  	public void postProcessBeanFactory(BundleContext bundleContext, ConfigurableListableBeanFactory beanFactory)
48  			throws BeansException, OsgiException {
49  
50  		try {
51  			// Try and load the annotation code using the bundle classloader
52  			Class annotationBppClass = bundleContext.getBundle().loadClass(ANNOTATION_BPP_CLASS);
53  			// instantiate the class
54  			final BeanPostProcessor annotationBeanPostProcessor = (BeanPostProcessor) BeanUtils.instantiateClass(annotationBppClass);
55  
56  			// everything went okay so configure the BPP and add it to the BF
57  			((BeanFactoryAware) annotationBeanPostProcessor).setBeanFactory(beanFactory);
58  			((BeanClassLoaderAware) annotationBeanPostProcessor).setBeanClassLoader(beanFactory.getBeanClassLoader());
59  			((BundleContextAware) annotationBeanPostProcessor).setBundleContext(bundleContext);
60  			beanFactory.addBeanPostProcessor(annotationBeanPostProcessor);
61  		}
62  		catch (ClassNotFoundException exception) {
63  			log.info("Spring-DM annotation package could not be found; automatic annotation processing disabled");
64  			if (log.isDebugEnabled())
65  				log.debug("Cannot load annotation bpp", exception);
66  		}
67  	}
68  }