public class Configuration {
public abstract static class ConfigurationException extends RuntimeException {
- ConfigurationException(String msg) {
+ private static final long serialVersionUID = -7759423506815697761L;
+
+ ConfigurationException(final String msg) {
super(msg);
}
- ConfigurationException(String msg, Exception cause) {
+ ConfigurationException(final String msg, final Exception cause) {
super(msg, cause);
}
}
public static class ReadException extends ConfigurationException {
- ReadException(String msg, Exception e) {
- super(msg, e);
+ private static final long serialVersionUID = 1661483843463184121L;
+
+ ReadException(final String msg, final Exception exc) {
+ super(msg, exc);
}
}
public static class MissingException extends ConfigurationException {
+ private static final long serialVersionUID = 3406998256398889038L;
+
private final String key;
- MissingException(String key) {
+ MissingException(final String key) {
super("Key not found: " + key);
this.key = key;
}
}
public static class IllegalValueException extends ConfigurationException {
+ private static final long serialVersionUID = -1172346869408302687L;
+
private final String key;
private final String value;
- IllegalValueException(String key, String value) {
+ IllegalValueException(final String key, final String value) {
super("Key has an illegal value. Key: " + key + ", Value: " + value);
this.key = key;
this.value = value;
}
+ IllegalValueException(final String key, final String value, final Exception cause) {
+ super("Key has an illegal value. Key: " + key + ", Value: " + value, cause);
+ this.key = key;
+ this.value = value;
+ }
+
public String getKey() {
return key;
}
}
}
- private String path;
private Properties properties;
- public Configuration(String path) throws ConfigurationException {
- this.path = path;
+ public Configuration() {
+ properties = new Properties();
+ }
+
+ public Configuration(final String path) throws ConfigurationException {
try {
this.properties = readFromPath(path);
} catch (IOException ioe) {
}
}
- private Properties readFromPath(String path) throws IOException {
- return readFromFile(new File(path));
+ private Properties readFromPath(final String filePath) throws IOException {
+ return readFromFile(new File(filePath));
}
- private Properties readFromFile(File file) throws IOException {
+ private Properties readFromFile(final File file) throws IOException {
FileInputStream stream = new FileInputStream(file);
properties = readFrom(stream);
return properties;
}
- private Properties readFrom(InputStream stream) throws IOException {
+ private static Properties readFrom(final InputStream stream) throws IOException {
Properties properties = new Properties();
properties.load(stream);
return properties;
}
- public String get(String key) {
+ public void set(final String key, final String value) {
+ properties.setProperty(key, value);
+ }
+
+ String get(final String key) {
String result = (String) properties.get(key);
- if (result == null)
+ if (result == null) {
throw new MissingException(key);
+ }
return result;
}
- public int getAsPort(String key) {
- String s = get(key);
+ public int getAsPort(final String key) {
+ String keyValue = get(key);
try {
- int newPort = Integer.parseInt(s);
- if (newPort < 0 || newPort > 65535)
- throw new IllegalValueException(key, s);
+ int newPort = Integer.parseInt(keyValue);
+ if (newPort < 0 || newPort > 65535) {
+ throw new IllegalValueException(key, keyValue);
+ }
return newPort;
} catch (NumberFormatException e) {
- throw new IllegalValueException(key, s);
+ throw new IllegalValueException(key, keyValue, e);
}
}
}