1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.osgi.service.exporter.support;
18
19 import java.util.Map;
20
21 import org.osgi.framework.ServiceRegistration;
22 import org.springframework.beans.factory.DisposableBean;
23 import org.springframework.osgi.service.exporter.OsgiServiceRegistrationListener;
24
25 /**
26 * Base exporter class providing common functionality for registering (also
27 * known as exporting) Spring beans as OSGi services.
28 *
29 * @author Costin Leau
30 */
31 abstract class AbstractOsgiServiceExporter implements DisposableBean {
32
33 /** listeners */
34 private OsgiServiceRegistrationListener[] listeners = new OsgiServiceRegistrationListener[0];
35
36
37 /**
38 * Takes care of notifying the listeners on both startup and shutdown (by
39 * wrapping the service registration).
40 *
41 * @param service object published as OSGi service
42 * @param properties exported OSGi service properties
43 * @param registration original service registration
44 * @return
45 */
46 ServiceRegistration notifyListeners(Object service, Map properties, ServiceRegistration registration) {
47
48 callRegisteredOnListeners(service, properties);
49
50 return new ServiceRegistrationDecorator(service, registration, listeners);
51 }
52
53 /**
54 * Call registration on listeners.
55 *
56 * @param properties
57 */
58 private void callRegisteredOnListeners(Object service, Map properties) {
59 for (int i = 0; i < listeners.length; i++) {
60 if (listeners[i] != null) {
61 try {
62 listeners[i].registered(service, properties);
63 }
64 catch (Exception ex) {
65
66
67 }
68 }
69 }
70 }
71
72 /**
73 * Sets the listeners interested in registration and unregistration events.
74 *
75 * @param listeners registration/unregistration listeners.
76 */
77 public void setListeners(OsgiServiceRegistrationListener[] listeners) {
78 if (listeners != null)
79 this.listeners = listeners;
80 }
81
82 public void destroy() {
83 unregisterService();
84 }
85
86 /**
87 * Registers/Exports the OSGi service.
88 */
89 abstract void registerService();
90
91 /**
92 * Unregisters/de-exports the OSGi service.
93 */
94 abstract void unregisterService();
95 }