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 ConfigurationException(final String msg) {
23 ConfigurationException(final String msg, final Exception cause) {
28 public static class ReadException extends ConfigurationException {
29 ReadException(final String msg, final Exception exc) {
34 public static class MissingException extends ConfigurationException {
35 private final String key;
37 MissingException(final String key) {
38 super("Key not found: " + key);
42 public String getKey() {
47 public static class IllegalValueException extends ConfigurationException {
48 private final String key;
49 private final String value;
51 IllegalValueException(final String key, final String value) {
52 super("Key has an illegal value. Key: " + key + ", Value: " + value);
57 IllegalValueException(final String key, final String value, final Exception cause) {
58 super("Key has an illegal value. Key: " + key + ", Value: " + value, cause);
63 public String getKey() {
67 public String getValue() {
72 private Properties properties;
74 public Configuration() {
75 properties = new Properties();
78 public Configuration(final String path) throws ConfigurationException {
80 this.properties = readFromPath(path);
81 } catch (IOException ioe) {
82 throw new ReadException(path, ioe);
86 private Properties readFromPath(final String filePath) throws IOException {
87 return readFromFile(new File(filePath));
90 private Properties readFromFile(final File file) throws IOException {
91 FileInputStream stream = new FileInputStream(file);
92 properties = readFrom(stream);
96 private static Properties readFrom(final InputStream stream) throws IOException {
97 Properties properties = new Properties();
98 properties.load(stream);
102 public void set(final String key, final String value) {
103 properties.setProperty(key, value);
106 String get(final String key) {
107 String result = (String) properties.get(key);
108 if (result == null) {
109 throw new MissingException(key);
114 public int getAsPort(final String key) {
115 String keyValue = get(key);
117 int newPort = Integer.parseInt(keyValue);
118 if (newPort < 0 || newPort > 65535) {
119 throw new IllegalValueException(key, keyValue);
122 } catch (NumberFormatException e) {
123 throw new IllegalValueException(key, keyValue, e);