1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.springframework.osgi.service.exporter.support;
20
21 import java.util.Map;
22
23 import org.osgi.framework.BundleContext;
24 import org.osgi.framework.Constants;
25 import org.springframework.beans.factory.InitializingBean;
26 import org.springframework.osgi.context.BundleContextAware;
27 import org.springframework.osgi.service.exporter.OsgiServicePropertiesResolver;
28 import org.springframework.osgi.util.internal.MapBasedDictionary;
29 import org.springframework.util.Assert;
30 import org.springframework.util.StringUtils;
31
32 /**
33 * {@link OsgiServicePropertiesResolver} that creates a service property set
34 * with the following properties:
35 * <ul>
36 * <li>Bundle-SymbolicName=<bundle symbolic name></li>
37 * <li>Bundle-Version=<bundle version></li>
38 * <li>org.springframework.osgi.bean.name="<bean name></li>
39 * </ul>
40 *
41 *
42 * @author Adrian Colyer
43 * @author Hal Hildebrand
44 *
45 * @see OsgiServicePropertiesResolver
46 * @see OsgiServiceFactoryBean
47 *
48 */
49 public class BeanNameServicePropertiesResolver implements OsgiServicePropertiesResolver, BundleContextAware,
50 InitializingBean {
51
52 private BundleContext bundleContext;
53
54
55 public Map getServiceProperties(String beanName) {
56 Map p = new MapBasedDictionary();
57 p.put(BEAN_NAME_PROPERTY_KEY, beanName);
58 String name = getSymbolicName();
59 if (StringUtils.hasLength(name)) {
60 p.put(Constants.BUNDLE_SYMBOLICNAME, name);
61 }
62 String version = getBundleVersion();
63 if (StringUtils.hasLength(version)) {
64 p.put(Constants.BUNDLE_VERSION, version);
65 }
66 return p;
67 }
68
69 private String getBundleVersion() {
70 return (String) this.bundleContext.getBundle().getHeaders().get(Constants.BUNDLE_VERSION);
71 }
72
73 private String getSymbolicName() {
74 return this.bundleContext.getBundle().getSymbolicName();
75 }
76
77 public void setBundleContext(BundleContext context) {
78 this.bundleContext = context;
79 }
80
81 public void afterPropertiesSet() throws Exception {
82 Assert.notNull(bundleContext, "required property bundleContext has not been set");
83 }
84
85 }