Refactor shutdown-impl: add parameter to RPC, remove secret validation and masking.
[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     private final BlankTransactionServiceTracker blankTransactionServiceTracker;
38     private static final Logger logger = LoggerFactory.getLogger(ExtenderBundleTracker.class);
39
40     public ExtenderBundleTracker(BundleContext context, BlankTransactionServiceTracker blankTransactionServiceTracker) {
41         super(context, Bundle.ACTIVE, null);
42         this.blankTransactionServiceTracker = blankTransactionServiceTracker;
43         logger.trace("Registered as extender with context {}", context);
44     }
45
46     @Override
47     public Object addingBundle(Bundle bundle, BundleEvent event) {
48         URL resource = bundle.getEntry("META-INF/services/" + ModuleFactory.class.getName());
49         logger.trace("Got addingBundle event of bundle {}, resource {}, event {}",
50                 bundle, resource, event);
51         if (resource != null) {
52             try (InputStream inputStream = resource.openStream()) {
53                 List<String> lines = IOUtils.readLines(inputStream);
54                 for (String factoryClassName : lines) {
55                     registerFactory(factoryClassName, bundle);
56                 }
57             } catch (Exception e) {
58                 logger.error("Error while reading {}", resource, e);
59                 throw new RuntimeException(e);
60             }
61         }
62         return bundle;
63     }
64
65     @Override
66     public void removedBundle(Bundle bundle, BundleEvent event, Object object) {
67         super.removedBundle(bundle,event,object);
68         // workaround for service tracker not getting removed service event
69         blankTransactionServiceTracker.blankTransaction();
70     }
71
72     // TODO:test
73     private static ServiceRegistration<?> registerFactory(String factoryClassName, Bundle bundle) {
74         String errorMessage;
75         try {
76             Class<?> clazz = bundle.loadClass(factoryClassName);
77             if (ModuleFactory.class.isAssignableFrom(clazz)) {
78                 try {
79                     logger.debug("Registering {} in bundle {}",
80                             clazz.getName(), bundle);
81                     return bundle.getBundleContext().registerService(
82                             ModuleFactory.class.getName(), clazz.newInstance(),
83                             null);
84                 } catch (InstantiationException e) {
85                     errorMessage = logMessage(
86                             "Could not instantiate {} in bundle {}, reason {}",
87                             factoryClassName, bundle, e);
88                 } catch (IllegalAccessException e) {
89                     errorMessage = logMessage(
90                             "Illegal access during instatiation of class {} in bundle {}, reason {}",
91                             factoryClassName, bundle, e);
92                 }
93             } else {
94                 errorMessage = logMessage(
95                         "Class {} does not implement {} in bundle {}", clazz,
96                         ModuleFactory.class, bundle);
97             }
98         } catch (ClassNotFoundException e) {
99             errorMessage = logMessage(
100                     "Could not find class {} in bunde {}, reason {}",
101                     factoryClassName, bundle, e);
102         }
103         throw new IllegalStateException(errorMessage);
104     }
105
106     public static String logMessage(String slfMessage, Object... params) {
107         logger.info(slfMessage, params);
108         String formatMessage = slfMessage.replaceAll("\\{\\}", "%s");
109         return format(formatMessage, params);
110     }
111 }