Logging related enhancements.
[controller.git] / opendaylight / sal / api / src / main / java / org / opendaylight / controller / sal / utils / ServiceHelper.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
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
8  */
9
10 /**
11  * @file   ServiceHelper.java
12  *
13  * @brief  The class helps to register and retrieve OSGi service registry
14  */
15 package org.opendaylight.controller.sal.utils;
16
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;
25
26 /**
27  * The class helps to register and retrieve OSGi service registry
28  *
29  *
30  */
31 public class ServiceHelper {
32     private static final Logger logger = LoggerFactory
33             .getLogger(ServiceHelper.class);
34
35     /**
36      * Register a Service in the OSGi service registry
37      *
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
44      * registration
45      * @return true if registration happened, false otherwise
46      */
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>();
51         }
52         properties.put("containerName", containerName);
53         return registerGlobalService(clazz, instance, properties);
54     }
55
56     /**
57      * Register a Global Service in the OSGi service registry
58      *
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
64      * registration
65      * @return true if registration happened, false otherwise
66      */
67     public static boolean registerGlobalService(Class<?> clazz,
68             Object instance, Dictionary<String, Object> properties) {
69         try {
70             BundleContext bCtx = FrameworkUtil.getBundle(instance.getClass())
71                     .getBundleContext();
72             if (bCtx == null) {
73                 logger.error("Could not retrieve the BundleContext");
74                 return false;
75             }
76
77             ServiceRegistration registration = bCtx.registerService(clazz
78                     .getName(), instance, properties);
79             if (registration == null) {
80                 logger.error("Failed to register {} for instance {}", clazz,
81                         instance);
82             }
83             return true;
84         } catch (Exception e) {
85             logger.error("Exception {} while registering the service {}",
86                     e.getMessage(), instance.toString());
87         }
88         return false;
89     }
90
91     /**
92      * Retrieve instance of a class via OSGI registry, if there
93      * are many only the first is returned.
94      *
95      * @param clazz The target class
96      * @param containerName The container name
97      * @param bundle The caller
98      */
99     public static Object getInstance(Class<?> clazz, String containerName,
100             Object bundle) {
101         // Back-end convention: Container id in lower case. Let's enforce it here
102         return getInstance(clazz, containerName.toLowerCase(), bundle, null);
103     }
104
105     /**
106      * Retrieve global instance of a class via OSGI registry, if
107      * there are many only the first is returned.
108      *
109      * @param clazz The target class
110      * @param bundle The caller
111      */
112     public static Object getGlobalInstance(Class<?> clazz, Object bundle) {
113         return getGlobalInstance(clazz, bundle, null);
114     }
115
116     /**
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
120      *
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
125      */
126     public static Object getInstance(Class<?> clazz, String containerName,
127             Object bundle, String serviceFilter) {
128         Object[] instances = getInstances(clazz, containerName, bundle,
129                 serviceFilter);
130         if (instances != null) {
131             return instances[0];
132         }
133         return null;
134     }
135
136     /**
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
140      *
141      * @param clazz The target class
142      * @param bundle The caller
143      * @param serviceFilter LDAP filter to be applied in the search
144      */
145     public static Object getGlobalInstance(Class<?> clazz, Object bundle,
146             String serviceFilter) {
147         Object[] instances = getGlobalInstances(clazz, bundle, serviceFilter);
148         if (instances != null) {
149             return instances[0];
150         }
151         return null;
152     }
153
154     /**
155      * Retrieve all the Instances of a Service, optionally
156      * filtered via serviceFilter if non-null else all the results are
157      * returned if null
158      *
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
163      */
164     public static Object[] getInstances(Class<?> clazz, String containerName,
165             Object bundle, String serviceFilter) {
166         Object instances[] = null;
167         try {
168             BundleContext bCtx = FrameworkUtil.getBundle(bundle.getClass())
169                     .getBundleContext();
170
171             ServiceReference[] services = null;
172             if (serviceFilter != null) {
173                 services = bCtx.getServiceReferences(clazz.getName(),
174                         "(&(containerName=" + containerName + ")"
175                                 + serviceFilter + ")");
176             } else {
177                 services = bCtx.getServiceReferences(clazz.getName(),
178                         "(containerName=" + containerName + ")");
179             }
180
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]);
185                 }
186             }
187         } catch (Exception e) {
188             logger.error("Instance reference is NULL");
189         }
190         return instances;
191     }
192
193     /**
194      * Retrieve all the Instances of a Service, optionally
195      * filtered via serviceFilter if non-null else all the results are
196      * returned if null
197      *
198      * @param clazz The target class
199      * @param bundle The caller
200      * @param serviceFilter LDAP filter to be applied in the search
201      */
202     public static Object[] getGlobalInstances(Class<?> clazz, Object bundle,
203             String serviceFilter) {
204         Object instances[] = null;
205         try {
206             BundleContext bCtx = FrameworkUtil.getBundle(bundle.getClass())
207                     .getBundleContext();
208
209             ServiceReference[] services = bCtx.getServiceReferences(clazz
210                     .getName(), serviceFilter);
211
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]);
216                 }
217             }
218         } catch (Exception e) {
219             logger.error("Instance reference is NULL");
220         }
221         return instances;
222     }
223 }