Release sparky 2.0.5 image
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / config / SparkyResourceLoader.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.sparky.config;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.nio.file.Files;
26 import java.nio.file.Paths;
27
28 import org.springframework.beans.factory.annotation.Value;
29 import org.springframework.context.ResourceLoaderAware;
30 import org.springframework.core.io.Resource;
31 import org.springframework.core.io.ResourceLoader;
32
33 public class SparkyResourceLoader implements ResourceLoaderAware {
34
35
36   private static final String FILE_URI = "file:";
37   private ResourceLoader resourceLoader;
38   
39   @Value("${CONFIG_HOME}") private String configHomeEnvVar;
40
41   // private static Logger LOG = LoggerFactory.getInstance().getLogger(SparkyResourceLoader.class);
42
43   @Override
44   public void setResourceLoader(ResourceLoader resourceLoader) {
45     this.resourceLoader = resourceLoader;
46   }
47
48   public String getFullFileUri(String uriFilePath) {
49     return FILE_URI + configHomeEnvVar + "/" + uriFilePath;
50   }
51
52   public String getAbsolutePath(String uriFilePath) {
53     return System.getProperty(configHomeEnvVar) + uriFilePath;
54   }
55
56   protected Resource getResource(String uriFilePath, boolean isRelative) {
57
58     String fileUri = uriFilePath;
59
60     if (!uriFilePath.startsWith(FILE_URI)) {
61
62       if (isRelative) {
63         fileUri = getFullFileUri(fileUri);
64       } else {
65         fileUri = FILE_URI + uriFilePath;
66       }
67
68     }
69
70     return resourceLoader.getResource(fileUri);
71
72   }
73
74   public File getResourceAsFile(String uriFilePath, boolean isRelativePath) throws IOException {
75
76     Resource resource = getResource(uriFilePath, isRelativePath);
77
78     if (resource.exists()) {
79       return resource.getFile();
80     }
81
82     return null;
83
84   }
85
86   public byte[] getResourceAsBytes(String uriFilePath, boolean isRelativePath) throws IOException {
87
88     Resource resource = getResource(uriFilePath, isRelativePath);
89
90     if (resource.exists()) {
91       return getResourceAsBytes(resource);
92     }
93
94     return null;
95   }
96
97   public byte[] getResourceAsBytes(Resource resource) throws IOException {
98
99     if (resource != null && resource.exists()) {
100       return Files.readAllBytes(Paths.get(resource.getFile().getAbsolutePath()));
101     }
102
103     return null;
104   }
105
106   public String getResourceAsString(String uriFilePath, boolean isRelativePath) throws IOException {
107
108     Resource resource = getResource(uriFilePath, isRelativePath);
109
110     if (resource.exists()) {
111       return new String(getResourceAsBytes(resource));
112     }
113
114     return null;
115   }
116
117   public String getConfigHomeEnvVar() {
118     return configHomeEnvVar;
119   }
120
121   public void setConfigHomeEnvVar(String configHomeEnvVar) {
122     this.configHomeEnvVar = configHomeEnvVar;
123   }
124
125 }