*/
package org.opendaylight.controller.config.manager.impl.osgi;
-import static java.lang.String.format;
-
import com.google.common.annotations.VisibleForTesting;
-import java.io.InputStream;
+import com.google.common.io.Resources;
+import java.io.IOException;
import java.net.URL;
-import java.util.List;
-
-import org.apache.commons.io.IOUtils;
+import java.nio.charset.StandardCharsets;
import org.opendaylight.controller.config.spi.ModuleFactory;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-
/**
* OSGi extender that listens for bundle activation events. Reads file
* META-INF/services/org.opendaylight.controller.config.spi.ModuleFactory, each
* the services are unregistered automatically.
* Code based on http://www.toedter.com/blog/?p=236
*/
-public class ModuleFactoryBundleTracker implements BundleTrackerCustomizer<Object> {
+public class ModuleFactoryBundleTracker implements BundleTrackerCustomizer<Boolean> {
private final BlankTransactionServiceTracker blankTransactionServiceTracker;
- private static final Logger logger = LoggerFactory.getLogger(ModuleFactoryBundleTracker.class);
+ private static final Logger LOG = LoggerFactory.getLogger(ModuleFactoryBundleTracker.class);
public ModuleFactoryBundleTracker(BlankTransactionServiceTracker blankTransactionServiceTracker) {
this.blankTransactionServiceTracker = blankTransactionServiceTracker;
}
@Override
- public Object addingBundle(Bundle bundle, BundleEvent event) {
+ public Boolean addingBundle(Bundle bundle, BundleEvent event) {
URL resource = bundle.getEntry("META-INF/services/" + ModuleFactory.class.getName());
- logger.trace("Got addingBundle event of bundle {}, resource {}, event {}",
+ LOG.trace("Got addingBundle event of bundle {}, resource {}, event {}",
bundle, resource, event);
if (resource != null) {
- try (InputStream inputStream = resource.openStream()) {
- List<String> lines = IOUtils.readLines(inputStream);
- for (String factoryClassName : lines) {
+ try {
+ for (String factoryClassName : Resources.readLines(resource, StandardCharsets.UTF_8)) {
registerFactory(factoryClassName, bundle);
}
- } catch (Exception e) {
- logger.error("Error while reading {}", resource, e);
+
+ return Boolean.TRUE;
+ } catch (IOException e) {
+ LOG.error("Error while reading {}", resource, e);
throw new RuntimeException(e);
}
}
- return bundle;
+
+ return Boolean.FALSE;
}
@Override
- public void modifiedBundle(Bundle bundle, BundleEvent event, Object object) {
+ public void modifiedBundle(Bundle bundle, BundleEvent event, Boolean hasFactory) {
// NOOP
}
@Override
- public void removedBundle(Bundle bundle, BundleEvent event, Object object) {
- // workaround for service tracker not getting removed service event
- blankTransactionServiceTracker.blankTransaction();
+ public void removedBundle(Bundle bundle, BundleEvent event, Boolean hasFactory) {
+ if(hasFactory) {
+ // workaround for service tracker not getting removed service event
+ blankTransactionServiceTracker.blankTransactionSync();
+ }
}
@VisibleForTesting
Class<?> clazz = bundle.loadClass(factoryClassName);
if (ModuleFactory.class.isAssignableFrom(clazz)) {
try {
- logger.debug("Registering {} in bundle {}",
+ LOG.debug("Registering {} in bundle {}",
clazz.getName(), bundle);
return bundle.getBundleContext().registerService(
ModuleFactory.class.getName(), clazz.newInstance(),
}
public static String logMessage(String slfMessage, Object... params) {
- logger.info(slfMessage, params);
+ LOG.info(slfMessage, params);
String formatMessage = slfMessage.replaceAll("\\{\\}", "%s");
- return format(formatMessage, params);
+ return String.format(formatMessage, params);
}
}