1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }