Adjust to yangtools-2.0.0/odlparent-3.0.0 changes
[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 final String path;
73     private Properties properties;
74
75     public Configuration() {
76         path = "<no-path>";
77         properties = new Properties();
78     }
79
80     public Configuration(final String path) throws ConfigurationException {
81         this.path = path;
82         try {
83             this.properties = readFromPath(path);
84         } catch (IOException ioe) {
85             throw new ReadException(path, ioe);
86         }
87     }
88
89     private Properties readFromPath(final String filePath) throws IOException {
90         return readFromFile(new File(filePath));
91     }
92
93     private Properties readFromFile(final File file) throws IOException {
94         FileInputStream stream = new FileInputStream(file);
95         properties = readFrom(stream);
96         return properties;
97     }
98
99     private static Properties readFrom(final InputStream stream) throws IOException {
100         Properties properties = new Properties();
101         properties.load(stream);
102         return properties;
103     }
104
105     public void set(final String key, final String value) {
106         properties.setProperty(key, value);
107     }
108
109     String get(final String key) {
110         String result = (String) properties.get(key);
111         if (result == null) {
112             throw new MissingException(key);
113         }
114         return result;
115     }
116
117     public int getAsPort(final String key) {
118         String keyValue = get(key);
119         try {
120             int newPort = Integer.parseInt(keyValue);
121             if (newPort < 0 || newPort > 65535) {
122                 throw new IllegalValueException(key, keyValue);
123             }
124             return newPort;
125         } catch (NumberFormatException e) {
126             throw new IllegalValueException(key, keyValue, e);
127         }
128     }
129 }