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 static java.lang.String.format;
12 import java.io.InputStream;
14 import java.util.List;
16 import org.apache.commons.io.IOUtils;
17 import org.opendaylight.controller.config.spi.ModuleFactory;
18 import org.osgi.framework.Bundle;
19 import org.osgi.framework.BundleContext;
20 import org.osgi.framework.BundleEvent;
21 import org.osgi.framework.ServiceRegistration;
22 import org.osgi.util.tracker.BundleTracker;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
28 * OSGi extender that listens for bundle activation events. Reads file
29 * META-INF/services/org.opendaylight.controller.config.spi.ModuleFactory, each
30 * line should contain an implementation of ModuleFactory interface. Creates new
31 * instance with default constructor and registers it into OSGi service
32 * registry. There is no need for listening for implementing removedBundle as
33 * the services are unregistered automatically.
34 * Code based on http://www.toedter.com/blog/?p=236
36 public class ExtenderBundleTracker extends BundleTracker<Object> {
38 private static final Logger logger = LoggerFactory.getLogger(ExtenderBundleTracker.class);
40 public ExtenderBundleTracker(BundleContext context) {
41 super(context, Bundle.ACTIVE, null);
42 logger.trace("Registered as extender with context {}", context);
46 public Object addingBundle(Bundle bundle, BundleEvent event) {
47 URL resource = bundle.getEntry("META-INF/services/" + ModuleFactory.class.getName());
48 logger.trace("Got addingBundle event of bundle {}, resource {}, event {}",
49 bundle, resource, event);
50 if (resource != null) {
51 try (InputStream inputStream = resource.openStream()) {
52 List<String> lines = IOUtils.readLines(inputStream);
53 for (String factoryClassName : lines) {
54 registerFactory(factoryClassName, bundle);
56 } catch (Exception e) {
57 logger.error("Error while reading {}", resource, e);
58 throw new RuntimeException(e);
65 public void removedBundle(Bundle bundle, BundleEvent event, Object object) {
66 super.removedBundle(bundle,event,object);
70 private static ServiceRegistration<?> registerFactory(String factoryClassName, Bundle bundle) {
73 Class<?> clazz = bundle.loadClass(factoryClassName);
74 if (ModuleFactory.class.isAssignableFrom(clazz)) {
76 logger.debug("Registering {} in bundle {}",
77 clazz.getName(), bundle);
78 return bundle.getBundleContext().registerService(
79 ModuleFactory.class.getName(), clazz.newInstance(),
81 } catch (InstantiationException e) {
82 errorMessage = logMessage(
83 "Could not instantiate {} in bundle {}, reason {}",
84 factoryClassName, bundle, e);
85 } catch (IllegalAccessException e) {
86 errorMessage = logMessage(
87 "Illegal access during instatiation of class {} in bundle {}, reason {}",
88 factoryClassName, bundle, e);
91 errorMessage = logMessage(
92 "Class {} does not implement {} in bundle {}", clazz,
93 ModuleFactory.class, bundle);
95 } catch (ClassNotFoundException e) {
96 errorMessage = logMessage(
97 "Could not find class {} in bunde {}, reason {}",
98 factoryClassName, bundle, e);
100 throw new IllegalStateException(errorMessage);
103 public static String logMessage(String slfMessage, Object... params) {
104 logger.info(slfMessage, params);
105 String formatMessage = slfMessage.replaceAll("\\{\\}", "%s");
106 return format(formatMessage, params);