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(), serviceName); switch (eventType) { case ServiceEvent.REGISTERED: final T wrappedService = ContextClassLoaderHelper.getWrappedServiceWithCorrectContextClassLoader(theService); registration.registerService(desc, wrappedService); break; case ServiceEvent.UNREGISTERING: registration.unregisterService(desc.getRegistrationName()); break; default: break; } return true; }
@Override @BeforeMethod(groups = "fast") public void beforeMethod() { if (hasFailed()) { return; } super.beforeMethod(); for (final String name : pluginRegistry.getAllServices()) { pluginRegistry.unregisterService(name); } }