2 * ============LICENSE_START===================================================
3 * SPARKY (AAI UI service)
4 * ============================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=====================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
25 package org.onap.aai.sparky.config;
28 import java.io.IOException;
29 import java.nio.file.Files;
30 import java.nio.file.Paths;
32 import org.springframework.context.ResourceLoaderAware;
33 import org.springframework.core.io.Resource;
34 import org.springframework.core.io.ResourceLoader;
36 public class SparkyResourceLoader implements ResourceLoaderAware {
39 private static final String FILE_URI = "file:";
40 private ResourceLoader resourceLoader;
41 private String configHomeEnvVar;
43 // private static Logger LOG = LoggerFactory.getInstance().getLogger(SparkyResourceLoader.class);
46 public void setResourceLoader(ResourceLoader resourceLoader) {
47 this.resourceLoader = resourceLoader;
50 public String getFullFileUri(String uriFilePath) {
51 return FILE_URI + System.getProperty(configHomeEnvVar) + uriFilePath;
54 public String getAbsolutePath(String uriFilePath) {
55 return System.getProperty(configHomeEnvVar) + uriFilePath;
58 protected Resource getResource(String uriFilePath, boolean isRelative) {
60 String fileUri = uriFilePath;
62 if (!uriFilePath.startsWith("file:")) {
63 fileUri = "file:" + uriFilePath;
67 return resourceLoader.getResource(getFullFileUri(fileUri));
69 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;