Add test cases for sli
[ccsdk/sli/core.git] / sli / provider / src / main / java / org / onap / ccsdk / sli / core / sli / provider / SvcLogicClassResolver.java
1 package org.onap.ccsdk.sli.core.sli.provider;
2
3 import org.osgi.framework.Bundle;
4 import org.osgi.framework.BundleContext;
5 import org.osgi.framework.FrameworkUtil;
6 import org.osgi.framework.ServiceReference;
7 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory;
9
10 public class SvcLogicClassResolver {
11     private static final Logger LOG = LoggerFactory.getLogger(SvcLogicClassResolver.class);
12
13     public static Object resolve(String className) {
14
15         Bundle bundle = FrameworkUtil.getBundle(SvcLogicClassResolver.class);
16
17         if (bundle == null) {
18             // Running outside OSGi container (e.g. jUnit).  Use Reflection
19             // to resolve class
20             try {
21                 return(Class.forName(className).newInstance());
22             } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
23
24                 LOG.error("Could not resolve class "+className, e);
25                 return null;
26             }
27
28         } else {
29             BundleContext bctx = bundle.getBundleContext();
30             ServiceReference sref = bctx.getServiceReference(className);
31             if (sref != null) {
32                 return bctx.getService(sref);
33             } else {
34
35                 LOG.warn("Could not find service reference object for class " + className);
36                 return null;
37             }
38         }
39     }
40
41 }