6663cddacfbbcbd6e143c1c5a488720a7f4d158d
[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.ServiceRegistration;
22 import org.osgi.util.tracker.BundleTracker;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
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.
34  * Code based on http://www.toedter.com/blog/?p=236
35  */
36 public class ExtenderBundleTracker extends BundleTracker<Object> {
37
38     private static final Logger logger = LoggerFactory.getLogger(ExtenderBundleTracker.class);
39
40     public ExtenderBundleTracker(BundleContext context) {
41         super(context, Bundle.ACTIVE, null);
42         logger.trace("Registered as extender with context {}", context);
43     }
44
45     @Override
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);
55                 }
56             } catch (Exception e) {
57                 logger.error("Error while reading {}", resource, e);
58                 throw new RuntimeException(e);
59             }
60         }
61         return bundle;
62     }
63
64     @Override
65     public void removedBundle(Bundle bundle, BundleEvent event, Object object) {
66         super.removedBundle(bundle,event,object);
67     }
68
69     // TODO:test
70     private static ServiceRegistration<?> registerFactory(String factoryClassName, Bundle bundle) {
71         String errorMessage;
72         try {
73             Class<?> clazz = bundle.loadClass(factoryClassName);
74             if (ModuleFactory.class.isAssignableFrom(clazz)) {
75                 try {
76                     logger.debug("Registering {} in bundle {}",
77                             clazz.getName(), bundle);
78                     return bundle.getBundleContext().registerService(
79                             ModuleFactory.class.getName(), clazz.newInstance(),
80                             null);
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);
89                 }
90             } else {
91                 errorMessage = logMessage(
92                         "Class {} does not implement {} in bundle {}", clazz,
93                         ModuleFactory.class, bundle);
94             }
95         } catch (ClassNotFoundException e) {
96             errorMessage = logMessage(
97                     "Could not find class {} in bunde {}, reason {}",
98                     factoryClassName, bundle, e);
99         }
100         throw new IllegalStateException(errorMessage);
101     }
102
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);
107     }
108 }