private <T> boolean listenForServiceType( final ServiceReference serviceReference, final int eventType, final Class<T> claz, final OSGIServiceRegistration<T> registration) { // Make sure we can retrieve the plugin name final String serviceName = (String) serviceReference.getProperty(OSGIPluginProperties.PLUGIN_NAME_PROP); if (serviceName == null || !checkSanityPluginRegistrationName(serviceName)) { // Quite common for non Killbill bundles logger.debug( "Ignoring registered OSGI service {} with no {} property", claz.getName(), OSGIPluginProperties.PLUGIN_NAME_PROP); return true; } final Object theServiceObject = context.getService(serviceReference); // Is that for us? We look for a subclass here for greater flexibility (e.g. HttpServlet for a // Servlet service) if (theServiceObject == null || !claz.isAssignableFrom(theServiceObject.getClass())) { return false; } final T theService = (T) theServiceObject; final OSGIServiceDescriptor desc = new DefaultOSGIServiceDescriptor( serviceReference.getBundle().getSymbolicName(), bundleRegistry.getPluginName(serviceReference.getBundle()), serviceName); switch (eventType) { case ServiceEvent.REGISTERED: final T wrappedService = ContextClassLoaderHelper.getWrappedServiceWithCorrectContextClassLoader( theService, registration.getServiceType(), serviceName, metricsRegistry); registration.registerService(desc, wrappedService); bundleRegistry.registerService(desc, registration.getServiceType().getName()); break; case ServiceEvent.UNREGISTERING: registration.unregisterService(desc.getRegistrationName()); bundleRegistry.unregisterService(desc, registration.getServiceType().getName()); break; default: break; } return true; }
@Override public void serviceChanged(final ServiceEvent event) { if (context == null || (event.getType() != ServiceEvent.REGISTERED && event.getType() != ServiceEvent.UNREGISTERING)) { // We are not initialized or uninterested return; } final ServiceReference serviceReference = event.getServiceReference(); for (final OSGIServiceRegistration cur : allRegistrationHandlers) { if (listenForServiceType(serviceReference, event.getType(), cur.getServiceType(), cur)) { break; } } }