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
32 public class ServiceHelper {
33 private static final Logger logger = LoggerFactory
34 .getLogger(ServiceHelper.class);
37 * Register a Service in the OSGi service registry
39 * @param clazz The target class
40 * @param containerName The container name
41 * @param instance of the object exporting the service, be careful
42 * the object must implement/extend clazz else the registration
43 * will fail unless a ServiceFactory is passed as parameter
44 * @param properties The properties to be attached to the service
46 * @return true if registration happened, false otherwise
48 public static boolean registerService(Class<?> clazz, String containerName,
49 Object instance, Dictionary<String, Object> properties) {
50 if (properties == null) {
51 properties = (Dictionary<String, Object>) new Hashtable<String, Object>();
53 properties.put("containerName", containerName);
54 return registerGlobalService(clazz, instance, properties);
58 * Register a Global Service in the OSGi service registry
60 * @param clazz The target class
61 * @param instance of the object exporting the service, be careful
62 * the object must implement/extend clazz else the registration
63 * will fail unless a ServiceFactory is passed as parameter
64 * @param properties The properties to be attached to the service
66 * @return true if registration happened, false otherwise
68 public static boolean registerGlobalService(Class<?> clazz,
70 Dictionary<String, Object> properties) {
71 ServiceRegistration<?> registration = registerGlobalServiceWReg(clazz, instance, properties);
72 if (registration == null) {
73 logger.error("Failed to register {} for instance {}", clazz, instance);
80 * Register a Service in the OSGi service registry and return the ServiceRegistration
82 * @param clazz The target class
83 * @param containerName The container name
84 * @param instance of the object exporting the service, be careful
85 * the object must implement/extend clazz else the registration
86 * will fail unless a ServiceFactory is passed as parameter
87 * @param properties The properties to be attached to the service
89 * @return the ServiceRegistration if registration happened, null otherwise
91 public static ServiceRegistration<?> registerServiceWReg(Class<?> clazz, String containerName,
92 Object instance, Dictionary<String, Object> properties) {
93 if (properties == null) {
94 properties = (Dictionary<String, Object>) new Hashtable<String, Object>();
96 properties.put("containerName", containerName);
97 return registerGlobalServiceWReg(clazz, instance, properties);
101 * Register a Global Service in the OSGi service registry
103 * @param clazz The target class
104 * @param instance of the object exporting the service, be careful
105 * the object must implement/extend clazz else the registration
106 * will fail unless a ServiceFactory is passed as parameter
107 * @param properties The properties to be attached to the service
109 * @return the ServiceRegistration if registration happened, null otherwise
111 public static ServiceRegistration<?> registerGlobalServiceWReg(Class<?> clazz,
113 Dictionary<String, Object> properties) {
115 BundleContext bCtx = FrameworkUtil.getBundle(instance.getClass()).getBundleContext();
117 logger.error("Could not retrieve the BundleContext");
121 ServiceRegistration<?> registration = bCtx.registerService(clazz.getName(), instance, properties);
123 } catch (Exception e) {
124 logger.error("Exception {} while registering the service {}",
125 e.getMessage(), instance.toString());
131 * Retrieve instance of a class via OSGI registry, if there
132 * are many only the first is returned.
134 * @param clazz The target class
135 * @param containerName The container name
136 * @param bundle The caller
138 public static Object getInstance(Class<?> clazz, String containerName,
140 // Back-end convention: Container id in lower case. Let's enforce it here
141 return getInstance(clazz, containerName.toLowerCase(), bundle, null);
145 * Retrieve global instance of a class via OSGI registry, if
146 * there are many only the first is returned.
148 * @param clazz The target class
149 * @param bundle The caller
151 public static Object getGlobalInstance(Class<?> clazz, Object bundle) {
152 return getGlobalInstance(clazz, bundle, null);
156 * Retrieve instance of a class via OSGI registry, if there
157 * are many only the first is returned. On this version an LDAP
158 * type of filter is applied
160 * @param clazz The target class
161 * @param containerName The container name
162 * @param bundle The caller
163 * @param serviceFilter LDAP filter to be applied in the search
165 public static Object getInstance(Class<?> clazz, String containerName,
166 Object bundle, String serviceFilter) {
167 Object[] instances = getInstances(clazz, containerName, bundle,
169 if (instances != null) {
176 * Retrieve global instance of a class via OSGI registry, if
177 * there are many only the first is returned. On this version an LDAP
178 * type of filter is applied
180 * @param clazz The target class
181 * @param bundle The caller
182 * @param serviceFilter LDAP filter to be applied in the search
184 public static Object getGlobalInstance(Class<?> clazz, Object bundle,
185 String serviceFilter) {
186 Object[] instances = getGlobalInstances(clazz, bundle, serviceFilter);
187 if (instances != 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 containerName The container name
200 * @param bundle The caller
201 * @param serviceFilter LDAP filter to be applied in the search
203 public static Object[] getInstances(Class<?> clazz, String containerName,
204 Object bundle, String serviceFilter) {
205 Object instances[] = null;
207 BundleContext bCtx = FrameworkUtil.getBundle(bundle.getClass())
210 ServiceReference<?>[] services = null;
211 if (serviceFilter != null) {
212 services = bCtx.getServiceReferences(clazz.getName(),
213 "(&(containerName=" + containerName + ")"
214 + serviceFilter + ")");
216 services = bCtx.getServiceReferences(clazz.getName(),
217 "(containerName=" + containerName + ")");
220 if (services != null) {
221 instances = new Object[services.length];
222 for (int i = 0; i < services.length; i++) {
223 instances[i] = bCtx.getService(services[i]);
226 } catch (Exception e) {
227 logger.error("Instance reference is NULL");
233 * Retrieve all the Instances of a Service, optionally
234 * filtered via serviceFilter if non-null else all the results are
237 * @param clazz The target class
238 * @param bundle The caller
239 * @param serviceFilter LDAP filter to be applied in the search
241 public static Object[] getGlobalInstances(Class<?> clazz, Object bundle,
242 String serviceFilter) {
243 Object instances[] = null;
245 BundleContext bCtx = FrameworkUtil.getBundle(bundle.getClass())
248 ServiceReference<?>[] services = bCtx.getServiceReferences(clazz
249 .getName(), serviceFilter);
251 if (services != null) {
252 instances = new Object[services.length];
253 for (int i = 0; i < services.length; i++) {
254 instances[i] = bCtx.getService(services[i]);
257 } catch (Exception e) {
258 logger.error("Instance reference is NULL");