JavaDoc, refactored properties API.

This commit is contained in:
Michael Jumper
2011-05-13 00:48:15 -07:00
parent 3dd0c20ba9
commit a8f5862439
7 changed files with 232 additions and 151 deletions

View File

@@ -19,16 +19,41 @@ package net.sourceforge.guacamole;
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* A generic exception thrown when parts of the Guacamole API encounter
* errors.
*
* @author Michael Jumper
*/
public class GuacamoleException extends Exception {
/**
* Creates a new GuacamoleException with the given message and cause.
*
* @param message A human readable description of the exception that
* occurred.
* @param cause The cause of this exception.
*/
public GuacamoleException(String message, Throwable cause) {
super(message, cause);
}
/**
* Creates a new GuacamoleException with the given message.
*
* @param message A human readable description of the exception that
* occurred.
*/
public GuacamoleException(String message) {
super(message);
}
/**
* Creates a new GuacamoleException with the given cause.
*
* @param cause The cause of this exception.
*/
public GuacamoleException(Throwable cause) {
super(cause);
}

View File

@@ -1,151 +0,0 @@
package net.sourceforge.guacamole;
/*
* Guacamole - Clientless Remote Desktop
* Copyright (C) 2010 Michael Jumper
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class GuacamoleProperties {
private static final Properties properties;
private static GuacamoleException exception;
static {
properties = new Properties();
try {
InputStream stream = GuacamoleProperties.class.getResourceAsStream("/guacamole.properties");
if (stream == null)
throw new IOException("Resource /guacamole.properties not found.");
properties.load(stream);
}
catch (IOException e) {
exception = new GuacamoleException("Error reading guacamole.properties", e);
}
}
public static String getProperty(String name) throws GuacamoleException {
if (exception != null) throw exception;
return properties.getProperty(name);
}
protected static String humanReadableList(Object... values) {
String list = "";
for (int i=0; i<values.length; i++) {
if (i >= 1)
list += ", ";
if (i == values.length -1)
list += " or ";
list += "\"" + values[i] + "\"";
}
return list;
}
public static String getProperty(String name, String defaultValue, String... allowedValues) throws GuacamoleException {
String value = getProperty(name);
// Use default if not specified
if (value == null) {
if (defaultValue == null)
throw new GuacamoleException("Parameter \"" + name + "\" is required.");
return defaultValue;
}
// If not restricted to certain values, just return whatever is given.
if (allowedValues.length == 0)
return value;
// If restricted, only return value within given list
for (String allowedValue : allowedValues)
if (value.equals(allowedValue))
return value;
throw new GuacamoleException("Parameter \"" + name + "\" must be " + humanReadableList((Object) allowedValues));
}
public static boolean getBooleanProperty(String name, Boolean defaultValue) throws GuacamoleException {
String value = getProperty(name);
// Use default if not specified
if (value == null) {
if (defaultValue == null)
throw new GuacamoleException("Parameter \"" + name + "\" is required.");
return defaultValue;
}
value = value.trim();
if (value.equals("true"))
return true;
if (value.equals("false"))
return false;
throw new GuacamoleException("Parameter \"" + name + "\" must be \"true\" or \"false\".");
}
public static int getIntProperty(String name, Integer defaultValue, Integer... allowedValues) throws GuacamoleException {
String parmString = getProperty(name);
// Use default if not specified
if (parmString== null) {
if (defaultValue == null)
throw new GuacamoleException("Parameter \"" + name + "\" is required.");
return defaultValue;
}
try {
int value = Integer.parseInt(parmString);
// If not restricted to certain values, just return whatever is given.
if (allowedValues.length == 0)
return value;
// If restricted, only return value within given list
for (int allowedValue : allowedValues)
if (value == allowedValue)
return value;
throw new GuacamoleException("Parameter \"" + name + "\" must be " + humanReadableList((Object) allowedValues));
}
catch (NumberFormatException e) {
throw new GuacamoleException("Parameter \"" + name + "\" must be an integer.", e);
}
}
}

View File

@@ -0,0 +1,32 @@
package net.sourceforge.guacamole.properties;
/*
* Guacamole - Clientless Remote Desktop
* Copyright (C) 2010 Michael Jumper
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import java.io.File;
import net.sourceforge.guacamole.GuacamoleException;
public abstract class FileGuacamoleProperty implements GuacamoleProperty<File> {
@Override
public File parseValue(String value) throws GuacamoleException {
return new File(value);
}
}

View File

@@ -0,0 +1,74 @@
package net.sourceforge.guacamole.properties;
/*
* Guacamole - Clientless Remote Desktop
* Copyright (C) 2010 Michael Jumper
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import net.sourceforge.guacamole.GuacamoleException;
public class GuacamoleProperties {
public static final StringGuacamoleProperty GUACD_HOSTNAME = new StringGuacamoleProperty() {
@Override
public String getName() { return "guacd-hostname"; }
};
public static final IntegerGuacamoleProperty GUACD_PORT = new IntegerGuacamoleProperty() {
@Override
public String getName() { return "guacd-port"; }
};
private static final Properties properties;
private static GuacamoleException exception;
static {
properties = new Properties();
try {
InputStream stream = GuacamoleProperties.class.getResourceAsStream("/guacamole.properties");
if (stream == null)
throw new IOException("Resource /guacamole.properties not found.");
properties.load(stream);
}
catch (IOException e) {
exception = new GuacamoleException("Error reading guacamole.properties", e);
}
}
public static <Type> Type getProperty(GuacamoleProperty<Type> property) throws GuacamoleException {
if (exception != null)
throw exception;
return property.parseValue(properties.getProperty(property.getName()));
}
}

View File

@@ -0,0 +1,31 @@
package net.sourceforge.guacamole.properties;
import net.sourceforge.guacamole.GuacamoleException;
/*
* Guacamole - Clientless Remote Desktop
* Copyright (C) 2010 Michael Jumper
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
public interface GuacamoleProperty<Type> {
public String getName();
public Type parseValue(String value) throws GuacamoleException;
}

View File

@@ -0,0 +1,39 @@
package net.sourceforge.guacamole.properties;
/*
* Guacamole - Clientless Remote Desktop
* Copyright (C) 2010 Michael Jumper
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import net.sourceforge.guacamole.GuacamoleException;
public abstract class IntegerGuacamoleProperty implements GuacamoleProperty<Integer> {
@Override
public Integer parseValue(String value) throws GuacamoleException {
try {
Integer integer = new Integer(value);
return integer;
}
catch (NumberFormatException e) {
throw new GuacamoleException("Property \"" + getName() + "\" must be an integer.", e);
}
}
}

View File

@@ -0,0 +1,31 @@
package net.sourceforge.guacamole.properties;
/*
* Guacamole - Clientless Remote Desktop
* Copyright (C) 2010 Michael Jumper
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import net.sourceforge.guacamole.GuacamoleException;
public abstract class StringGuacamoleProperty implements GuacamoleProperty<String> {
@Override
public String parseValue(String value) throws GuacamoleException {
return value;
}
}