1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.argeo.slc.osgi;
17
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.Hashtable;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Properties;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.osgi.framework.Bundle;
28 import org.osgi.framework.BundleContext;
29 import org.osgi.framework.Constants;
30 import org.springframework.context.ApplicationContext;
31 import org.springframework.context.ApplicationEvent;
32 import org.springframework.context.ApplicationListener;
33 import org.springframework.context.event.ContextRefreshedEvent;
34 import org.springframework.core.Ordered;
35
36
37 @SuppressWarnings(value = { "unchecked", "rawtypes" })
38 public class MultipleServiceExporterPostProcessor implements
39 ApplicationListener, Ordered {
40 private final static Log log = LogFactory
41 .getLog(MultipleServiceExporterPostProcessor.class);
42
43 private List<Class> interfaces = new ArrayList<Class>();
44
45 private int order = Ordered.LOWEST_PRECEDENCE;
46
47 private BundleContext bundleContext = null;
48
49
50
51
52 public void onApplicationEvent(ApplicationEvent event) {
53 Map<String, Object> beans = new HashMap<String, Object>();
54 if (event instanceof ContextRefreshedEvent) {
55 if (bundleContext != null) {
56 for (Class clss : interfaces) {
57 ApplicationContext ac = ((ContextRefreshedEvent) event)
58 .getApplicationContext();
59 beans.putAll(ac.getBeansOfType(clss, false, false));
60 }
61
62 int count = 0;
63 for (String beanName : beans.keySet()) {
64 Object bean = beans.get(beanName);
65 List<String> classes = new ArrayList<String>();
66 for (Class clss : interfaces) {
67 if (clss.isAssignableFrom(bean.getClass())) {
68 classes.add(clss.getName());
69 }
70 }
71 Properties props = new Properties();
72 Bundle bundle = bundleContext.getBundle();
73 props.put(Constants.BUNDLE_SYMBOLICNAME,
74 bundle.getSymbolicName());
75 props.put(Constants.BUNDLE_VERSION, bundle.getVersion());
76
77 props.put("org.eclipse.gemini.blueprint.bean.name", beanName);
78 bundleContext.registerService(
79 classes.toArray(new String[classes.size()]), bean,
80 new Hashtable(props));
81 count++;
82 }
83 if (log.isTraceEnabled())
84 log.trace("Published " + count + " " + interfaces
85 + " as OSGi services from bundle "
86 + bundleContext.getBundle().getSymbolicName() + " "
87 + bundleContext.getBundle().getVersion());
88
89
90 }
91 }
92 }
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136 public void setInterfaces(List<Class> interfaces) {
137 this.interfaces = interfaces;
138 }
139
140
141
142
143
144 public int getOrder() {
145 return order;
146 }
147
148 public void setOrder(int order) {
149 this.order = order;
150 }
151
152
153
154
155
156
157
158 public void setBundleContext(BundleContext bundleContext) {
159 this.bundleContext = bundleContext;
160 }
161 }