2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.config.manager.impl.osgi;
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.io.Resources;
12 import java.io.IOException;
14 import java.nio.charset.StandardCharsets;
15 import org.opendaylight.controller.config.spi.ModuleFactory;
16 import org.osgi.framework.Bundle;
17 import org.osgi.framework.BundleEvent;
18 import org.osgi.framework.ServiceRegistration;
19 import org.osgi.util.tracker.BundleTrackerCustomizer;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 * OSGi extender that listens for bundle activation events. Reads file
25 * META-INF/services/org.opendaylight.controller.config.spi.ModuleFactory, each
26 * line should contain an implementation of ModuleFactory interface. Creates new
27 * instance with default constructor and registers it into OSGi service
28 * registry. There is no need for listening for implementing removedBundle as
29 * the services are unregistered automatically.
30 * Code based on http://www.toedter.com/blog/?p=236
32 public class ModuleFactoryBundleTracker implements BundleTrackerCustomizer<Boolean> {
33 private final BlankTransactionServiceTracker blankTransactionServiceTracker;
34 private static final Logger LOG = LoggerFactory.getLogger(ModuleFactoryBundleTracker.class);
36 public ModuleFactoryBundleTracker(final BlankTransactionServiceTracker blankTransactionServiceTracker) {
37 this.blankTransactionServiceTracker = blankTransactionServiceTracker;
41 public Boolean addingBundle(final Bundle bundle, final BundleEvent event) {
42 URL resource = bundle.getEntry("META-INF/services/" + ModuleFactory.class.getName());
43 LOG.trace("Got addingBundle event of bundle {}, resource {}, event {}",
44 bundle, resource, event);
45 if (resource != null) {
47 for (String factoryClassName : Resources.readLines(resource, StandardCharsets.UTF_8)) {
48 registerFactory(factoryClassName, bundle);
52 } catch (final IOException e) {
53 LOG.error("Error while reading {}", resource, e);
54 throw new RuntimeException(e);
62 public void modifiedBundle(final Bundle bundle, final BundleEvent event, final Boolean hasFactory) {
67 public void removedBundle(final Bundle bundle, final BundleEvent event, final Boolean hasFactory) {
69 // workaround for service tracker not getting removed service event
70 blankTransactionServiceTracker.blankTransactionSync();
75 protected static ServiceRegistration<?> registerFactory(final String factoryClassName, final Bundle bundle) {
79 Class<?> clazz = bundle.loadClass(factoryClassName);
80 if (ModuleFactory.class.isAssignableFrom(clazz)) {
82 LOG.debug("Registering {} in bundle {}",
83 clazz.getName(), bundle);
84 return bundle.getBundleContext().registerService(
85 ModuleFactory.class.getName(), clazz.newInstance(),
87 } catch (final InstantiationException e) {
88 errorMessage = logMessage(
89 "Could not instantiate {} in bundle {}, reason {}",
90 factoryClassName, bundle, e);
92 } catch (final IllegalAccessException e) {
93 errorMessage = logMessage(
94 "Illegal access during instantiation of class {} in bundle {}, reason {}",
95 factoryClassName, bundle, e);
99 errorMessage = logMessage(
100 "Class {} does not implement {} in bundle {}", clazz,
101 ModuleFactory.class, bundle);
103 } catch (final ClassNotFoundException e) {
104 errorMessage = logMessage(
105 "Could not find class {} in bundle {}, reason {}",
106 factoryClassName, bundle, e);
110 throw ex == null ? new IllegalStateException(errorMessage) : new IllegalStateException(errorMessage, ex);
113 public static String logMessage(final String slfMessage, final Object... params) {
114 LOG.info(slfMessage, params);
115 String formatMessage = slfMessage.replaceAll("\\{\\}", "%s");
116 return String.format(formatMessage, params);