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;
|
||||
|
||||
import org.elasticsearch.Version;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
import org.elasticsearch.xpack.sql.jdbc.jdbc.JdbcDriver;
|
||||
|
||||
|
@ -13,11 +14,25 @@ import java.security.PrivilegedExceptionAction;
|
|||
import java.sql.Driver;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class DriverManagerRegistrationTests extends ESTestCase {
|
||||
|
||||
|
||||
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/";
|
||||
Driver driver = null;
|
||||
try {
|
||||
|
@ -27,11 +42,15 @@ public class DriverManagerRegistrationTests extends ESTestCase {
|
|||
assertEquals("No suitable driver", ex.getMessage());
|
||||
}
|
||||
boolean set = driver != null;
|
||||
|
||||
try {
|
||||
Driver d = JdbcDriver.register();
|
||||
JdbcDriver d = JdbcDriver.register();
|
||||
if (driver != null) {
|
||||
assertEquals(driver, d);
|
||||
}
|
||||
|
||||
c.accept(d);
|
||||
|
||||
AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
|
||||
// mimic DriverManager and unregister the driver
|
||||
JdbcDriver.deregister();
|
||||
|
|
|
@ -54,12 +54,12 @@ public class Version {
|
|||
try {
|
||||
res = Version.class.getClassLoader().getResources(target);
|
||||
} 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) {
|
||||
List<URL> urls = Collections.list(res);
|
||||
Set<String> normalized = new LinkedHashSet<String>();
|
||||
Set<String> normalized = new LinkedHashSet<>();
|
||||
|
||||
for (URL url : urls) {
|
||||
normalized.add(StringUtils.normalize(url.toString()));
|
||||
|
@ -68,7 +68,7 @@ public class Version {
|
|||
int foundJars = 0;
|
||||
if (normalized.size() > 1) {
|
||||
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) {
|
||||
if (s.contains("jar:")) {
|
||||
foundJars++;
|
||||
|
@ -91,7 +91,7 @@ public class Version {
|
|||
String ver = "Unknown";
|
||||
String hash = ver;
|
||||
|
||||
if (urlStr.startsWith("file:/") && urlStr.endsWith(".jar")) {
|
||||
if (urlStr.endsWith(".jar")) {
|
||||
try (JarInputStream jar = new JarInputStream(url.openStream())) {
|
||||
Manifest manifest = jar.getManifest();
|
||||
hash = manifest.getMainAttributes().getValue("Change");
|
||||
|
@ -101,7 +101,7 @@ public class Version {
|
|||
min = vers[1];
|
||||
rev = vers[2];
|
||||
} 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);
|
||||
|
|
Loading…
Reference in New Issue