From 4a3a8905ef0cfd1fbec15dd59afb101ac8f83d1f Mon Sep 17 00:00:00 2001 From: jaroslawmalekcodete Date: Fri, 17 Jul 2020 13:09:14 +0200 Subject: [PATCH] #kernel_base_10: add BxDriverManager --- .../beakerx/kernel/RuntimetoolsImpl.java | 22 ++++++-- runtimetools/build.gradle | 32 +++++++++++ .../com/twosigma/beakerx/BxDriverManager.java | 53 +++++++++++++++++++ settings.gradle | 5 +- 4 files changed, 106 insertions(+), 6 deletions(-) create mode 100644 runtimetools/build.gradle create mode 100644 runtimetools/src/main/java/com/twosigma/beakerx/BxDriverManager.java diff --git a/base/src/main/java/com/twosigma/beakerx/kernel/RuntimetoolsImpl.java b/base/src/main/java/com/twosigma/beakerx/kernel/RuntimetoolsImpl.java index 4278a6a..d6f32ce 100644 --- a/base/src/main/java/com/twosigma/beakerx/kernel/RuntimetoolsImpl.java +++ b/base/src/main/java/com/twosigma/beakerx/kernel/RuntimetoolsImpl.java @@ -18,18 +18,30 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.Arrays; +import java.util.Optional; + +import static java.util.Arrays.asList; public class RuntimetoolsImpl implements Runtimetools { - public void configRuntimeJars(KernelFunctionality kernel) { -// kernel.addJarsToClasspath(Arrays.asList(new PathToJar(getJar("runtimetools")))); -// kernel.addImport(new ImportPath("com.twosigma.beakerx.BxDriverManager")); + public void configRuntimeJars(KernelFunctionality kernel) { + Optional runtimetools = getJar("runtimetools"); + if (runtimetools.isPresent()) { + kernel.addJarsToClasspath(asList(new PathToJar(runtimetools.get()))); + kernel.addImport(new ImportPath("com.twosigma.beakerx.BxDriverManager")); + } } - private String getJar(String name) { + private Optional getJar(String name) { try { Path path = Paths.get(KernelFunctionality.class.getProtectionDomain().getCodeSource().getLocation().toURI()); - return path.getParent().getParent().getParent().resolve(name).resolve("lib").resolve(name + ".jar").toString(); + Path location = path.getParent().getParent().getParent().resolve("kernel").resolve("ext"); + if (location.toFile().exists()) { + Optional jarFile = Arrays.stream(location.toFile().list()).filter(jar -> jar.contains(name)).findFirst(); + return jarFile.map(s -> location.resolve(s).toString()); + } else { + return Optional.empty(); + } } catch (Exception e) { throw new RuntimeException(e); } diff --git a/runtimetools/build.gradle b/runtimetools/build.gradle new file mode 100644 index 0000000..2b087e4 --- /dev/null +++ b/runtimetools/build.gradle @@ -0,0 +1,32 @@ +/* + * Copyright 2020 TWO SIGMA OPEN SOURCE, LLC + * + * Licensed 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. + */ + + +dependencies { + testCompile group: 'junit', name: 'junit', version: '4.12' +} + + +publishing { + publications { + maven(MavenPublication) { + groupId 'com.twosigma' + artifactId 'beakerx-runtimetools' + version "$beakerxVersion" + from components.java + } + } +} diff --git a/runtimetools/src/main/java/com/twosigma/beakerx/BxDriverManager.java b/runtimetools/src/main/java/com/twosigma/beakerx/BxDriverManager.java new file mode 100644 index 0000000..fbc83f7 --- /dev/null +++ b/runtimetools/src/main/java/com/twosigma/beakerx/BxDriverManager.java @@ -0,0 +1,53 @@ +/* + * Copyright 2018 TWO SIGMA OPEN SOURCE, LLC + * + * Licensed 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 com.twosigma.beakerx; + +import java.sql.Connection; +import java.sql.Driver; +import java.sql.DriverManager; +import java.sql.SQLException; + +public class BxDriverManager { + + public static Connection getConnection(String url) throws SQLException { + return DriverManager.getConnection(url); + } + + public static Connection getConnection(String url, String user, String password) throws SQLException { + return DriverManager.getConnection(url, user, password); + } + + public static Connection getConnection(String url, java.util.Properties info) throws SQLException { + return DriverManager.getConnection(url, info); + } + + public static Driver getDriver(String url) throws SQLException { + return DriverManager.getDriver(url); + } + + public static java.util.Enumeration getDrivers() { + return DriverManager.getDrivers(); + } + + public static synchronized void registerDriver(Driver driver) throws SQLException { + DriverManager.registerDriver(driver); + } + + public static synchronized void deregisterDriver(Driver driver) throws SQLException { + DriverManager.deregisterDriver(driver); + } + +} diff --git a/settings.gradle b/settings.gradle index 5c1335f..8cdf9b9 100644 --- a/settings.gradle +++ b/settings.gradle @@ -17,9 +17,12 @@ include 'doclet' include 'base-api' include 'base-test' include 'base' +include 'runtimetools' // uncomment when demo projects have to be rebuild //include 'demoProjects' //include 'demoProjects:loadMagicJarDemo' //include 'demoProjects:demo' -//include 'demoProjects:BeakerXClasspathTest' \ No newline at end of file +//include 'demoProjects:BeakerXClasspathTest' + +