2 * Copyright (c) 2016 Brocade Communication Systems and others. All rights reserved.
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
9 package org.opendaylight.netconf.callhome.mount;
12 import java.io.FileInputStream;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.util.Properties;
17 public class Configuration {
18 public abstract static class ConfigurationException extends RuntimeException {
19 private static final long serialVersionUID = -7759423506815697761L;
21 ConfigurationException(final String msg) {
25 ConfigurationException(final String msg, final Exception cause) {
30 public static class ReadException extends ConfigurationException {
31 private static final long serialVersionUID = 1661483843463184121L;
33 ReadException(final String msg, final Exception exc) {
38 public static class MissingException extends ConfigurationException {
39 private static final long serialVersionUID = 3406998256398889038L;
41 private final String key;
43 MissingException(final String key) {
44 super("Key not found: " + key);
48 public String getKey() {
53 public static class IllegalValueException extends ConfigurationException {
54 private static final long serialVersionUID = -1172346869408302687L;
56 private final String key;
57 private final String value;
59 IllegalValueException(final String key, final String value) {
60 super("Key has an illegal value. Key: " + key + ", Value: " + value);
65 IllegalValueException(final String key, final String value, final Exception cause) {
66 super("Key has an illegal value. Key: " + key + ", Value: " + value, cause);
71 public String getKey() {
75 public String getValue() {
80 private Properties properties;
82 public Configuration() {
83 properties = new Properties();
86 public Configuration(final String path) throws ConfigurationException {
88 this.properties = readFromPath(path);
89 } catch (IOException ioe) {
90 throw new ReadException(path, ioe);
94 private Properties readFromPath(final String filePath) throws IOException {
95 return readFromFile(new File(filePath));
98 private Properties readFromFile(final File file) throws IOException {
99 FileInputStream stream = new FileInputStream(file);
100 properties = readFrom(stream);
104 private static Properties readFrom(final InputStream stream) throws IOException {
105 Properties properties = new Properties();
106 properties.load(stream);
110 public void set(final String key, final String value) {
111 properties.setProperty(key, value);
114 String get(final String key) {
115 String result = (String) properties.get(key);
116 if (result == null) {
117 throw new MissingException(key);
122 public int getAsPort(final String key) {
123 String keyValue = get(key);
125 int newPort = Integer.parseInt(keyValue);
126 if (newPort < 0 || newPort > 65535) {
127 throw new IllegalValueException(key, keyValue);
130 } catch (NumberFormatException e) {
131 throw new IllegalValueException(key, keyValue, e);