14ab22a7f66af1978e4a0cf5a209de0fb9b843ae
[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(final String msg) {
20             super(msg);
21         }
22
23         ConfigurationException(final String msg, final Exception cause) {
24             super(msg, cause);
25         }
26     }
27
28     public static class ReadException extends ConfigurationException {
29         ReadException(final String msg, final Exception exc) {
30             super(msg, exc);
31         }
32     }
33
34     public static class MissingException extends ConfigurationException {
35         private final String key;
36
37         MissingException(final 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(final String key, final String value) {
52             super("Key has an illegal value. Key: " + key + ", Value: " + value);
53             this.key = key;
54             this.value = value;
55         }
56
57         IllegalValueException(final String key, final String value, final Exception cause) {
58             super("Key has an illegal value. Key: " + key + ", Value: " + value, cause);
59             this.key = key;
60             this.value = value;
61         }
62
63         public String getKey() {
64             return key;
65         }
66
67         public String getValue() {
68             return value;
69         }
70     }
71
72     private Properties properties;
73
74     public Configuration() {
75         properties = new Properties();
76     }
77
78     public Configuration(final String path) throws ConfigurationException {
79         try {
80             this.properties = readFromPath(path);
81         } catch (IOException ioe) {
82             throw new ReadException(path, ioe);
83         }
84     }
85
86     private Properties readFromPath(final String filePath) throws IOException {
87         return readFromFile(new File(filePath));
88     }
89
90     private Properties readFromFile(final File file) throws IOException {
91         FileInputStream stream = new FileInputStream(file);
92         properties = readFrom(stream);
93         return properties;
94     }
95
96     private static Properties readFrom(final InputStream stream) throws IOException {
97         Properties properties = new Properties();
98         properties.load(stream);
99         return properties;
100     }
101
102     public void set(final String key, final String value) {
103         properties.setProperty(key, value);
104     }
105
106     String get(final String key) {
107         String result = (String) properties.get(key);
108         if (result == null) {
109             throw new MissingException(key);
110         }
111         return result;
112     }
113
114     public int getAsPort(final String key) {
115         String keyValue = get(key);
116         try {
117             int newPort = Integer.parseInt(keyValue);
118             if (newPort < 0 || newPort > 65535) {
119                 throw new IllegalValueException(key, keyValue);
120             }
121             return newPort;
122         } catch (NumberFormatException e) {
123             throw new IllegalValueException(key, keyValue, e);
124         }
125     }
126 }