1 /*******************************************************************************
2 * ============LICENSE_START========================================================================
3 * ONAP : ccsdk feature sdnr wt
4 * =================================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6 * =================================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software distributed under the License
13 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14 * or implied. See the License for the specific language governing permissions and limitations under
16 * ============LICENSE_END==========================================================================
17 ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.devicemanager.base.internalTypes;
20 import java.io.BufferedReader;
21 import java.io.BufferedWriter;
23 import java.io.FileReader;
24 import java.io.FileWriter;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.LinkedHashMap;
28 import java.util.List;
29 import java.util.Map.Entry;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class IniConfigurationFile {
35 private static final Logger LOG = LoggerFactory.getLogger(IniConfigurationFile.class);
37 private static final String SECTIONNAME_ROOT = "";
38 private static final String DELIMITER = "=";
39 private static final String COMMENTCHARS[] = {"#", ";"};
40 private static final String LR = "\n";
42 private final File mFile;
43 private final List<Section> sections;
45 public IniConfigurationFile(File f) {
47 this.sections = new ArrayList<>();
48 this.sections.add(new Section(SECTIONNAME_ROOT));
51 public void load() throws ConfigurationException {
52 String curSectionName = SECTIONNAME_ROOT;
53 LOG.debug("loading file");
54 BufferedReader br = null;
56 br = new BufferedReader(new FileReader(this.mFile));
57 for (String line; (line = br.readLine()) != null;) {
62 if (line.startsWith("[") && line.endsWith("]")) {
63 curSectionName = line.substring(1, line.length() - 1);
64 this.addSection(curSectionName);
66 this.getSection(curSectionName).addLine(line);
70 } catch (Exception e) {
71 throw new ConfigurationException(e.getMessage());
77 } catch (IOException e) {
80 LOG.debug("finished loading file");
81 LOG.debug("start parsing sections");
82 for (Section section : this.sections) {
85 LOG.debug("finished parsing " + this.sections.size() + " sections");
88 private Section getSection(String name) {
89 for (Section s : this.sections) {
90 if (s.Name.equals(name)) {
94 return this.addSection(name);
98 private Section addSection(String name) {
100 Section s = new Section(name);
101 this.sections.add(s);
105 public void reLoad() throws ConfigurationException {
106 this.sections.clear();
107 this.sections.add(new Section(SECTIONNAME_ROOT));
111 public String getProperty(String key, String defValue) {
113 if (key.contains(".")) {
114 s = this.getSection(key.substring(0, key.indexOf(".")));
115 key = key.substring(key.indexOf(".") + 1);
117 s = this.getSection(SECTIONNAME_ROOT);
120 String v = s.getProperty(key);
121 if (v == null || v.isEmpty()) {
127 public void setProperty(String key, String value) {
129 if (key.contains(".")) {
130 s = this.getSection(key.substring(0, key.indexOf(".")));
131 key = key.substring(key.indexOf(".") + 1);
133 s = this.getSection(SECTIONNAME_ROOT);
135 s.setProperty(key, value);
138 public int getProperty(String key, int defValue) throws ConversionException {
140 if (key.contains(".")) {
141 s = this.getSection(key.substring(0, key.indexOf(".")));
142 key = key.substring(key.indexOf(".") + 1);
144 s = this.getSection(SECTIONNAME_ROOT);
147 return s.getInt(key, defValue);
150 public void setProperty(String key, int value) {
152 if (key.contains(".")) {
153 s = this.getSection(key.substring(0, key.indexOf(".")));
154 key = key.substring(key.indexOf(".") + 1);
156 s = this.getSection(SECTIONNAME_ROOT);
158 s.setProperty(key, String.format("%d", value));
161 public boolean getProperty(String key, boolean defValue) throws ConversionException {
163 if (key.contains(".")) {
164 s = this.getSection(key.substring(0, key.indexOf(".")));
165 key = key.substring(key.indexOf(".") + 1);
167 s = this.getSection(SECTIONNAME_ROOT);
170 return s.getBoolean(key, defValue);
173 public void setProperty(String key, boolean value) {
175 if (key.contains(".")) {
176 s = this.getSection(key.substring(0, key.indexOf(".")));
177 key = key.substring(key.indexOf(".") + 1);
179 s = this.getSection(SECTIONNAME_ROOT);
181 s.setProperty(key, value ? "true" : "false");
184 public void setProperty(String key, Object value) {
185 this.setProperty(key, value == null ? "null" : value.toString());
189 try (BufferedWriter bw = new BufferedWriter(new FileWriter(this.mFile, false))) {
190 for (Section section : this.sections) {
191 if (section.hasValues()) {
192 bw.write(String.join(LR, section.toLines()) + LR + LR);
196 } catch (Exception e) {
197 LOG.warn("problem saving value: " + e.getMessage());
201 public Section subset(String section) {
202 return this.getSection(section);
205 public static class ConfigurationException extends Exception {
207 private static final long serialVersionUID = 733061908616404383L;
209 public ConfigurationException(String m) {
214 public static class ConversionException extends Exception {
215 private static final long serialVersionUID = 5179891576029923079L;
217 public ConversionException(String m) {
222 private static class SectionValue {
223 private String Value;
224 private final List<String> Comments;
225 private boolean IsUncommented;
227 public SectionValue(String value) {
228 this(value, new ArrayList<String>(), false);
231 public SectionValue(String value, List<String> commentsForValue, boolean isuncommented) {
232 this.Comments = commentsForValue;
234 this.IsUncommented = isuncommented;
238 public static class Section {
239 private final String Name;
240 private final List<String> rawLines;
241 private final LinkedHashMap<String, SectionValue> values;
243 public Section(String name) {
244 LOG.debug("new section created:" + name);
246 this.rawLines = new ArrayList<>();
247 this.values = new LinkedHashMap<>();
250 public void addLine(String line) {
251 LOG.trace("adding raw line:" + line);
252 this.rawLines.add(line);
255 public String getProperty(String key) {
256 return this.getProperty(key, null);
259 public String getProperty(String key, String defValue) {
260 if (values.containsKey(key)) {
261 return values.get(key).Value;
266 public void setProperty(String key, String value) {
267 boolean isuncommented = this.isCommentLine(key);
269 key = key.substring(1);
271 if (this.values.containsKey(key)) {
272 this.values.get(key).Value = value;
273 this.values.get(key).IsUncommented = isuncommented;
275 SectionValue sv = new SectionValue(value);
276 sv.IsUncommented = isuncommented;
277 this.values.put(key, sv);
281 public void parseLines() {
283 List<String> commentsForValue = new ArrayList<>();
284 boolean uncommented = false;
285 for (String line : rawLines) {
287 if (this.isCommentLine(line)) {
288 if (!line.contains(DELIMITER)) {
289 commentsForValue.add(line);
293 line = line.substring(1);
296 if (!line.contains(DELIMITER)) {
299 String hlp[] = line.split(DELIMITER);
300 if (hlp.length > 1) {
303 line.length() > (key + DELIMITER).length() ? line.substring((key + DELIMITER).length())
305 if (this.values.containsKey(key)) {
306 this.values.get(key).Value = value;
308 this.values.put(key, new SectionValue(value, commentsForValue, uncommented));
309 commentsForValue = new ArrayList<>();
312 LOG.warn("ignoring unknown formatted line:" + line);
318 private boolean isCommentLine(String line) {
319 for (String c : COMMENTCHARS) {
320 if (line.startsWith(c)) {
327 public String[] toLines() {
328 List<String> lines = new ArrayList<>();
329 if (!this.Name.isEmpty()) {
330 lines.add("[" + this.Name + "]");
332 for (Entry<String, SectionValue> entry : this.values.entrySet()) {
333 if (entry.getValue().Comments.size() > 0) {
334 for (String comment : entry.getValue().Comments) {
338 lines.add((entry.getValue().IsUncommented ? COMMENTCHARS[0] : "") + entry.getKey() + DELIMITER
339 + entry.getValue().Value);
341 String[] alines = new String[lines.size()];
342 return lines.toArray(alines);
345 public String getString(String key, String def) {
346 return this.getProperty(key, def);
349 public boolean getBoolean(String key, boolean def) throws ConversionException {
350 String v = this.getProperty(key);
351 if (v == null || v.isEmpty()) {
354 if (v.equals("true")) {
357 if (v.equals("false")) {
360 throw new ConversionException("invalid value for key " + key);
363 public int getInt(String key, int def) throws ConversionException {
364 String v = this.getProperty(key);
365 if (v == null || v.isEmpty()) {
369 return Integer.parseInt(v);
370 } catch (NumberFormatException e) {
371 throw new ConversionException(e.getMessage());
375 public long getLong(String key, long def) throws ConversionException {
376 String v = this.getProperty(key);
377 if (v == null || v.isEmpty()) {
381 return Long.parseLong(v);
382 } catch (NumberFormatException e) {
383 throw new ConversionException(e.getMessage());
387 public boolean hasValues() {
388 return this.values.size() > 0;
391 public boolean hasKey(String key) {
392 return this.values.containsKey(key);