2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.sparky.config;
24 import java.io.IOException;
25 import java.nio.file.Files;
26 import java.nio.file.Paths;
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;
33 public class SparkyResourceLoader implements ResourceLoaderAware {
36 private static final String FILE_URI = "file:";
37 private ResourceLoader resourceLoader;
39 @Value("${CONFIG_HOME}") private String configHomeEnvVar;
41 // private static Logger LOG = LoggerFactory.getInstance().getLogger(SparkyResourceLoader.class);
44 public void setResourceLoader(ResourceLoader resourceLoader) {
45 this.resourceLoader = resourceLoader;
48 public String getFullFileUri(String uriFilePath) {
49 return FILE_URI + configHomeEnvVar + "/" + uriFilePath;
52 public String getAbsolutePath(String uriFilePath) {
53 return System.getProperty(configHomeEnvVar) + uriFilePath;
56 protected Resource getResource(String uriFilePath, boolean isRelative) {
58 String fileUri = uriFilePath;
60 if (!uriFilePath.startsWith(FILE_URI)) {
63 fileUri = getFullFileUri(fileUri);
65 fileUri = FILE_URI + uriFilePath;
70 return resourceLoader.getResource(fileUri);
74 public File getResourceAsFile(String uriFilePath, boolean isRelativePath) throws IOException {
76 Resource resource = getResource(uriFilePath, isRelativePath);
78 if (resource.exists()) {
79 return resource.getFile();
86 public byte[] getResourceAsBytes(String uriFilePath, boolean isRelativePath) throws IOException {
88 Resource resource = getResource(uriFilePath, isRelativePath);
90 if (resource.exists()) {
91 return getResourceAsBytes(resource);
97 public byte[] getResourceAsBytes(Resource resource) throws IOException {
99 if (resource != null && resource.exists()) {
100 return Files.readAllBytes(Paths.get(resource.getFile().getAbsolutePath()));
106 public String getResourceAsString(String uriFilePath, boolean isRelativePath) throws IOException {
108 Resource resource = getResource(uriFilePath, isRelativePath);
110 if (resource.exists()) {
111 return new String(getResourceAsBytes(resource));
117 public String getConfigHomeEnvVar() {
118 return configHomeEnvVar;
121 public void setConfigHomeEnvVar(String configHomeEnvVar) {
122 this.configHomeEnvVar = configHomeEnvVar;