Bug-8187: call home throwing exception on startup
[netconf.git] / netconf / callhome-provider / src / main / java / org / opendaylight / netconf / callhome / mount / Configuration.java
1 /*
2  * Copyright (c) 2016 Brocade Communication Systems and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.netconf.callhome.mount;
10
11 import java.io.File;
12 import java.io.FileInputStream;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.util.Properties;
16
17 public class Configuration {
18     public abstract static class ConfigurationException extends RuntimeException {
19         ConfigurationException(String msg) {
20             super(msg);
21         }
22
23         ConfigurationException(String msg, Exception cause) {
24             super(msg, cause);
25         }
26     }
27
28     public static class ReadException extends ConfigurationException {
29         ReadException(String msg, Exception e) {
30             super(msg, e);
31         }
32     }
33
34     public static class MissingException extends ConfigurationException {
35         private final String key;
36
37         MissingException(String key) {
38             super("Key not found: " + key);
39             this.key = key;
40         }
41
42         public String getKey() {
43             return key;
44         }
45     }
46
47     public static class IllegalValueException extends ConfigurationException {
48         private final String key;
49         private final String value;
50
51         IllegalValueException(String key, String value) {
52             super("Key has an illegal value. Key: " + key + ", Value: " + value);
53             this.key = key;
54             this.value = value;
55         }
56
57         public String getKey() {
58             return key;
59         }
60
61         public String getValue() {
62             return value;
63         }
64     }
65
66     private String path;
67     private Properties properties;
68
69     public Configuration() {
70         path = "<no-path>";
71         properties = new Properties();
72     }
73
74     public Configuration(String path) throws ConfigurationException {
75         this.path = path;
76         try {
77             this.properties = readFromPath(path);
78         } catch (IOException ioe) {
79             throw new ReadException(path, ioe);
80         }
81     }
82
83     private Properties readFromPath(String path) throws IOException {
84         return readFromFile(new File(path));
85     }
86
87     private Properties readFromFile(File file) throws IOException {
88         FileInputStream stream = new FileInputStream(file);
89         properties = readFrom(stream);
90         return properties;
91     }
92
93     private Properties readFrom(InputStream stream) throws IOException {
94         Properties properties = new Properties();
95         properties.load(stream);
96         return properties;
97     }
98
99     public void set(String key, String value) {
100         properties.setProperty(key, value);
101     }
102
103     String get(String key) {
104         String result = (String) properties.get(key);
105         if (result == null)
106             throw new MissingException(key);
107         return result;
108     }
109
110     public int getAsPort(String key) {
111         String s = get(key);
112         try {
113             int newPort = Integer.parseInt(s);
114             if (newPort < 0 || newPort > 65535)
115                 throw new IllegalValueException(key, s);
116             return newPort;
117         } catch (NumberFormatException e) {
118             throw new IllegalValueException(key, s);
119         }
120     }
121 }