d2b164e3b30b97c46c3769b4c933ca27eea33add
[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 java.io.File;
24 import java.net.URL;
25 import java.util.Optional;
26 import org.onap.ccsdk.sli.core.dblib.DBLIBResourceProvider;
27
28 /**
29  * Resolves dblib properties files relative to the karaf root directory.
30  */
31 public class DblibKarafRootFileResolver implements DblibPropertiesFileResolver {
32
33     final DBLIBResourceProvider dblibResourceProvider;
34
35     private final String successMessage;
36
37     public DblibKarafRootFileResolver(final String successMessage, final DBLIBResourceProvider dblibResourceProvider) {
38         this.successMessage = successMessage;
39         this.dblibResourceProvider = dblibResourceProvider;
40     }
41
42     /**
43      * Parse a properties file location relative to the karaf root
44      *
45      * @return an Optional File containing the location if it exists, or an empty Optional
46      */
47     @Override
48     public Optional<File> resolveFile(final String dblibFileName) {
49         final URL fromKarafRoot = dblibResourceProvider.getClass().getResource(dblibFileName);
50         if (fromKarafRoot != null) {
51             final File propertiesFile = new File(fromKarafRoot.getFile());
52             if (propertiesFile.exists()) {
53                 return Optional.of(propertiesFile);
54             }
55             return Optional.empty();
56         }
57         return Optional.empty();
58     }
59
60     @Override
61     public String getSuccessfulResolutionMessage() {
62         return this.successMessage;
63     }
64 }