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.BundleException;
22 import org.osgi.framework.ServiceRegistration;
23 import org.osgi.util.tracker.BundleTracker;
24 import org.slf4j.Logger;
25 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. Code based on
34 * http://www.toedter.com/blog/?p=236
37 public class ExtenderBundleTracker extends BundleTracker<Object> {
39 private static final Logger logger = LoggerFactory
40 .getLogger(ExtenderBundleTracker.class);
42 public ExtenderBundleTracker(BundleContext context) {
43 super(context, Bundle.ACTIVE, null);
44 logger.trace("Registered as extender with context {}", context);
48 public Object addingBundle(Bundle bundle, BundleEvent event) {
49 URL resource = bundle.getEntry("META-INF/services/"
50 + ModuleFactory.class.getName());
52 "Got addingBundle event of bundle {}, resource {}, event {}",
53 bundle, resource, event);
54 if (resource != null) {
55 try (InputStream inputStream = resource.openStream()) {
56 List<String> lines = IOUtils.readLines(inputStream);
57 for (String factoryClassName : lines) {
58 registerFactory(factoryClassName, bundle);
60 } catch (Exception e) {
61 logger.error("Error while reading {}, stopping bundle {}",
63 stopBundleQuietly(bundle);
64 throw new RuntimeException(e);
71 private static void stopBundleQuietly(Bundle bundle) {
74 } catch (BundleException e2) {
76 "Ignoring fact that bundle.stop failed on {}, reason {}",
77 bundle, e2.toString());
82 private static ServiceRegistration<?> registerFactory(
83 String factoryClassName, Bundle bundle) {
86 Class<?> clazz = bundle.loadClass(factoryClassName);
87 if (ModuleFactory.class.isAssignableFrom(clazz)) {
89 logger.debug("Registering {} in bundle {}",
90 clazz.getName(), bundle);
91 return bundle.getBundleContext().registerService(
92 ModuleFactory.class.getName(), clazz.newInstance(),
94 } catch (InstantiationException e) {
95 errorMessage = logMessage(
96 "Could not instantiate {} in bundle {}, reason {}",
97 factoryClassName, bundle, e);
98 } catch (IllegalAccessException e) {
99 errorMessage = logMessage(
100 "Illegal access during instatiation of class {} in bundle {}, reason {}",
101 factoryClassName, bundle, e);
104 errorMessage = logMessage(
105 "Class {} does not implement {} in bundle {}", clazz,
106 ModuleFactory.class, bundle);
108 } catch (ClassNotFoundException e) {
109 errorMessage = logMessage(
110 "Could not find class {} in bunde {}, reason {}",
111 factoryClassName, bundle, e);
113 throw new IllegalStateException(errorMessage);
116 public static String logMessage(String slfMessage, Object... params) {
117 logger.info(slfMessage, params);
118 String formatMessage = slfMessage.replaceAll("\\{\\}", "%s");
119 return format(formatMessage, params);