2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2016 - 2017 ONAP
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.ccsdk.sli.core.utils.common;
24 import java.nio.file.Path;
25 import java.nio.file.Paths;
26 import java.util.Optional;
28 import org.onap.ccsdk.sli.core.utils.PropertiesFileResolver;
29 import org.osgi.framework.FrameworkUtil;
30 import org.slf4j.LoggerFactory;
32 import com.google.common.base.Strings;
35 * Resolves properties files from runtime property value <code>SDNC_CONFIG_DIR</code> defined in the osgi properties.
37 public class BundleContextFileResolver implements PropertiesFileResolver {
40 * Key for osgi variable representing the configuration directory
42 private static final String SDNC_CONFIG_DIR_PROP_KEY = "SDNC_CONFIG_DIR";
44 private final String successMessage;
45 private final Class<?> clazz;
47 public BundleContextFileResolver(final String successMessage, final Class<?> clazz) {
48 this.successMessage = successMessage;
53 * Parse a properties file location based on JRE argument
55 * @return an Optional File containing the location if it exists, or an empty Optional
58 public Optional<File> resolveFile(final String filename) {
60 if (FrameworkUtil.getBundle(clazz) == null) {
61 return Optional.empty();
63 final String pathProperty = FrameworkUtil.getBundle(this.clazz).getBundleContext()
64 .getProperty(SDNC_CONFIG_DIR_PROP_KEY);
65 if (Strings.isNullOrEmpty(pathProperty)) {
66 return Optional.empty();
68 final Path dblibPath = Paths.get(pathProperty);
69 return Optional.of(dblibPath.resolve(filename).toFile());
72 } catch (Exception|NoClassDefFoundError e) {
73 LoggerFactory.getLogger(this.getClass()).error("", e);
74 return Optional.empty();
79 public String getSuccessfulResolutionMessage() {
80 return this.successMessage;