Add ModuleFactory#getDefaultModules method to config-api.
[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.api.jmx.CommitStatus;
18 import org.opendaylight.controller.config.manager.impl.ConfigRegistryImpl;
19 import org.opendaylight.controller.config.spi.ModuleFactory;
20 import org.osgi.framework.Bundle;
21 import org.osgi.framework.BundleContext;
22 import org.osgi.framework.BundleEvent;
23 import org.osgi.framework.BundleException;
24 import org.osgi.framework.ServiceRegistration;
25 import org.osgi.util.tracker.BundleTracker;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import javax.management.ObjectName;
30
31 /**
32  * OSGi extender that listens for bundle activation events. Reads file
33  * META-INF/services/org.opendaylight.controller.config.spi.ModuleFactory, each
34  * line should contain an implementation of ModuleFactory interface. Creates new
35  * instance with default constructor and registers it into OSGi service
36  * registry. There is no need for listening for implementing removedBundle as
37  * the services are unregistered automatically.
38  * Code based on http://www.toedter.com/blog/?p=236
39  */
40 public class ExtenderBundleTracker extends BundleTracker<Object> {
41
42     private static final Logger logger = LoggerFactory.getLogger(ExtenderBundleTracker.class);
43
44     public ExtenderBundleTracker(BundleContext context) {
45         super(context, Bundle.ACTIVE, null);
46         logger.trace("Registered as extender with context {}", context);
47     }
48
49     @Override
50     public Object addingBundle(Bundle bundle, BundleEvent event) {
51         URL resource = bundle.getEntry("META-INF/services/" + ModuleFactory.class.getName());
52         logger.trace("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 {}", resource, e);
62                 throw new RuntimeException(e);
63             }
64         }
65         return bundle;
66     }
67
68     @Override
69     public void removedBundle(Bundle bundle, BundleEvent event, Object object) {
70         super.removedBundle(bundle,event,object);
71     }
72
73     // TODO:test
74     private static ServiceRegistration<?> registerFactory(String factoryClassName, Bundle bundle) {
75         String errorMessage;
76         try {
77             Class<?> clazz = bundle.loadClass(factoryClassName);
78             if (ModuleFactory.class.isAssignableFrom(clazz)) {
79                 try {
80                     logger.debug("Registering {} in bundle {}",
81                             clazz.getName(), bundle);
82                     return bundle.getBundleContext().registerService(
83                             ModuleFactory.class.getName(), clazz.newInstance(),
84                             null);
85                 } catch (InstantiationException e) {
86                     errorMessage = logMessage(
87                             "Could not instantiate {} in bundle {}, reason {}",
88                             factoryClassName, bundle, e);
89                 } catch (IllegalAccessException e) {
90                     errorMessage = logMessage(
91                             "Illegal access during instatiation of class {} in bundle {}, reason {}",
92                             factoryClassName, bundle, e);
93                 }
94             } else {
95                 errorMessage = logMessage(
96                         "Class {} does not implement {} in bundle {}", clazz,
97                         ModuleFactory.class, bundle);
98             }
99         } catch (ClassNotFoundException e) {
100             errorMessage = logMessage(
101                     "Could not find class {} in bunde {}, reason {}",
102                     factoryClassName, bundle, e);
103         }
104         throw new IllegalStateException(errorMessage);
105     }
106
107     public static String logMessage(String slfMessage, Object... params) {
108         logger.info(slfMessage, params);
109         String formatMessage = slfMessage.replaceAll("\\{\\}", "%s");
110         return format(formatMessage, params);
111     }
112 }