GUACAMOLE-641: Allow extensions to add arbitrary sources of Guacamole properties.

This commit is contained in:
Michael Jumper
2020-08-25 02:25:08 -07:00
parent 9d7b979195
commit 61a3ec1331
7 changed files with 648 additions and 4 deletions

View File

@@ -19,9 +19,11 @@
package org.apache.guacamole.environment;
import org.apache.guacamole.properties.GuacamoleProperties;
import java.io.File;
import java.util.Map;
import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.GuacamoleUnsupportedException;
import org.apache.guacamole.net.auth.GuacamoleProxyConfiguration;
import org.apache.guacamole.properties.BooleanGuacamoleProperty;
import org.apache.guacamole.properties.GuacamoleProperty;
@@ -162,4 +164,24 @@ public interface Environment {
public GuacamoleProxyConfiguration getDefaultGuacamoleProxyConfiguration()
throws GuacamoleException;
/**
* Adds another possible source of Guacamole configuration properties to
* this Environment. Properties not already defined by other sources of
* Guacamole configuration properties will alternatively be read from the
* given {@link GuacamoleProperties}.
*
* @param properties
* The GuacamoleProperties to add to this Environment.
*
* @throws GuacamoleException
* If the given GuacamoleProperties cannot be added, or if this
* Environment does not support this operation.
*/
public default void addGuacamoleProperties(GuacamoleProperties properties)
throws GuacamoleException {
throw new GuacamoleUnsupportedException(String.format("%s does not "
+ "support dynamic definition of Guacamole properties.",
getClass()));
}
}

View File

@@ -0,0 +1,85 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.guacamole.properties;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.GuacamoleServerException;
/**
* GuacamoleProperties implementation which reads all properties from a
* standard Java properties file.
*/
public class FileGuacamoleProperties extends PropertiesGuacamoleProperties {
/**
* Reads the given Java properties file, storing all property name/value
* pairs in a new {@link Properties} object.
*
* @param propertiesFile
* The Java properties file to read.
*
* @return
* A new Properties containing all property name/value pairs defined in
* the given file.
*
* @throws GuacamoleException
* If an error prevents reading the given Java properties file.
*/
private static Properties read(File propertiesFile) throws GuacamoleException {
// Fail early if file simply does not exist
if (!propertiesFile.exists())
throw new GuacamoleServerException(String.format("\"%s\" does not "
+ "exist.", propertiesFile));
// Load properties from stream, if any, always closing stream when done
Properties properties = new Properties();
try (InputStream stream = new FileInputStream(propertiesFile)) {
properties.load(stream);
}
catch (IOException e) {
throw new GuacamoleServerException(String.format("\"%s\" cannot "
+ "be read: %s", propertiesFile, e.getMessage()), e);
}
return properties;
}
/**
* Creates a new FileGuacamoleProperties which reads all properties from
* the given standard Java properties file.
*
* @param propertiesFile
* The Java properties file to read.
*
* @throws GuacamoleException
* If an error prevents reading the given Java properties file.
*/
public FileGuacamoleProperties(File propertiesFile) throws GuacamoleException {
super(read(propertiesFile));
}
}

View File

@@ -0,0 +1,49 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.guacamole.properties;
import java.util.Properties;
import org.apache.guacamole.GuacamoleException;
/**
* An arbitrary set of Guacamole configuration property name/value pairs. This
* interface is similar in concept to {@link Properties} except that
* implementations are not required to allow properties to be enumerated or
* iterated. Properties may simply be retrieved by their names, if known.
*/
public interface GuacamoleProperties {
/**
* Returns the value of the property having the given name, if defined. If
* no such property exists, null is returned.
*
* @param name
* The name of the property to retrieve.
*
* @return
* The value of the given property, or null if no such property is
* defined.
*
* @throws GuacamoleException
* If an error prevents the given property from being read.
*/
String getProperty(String name) throws GuacamoleException;
}

View File

@@ -0,0 +1,54 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.guacamole.properties;
import java.util.Properties;
import org.apache.guacamole.GuacamoleException;
/**
* GuacamoleProperties implementation which reads all properties from a
* {@link Properties} object.
*/
public class PropertiesGuacamoleProperties implements GuacamoleProperties {
/**
* The Properties from which all property values should be read.
*/
private final Properties properties;
/**
* Creates a new PropertiesGuacamoleProperties which wraps the given
* {@link Properties}, providing access to the values of any properties
* defined therein.
*
* @param properties
* The Properties that should be used as the source of all property
* values exposed by this instance of PropertiesGuacamoleProperties.
*/
public PropertiesGuacamoleProperties(Properties properties) {
this.properties = properties;
}
@Override
public String getProperty(String name) throws GuacamoleException {
return properties.getProperty(name);
}
}