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