SQL: Fix version loading in JDBC (elastic/x-pack-elasticsearch#4425)
When deployed, JDBC version needs to look at jars inside the classpath not on the file system. Failing to do that, causes the version information to be missing Original commit: elastic/x-pack-elasticsearch@d35e8abb29
This commit is contained in:
parent
935df40f70
commit
e6f69ee269
|
@ -5,6 +5,7 @@
|
||||||
*/
|
*/
|
||||||
package org.elasticsearch.xpack.sql.jdbc;
|
package org.elasticsearch.xpack.sql.jdbc;
|
||||||
|
|
||||||
|
import org.elasticsearch.Version;
|
||||||
import org.elasticsearch.test.ESTestCase;
|
import org.elasticsearch.test.ESTestCase;
|
||||||
import org.elasticsearch.xpack.sql.jdbc.jdbc.JdbcDriver;
|
import org.elasticsearch.xpack.sql.jdbc.jdbc.JdbcDriver;
|
||||||
|
|
||||||
|
@ -13,11 +14,25 @@ import java.security.PrivilegedExceptionAction;
|
||||||
import java.sql.Driver;
|
import java.sql.Driver;
|
||||||
import java.sql.DriverManager;
|
import java.sql.DriverManager;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public class DriverManagerRegistrationTests extends ESTestCase {
|
public class DriverManagerRegistrationTests extends ESTestCase {
|
||||||
|
|
||||||
|
|
||||||
public void testRegistration() throws Exception {
|
public void testRegistration() throws Exception {
|
||||||
|
driverManagerTemplate(d -> assertNotNull(d));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testVersioning() throws Exception {
|
||||||
|
driverManagerTemplate(d -> {
|
||||||
|
/* This test will only work properly in gradle because in gradle we run the tests
|
||||||
|
* using the jar. */
|
||||||
|
|
||||||
|
assertNotEquals(String.valueOf(Version.CURRENT.major), d.getMajorVersion());
|
||||||
|
assertNotEquals(String.valueOf(Version.CURRENT.minor), d.getMinorVersion());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void driverManagerTemplate(Consumer<JdbcDriver> c) throws Exception {
|
||||||
String url = "jdbc:es:localhost:9200/";
|
String url = "jdbc:es:localhost:9200/";
|
||||||
Driver driver = null;
|
Driver driver = null;
|
||||||
try {
|
try {
|
||||||
|
@ -27,11 +42,15 @@ public class DriverManagerRegistrationTests extends ESTestCase {
|
||||||
assertEquals("No suitable driver", ex.getMessage());
|
assertEquals("No suitable driver", ex.getMessage());
|
||||||
}
|
}
|
||||||
boolean set = driver != null;
|
boolean set = driver != null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Driver d = JdbcDriver.register();
|
JdbcDriver d = JdbcDriver.register();
|
||||||
if (driver != null) {
|
if (driver != null) {
|
||||||
assertEquals(driver, d);
|
assertEquals(driver, d);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.accept(d);
|
||||||
|
|
||||||
AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
|
AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
|
||||||
// mimic DriverManager and unregister the driver
|
// mimic DriverManager and unregister the driver
|
||||||
JdbcDriver.deregister();
|
JdbcDriver.deregister();
|
||||||
|
|
|
@ -54,12 +54,12 @@ public class Version {
|
||||||
try {
|
try {
|
||||||
res = Version.class.getClassLoader().getResources(target);
|
res = Version.class.getClassLoader().getResources(target);
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
throw new IllegalArgumentException("Cannot detect Elasticsearch SQL jar; it typically indicates a deployment issue...");
|
throw new IllegalArgumentException("Cannot detect Elasticsearch JDBC jar; it typically indicates a deployment issue...");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (res != null) {
|
if (res != null) {
|
||||||
List<URL> urls = Collections.list(res);
|
List<URL> urls = Collections.list(res);
|
||||||
Set<String> normalized = new LinkedHashSet<String>();
|
Set<String> normalized = new LinkedHashSet<>();
|
||||||
|
|
||||||
for (URL url : urls) {
|
for (URL url : urls) {
|
||||||
normalized.add(StringUtils.normalize(url.toString()));
|
normalized.add(StringUtils.normalize(url.toString()));
|
||||||
|
@ -68,7 +68,7 @@ public class Version {
|
||||||
int foundJars = 0;
|
int foundJars = 0;
|
||||||
if (normalized.size() > 1) {
|
if (normalized.size() > 1) {
|
||||||
StringBuilder sb = new StringBuilder(
|
StringBuilder sb = new StringBuilder(
|
||||||
"Multiple Elasticsearch SQL versions detected in the classpath; please use only one\n");
|
"Multiple Elasticsearch JDBC versions detected in the classpath; please use only one\n");
|
||||||
for (String s : normalized) {
|
for (String s : normalized) {
|
||||||
if (s.contains("jar:")) {
|
if (s.contains("jar:")) {
|
||||||
foundJars++;
|
foundJars++;
|
||||||
|
@ -91,7 +91,7 @@ public class Version {
|
||||||
String ver = "Unknown";
|
String ver = "Unknown";
|
||||||
String hash = ver;
|
String hash = ver;
|
||||||
|
|
||||||
if (urlStr.startsWith("file:/") && urlStr.endsWith(".jar")) {
|
if (urlStr.endsWith(".jar")) {
|
||||||
try (JarInputStream jar = new JarInputStream(url.openStream())) {
|
try (JarInputStream jar = new JarInputStream(url.openStream())) {
|
||||||
Manifest manifest = jar.getManifest();
|
Manifest manifest = jar.getManifest();
|
||||||
hash = manifest.getMainAttributes().getValue("Change");
|
hash = manifest.getMainAttributes().getValue("Change");
|
||||||
|
@ -101,7 +101,7 @@ public class Version {
|
||||||
min = vers[1];
|
min = vers[1];
|
||||||
rev = vers[2];
|
rev = vers[2];
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
throw new IllegalArgumentException("Detected Elasticsearch SQL jar but cannot retrieve its version", ex);
|
throw new IllegalArgumentException("Detected Elasticsearch JDBC jar but cannot retrieve its version", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
CURRENT = new Version(ver, hash, maj, min, rev);
|
CURRENT = new Version(ver, hash, maj, min, rev);
|
||||||
|
|
Loading…
Reference in New Issue