3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
11 * @file ServiceHelper.java
13 * @brief The class helps to register and retrieve OSGi service registry
15 package org.opendaylight.controller.sal.utils;
17 import java.util.Hashtable;
18 import org.osgi.framework.ServiceRegistration;
19 import java.util.Dictionary;
20 import org.osgi.framework.BundleContext;
21 import org.osgi.framework.FrameworkUtil;
22 import org.osgi.framework.ServiceReference;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * The class helps to register and retrieve OSGi service registry
31 public class ServiceHelper {
32 private static final Logger logger = LoggerFactory
33 .getLogger(ServiceHelper.class);
36 * Register a Service in the OSGi service registry
38 * @param clazz The target class
39 * @param containerName The container name
40 * @param instance of the object exporting the service, be careful
41 * the object must implement/extend clazz else the registration
42 * will fail unless a ServiceFactory is passed as parameter
43 * @param properties The properties to be attached to the service
45 * @return true if registration happened, false otherwise
47 public static boolean registerService(Class<?> clazz, String containerName,
48 Object instance, Dictionary<String, Object> properties) {
49 if (properties == null) {
50 properties = (Dictionary<String, Object>) new Hashtable<String, Object>();
52 properties.put("containerName", containerName);
53 return registerGlobalService(clazz, instance, properties);
57 * Register a Global Service in the OSGi service registry
59 * @param clazz The target class
60 * @param instance of the object exporting the service, be careful
61 * the object must implement/extend clazz else the registration
62 * will fail unless a ServiceFactory is passed as parameter
63 * @param properties The properties to be attached to the service
65 * @return true if registration happened, false otherwise
67 public static boolean registerGlobalService(Class<?> clazz,
69 Dictionary<String, Object> properties) {
70 ServiceRegistration registration = registerGlobalServiceWReg(clazz, instance, properties);
71 if (registration == null) {
72 logger.error("Failed to register {} for instance {}", clazz, instance);
79 * Register a Service in the OSGi service registry and return the ServiceRegistration
81 * @param clazz The target class
82 * @param containerName The container name
83 * @param instance of the object exporting the service, be careful
84 * the object must implement/extend clazz else the registration
85 * will fail unless a ServiceFactory is passed as parameter
86 * @param properties The properties to be attached to the service
88 * @return the ServiceRegistration if registration happened, null otherwise
90 public static ServiceRegistration registerServiceWReg(Class<?> clazz, String containerName,
91 Object instance, Dictionary<String, Object> properties) {
92 if (properties == null) {
93 properties = (Dictionary<String, Object>) new Hashtable<String, Object>();
95 properties.put("containerName", containerName);
96 return registerGlobalServiceWReg(clazz, instance, properties);
100 * Register a Global Service in the OSGi service registry
102 * @param clazz The target class
103 * @param instance of the object exporting the service, be careful
104 * the object must implement/extend clazz else the registration
105 * will fail unless a ServiceFactory is passed as parameter
106 * @param properties The properties to be attached to the service
108 * @return the ServiceRegistration if registration happened, null otherwise
110 public static ServiceRegistration registerGlobalServiceWReg(Class<?> clazz,
112 Dictionary<String, Object> properties) {
114 BundleContext bCtx = FrameworkUtil.getBundle(instance.getClass()).getBundleContext();
116 logger.error("Could not retrieve the BundleContext");
120 ServiceRegistration registration = bCtx.registerService(clazz.getName(), instance, properties);
122 } catch (Exception e) {
123 logger.error("Exception {} while registering the service {}",
124 e.getMessage(), instance.toString());
130 * Retrieve instance of a class via OSGI registry, if there
131 * are many only the first is returned.
133 * @param clazz The target class
134 * @param containerName The container name
135 * @param bundle The caller
137 public static Object getInstance(Class<?> clazz, String containerName,
139 // Back-end convention: Container id in lower case. Let's enforce it here
140 return getInstance(clazz, containerName.toLowerCase(), bundle, null);
144 * Retrieve global instance of a class via OSGI registry, if
145 * there are many only the first is returned.
147 * @param clazz The target class
148 * @param bundle The caller
150 public static Object getGlobalInstance(Class<?> clazz, Object bundle) {
151 return getGlobalInstance(clazz, bundle, null);
155 * Retrieve instance of a class via OSGI registry, if there
156 * are many only the first is returned. On this version an LDAP
157 * type of filter is applied
159 * @param clazz The target class
160 * @param containerName The container name
161 * @param bundle The caller
162 * @param serviceFilter LDAP filter to be applied in the search
164 public static Object getInstance(Class<?> clazz, String containerName,
165 Object bundle, String serviceFilter) {
166 Object[] instances = getInstances(clazz, containerName, bundle,
168 if (instances != null) {
175 * Retrieve global instance of a class via OSGI registry, if
176 * there are many only the first is returned. On this version an LDAP
177 * type of filter is applied
179 * @param clazz The target class
180 * @param bundle The caller
181 * @param serviceFilter LDAP filter to be applied in the search
183 public static Object getGlobalInstance(Class<?> clazz, Object bundle,
184 String serviceFilter) {
185 Object[] instances = getGlobalInstances(clazz, bundle, serviceFilter);
186 if (instances != null) {
193 * Retrieve all the Instances of a Service, optionally
194 * filtered via serviceFilter if non-null else all the results are
197 * @param clazz The target class
198 * @param containerName The container name
199 * @param bundle The caller
200 * @param serviceFilter LDAP filter to be applied in the search
202 public static Object[] getInstances(Class<?> clazz, String containerName,
203 Object bundle, String serviceFilter) {
204 Object instances[] = null;
206 BundleContext bCtx = FrameworkUtil.getBundle(bundle.getClass())
209 ServiceReference[] services = null;
210 if (serviceFilter != null) {
211 services = bCtx.getServiceReferences(clazz.getName(),
212 "(&(containerName=" + containerName + ")"
213 + serviceFilter + ")");
215 services = bCtx.getServiceReferences(clazz.getName(),
216 "(containerName=" + containerName + ")");
219 if (services != null) {
220 instances = new Object[services.length];
221 for (int i = 0; i < services.length; i++) {
222 instances[i] = bCtx.getService(services[i]);
225 } catch (Exception e) {
226 logger.error("Instance reference is NULL");
232 * Retrieve all the Instances of a Service, optionally
233 * filtered via serviceFilter if non-null else all the results are
236 * @param clazz The target class
237 * @param bundle The caller
238 * @param serviceFilter LDAP filter to be applied in the search
240 public static Object[] getGlobalInstances(Class<?> clazz, Object bundle,
241 String serviceFilter) {
242 Object instances[] = null;
244 BundleContext bCtx = FrameworkUtil.getBundle(bundle.getClass())
247 ServiceReference[] services = bCtx.getServiceReferences(clazz
248 .getName(), serviceFilter);
250 if (services != null) {
251 instances = new Object[services.length];
252 for (int i = 0; i < services.length; i++) {
253 instances[i] = bCtx.getService(services[i]);
256 } catch (Exception e) {
257 logger.error("Instance reference is NULL");