Upversion to Spring Boot 1.5.17
[aai/gizmo.git] / src / main / java / org / onap / schema / util / SchemaIngestPropertyReader.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21 package org.onap.schema.util;
22
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.net.URISyntaxException;
26 import java.nio.file.Files;
27 import java.nio.file.NoSuchFileException;
28 import java.nio.file.Path;
29 import java.nio.file.Paths;
30 import java.util.Properties;
31 import org.onap.aai.cl.eelf.LoggerFactory;
32 import org.onap.crud.exception.CrudException;
33 import org.onap.crud.logging.CrudServiceMsgs;
34
35 public class SchemaIngestPropertyReader {
36
37     private static final String SCHEMA_INGEST_PROPERTIES_FILE = "schemaIngest.properties";
38
39     private static final String SCHEMA_INGEST_PROPERTIES_LOCATION =
40             System.getProperty("CONFIG_HOME") + "/" + SCHEMA_INGEST_PROPERTIES_FILE;
41
42     private static org.onap.aai.cl.api.Logger logger =
43             LoggerFactory.getInstance().getLogger(SchemaIngestPropertyReader.class.getName());
44
45     /**
46      * Gets the location of the OXM
47      *
48      * @return
49      * @throws CrudException
50      */
51     public String getNodeDir() throws CrudException {
52
53         return getProps ().getProperty("nodeDir");
54     }
55
56     /**
57      * Gets the location of the Edge Rules
58      *
59      * @return
60      * @throws CrudException
61      */
62     public String getEdgeDir() throws CrudException {
63
64         return getProps ().getProperty("edgeDir");
65     }
66
67     /**
68      * Gets the location of the Edge Properties
69      *
70      * @return
71      * @throws CrudException
72      */
73     public String getEdgePropsDir() throws CrudException {
74
75         return getProps ().getProperty("edgePropsDir");
76     }
77
78     private Properties getProps() throws CrudException {
79
80         Properties prop = new Properties();
81         try {
82             prop = loadFromFile(SCHEMA_INGEST_PROPERTIES_LOCATION);
83         } catch (NoSuchFileException e) {
84             // if file not found, try via classpath
85             try {
86                 prop = loadFromClasspath(SCHEMA_INGEST_PROPERTIES_FILE);
87             } catch (URISyntaxException | IOException e1) {
88                 logger.error(CrudServiceMsgs.SCHEMA_INGEST_LOAD_ERROR, e1.getMessage());
89                 throw new CrudException("Failed to load schemaIngest.properties", e1);
90             }
91         } catch (IOException e) {
92             logger.error(CrudServiceMsgs.SCHEMA_INGEST_LOAD_ERROR, e.getMessage());
93             throw new CrudException("Failed to load schemaIngest.properties", e);
94         }
95         return prop;
96     }
97
98     private Properties loadFromFile(String filename) throws IOException {
99         Path configLocation = Paths.get(filename);
100         try (InputStream stream = Files.newInputStream(configLocation)) {
101             return loadProperties(stream);
102         }
103     }
104
105     private Properties loadFromClasspath(String resourceName) throws URISyntaxException, IOException {
106         Path path = Paths.get(ClassLoader.getSystemResource(resourceName).toURI());
107         try (InputStream stream = Files.newInputStream(path)) {
108             return loadProperties(stream);
109         }
110     }
111
112     private Properties loadProperties(InputStream stream) throws IOException {
113         Properties config = new Properties();
114         config.load(stream);
115         return config;
116     }
117 }