356009f3f03e2d15d555383d70e9cea4e210128f
[ccsdk/sli.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * onap
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.ccsdk.sli.core.utils.common;
22
23 import java.io.File;
24 import java.nio.file.Path;
25 import java.nio.file.Paths;
26 import java.util.Optional;
27
28 import org.onap.ccsdk.sli.core.utils.PropertiesFileResolver;
29 import org.osgi.framework.FrameworkUtil;
30 import org.slf4j.LoggerFactory;
31
32 import com.google.common.base.Strings;
33
34 /**
35  * Resolves properties files from runtime property value <code>SDNC_CONFIG_DIR</code> defined in the osgi properties.
36  */
37 public class BundleContextFileResolver implements PropertiesFileResolver {
38
39     /**
40      * Key for osgi variable representing the configuration directory
41      */
42     private static final String SDNC_CONFIG_DIR_PROP_KEY = "SDNC_CONFIG_DIR";
43
44     private final String successMessage;
45     private final Class<?> clazz;
46
47     public BundleContextFileResolver(final String successMessage, final Class<?> clazz) {
48         this.successMessage = successMessage;
49         this.clazz = clazz;
50     }
51
52     /**
53      * Parse a properties file location based on JRE argument
54      *
55      * @return an Optional File containing the location if it exists, or an empty Optional
56      */
57     @Override
58     public Optional<File> resolveFile(final String filename) {
59         try {
60             if (FrameworkUtil.getBundle(clazz) == null) {
61                 return Optional.empty();
62             } else {
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();
67                     }
68                     final Path dblibPath = Paths.get(pathProperty);
69                     return Optional.of(dblibPath.resolve(filename).toFile());
70
71             }
72         } catch (Exception|NoClassDefFoundError e) {
73             LoggerFactory.getLogger(this.getClass()).error("", e);
74             return Optional.empty();
75         }
76     }
77
78     @Override
79     public String getSuccessfulResolutionMessage() {
80         return this.successMessage;
81     }
82 }