Merge "Added the Buffer-Id to packet-in."
[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
17 import org.apache.commons.io.IOUtils;
18 import org.opendaylight.controller.config.spi.ModuleFactory;
19 import org.osgi.framework.Bundle;
20 import org.osgi.framework.BundleEvent;
21 import org.osgi.framework.ServiceRegistration;
22 import org.osgi.util.tracker.BundleTrackerCustomizer;
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 ModuleFactoryBundleTracker implements BundleTrackerCustomizer<Object> {
37     private final BlankTransactionServiceTracker blankTransactionServiceTracker;
38     private static final Logger logger = LoggerFactory.getLogger(ModuleFactoryBundleTracker.class);
39
40     public ModuleFactoryBundleTracker(BlankTransactionServiceTracker blankTransactionServiceTracker) {
41         this.blankTransactionServiceTracker = blankTransactionServiceTracker;
42     }
43
44     @Override
45     public Object addingBundle(Bundle bundle, BundleEvent event) {
46         URL resource = bundle.getEntry("META-INF/services/" + ModuleFactory.class.getName());
47         logger.trace("Got addingBundle event of bundle {}, resource {}, event {}",
48                 bundle, resource, event);
49         if (resource != null) {
50             try (InputStream inputStream = resource.openStream()) {
51                 List<String> lines = IOUtils.readLines(inputStream);
52                 for (String factoryClassName : lines) {
53                     registerFactory(factoryClassName, bundle);
54                 }
55             } catch (Exception e) {
56                 logger.error("Error while reading {}", resource, e);
57                 throw new RuntimeException(e);
58             }
59         }
60         return bundle;
61     }
62
63     @Override
64     public void modifiedBundle(Bundle bundle, BundleEvent event, Object object) {
65         // NOOP
66     }
67
68     @Override
69     public void removedBundle(Bundle bundle, BundleEvent event, Object object) {
70         // workaround for service tracker not getting removed service event
71         blankTransactionServiceTracker.blankTransaction();
72     }
73
74     @VisibleForTesting
75     protected static ServiceRegistration<?> registerFactory(String factoryClassName, Bundle bundle) {
76         String errorMessage;
77         Exception ex = null;
78         try {
79             Class<?> clazz = bundle.loadClass(factoryClassName);
80             if (ModuleFactory.class.isAssignableFrom(clazz)) {
81                 try {
82                     logger.debug("Registering {} in bundle {}",
83                             clazz.getName(), bundle);
84                     return bundle.getBundleContext().registerService(
85                             ModuleFactory.class.getName(), clazz.newInstance(),
86                             null);
87                 } catch (InstantiationException e) {
88                     errorMessage = logMessage(
89                             "Could not instantiate {} in bundle {}, reason {}",
90                             factoryClassName, bundle, e);
91                     ex = e;
92                 } catch (IllegalAccessException e) {
93                     errorMessage = logMessage(
94                             "Illegal access during instantiation of class {} in bundle {}, reason {}",
95                             factoryClassName, bundle, e);
96                     ex = e;
97                 }
98             } else {
99                 errorMessage = logMessage(
100                         "Class {} does not implement {} in bundle {}", clazz,
101                         ModuleFactory.class, bundle);
102             }
103         } catch (ClassNotFoundException e) {
104             errorMessage = logMessage(
105                     "Could not find class {} in bundle {}, reason {}",
106                     factoryClassName, bundle, e);
107             ex = e;
108         }
109
110         throw ex == null ? new IllegalStateException(errorMessage) : new IllegalStateException(errorMessage, ex);
111     }
112
113     public static String logMessage(String slfMessage, Object... params) {
114         logger.info(slfMessage, params);
115         String formatMessage = slfMessage.replaceAll("\\{\\}", "%s");
116         return format(formatMessage, params);
117     }
118 }