mirror of
https://github.com/gyurix1968/guacamole-client.git
synced 2025-09-06 05:07:41 +00:00
GUACAMOLE-1407: Merge automatic detection of whether MySQL/MariaDB driver
This commit is contained in:
@@ -192,4 +192,24 @@ public abstract class JDBCEnvironment extends DelegatingEnvironment {
|
|||||||
*/
|
*/
|
||||||
public abstract String getPassword() throws GuacamoleException;
|
public abstract String getPassword() throws GuacamoleException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns whether the given Java class is defined within the classpath.
|
||||||
|
*
|
||||||
|
* @param classname
|
||||||
|
* The name of the Java class to check.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
* true if the given Java class is present within the classpath, false
|
||||||
|
* otherwise.
|
||||||
|
*/
|
||||||
|
public static boolean isClassDefined(String classname) {
|
||||||
|
try {
|
||||||
|
Class.forName(classname, false, JDBCEnvironment.class.getClassLoader());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (ClassNotFoundException e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
package org.apache.guacamole.auth.mysql.conf;
|
package org.apache.guacamole.auth.mysql.conf;
|
||||||
|
|
||||||
|
import org.apache.guacamole.auth.jdbc.JDBCEnvironment;
|
||||||
import org.apache.guacamole.properties.EnumGuacamoleProperty.PropertyValue;
|
import org.apache.guacamole.properties.EnumGuacamoleProperty.PropertyValue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -31,11 +32,39 @@ public enum MySQLDriver {
|
|||||||
* MySQL driver.
|
* MySQL driver.
|
||||||
*/
|
*/
|
||||||
@PropertyValue("mysql")
|
@PropertyValue("mysql")
|
||||||
MYSQL,
|
MYSQL("com.mysql.jdbc.Driver"),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* MariaDB driver.
|
* MariaDB driver.
|
||||||
*/
|
*/
|
||||||
@PropertyValue("mariadb")
|
@PropertyValue("mariadb")
|
||||||
MARIADB;
|
MARIADB("org.mariadb.jdbc.Driver");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The name of the JDBC driver class.
|
||||||
|
*/
|
||||||
|
private final String driverClass;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new MySQLDriver that points to the given Java class as the
|
||||||
|
* entrypoint of the JDBC driver.
|
||||||
|
*
|
||||||
|
* @param classname
|
||||||
|
* The name of the JDBC driver class.
|
||||||
|
*/
|
||||||
|
private MySQLDriver(String classname) {
|
||||||
|
this.driverClass = classname;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns whether this MySQL JDBC driver is installed and can be found
|
||||||
|
* within the Java classpath.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
* true if this MySQL JDBC driver is installed, false otherwise.
|
||||||
|
*/
|
||||||
|
public boolean isInstalled() {
|
||||||
|
return JDBCEnvironment.isClassDefined(driverClass);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@@ -25,6 +25,7 @@ import java.sql.DatabaseMetaData;
|
|||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
import org.apache.guacamole.GuacamoleException;
|
import org.apache.guacamole.GuacamoleException;
|
||||||
|
import org.apache.guacamole.GuacamoleServerException;
|
||||||
import org.apache.guacamole.auth.jdbc.JDBCEnvironment;
|
import org.apache.guacamole.auth.jdbc.JDBCEnvironment;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@@ -53,11 +54,6 @@ public class MySQLEnvironment extends JDBCEnvironment {
|
|||||||
*/
|
*/
|
||||||
private static final MySQLVersion MYSQL_SUPPORTS_CTE = new MySQLVersion(8, 0, 1, false);
|
private static final MySQLVersion MYSQL_SUPPORTS_CTE = new MySQLVersion(8, 0, 1, false);
|
||||||
|
|
||||||
/**
|
|
||||||
* The default MySQL-compatible driver to use, if not specified.
|
|
||||||
*/
|
|
||||||
private static final MySQLDriver DEFAULT_DRIVER = MySQLDriver.MYSQL;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The default host to connect to, if MYSQL_HOSTNAME is not specified.
|
* The default host to connect to, if MYSQL_HOSTNAME is not specified.
|
||||||
*/
|
*/
|
||||||
@@ -178,21 +174,41 @@ public class MySQLEnvironment extends JDBCEnvironment {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the MySQL driver that will be used to talk to the MySQL-compatible
|
* Returns the MySQL driver that will be used to talk to the MySQL-compatible
|
||||||
* database server hosting the Guacamole Client database. If unspecified
|
* database server hosting the Guacamole database. If unspecified, the
|
||||||
* a default value of MySQL will be used.
|
* installed MySQL driver will be automatically detected by inspecting the
|
||||||
|
* classes available in the classpath.
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
* The MySQL driver that will be used to communicate with the MySQL-
|
* The MySQL driver that will be used to communicate with the MySQL-
|
||||||
* compatible server.
|
* compatible server.
|
||||||
*
|
*
|
||||||
* @throws GuacamoleException
|
* @throws GuacamoleException
|
||||||
* If guacamole.properties cannot be parsed.
|
* If guacamole.properties cannot be parsed, or if no MySQL-compatible
|
||||||
|
* JDBC driver is present.
|
||||||
*/
|
*/
|
||||||
public MySQLDriver getMySQLDriver() throws GuacamoleException {
|
public MySQLDriver getMySQLDriver() throws GuacamoleException {
|
||||||
return getProperty(
|
|
||||||
MySQLGuacamoleProperties.MYSQL_DRIVER,
|
// Use any explicitly-specified driver
|
||||||
DEFAULT_DRIVER
|
MySQLDriver driver = getProperty(MySQLGuacamoleProperties.MYSQL_DRIVER);
|
||||||
);
|
if (driver != null)
|
||||||
|
return driver;
|
||||||
|
|
||||||
|
// Attempt autodetection based on presence of JDBC driver within
|
||||||
|
// classpath...
|
||||||
|
|
||||||
|
if (MySQLDriver.MARIADB.isInstalled()) {
|
||||||
|
logger.info("Installed JDBC driver for MySQL/MariaDB detected as \"MariaDB Connector/J\".");
|
||||||
|
return MySQLDriver.MARIADB;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (MySQLDriver.MYSQL.isInstalled()) {
|
||||||
|
logger.info("Installed JDBC driver for MySQL/MariaDB detected as \"MySQL Connector/J\".");
|
||||||
|
return MySQLDriver.MYSQL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// No driver found at all
|
||||||
|
throw new GuacamoleServerException("No JDBC driver for MySQL/MariaDB is installed.");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user