Merge "Bug 2267: Reuse leaf and leaf set entry builders in NormalizedNodeInputStreamR...
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / osgi / ModuleFactoryBundleTracker.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 com.google.common.annotations.VisibleForTesting;
13 import java.io.InputStream;
14 import java.net.URL;
15 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.BundleEvent;
20 import org.osgi.framework.ServiceRegistration;
21 import org.osgi.util.tracker.BundleTrackerCustomizer;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25
26 /**
27  * OSGi extender that listens for bundle activation events. Reads file
28  * META-INF/services/org.opendaylight.controller.config.spi.ModuleFactory, each
29  * line should contain an implementation of ModuleFactory interface. Creates new
30  * instance with default constructor and registers it into OSGi service
31  * registry. There is no need for listening for implementing removedBundle as
32  * the services are unregistered automatically.
33  * Code based on http://www.toedter.com/blog/?p=236
34  */
35 public class ModuleFactoryBundleTracker implements BundleTrackerCustomizer<Object> {
36     private final BlankTransactionServiceTracker blankTransactionServiceTracker;
37     private static final Logger LOG = LoggerFactory.getLogger(ModuleFactoryBundleTracker.class);
38
39     public ModuleFactoryBundleTracker(BlankTransactionServiceTracker blankTransactionServiceTracker) {
40         this.blankTransactionServiceTracker = blankTransactionServiceTracker;
41     }
42
43     @Override
44     public Object addingBundle(Bundle bundle, BundleEvent event) {
45         URL resource = bundle.getEntry("META-INF/services/" + ModuleFactory.class.getName());
46         LOG.trace("Got addingBundle event of bundle {}, resource {}, event {}",
47                 bundle, resource, event);
48         if (resource != null) {
49             try (InputStream inputStream = resource.openStream()) {
50                 List<String> lines = IOUtils.readLines(inputStream);
51                 for (String factoryClassName : lines) {
52                     registerFactory(factoryClassName, bundle);
53                 }
54             } catch (Exception e) {
55                 LOG.error("Error while reading {}", resource, e);
56                 throw new RuntimeException(e);
57             }
58         }
59         return bundle;
60     }
61
62     @Override
63     public void modifiedBundle(Bundle bundle, BundleEvent event, Object object) {
64         // NOOP
65     }
66
67     @Override
68     public void removedBundle(Bundle bundle, BundleEvent event, Object object) {
69         // workaround for service tracker not getting removed service event
70         blankTransactionServiceTracker.blankTransaction();
71     }
72
73     @VisibleForTesting
74     protected static ServiceRegistration<?> registerFactory(String factoryClassName, Bundle bundle) {
75         String errorMessage;
76         Exception ex = null;
77         try {
78             Class<?> clazz = bundle.loadClass(factoryClassName);
79             if (ModuleFactory.class.isAssignableFrom(clazz)) {
80                 try {
81                     LOG.debug("Registering {} in bundle {}",
82                             clazz.getName(), bundle);
83                     return bundle.getBundleContext().registerService(
84                             ModuleFactory.class.getName(), clazz.newInstance(),
85                             null);
86                 } catch (InstantiationException e) {
87                     errorMessage = logMessage(
88                             "Could not instantiate {} in bundle {}, reason {}",
89                             factoryClassName, bundle, e);
90                     ex = e;
91                 } catch (IllegalAccessException e) {
92                     errorMessage = logMessage(
93                             "Illegal access during instantiation of class {} in bundle {}, reason {}",
94                             factoryClassName, bundle, e);
95                     ex = e;
96                 }
97             } else {
98                 errorMessage = logMessage(
99                         "Class {} does not implement {} in bundle {}", clazz,
100                         ModuleFactory.class, bundle);
101             }
102         } catch (ClassNotFoundException e) {
103             errorMessage = logMessage(
104                     "Could not find class {} in bundle {}, reason {}",
105                     factoryClassName, bundle, e);
106             ex = e;
107         }
108
109         throw ex == null ? new IllegalStateException(errorMessage) : new IllegalStateException(errorMessage, ex);
110     }
111
112     public static String logMessage(String slfMessage, Object... params) {
113         LOG.info(slfMessage, params);
114         String formatMessage = slfMessage.replaceAll("\\{\\}", "%s");
115         return format(formatMessage, params);
116     }
117 }