0b69109f92d2aa9fbfe1a2304511af49aab800a7
[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.dblib.propertiesfileresolver;
22
23 import com.google.common.base.Strings;
24
25 import java.io.File;
26 import java.nio.file.Paths;
27 import java.util.Optional;
28
29 /**
30  * Resolves dblib properties files relative to the directory identified by the <code>SDNC_CONFIG_DIR</code>
31  * environment variable.
32  */
33 public class DblibEnvVarFileResolver implements DblibPropertiesFileResolver {
34
35     /**
36      * Key for environment variable representing the configuration directory
37      */
38     private static final String SDNC_CONFIG_DIR_PROP_KEY = "SDNC_CONFIG_DIR";
39
40     private final String successMessage;
41
42     public DblibEnvVarFileResolver(final String successMessage) {
43         this.successMessage = successMessage;
44     }
45
46     /**
47      * Parse a properties file location based on System environment variable
48      *
49      * @return an Optional File containing the location if it exists, or an empty Optional
50      */
51     @Override
52     public Optional<File> resolveFile(final String dblibFileName) {
53         // attempt to resolve the property directory from the corresponding environment variable
54         final String propDirectoryFromEnvVariable = System.getenv(SDNC_CONFIG_DIR_PROP_KEY);
55         final File fileFromEnvVariable;
56         if (!Strings.isNullOrEmpty(propDirectoryFromEnvVariable)) {
57             fileFromEnvVariable = Paths.get(propDirectoryFromEnvVariable).resolve(dblibFileName).toFile();
58             if(fileFromEnvVariable.exists()) {
59                 return Optional.of(fileFromEnvVariable);
60             }
61         }
62         return Optional.empty();
63     }
64
65     @Override
66     public String getSuccessfulResolutionMessage() {
67         return this.successMessage;
68     }
69 }