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:")) {
59 fileUri = "file:" + uriFilePath;
63 return resourceLoader.getResource(getFullFileUri(fileUri));
65 return resourceLoader.getResource(fileUri);
70 public File getResourceAsFile(String uriFilePath, boolean isRelativePath) throws IOException {
72 Resource resource = getResource(uriFilePath, isRelativePath);
74 if (resource.exists()) {
75 return resource.getFile();
82 public byte[] getResourceAsBytes(String uriFilePath, boolean isRelativePath) throws IOException {
84 Resource resource = getResource(uriFilePath, isRelativePath);
86 if (resource.exists()) {
87 return getResourceAsBytes(resource);
93 public byte[] getResourceAsBytes(Resource resource) throws IOException {
95 if ( resource != null && resource.exists()) {
96 return Files.readAllBytes(Paths.get(resource.getFile().getAbsolutePath()));
102 public String getResourceAsString(String uriFilePath, boolean isRelativePath) throws IOException {
104 Resource resource = getResource(uriFilePath, isRelativePath);
106 if (resource.exists()) {
107 return new String(getResourceAsBytes(resource));
113 public String getConfigHomeEnvVar() {
114 return configHomeEnvVar;
117 public void setConfigHomeEnvVar(String configHomeEnvVar) {
118 this.configHomeEnvVar = configHomeEnvVar;