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() {
71 properties = new Properties();
74 public Configuration(String path) throws ConfigurationException {
77 this.properties = readFromPath(path);
78 } catch (IOException ioe) {
79 throw new ReadException(path, ioe);
83 private Properties readFromPath(String path) throws IOException {
84 return readFromFile(new File(path));
87 private Properties readFromFile(File file) throws IOException {
88 FileInputStream stream = new FileInputStream(file);
89 properties = readFrom(stream);
93 private Properties readFrom(InputStream stream) throws IOException {
94 Properties properties = new Properties();
95 properties.load(stream);
99 public void set(String key, String value) {
100 properties.setProperty(key, value);
103 String get(String key) {
104 String result = (String) properties.get(key);
106 throw new MissingException(key);
110 public int getAsPort(String key) {
113 int newPort = Integer.parseInt(s);
114 if (newPort < 0 || newPort > 65535)
115 throw new IllegalValueException(key, s);
117 } catch (NumberFormatException e) {
118 throw new IllegalValueException(key, s);