673ccbf03afb03e56e3873cde0909bbe67ff237c
[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.URISyntaxException;
25 import java.net.URL;
26 import java.nio.file.Path;
27 import java.nio.file.Paths;
28 import java.util.Optional;
29 import org.onap.ccsdk.sli.core.dblib.DBLIBResourceProvider;
30 import org.osgi.framework.FrameworkUtil;
31
32 /**
33  * Resolves dblib properties files relative to the directory identified by the JRE property
34  * <code>dblib.properties</code>.
35  */
36 public class DblibJREFileResolver implements DblibPropertiesFileResolver {
37
38     /**
39      * Key for JRE argument representing the configuration directory
40      */
41     private static final String DBLIB_JRE_PROPERTY_KEY = "dblib.properties";
42
43     private final String successMessage;
44
45     public DblibJREFileResolver(final String successMessage) {
46         this.successMessage = successMessage;
47     }
48
49     /**
50      * Parse a properties file location based on JRE argument
51      *
52      * @return an Optional File containing the location if it exists, or an empty Optional
53      */
54     @Override
55     public Optional<File> resolveFile(final String dblibFileName) {
56         final URL jreArgumentUrl = FrameworkUtil.getBundle(DBLIBResourceProvider.class)
57                 .getResource(DBLIB_JRE_PROPERTY_KEY);
58         try {
59             if (jreArgumentUrl == null) {
60                 return Optional.empty();
61             }
62             final Path dblibPath = Paths.get(jreArgumentUrl.toURI());
63             return Optional.of(dblibPath.resolve(dblibFileName).toFile());
64         } catch(final URISyntaxException e) {
65             return Optional.empty();
66         }
67     }
68
69     @Override
70     public String getSuccessfulResolutionMessage() {
71         return this.successMessage;
72     }
73 }