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(String msg) {
23 ConfigurationException(String msg, Exception cause) {
28 public static class ReadException extends ConfigurationException {
29 ReadException(String msg, Exception e) {
34 public static class MissingException extends ConfigurationException {
35 private final String key;
37 MissingException(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(String key, String value) {
52 super("Key has an illegal value. Key: " + key + ", Value: " + value);
57 public String getKey() {
61 public String getValue() {
67 private Properties properties;
69 public Configuration(String path) throws ConfigurationException {
72 this.properties = readFromPath(path);
73 } catch (IOException ioe) {
74 throw new ReadException(path, ioe);
78 private Properties readFromPath(String path) throws IOException {
79 return readFromFile(new File(path));
82 private Properties readFromFile(File file) throws IOException {
83 FileInputStream stream = new FileInputStream(file);
84 properties = readFrom(stream);
88 private Properties readFrom(InputStream stream) throws IOException {
89 Properties properties = new Properties();
90 properties.load(stream);
94 public String get(String key) {
95 String result = (String) properties.get(key);
97 throw new MissingException(key);
101 public int getAsPort(String key) {
104 int newPort = Integer.parseInt(s);
105 if (newPort < 0 || newPort > 65535)
106 throw new IllegalValueException(key, s);
108 } catch (NumberFormatException e) {
109 throw new IllegalValueException(key, s);