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));
112 public void setProperty(String key, String value) {
114 if (key.contains(".")) {
115 s = this.getSection(key.substring(0, key.indexOf(".")));
116 key = key.substring(key.indexOf(".") + 1);
118 s = this.getSection(SECTIONNAME_ROOT);
120 s.setProperty(key, value);
124 public void setProperty(String key, int value) {
126 if (key.contains(".")) {
127 s = this.getSection(key.substring(0, key.indexOf(".")));
128 key = key.substring(key.indexOf(".") + 1);
130 s = this.getSection(SECTIONNAME_ROOT);
132 s.setProperty(key, String.format("%d", value));
136 public void setProperty(String key, boolean value) {
138 if (key.contains(".")) {
139 s = this.getSection(key.substring(0, key.indexOf(".")));
140 key = key.substring(key.indexOf(".") + 1);
142 s = this.getSection(SECTIONNAME_ROOT);
144 s.setProperty(key, value ? "true" : "false");
147 public void setProperty(String key, Object value) {
148 this.setProperty(key, value == null ? "null" : value.toString());
152 try (BufferedWriter bw = new BufferedWriter(new FileWriter(this.mFile, false))) {
153 for (Section section : this.sections) {
154 if (section.hasValues()) {
155 bw.write(String.join(LR, section.toLines()) + LR + LR);
159 } catch (Exception e) {
160 LOG.warn("problem saving value: " + e.getMessage());
164 public Section subset(String section) {
165 return this.getSection(section);
168 public static class ConfigurationException extends Exception {
170 private static final long serialVersionUID = 733061908616404383L;
172 public ConfigurationException(String m) {
177 public static class ConversionException extends Exception {
178 private static final long serialVersionUID = 5179891576029923079L;
180 public ConversionException(String m) {
185 private static class SectionValue {
186 private String Value;
187 private final List<String> Comments;
188 private boolean IsUncommented;
190 public SectionValue(String value) {
191 this(value, new ArrayList<String>(), false);
194 public SectionValue(String value, List<String> commentsForValue, boolean isuncommented) {
195 this.Comments = commentsForValue;
197 this.IsUncommented = isuncommented;
201 public static class Section {
202 private final String Name;
203 private final List<String> rawLines;
204 private final LinkedHashMap<String, SectionValue> values;
206 public Section(String name) {
207 LOG.debug("new section created:" + name);
209 this.rawLines = new ArrayList<>();
210 this.values = new LinkedHashMap<>();
213 public void addLine(String line) {
214 LOG.trace("adding raw line:" + line);
215 this.rawLines.add(line);
218 public String getProperty(String key) {
219 return this.getProperty(key, null);
222 public String getProperty(String key, String defValue) {
223 if (values.containsKey(key)) {
224 return values.get(key).Value;
229 public void setProperty(String key, String value) {
230 boolean isuncommented = this.isCommentLine(key);
232 key = key.substring(1);
234 if (this.values.containsKey(key)) {
235 this.values.get(key).Value = value;
236 this.values.get(key).IsUncommented = isuncommented;
238 SectionValue sv = new SectionValue(value);
239 sv.IsUncommented = isuncommented;
240 this.values.put(key, sv);
244 public void parseLines() {
246 List<String> commentsForValue = new ArrayList<>();
247 boolean uncommented = false;
248 for (String line : rawLines) {
250 if (this.isCommentLine(line)) {
251 if (!line.contains(DELIMITER)) {
252 commentsForValue.add(line);
256 line = line.substring(1);
259 if (!line.contains(DELIMITER)) {
262 String hlp[] = line.split(DELIMITER);
263 if (hlp.length > 1) {
266 line.length() > (key + DELIMITER).length() ? line.substring((key + DELIMITER).length())
268 if (this.values.containsKey(key)) {
269 this.values.get(key).Value = value;
271 this.values.put(key, new SectionValue(value, commentsForValue, uncommented));
272 commentsForValue = new ArrayList<>();
275 LOG.warn("ignoring unknown formatted line:" + line);
281 private boolean isCommentLine(String line) {
282 for (String c : COMMENTCHARS) {
283 if (line.startsWith(c)) {
290 public String[] toLines() {
291 List<String> lines = new ArrayList<>();
292 if (!this.Name.isEmpty()) {
293 lines.add("[" + this.Name + "]");
295 for (Entry<String, SectionValue> entry : this.values.entrySet()) {
296 if (entry.getValue().Comments.size() > 0) {
297 for (String comment : entry.getValue().Comments) {
301 lines.add((entry.getValue().IsUncommented ? COMMENTCHARS[0] : "") + entry.getKey() + DELIMITER
302 + entry.getValue().Value);
304 String[] alines = new String[lines.size()];
305 return lines.toArray(alines);
308 public String getString(String key, String def) {
309 return this.getProperty(key, def);
312 public boolean getBoolean(String key, boolean def) throws ConversionException {
313 String v = this.getProperty(key);
314 if (v == null || v.isEmpty()) {
317 if (v.equals("true")) {
320 if (v.equals("false")) {
323 throw new ConversionException("invalid value for key " + key);
326 public int getInt(String key, int def) throws ConversionException {
327 String v = this.getProperty(key);
328 if (v == null || v.isEmpty()) {
332 return Integer.parseInt(v);
333 } catch (NumberFormatException e) {
334 throw new ConversionException(e.getMessage());
338 public long getLong(String key, long def) throws ConversionException {
339 String v = this.getProperty(key);
340 if (v == null || v.isEmpty()) {
344 return Long.parseLong(v);
345 } catch (NumberFormatException e) {
346 throw new ConversionException(e.getMessage());
350 public boolean hasValues() {
351 return this.values.size() > 0;
354 public boolean hasKey(String key) {
355 return this.values.containsKey(key);