X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fsal%2Fapi%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Futils%2FServiceHelper.java;h=0237b9c499a599ffb646e7252aa723a8e18cd462;hb=57fecc4d1681146cef30525950dd74a7ff657850;hp=ef9f2f4bada62350d979e4ee0842e45018a9885e;hpb=c62c3615c0812460a8880f7ff0a1d3f6be548952;p=controller.git diff --git a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/utils/ServiceHelper.java b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/utils/ServiceHelper.java index ef9f2f4bad..0237b9c499 100644 --- a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/utils/ServiceHelper.java +++ b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/utils/ServiceHelper.java @@ -65,27 +65,65 @@ public class ServiceHelper { * @return true if registration happened, false otherwise */ public static boolean registerGlobalService(Class clazz, - Object instance, Dictionary properties) { + Object instance, + Dictionary properties) { + ServiceRegistration registration = registerGlobalServiceWReg(clazz, instance, properties); + if (registration == null) { + logger.error("Failed to register {} for instance {}", clazz, instance); + return false; + } + return true; + } + + /** + * Register a Service in the OSGi service registry and return the ServiceRegistration + * + * @param clazz The target class + * @param containerName The container name + * @param instance of the object exporting the service, be careful + * the object must implement/extend clazz else the registration + * will fail unless a ServiceFactory is passed as parameter + * @param properties The properties to be attached to the service + * registration + * @return the ServiceRegistration if registration happened, null otherwise + */ + public static ServiceRegistration registerServiceWReg(Class clazz, String containerName, + Object instance, Dictionary properties) { + if (properties == null) { + properties = (Dictionary) new Hashtable(); + } + properties.put("containerName", containerName); + return registerGlobalServiceWReg(clazz, instance, properties); + } + + /** + * Register a Global Service in the OSGi service registry + * + * @param clazz The target class + * @param instance of the object exporting the service, be careful + * the object must implement/extend clazz else the registration + * will fail unless a ServiceFactory is passed as parameter + * @param properties The properties to be attached to the service + * registration + * @return the ServiceRegistration if registration happened, null otherwise + */ + public static ServiceRegistration registerGlobalServiceWReg(Class clazz, + Object instance, + Dictionary properties) { try { - BundleContext bCtx = FrameworkUtil.getBundle(instance.getClass()) - .getBundleContext(); + BundleContext bCtx = FrameworkUtil.getBundle(instance.getClass()).getBundleContext(); if (bCtx == null) { logger.error("Could not retrieve the BundleContext"); - return false; + return null; } - ServiceRegistration registration = bCtx.registerService(clazz - .getName(), instance, properties); - if (registration == null) { - logger.error("Failed to register {} for instance {}", clazz, - instance); - } - return true; + ServiceRegistration registration = bCtx.registerService(clazz.getName(), instance, properties); + return registration; } catch (Exception e) { logger.error("Exception {} while registering the service {}", - e.getMessage(), instance.toString()); + e.getMessage(), instance.toString()); } - return false; + return null; } /**