Initial code drop of yang model driven configuration system
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / osgi / ExtenderBundleTracker.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.config.manager.impl.osgi;
9
10 import static java.lang.String.format;
11
12 import java.io.InputStream;
13 import java.net.URL;
14 import java.util.List;
15
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;
26
27 /**
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
35  */
36
37 public class ExtenderBundleTracker extends BundleTracker<Object> {
38
39     private static final Logger logger = LoggerFactory
40             .getLogger(ExtenderBundleTracker.class);
41
42     public ExtenderBundleTracker(BundleContext context) {
43         super(context, Bundle.ACTIVE, null);
44         logger.trace("Registered as extender with context {}", context);
45     }
46
47     @Override
48     public Object addingBundle(Bundle bundle, BundleEvent event) {
49         URL resource = bundle.getEntry("META-INF/services/"
50                 + ModuleFactory.class.getName());
51         logger.trace(
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);
59                 }
60             } catch (Exception e) {
61                 logger.error("Error while reading {}, stopping bundle {}",
62                         resource, bundle, e);
63                 stopBundleQuietly(bundle);
64                 throw new RuntimeException(e);
65             }
66
67         }
68         return bundle;
69     }
70
71     private static void stopBundleQuietly(Bundle bundle) {
72         try {
73             bundle.stop();
74         } catch (BundleException e2) {
75             logger.warn(
76                     "Ignoring fact that bundle.stop failed on {}, reason {}",
77                     bundle, e2.toString());
78         }
79     }
80
81     // TODO:test
82     private static ServiceRegistration<?> registerFactory(
83             String factoryClassName, Bundle bundle) {
84         String errorMessage;
85         try {
86             Class<?> clazz = bundle.loadClass(factoryClassName);
87             if (ModuleFactory.class.isAssignableFrom(clazz)) {
88                 try {
89                     logger.debug("Registering {} in bundle {}",
90                             clazz.getName(), bundle);
91                     return bundle.getBundleContext().registerService(
92                             ModuleFactory.class.getName(), clazz.newInstance(),
93                             null);
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);
102                 }
103             } else {
104                 errorMessage = logMessage(
105                         "Class {} does not implement {} in bundle {}", clazz,
106                         ModuleFactory.class, bundle);
107             }
108         } catch (ClassNotFoundException e) {
109             errorMessage = logMessage(
110                     "Could not find class {} in bunde {}, reason {}",
111                     factoryClassName, bundle, e);
112         }
113         throw new IllegalStateException(errorMessage);
114     }
115
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);
120     }
121 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.