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 java.util.Iterator;
20  import java.util.List;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.osgi.framework.BundleContext;
25  import org.osgi.framework.BundleException;
26  import org.osgi.framework.InvalidSyntaxException;
27  import org.springframework.beans.BeansException;
28  import org.springframework.beans.FatalBeanException;
29  import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
30  import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
31  import org.springframework.osgi.extender.OsgiBeanFactoryPostProcessor;
32  
33  /**
34   * Simple adapter for wrapping OsgiBeanPostProcessors to normal Spring post
35   * processors.
36   * 
37   * @author Costin Leau
38   * 
39   */
40  public class OsgiBeanFactoryPostProcessorAdapter implements BeanFactoryPostProcessor {
41  
42  	/** logger */
43  	private static final Log log = LogFactory.getLog(OsgiBeanFactoryPostProcessorAdapter.class);
44  
45  	private final BundleContext bundleContext;
46  
47  	private List osgiPostProcessors;
48  
49  
50  	public OsgiBeanFactoryPostProcessorAdapter(BundleContext bundleContext, List postProcessors) {
51  		this.bundleContext = bundleContext;
52  		this.osgiPostProcessors = postProcessors;
53  	}
54  
55  	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
56  		boolean trace = log.isTraceEnabled();
57  
58  		Exception processingException = null;
59  
60  		for (Iterator iterator = osgiPostProcessors.iterator(); iterator.hasNext();) {
61  			OsgiBeanFactoryPostProcessor osgiPostProcessor = (OsgiBeanFactoryPostProcessor) iterator.next();
62  			if (trace)
63  				log.trace("Calling OsgiBeanFactoryPostProcessor " + osgiPostProcessor + " for bean factory "
64  						+ beanFactory);
65  
66  			try {
67  				osgiPostProcessor.postProcessBeanFactory(bundleContext, beanFactory);
68  			}
69  			catch (InvalidSyntaxException ex) {
70  				processingException = ex;
71  			}
72  			catch (BundleException ex) {
73  				processingException = ex;
74  			}
75  
76  			if (processingException != null) {
77  				if (log.isDebugEnabled())
78  					log.debug("PostProcessor " + osgiPostProcessor + " threw exception", processingException);
79  				throw new FatalBeanException("Error encountered while executing OSGi post processing",
80  					processingException);
81  			}
82  		}
83  	}
84  }