aboutsummaryrefslogtreecommitdiffstats
path: root/sli/provider/src/main/java/org/onap/ccsdk/sli/core/sli/provider/SvcLogicClassResolver.java
blob: d2b733fc3cd09019af44c431fc7edcb28cc7be4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package org.onap.ccsdk.sli.core.sli.provider;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SvcLogicClassResolver {
    private static final Logger LOG = LoggerFactory.getLogger(SvcLogicClassResolver.class);

    public static Object resolve(String className) {

        Bundle bundle = FrameworkUtil.getBundle(SvcLogicClassResolver.class);

        if (bundle == null) {
            // Running outside OSGi container (e.g. jUnit).  Use Reflection
            // to resolve class
            try {
                return(Class.forName(className).newInstance());
            } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {

                LOG.error("Could not resolve class "+className, e);
                return null;
            }

        } else {
            BundleContext bctx = bundle.getBundleContext();
            ServiceReference sref = bctx.getServiceReference(className);
            if (sref != null) {
                return bctx.getService(sref);
            } else {

                LOG.warn("Could not find service reference object for class " + className);
                return null;
            }
        }
    }

}