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,
68 Object instance, Dictionary<String, Object> properties) {
70 BundleContext bCtx = FrameworkUtil.getBundle(instance.getClass())
73 logger.error("Could not retrieve the BundleContext");
77 ServiceRegistration registration = bCtx.registerService(clazz
78 .getName(), instance, properties);
79 if (registration == null) {
80 logger.error("Failed to register {} for instance {}", clazz,
84 } catch (Exception e) {
85 logger.error("Exception {} while registering the service {}",
86 e.getMessage(), instance.toString());
92 * Retrieve instance of a class via OSGI registry, if there
93 * are many only the first is returned.
95 * @param clazz The target class
96 * @param containerName The container name
97 * @param bundle The caller
99 public static Object getInstance(Class<?> clazz, String containerName,
101 // Back-end convention: Container id in lower case. Let's enforce it here
102 return getInstance(clazz, containerName.toLowerCase(), bundle, null);
106 * Retrieve global instance of a class via OSGI registry, if
107 * there are many only the first is returned.
109 * @param clazz The target class
110 * @param bundle The caller
112 public static Object getGlobalInstance(Class<?> clazz, Object bundle) {
113 return getGlobalInstance(clazz, bundle, null);
117 * Retrieve instance of a class via OSGI registry, if there
118 * are many only the first is returned. On this version an LDAP
119 * type of filter is applied
121 * @param clazz The target class
122 * @param containerName The container name
123 * @param bundle The caller
124 * @param serviceFilter LDAP filter to be applied in the search
126 public static Object getInstance(Class<?> clazz, String containerName,
127 Object bundle, String serviceFilter) {
128 Object[] instances = getInstances(clazz, containerName, bundle,
130 if (instances != null) {
137 * Retrieve global instance of a class via OSGI registry, if
138 * there are many only the first is returned. On this version an LDAP
139 * type of filter is applied
141 * @param clazz The target class
142 * @param bundle The caller
143 * @param serviceFilter LDAP filter to be applied in the search
145 public static Object getGlobalInstance(Class<?> clazz, Object bundle,
146 String serviceFilter) {
147 Object[] instances = getGlobalInstances(clazz, bundle, serviceFilter);
148 if (instances != null) {
155 * Retrieve all the Instances of a Service, optionally
156 * filtered via serviceFilter if non-null else all the results are
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[] getInstances(Class<?> clazz, String containerName,
165 Object bundle, String serviceFilter) {
166 Object instances[] = null;
168 BundleContext bCtx = FrameworkUtil.getBundle(bundle.getClass())
171 ServiceReference[] services = null;
172 if (serviceFilter != null) {
173 services = bCtx.getServiceReferences(clazz.getName(),
174 "(&(containerName=" + containerName + ")"
175 + serviceFilter + ")");
177 services = bCtx.getServiceReferences(clazz.getName(),
178 "(containerName=" + containerName + ")");
181 if (services != null) {
182 instances = new Object[services.length];
183 for (int i = 0; i < services.length; i++) {
184 instances[i] = bCtx.getService(services[i]);
187 } catch (Exception e) {
188 logger.error("Instance reference is NULL");
194 * Retrieve all the Instances of a Service, optionally
195 * filtered via serviceFilter if non-null else all the results are
198 * @param clazz The target class
199 * @param bundle The caller
200 * @param serviceFilter LDAP filter to be applied in the search
202 public static Object[] getGlobalInstances(Class<?> clazz, Object bundle,
203 String serviceFilter) {
204 Object instances[] = null;
206 BundleContext bCtx = FrameworkUtil.getBundle(bundle.getClass())
209 ServiceReference[] services = bCtx.getServiceReferences(clazz
210 .getName(), serviceFilter);
212 if (services != null) {
213 instances = new Object[services.length];
214 for (int i = 0; i < services.length; i++) {
215 instances[i] = bCtx.getService(services[i]);
218 } catch (Exception e) {
219 logger.error("Instance reference is NULL");