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