1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.osgi.service.importer.support.internal.util;
18
19 import java.util.Dictionary;
20 import java.util.Map;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.osgi.framework.BundleContext;
25 import org.osgi.framework.ServiceReference;
26 import org.springframework.osgi.service.importer.OsgiServiceLifecycleListener;
27 import org.springframework.osgi.util.OsgiServiceReferenceUtils;
28 import org.springframework.util.ObjectUtils;
29
30 /**
31 * @author Costin Leau
32 *
33 */
34 public abstract class OsgiServiceBindingUtils {
35
36 private static final Log log = LogFactory.getLog(OsgiServiceBindingUtils.class);
37
38
39 public static void callListenersBind(BundleContext context, Object serviceProxy, ServiceReference reference,
40 OsgiServiceLifecycleListener[] listeners) {
41 if (!ObjectUtils.isEmpty(listeners)) {
42 boolean debug = log.isDebugEnabled();
43
44
45 Dictionary properties = OsgiServiceReferenceUtils.getServicePropertiesSnapshot(reference);
46 for (int i = 0; i < listeners.length; i++) {
47 if (debug)
48 log.debug("Calling bind on " + listeners[i] + " w/ reference " + reference);
49 try {
50 listeners[i].bind(serviceProxy, (Map) properties);
51 }
52 catch (Exception ex) {
53 log.warn("Bind method on listener " + listeners[i] + " threw exception ", ex);
54 }
55 if (debug)
56 log.debug("Called bind on " + listeners[i] + " w/ reference " + reference);
57 }
58 }
59 }
60
61 public static void callListenersUnbind(BundleContext context, Object serviceProxy, ServiceReference reference,
62 OsgiServiceLifecycleListener[] listeners) {
63 if (!ObjectUtils.isEmpty(listeners)) {
64 boolean debug = log.isDebugEnabled();
65
66 Dictionary properties = OsgiServiceReferenceUtils.getServicePropertiesSnapshot(reference);
67 for (int i = 0; i < listeners.length; i++) {
68 if (debug)
69 log.debug("Calling unbind on " + listeners[i] + " w/ reference " + reference);
70 try {
71 listeners[i].unbind(serviceProxy, (Map) properties);
72 }
73 catch (Exception ex) {
74 log.warn("Unbind method on listener " + listeners[i] + " threw exception ", ex);
75 }
76 if (debug)
77 log.debug("Called unbind on " + listeners[i] + " w/ reference " + reference);
78 }
79 }
80 }
81 }