diff options
Diffstat (limited to 'sli/provider/src/main/java/org/onap/ccsdk')
3 files changed, 64 insertions, 71 deletions
diff --git a/sli/provider/src/main/java/org/onap/ccsdk/sli/core/sli/provider/SvcLogicAdaptorFactory.java b/sli/provider/src/main/java/org/onap/ccsdk/sli/core/sli/provider/SvcLogicAdaptorFactory.java index 7316db57..ecfe6b69 100644 --- a/sli/provider/src/main/java/org/onap/ccsdk/sli/core/sli/provider/SvcLogicAdaptorFactory.java +++ b/sli/provider/src/main/java/org/onap/ccsdk/sli/core/sli/provider/SvcLogicAdaptorFactory.java @@ -8,9 +8,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -35,7 +35,7 @@ public class SvcLogicAdaptorFactory { private static final Logger LOG = LoggerFactory .getLogger(SvcLogicAdaptorFactory.class); - private static HashMap<String, SvcLogicAdaptor> adaptorMap = new HashMap<String, SvcLogicAdaptor>(); + private static HashMap<String, SvcLogicAdaptor> adaptorMap = new HashMap<>(); public static void registerAdaptor(SvcLogicAdaptor adaptor) { String name = adaptor.getClass().getName(); @@ -51,36 +51,18 @@ public class SvcLogicAdaptorFactory { } } - public static SvcLogicAdaptor getInstance(String name) { - if (adaptorMap.containsKey(name)) { - return (adaptorMap.get(name)); - } else { - BundleContext bctx = null; - try - { - bctx = FrameworkUtil.getBundle(SvcLogicAdaptorFactory.class) - .getBundleContext(); - } - catch (Exception e) - { - LOG.debug("Caught exception trying to locate device adaptor "+name, e); - return(null); - } - - ServiceReference sref = bctx.getServiceReference(name); + public static SvcLogicAdaptor getInstance(String name) { + if (adaptorMap.containsKey(name)) { + return adaptorMap.get(name); + } else { - if (sref != null) { - SvcLogicAdaptor adaptor = (SvcLogicAdaptor) bctx - .getService(sref); + SvcLogicAdaptor adaptor = (SvcLogicAdaptor) SvcLogicClassResolver.resolve(name); - if (adaptor != null) { - registerAdaptor(adaptor); + if (adaptor != null) { + registerAdaptor(adaptor); + } - return (adaptor); - } - return (null); - } - } - return(null); - } + return adaptor; + } + } } diff --git a/sli/provider/src/main/java/org/onap/ccsdk/sli/core/sli/provider/SvcLogicClassResolver.java b/sli/provider/src/main/java/org/onap/ccsdk/sli/core/sli/provider/SvcLogicClassResolver.java new file mode 100644 index 00000000..d2b733fc --- /dev/null +++ b/sli/provider/src/main/java/org/onap/ccsdk/sli/core/sli/provider/SvcLogicClassResolver.java @@ -0,0 +1,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; + } + } + } + +} diff --git a/sli/provider/src/main/java/org/onap/ccsdk/sli/core/sli/provider/SvcLogicNodeExecutor.java b/sli/provider/src/main/java/org/onap/ccsdk/sli/core/sli/provider/SvcLogicNodeExecutor.java index 593c972a..f34c261d 100644 --- a/sli/provider/src/main/java/org/onap/ccsdk/sli/core/sli/provider/SvcLogicNodeExecutor.java +++ b/sli/provider/src/main/java/org/onap/ccsdk/sli/core/sli/provider/SvcLogicNodeExecutor.java @@ -29,6 +29,7 @@ import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin; import org.onap.ccsdk.sli.core.sli.SvcLogicNode; import org.onap.ccsdk.sli.core.sli.SvcLogicRecorder; import org.onap.ccsdk.sli.core.sli.SvcLogicResource; +import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.FrameworkUtil; import org.osgi.framework.ServiceReference; @@ -53,56 +54,25 @@ public abstract class SvcLogicNodeExecutor { } + protected SvcLogicAdaptor getAdaptor(String adaptorName) { return SvcLogicAdaptorFactory.getInstance(adaptorName); } protected SvcLogicResource getSvcLogicResource(String plugin) { - BundleContext bctx = FrameworkUtil.getBundle(this.getClass()) - .getBundleContext(); - - ServiceReference sref = bctx.getServiceReference(plugin); - if (sref != null) { - SvcLogicResource resourcePlugin = (SvcLogicResource) bctx - .getService(sref); - return resourcePlugin; - } - else { - LOG.warn("Could not find service reference object for plugin " + plugin); - return null; - } + + return((SvcLogicResource) SvcLogicClassResolver.resolve(plugin)); } protected SvcLogicRecorder getSvcLogicRecorder(String plugin) { - BundleContext bctx = FrameworkUtil.getBundle(this.getClass()) - .getBundleContext(); - - ServiceReference sref = bctx.getServiceReference(plugin); - if (sref != null) { - SvcLogicRecorder resourcePlugin = (SvcLogicRecorder) bctx - .getService(sref); - return resourcePlugin; - } - else { - return null; - } + return((SvcLogicRecorder) SvcLogicClassResolver.resolve(plugin)); } protected SvcLogicJavaPlugin getSvcLogicJavaPlugin(String pluginName){ - BundleContext bctx = FrameworkUtil.getBundle(this.getClass()) - .getBundleContext(); - - ServiceReference sref = bctx.getServiceReference(pluginName); - - if (sref == null) { - LOG.warn("Could not find service reference object for plugin " + pluginName); - return null; - } else { - SvcLogicJavaPlugin plugin = (SvcLogicJavaPlugin) bctx - .getService(sref); - return plugin; - } - } + return((SvcLogicJavaPlugin) SvcLogicClassResolver.resolve(pluginName)); + + } + protected SvcLogicNode getNextNode(SvcLogicNode node, String outValue) { MetricLogger.resetContext(); SvcLogicNode nextNode = node.getOutcomeValue(outValue); |