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> {
37 private final BlankTransactionServiceTracker blankTransactionServiceTracker;
38 private static final Logger logger = LoggerFactory.getLogger(ExtenderBundleTracker.class);
40 public ExtenderBundleTracker(BundleContext context, BlankTransactionServiceTracker blankTransactionServiceTracker) {
41 super(context, Bundle.ACTIVE, null);
42 this.blankTransactionServiceTracker = blankTransactionServiceTracker;
43 logger.trace("Registered as extender with context {}", context);
47 public Object addingBundle(Bundle bundle, BundleEvent event) {
48 URL resource = bundle.getEntry("META-INF/services/" + ModuleFactory.class.getName());
49 logger.trace("Got addingBundle event of bundle {}, resource {}, event {}",
50 bundle, resource, event);
51 if (resource != null) {
52 try (InputStream inputStream = resource.openStream()) {
53 List<String> lines = IOUtils.readLines(inputStream);
54 for (String factoryClassName : lines) {
55 registerFactory(factoryClassName, bundle);
57 } catch (Exception e) {
58 logger.error("Error while reading {}", resource, e);
59 throw new RuntimeException(e);
66 public void removedBundle(Bundle bundle, BundleEvent event, Object object) {
67 super.removedBundle(bundle,event,object);
68 // workaround for service tracker not getting removed service event
69 blankTransactionServiceTracker.blankTransaction();
73 private static ServiceRegistration<?> registerFactory(String factoryClassName, Bundle bundle) {
76 Class<?> clazz = bundle.loadClass(factoryClassName);
77 if (ModuleFactory.class.isAssignableFrom(clazz)) {
79 logger.debug("Registering {} in bundle {}",
80 clazz.getName(), bundle);
81 return bundle.getBundleContext().registerService(
82 ModuleFactory.class.getName(), clazz.newInstance(),
84 } catch (InstantiationException e) {
85 errorMessage = logMessage(
86 "Could not instantiate {} in bundle {}, reason {}",
87 factoryClassName, bundle, e);
88 } catch (IllegalAccessException e) {
89 errorMessage = logMessage(
90 "Illegal access during instatiation of class {} in bundle {}, reason {}",
91 factoryClassName, bundle, e);
94 errorMessage = logMessage(
95 "Class {} does not implement {} in bundle {}", clazz,
96 ModuleFactory.class, bundle);
98 } catch (ClassNotFoundException e) {
99 errorMessage = logMessage(
100 "Could not find class {} in bunde {}, reason {}",
101 factoryClassName, bundle, e);
103 throw new IllegalStateException(errorMessage);
106 public static String logMessage(String slfMessage, Object... params) {
107 logger.info(slfMessage, params);
108 String formatMessage = slfMessage.replaceAll("\\{\\}", "%s");
109 return format(formatMessage, params);