1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.osgi.extender.support;
18
19 import org.osgi.framework.BundleContext;
20 import org.springframework.beans.factory.InitializingBean;
21 import org.springframework.osgi.context.DelegatedExecutionOsgiBundleApplicationContext;
22 import org.springframework.osgi.extender.OsgiApplicationContextCreator;
23 import org.springframework.util.Assert;
24
25 /**
26 * Useful {@link OsgiApplicationContextCreator} implementation that dictates
27 * whether the default application context used by the Spring-DM extender should
28 * be created (or not) based on a <code>boolean</code> value. This allows
29 * clients to handle only the bundleContext filtering while being decoupled from
30 * the context creation process:
31 *
32 * <pre class="code">
33 *
34 * ConditionalApplicationContextCreator creator = new ConditionalApplicationContextCreator();
35 *
36 * creator.setFilter(new ConditionalApplicationContextCreator.BundleContextFilter() {
37 * // filter bundles with no copyright
38 * public boolean matches(BundleContext bundleContext) {
39 * return bundleContext.getBundle().getHeaders().get(Constants.BUNDLE_COPYRIGHT) != null)
40 * }
41 * }
42 *
43 * creator.createApplicationContext(bundleContext);
44 * </pre>
45 *
46 * @see OsgiApplicationContextCreator
47 * @author Costin Leau
48 */
49 public class ConditionalApplicationContextCreator implements OsgiApplicationContextCreator, InitializingBean {
50
51 /**
52 * Callback used to filter the bundle contexts for which the default
53 * application contexts are created.
54 *
55 * @author Costin Leau
56 *
57 */
58 public static interface BundleContextFilter {
59
60 /**
61 * Determines if the given bundle context matches the filter criteria.
62 *
63 * @param bundleContext the OSGi bundle context to check
64 * @return true if the bundle context matches, false otherwise.
65 */
66 boolean matches(BundleContext bundleContext);
67 }
68
69
70 private BundleContextFilter filter;
71
72 private OsgiApplicationContextCreator delegatedContextCreator;
73
74
75 public void afterPropertiesSet() throws Exception {
76 Assert.notNull(filter, "filter property is required");
77 if (delegatedContextCreator == null)
78 delegatedContextCreator = new DefaultOsgiApplicationContextCreator();
79 }
80
81 public DelegatedExecutionOsgiBundleApplicationContext createApplicationContext(BundleContext bundleContext)
82 throws Exception {
83 if (filter.matches(bundleContext))
84 return delegatedContextCreator.createApplicationContext(bundleContext);
85 else
86 return null;
87 }
88
89 /**
90 * Sets the {@link BundleContextFilter} used by this context creator.
91 *
92 * @param filter The bundle context filter to set.
93 */
94 public void setFilter(BundleContextFilter filter) {
95 this.filter = filter;
96 }
97
98 /**
99 * Sets the {@link OsgiApplicationContextCreator} used by this context
100 * creator for the actual creation. If none is specified,
101 * {@link DefaultOsgiApplicationContextCreator} is used.
102 *
103 * @param delegatedContextCreator the instance used for creating the
104 * application context
105 */
106 public void setDelegatedApplicationContextCreator(OsgiApplicationContextCreator delegatedContextCreator) {
107 this.delegatedContextCreator = delegatedContextCreator;
108 }
109 }