ENT-1673 Created reusable "createDatasourceFromDriverJars" function

This commit is contained in:
Tudor Malene 2018-03-21 15:29:46 +00:00 committed by tudor.malene@gmail.com
parent 882b18184f
commit 8371a65ed8
2 changed files with 31 additions and 16 deletions

View File

@ -14,10 +14,14 @@ import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
import com.zaxxer.hikari.util.PropertyElf
import net.corda.core.internal.declaredField
import net.corda.core.internal.div
import org.h2.engine.Database
import org.h2.engine.Engine
import org.slf4j.LoggerFactory
import java.lang.reflect.Modifier
import java.net.URLClassLoader
import java.nio.file.Files
import java.nio.file.Path
import java.util.*
import javax.sql.DataSource
@ -53,4 +57,28 @@ object DataSourceFactory {
}
}
}
}
fun createDatasourceFromDriverJars(dataSourceProperties: Properties, baseClassLoader: ClassLoader, driverJarsPath: Path): DataSource {
return URLClassLoader(Files.newDirectoryStream(driverJarsPath, "*.jar").map { it.toUri().toURL() }.toTypedArray(), baseClassLoader).use { driversClassLoader ->
val dataSourceClassName = dataSourceProperties["dataSourceClassName"] as String?
val dataSourceClass = driversClassLoader.loadClass(dataSourceClassName)
val dataSourceInstance = dataSourceClass.newInstance() as DataSource
val props = Properties().also {
it.putAll(dataSourceProperties.propertyNames().toList()
.filter { name -> (name as String).startsWith("dataSource.") }
.map { name -> (name as String).substring("dataSource.".length) to (dataSourceProperties[name]) }.toMap())
}
PropertyElf.setTargetFromProperties(dataSourceInstance, props)
dataSourceInstance
}
}
fun createHikariDatasourceFromDriverJars(dataSourceProperties: Properties, baseClassLoader: ClassLoader, driverJarsPath: Path): DataSource {
val dataSource = createDatasourceFromDriverJars(dataSourceProperties, baseClassLoader, driverJarsPath)
val cfg = HikariConfig(dataSourceProperties)
cfg.dataSource = dataSource
return HikariDataSource(cfg)
}
}

View File

@ -22,6 +22,7 @@ import net.corda.core.internal.MigrationHelpers
import net.corda.core.internal.copyTo
import net.corda.core.internal.div
import net.corda.core.schemas.MappedSchema
import net.corda.node.internal.DataSourceFactory.createDatasourceFromDriverJars
import net.corda.node.internal.cordapp.CordappLoader
import net.corda.node.services.config.ConfigHelper
import net.corda.node.services.config.parseAsNodeConfiguration
@ -213,21 +214,7 @@ private fun getMigrationOutput(baseDirectory: Path, options: OptionSet): Writer
private fun runWithDataSource(config: Configuration, baseDirectory: Path, classLoader: ClassLoader, withDatasource: (DataSource) -> Unit) {
val driversFolder = baseDirectory / "drivers"
val dataSourceClass = config.dataSourceProperties["dataSourceClassName"] as String?
return URLClassLoader(Files.newDirectoryStream(driversFolder, "*.jar").map { it.toUri().toURL() }.toTypedArray(), classLoader).use { driversClassLoader ->
val driverClass = driversClassLoader.loadClass(dataSourceClass)
val dataSourceInstance = driverClass.newInstance() as DataSource
val props = Properties().also {
it.putAll(config.dataSourceProperties.propertyNames().toList()
.filter { name -> (name as String).startsWith("dataSource.") }
.map { name -> (name as String).substring("dataSource.".length) to (config.dataSourceProperties[name]) }.toMap())
}
PropertyElf.setTargetFromProperties(dataSourceInstance, props)
withDatasource(dataSourceInstance)
}
return withDatasource(createDatasourceFromDriverJars(config.dataSourceProperties, classLoader, driversFolder))
}
private fun errorAndExit(message: String?) {