
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-common.
 *
 * 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.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 {

            InputStream stream = GuacamoleProperties.class.getResourceAsStream("/guacamole.properties");
            if (stream == null)
                throw new IOException("Resource /guacamole.properties not found.");

            // 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 <Type> 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> Type getProperty(GuacamoleProperty<Type> 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 <Type> 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> Type getRequiredProperty(GuacamoleProperty<Type> property)
            throws GuacamoleException {

        Type value = getProperty(property);
        if (value == null)
            throw new GuacamoleServerException("Property " + property.getName() + " is required.");

        return value;

    }
}

@@ -1720,16 +1720,69 @@
 *** */%0A%0A
+import java.io.File;%0Aimport java.io.FileInputStream;%0A
 import j
@@ -1797,24 +1797,24 @@
 OException;%0A
-
 import java.
@@ -2941,27 +2941,672 @@
         
-InputStream
+// Attempt to find Guacamole home%0A            File guacHome;%0A            %0A            // Get explicitly specified directory, if any%0A            String desiredDir = System.getProperty(%22guacamole.home%22);%0A            if (desiredDir != null)%0A                guacHome = new File(desiredDir);%0A%0A            // If not explicitly-define directory, use ~/.guacamole%0A            else%0A                guacHome = new File(System.getProperty(%22user.home%22), %22.guacamole%22);%0A%0A            InputStream stream;%0A            %0A            // If not a directory, load from classpath%0A            if (!guacHome.isDirectory()) %7B%0A%0A                // Read from classpath%0A               
  stream 
@@ -3675,24 +3675,28 @@
 operties%22);%0A
+    
             
@@ -3723,32 +3723,36 @@
                 
+    
 throw new IOExce
@@ -3761,51 +3761,403 @@
 ion(
-%22Resource /guacamole.properties
+%0A                        %22guacamole.properties not loaded from %22 + guacHome%0A                      + %22 (not a directory), and guacamole.properties could%22%0A                      + %22
  not 
+be 
 found
-.%22
+ as a resource in the classpath.%22);%0A%0A            %7D%0A%0A            // Otherwise, try to load from file%0A            else%0A                stream = new FileInputStream(new File(guacHome, %22guacamole.properties%22)
 );%0A%0A

Read guacamole.properties from home directory first.