2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ========================LICENSE_END===================================
21 package org.oransc.policyagent.configuration;
23 import com.google.gson.GsonBuilder;
24 import com.google.gson.JsonElement;
25 import com.google.gson.JsonObject;
26 import com.google.gson.JsonParser;
27 import com.google.gson.JsonSyntaxException;
28 import com.google.gson.TypeAdapterFactory;
30 import java.io.BufferedInputStream;
31 import java.io.FileInputStream;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.io.InputStreamReader;
35 import java.util.Properties;
36 import java.util.ServiceLoader;
38 import javax.validation.constraints.NotEmpty;
39 import javax.validation.constraints.NotNull;
41 import org.oransc.policyagent.exceptions.ServiceException;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.beans.factory.annotation.Value;
46 import org.springframework.boot.context.properties.ConfigurationProperties;
47 import org.springframework.boot.context.properties.EnableConfigurationProperties;
48 import org.springframework.stereotype.Component;
49 import reactor.core.publisher.Mono;
52 @EnableConfigurationProperties
53 @ConfigurationProperties("app")
54 public class ApplicationConfig {
55 private static final Logger logger = LoggerFactory.getLogger(ApplicationConfig.class);
57 @Value("#{systemEnvironment}")
58 Properties systemEnvironment;
61 private String filepath;
64 public ApplicationConfig() {
67 public synchronized void setFilepath(String filepath) {
68 this.filepath = filepath;
72 * Reads the cloud configuration.
74 public void initialize() {
75 loadConfigurationFromFile();
78 Mono<Environment.Variables> getEnvironment(Properties systemEnvironment) {
79 return Environment.readEnvironmentVariables(systemEnvironment);
83 * Reads the configuration from file.
85 void loadConfigurationFromFile() {
86 GsonBuilder gsonBuilder = new GsonBuilder();
87 ServiceLoader.load(TypeAdapterFactory.class).forEach(gsonBuilder::registerTypeAdapterFactory);
89 try (InputStream inputStream = createInputStream(filepath)) {
90 JsonParser parser = new JsonParser();
91 JsonObject rootObject = getJsonElement(parser, inputStream).getAsJsonObject();
92 if (rootObject == null) {
93 throw new JsonSyntaxException("Root is not a json object");
95 ApplicationConfigParser appParser = new ApplicationConfigParser();
96 appParser.parse(rootObject);
97 logger.info("Local configuration file loaded: {}", filepath);
98 } catch (JsonSyntaxException | ServiceException | IOException e) {
99 logger.trace("Local configuration file not loaded: {}", filepath, e);
103 JsonElement getJsonElement(JsonParser parser, InputStream inputStream) {
104 return parser.parse(new InputStreamReader(inputStream));
107 InputStream createInputStream(@NotNull String filepath) throws IOException {
108 return new BufferedInputStream(new FileInputStream(filepath));