From 7f0135ee694113995ac1ac3f260dbd30d4416f5b Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Thu, 31 Jan 2013 10:07:11 -0800 Subject: [PATCH] Move GuacamoleProperties stuff to guacamole-ext. --- .../properties/FileGuacamoleProperty.java | 61 ++++++ .../properties/GuacamoleProperties.java | 175 ++++++++++++++++++ .../properties/GuacamoleProperty.java | 71 +++++++ .../properties/IntegerGuacamoleProperty.java | 67 +++++++ .../properties/StringGuacamoleProperty.java | 54 ++++++ .../guacamole/properties/package-info.java | 7 + 6 files changed, 435 insertions(+) create mode 100644 guacamole-ext/src/main/java/net/sourceforge/guacamole/properties/FileGuacamoleProperty.java create mode 100644 guacamole-ext/src/main/java/net/sourceforge/guacamole/properties/GuacamoleProperties.java create mode 100644 guacamole-ext/src/main/java/net/sourceforge/guacamole/properties/GuacamoleProperty.java create mode 100644 guacamole-ext/src/main/java/net/sourceforge/guacamole/properties/IntegerGuacamoleProperty.java create mode 100644 guacamole-ext/src/main/java/net/sourceforge/guacamole/properties/StringGuacamoleProperty.java create mode 100644 guacamole-ext/src/main/java/net/sourceforge/guacamole/properties/package-info.java diff --git a/guacamole-ext/src/main/java/net/sourceforge/guacamole/properties/FileGuacamoleProperty.java b/guacamole-ext/src/main/java/net/sourceforge/guacamole/properties/FileGuacamoleProperty.java new file mode 100644 index 000000000..9b2a4ceb4 --- /dev/null +++ b/guacamole-ext/src/main/java/net/sourceforge/guacamole/properties/FileGuacamoleProperty.java @@ -0,0 +1,61 @@ + +package net.sourceforge.guacamole.properties; + +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is guacamole-ext. + * + * The Initial Developer of the Original Code is + * Michael Jumper. + * Portions created by the Initial Developer are Copyright (C) 2010 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +import java.io.File; +import net.sourceforge.guacamole.GuacamoleException; + +/** + * A GuacamoleProperty whose value is a filename. + * + * @author Michael Jumper + */ +public abstract class FileGuacamoleProperty implements GuacamoleProperty { + + @Override + public File parseValue(String value) throws GuacamoleException { + + // If no property provided, return null. + if (value == null) + return null; + + return new File(value); + + } + +} diff --git a/guacamole-ext/src/main/java/net/sourceforge/guacamole/properties/GuacamoleProperties.java b/guacamole-ext/src/main/java/net/sourceforge/guacamole/properties/GuacamoleProperties.java new file mode 100644 index 000000000..81e2da480 --- /dev/null +++ b/guacamole-ext/src/main/java/net/sourceforge/guacamole/properties/GuacamoleProperties.java @@ -0,0 +1,175 @@ + +package net.sourceforge.guacamole.properties; + +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is guacamole-ext. + * + * The Initial Developer of the Original Code is + * Michael Jumper. + * Portions created by the Initial Developer are Copyright (C) 2010 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; +import net.sourceforge.guacamole.GuacamoleException; +import net.sourceforge.guacamole.GuacamoleServerException; + +/** + * Simple utility class for reading properties from the guacamole.properties + * file in the root of the classpath. + * + * @author Michael Jumper + */ +public class GuacamoleProperties { + + private GuacamoleProperties() {} + + /** + * The hostname of the server where guacd (the Guacamole proxy server) is + * running. + */ + public static final StringGuacamoleProperty GUACD_HOSTNAME = new StringGuacamoleProperty() { + + @Override + public String getName() { return "guacd-hostname"; } + + }; + + /** + * The port that guacd (the Guacamole proxy server) is listening on. + */ + 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 { + + // Attempt to find Guacamole home + File guacHome; + + // Get explicitly specified directory, if any + String desiredDir = System.getProperty("guacamole.home"); + if (desiredDir != null) + guacHome = new File(desiredDir); + + // If not explicitly-define directory, use ~/.guacamole + else + guacHome = new File(System.getProperty("user.home"), ".guacamole"); + + InputStream stream; + + // If not a directory, load from classpath + if (!guacHome.isDirectory()) { + + // Read from classpath + stream = GuacamoleProperties.class.getResourceAsStream("/guacamole.properties"); + if (stream == null) + throw new IOException( + "guacamole.properties not loaded from " + guacHome + + " (not a directory), and guacamole.properties could" + + " not be found as a resource in the classpath."); + + } + + // Otherwise, try to load from file + else + stream = new FileInputStream(new File(guacHome, "guacamole.properties")); + + // Load properties, always close stream + try { properties.load(stream); } + finally { stream.close(); } + + } + catch (IOException e) { + exception = new GuacamoleServerException("Error reading guacamole.properties", e); + } + + } + + /** + * Given a GuacamoleProperty, parses and returns the value set for that + * property in guacamole.properties, if any. + * + * @param The type that the given property is parsed into. + * @param property The property to read from guacamole.properties. + * @return The parsed value of the property as read from + * guacamole.properties. + * @throws GuacamoleException If an error occurs while parsing the value + * for the given property in + * guacamole.properties. + */ + public static Type getProperty(GuacamoleProperty property) throws GuacamoleException { + + if (exception != null) + throw exception; + + return property.parseValue(properties.getProperty(property.getName())); + + } + + /** + * Given a GuacamoleProperty, parses and returns the value set for that + * property in guacamole.properties. An exception is thrown if the value + * is not provided. + * + * @param The type that the given property is parsed into. + * @param property The property to read from guacamole.properties. + * @return The parsed value of the property as read from + * guacamole.properties. + * @throws GuacamoleException If an error occurs while parsing the value + * for the given property in + * guacamole.properties, or if the property is + * not specified. + */ + public static Type getRequiredProperty(GuacamoleProperty property) + throws GuacamoleException { + + Type value = getProperty(property); + if (value == null) + throw new GuacamoleServerException("Property " + property.getName() + " is required."); + + return value; + + } +} diff --git a/guacamole-ext/src/main/java/net/sourceforge/guacamole/properties/GuacamoleProperty.java b/guacamole-ext/src/main/java/net/sourceforge/guacamole/properties/GuacamoleProperty.java new file mode 100644 index 000000000..aae793623 --- /dev/null +++ b/guacamole-ext/src/main/java/net/sourceforge/guacamole/properties/GuacamoleProperty.java @@ -0,0 +1,71 @@ + +package net.sourceforge.guacamole.properties; + +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is guacamole-ext. + * + * The Initial Developer of the Original Code is + * Michael Jumper. + * Portions created by the Initial Developer are Copyright (C) 2010 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +import net.sourceforge.guacamole.GuacamoleException; + +/** + * An abstract representation of a property in the guacamole.properties file, + * which parses into a specific type. + * + * @author Michael Jumper + * @param The type this GuacamoleProperty will parse into. + */ +public interface GuacamoleProperty { + + /** + * Returns the name of the property in guacamole.properties that this + * GuacamoleProperty will parse. + * + * @return The name of the property in guacamole.properties that this + * GuacamoleProperty will parse. + */ + public String getName(); + + /** + * Parses the given string value into the type associated with this + * GuacamoleProperty. + * + * @param value The string value to parse. + * @return The parsed value. + * @throws GuacamoleException If an error occurs while parsing the + * provided value. + */ + public Type parseValue(String value) throws GuacamoleException; + +} diff --git a/guacamole-ext/src/main/java/net/sourceforge/guacamole/properties/IntegerGuacamoleProperty.java b/guacamole-ext/src/main/java/net/sourceforge/guacamole/properties/IntegerGuacamoleProperty.java new file mode 100644 index 000000000..479d2bd5b --- /dev/null +++ b/guacamole-ext/src/main/java/net/sourceforge/guacamole/properties/IntegerGuacamoleProperty.java @@ -0,0 +1,67 @@ + +package net.sourceforge.guacamole.properties; + +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is guacamole-ext. + * + * The Initial Developer of the Original Code is + * Michael Jumper. + * Portions created by the Initial Developer are Copyright (C) 2010 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +import net.sourceforge.guacamole.GuacamoleException; +import net.sourceforge.guacamole.GuacamoleServerException; + +/** + * A GuacamoleProperty whose value is an integer. + * + * @author Michael Jumper + */ +public abstract class IntegerGuacamoleProperty implements GuacamoleProperty { + + @Override + public Integer parseValue(String value) throws GuacamoleException { + + // If no property provided, return null. + if (value == null) + return null; + + try { + Integer integer = new Integer(value); + return integer; + } + catch (NumberFormatException e) { + throw new GuacamoleServerException("Property \"" + getName() + "\" must be an integer.", e); + } + + } + +} diff --git a/guacamole-ext/src/main/java/net/sourceforge/guacamole/properties/StringGuacamoleProperty.java b/guacamole-ext/src/main/java/net/sourceforge/guacamole/properties/StringGuacamoleProperty.java new file mode 100644 index 000000000..74fc97e6d --- /dev/null +++ b/guacamole-ext/src/main/java/net/sourceforge/guacamole/properties/StringGuacamoleProperty.java @@ -0,0 +1,54 @@ + +package net.sourceforge.guacamole.properties; + +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is guacamole-ext. + * + * The Initial Developer of the Original Code is + * Michael Jumper. + * Portions created by the Initial Developer are Copyright (C) 2010 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +import net.sourceforge.guacamole.GuacamoleException; + +/** + * A GuacamoleProperty whose value is a simple string. + * + * @author Michael Jumper + */ +public abstract class StringGuacamoleProperty implements GuacamoleProperty { + + @Override + public String parseValue(String value) throws GuacamoleException { + return value; + } + +} diff --git a/guacamole-ext/src/main/java/net/sourceforge/guacamole/properties/package-info.java b/guacamole-ext/src/main/java/net/sourceforge/guacamole/properties/package-info.java new file mode 100644 index 000000000..1c01c62c4 --- /dev/null +++ b/guacamole-ext/src/main/java/net/sourceforge/guacamole/properties/package-info.java @@ -0,0 +1,7 @@ + +/** + * Provides classes for reading properties from the web-application-wide + * guacamole.properties file. + */ +package net.sourceforge.guacamole.properties; +