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.context.ResourceLoaderAware;
29 import org.springframework.core.io.Resource;
30 import org.springframework.core.io.ResourceLoader;
32 public class SparkyResourceLoader implements ResourceLoaderAware {
35 private static final String FILE_URI = "file:";
36 private ResourceLoader resourceLoader;
37 private String configHomeEnvVar;
39 // private static Logger LOG = LoggerFactory.getInstance().getLogger(SparkyResourceLoader.class);
42 public void setResourceLoader(ResourceLoader resourceLoader) {
43 this.resourceLoader = resourceLoader;
46 public String getFullFileUri(String uriFilePath) {
47 return FILE_URI + System.getProperty(configHomeEnvVar) + uriFilePath;
50 public String getAbsolutePath(String uriFilePath) {
51 return System.getProperty(configHomeEnvVar) + uriFilePath;
54 protected Resource getResource(String uriFilePath, boolean isRelative) {
56 String fileUri = uriFilePath;
58 if (!uriFilePath.startsWith(FILE_URI)) {
61 fileUri = getFullFileUri(fileUri);
63 fileUri = FILE_URI + uriFilePath;
68 return resourceLoader.getResource(fileUri);
72 public File getResourceAsFile(String uriFilePath, boolean isRelativePath) throws IOException {
74 Resource resource = getResource(uriFilePath, isRelativePath);
76 if (resource.exists()) {
77 return resource.getFile();
84 public byte[] getResourceAsBytes(String uriFilePath, boolean isRelativePath) throws IOException {
86 Resource resource = getResource(uriFilePath, isRelativePath);
88 if (resource.exists()) {
89 return getResourceAsBytes(resource);
95 public byte[] getResourceAsBytes(Resource resource) throws IOException {
97 if (resource != null && resource.exists()) {
98 return Files.readAllBytes(Paths.get(resource.getFile().getAbsolutePath()));
104 public String getResourceAsString(String uriFilePath, boolean isRelativePath) throws IOException {
106 Resource resource = getResource(uriFilePath, isRelativePath);
108 if (resource.exists()) {
109 return new String(getResourceAsBytes(resource));
115 public String getConfigHomeEnvVar() {
116 return configHomeEnvVar;
119 public void setConfigHomeEnvVar(String configHomeEnvVar) {
120 this.configHomeEnvVar = configHomeEnvVar;