1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.batch.core.configuration.support;
18
19 import org.osgi.framework.BundleContext;
20 import org.springframework.beans.BeansException;
21 import org.springframework.context.ApplicationContext;
22 import org.springframework.context.ApplicationContextAware;
23 import org.springframework.context.ConfigurableApplicationContext;
24 import org.springframework.osgi.context.BundleContextAware;
25 import org.springframework.osgi.context.support.OsgiBundleXmlApplicationContext;
26
27
28
29
30
31
32
33
34 public class OsgiBundleXmlApplicationContextFactory implements BundleContextAware, ApplicationContextFactory,
35 ApplicationContextAware {
36
37 private BundleContext bundleContext;
38
39 private ApplicationContext parent;
40
41 private String path;
42
43 private String displayName;
44
45
46
47
48 public void setPath(String path) {
49 this.path = path;
50 }
51
52
53
54
55 public void setDisplayName(String displayName) {
56 this.displayName = displayName;
57 }
58
59
60
61
62
63
64 @Override
65 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
66 parent = applicationContext;
67 }
68
69
70
71
72
73
74
75 @Override
76 public void setBundleContext(BundleContext context) {
77 this.bundleContext = context;
78 }
79
80
81
82
83
84
85
86
87 @Override
88 public ConfigurableApplicationContext createApplicationContext() {
89 OsgiBundleXmlApplicationContext context = new OsgiBundleXmlApplicationContext(new String[] { path }, parent);
90 String displayName = bundleContext.getBundle().getSymbolicName() + ":" + this.displayName;
91 context.setDisplayName(displayName);
92 context.setBundleContext(bundleContext);
93 context.refresh();
94 return context;
95 }
96
97 @Override
98 public String toString() {
99 String bundleId = bundleContext == null ? null : (bundleContext.getBundle() == null ? bundleContext.toString()
100 : "" + bundleContext.getBundle().getBundleId());
101 return "OsgiBundleXmlApplicationContext [path=" + path + ", bundle=" + bundleId + "]";
102 }
103
104 @Override
105 public int hashCode() {
106 return toString().hashCode();
107 }
108
109 @Override
110 public boolean equals(Object obj) {
111 if (this == obj) {
112 return true;
113 }
114 if (obj == null) {
115 return false;
116 }
117 return toString().equals(obj.toString());
118 }
119
120 }