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 "+e.getMessage() +" while registering the service "+instance.toString());
91 * Retrieve instance of a class via OSGI registry, if there
92 * are many only the first is returned.
94 * @param clazz The target class
95 * @param containerName The container name
96 * @param bundle The caller
98 public static Object getInstance(Class<?> clazz, String containerName,
100 // Back-end convention: Container id in lower case. Let's enforce it here
101 return getInstance(clazz, containerName.toLowerCase(), bundle, null);
105 * Retrieve global instance of a class via OSGI registry, if
106 * there are many only the first is returned.
108 * @param clazz The target class
109 * @param bundle The caller
111 public static Object getGlobalInstance(Class<?> clazz, Object bundle) {
112 return getGlobalInstance(clazz, bundle, null);
116 * Retrieve instance of a class via OSGI registry, if there
117 * are many only the first is returned. On this version an LDAP
118 * type of filter is applied
120 * @param clazz The target class
121 * @param containerName The container name
122 * @param bundle The caller
123 * @param serviceFilter LDAP filter to be applied in the search
125 public static Object getInstance(Class<?> clazz, String containerName,
126 Object bundle, String serviceFilter) {
127 Object[] instances = getInstances(clazz, containerName, bundle,
129 if (instances != null) {
136 * Retrieve global instance of a class via OSGI registry, if
137 * there are many only the first is returned. On this version an LDAP
138 * type of filter is applied
140 * @param clazz The target class
141 * @param bundle The caller
142 * @param serviceFilter LDAP filter to be applied in the search
144 public static Object getGlobalInstance(Class<?> clazz, Object bundle,
145 String serviceFilter) {
146 Object[] instances = getGlobalInstances(clazz, bundle, serviceFilter);
147 if (instances != null) {
154 * Retrieve all the Instances of a Service, optionally
155 * filtered via serviceFilter if non-null else all the results are
158 * @param clazz The target class
159 * @param containerName The container name
160 * @param bundle The caller
161 * @param serviceFilter LDAP filter to be applied in the search
163 public static Object[] getInstances(Class<?> clazz, String containerName,
164 Object bundle, String serviceFilter) {
165 Object instances[] = null;
167 BundleContext bCtx = FrameworkUtil.getBundle(bundle.getClass())
170 ServiceReference[] services = null;
171 if (serviceFilter != null) {
172 services = bCtx.getServiceReferences(clazz.getName(),
173 "(&(containerName=" + containerName + ")"
174 + serviceFilter + ")");
176 services = bCtx.getServiceReferences(clazz.getName(),
177 "(containerName=" + containerName + ")");
180 if (services != null) {
181 instances = new Object[services.length];
182 for (int i = 0; i < services.length; i++) {
183 instances[i] = bCtx.getService(services[i]);
186 } catch (Exception e) {
187 logger.error("Instance reference is 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 bundle The caller
199 * @param serviceFilter LDAP filter to be applied in the search
201 public static Object[] getGlobalInstances(Class<?> clazz, Object bundle,
202 String serviceFilter) {
203 Object instances[] = null;
205 BundleContext bCtx = FrameworkUtil.getBundle(bundle.getClass())
208 ServiceReference[] services = bCtx.getServiceReferences(clazz
209 .getName(), serviceFilter);
211 if (services != null) {
212 instances = new Object[services.length];
213 for (int i = 0; i < services.length; i++) {
214 instances[i] = bCtx.getService(services[i]);
217 } catch (Exception e) {
218 logger.error("Instance reference is NULL");