From 18ddb06209accbf5c844a46120f58d52a76349a5 Mon Sep 17 00:00:00 2001 From: Ingwersaft Date: Wed, 20 Feb 2019 18:26:10 +0100 Subject: [PATCH] changed printlns to slf4j logger (#16); rethrowing exceptions so the task will fail (#17) --- .../wildlydeplyplugin/CliExecutioner.kt | 15 +++--- .../wildlydeplyplugin/DeployWildflyPlugin.kt | 21 ++++---- .../wildlydeplyplugin/ExecuteWildflyTask.kt | 12 +++-- .../mkring/wildlydeplyplugin/FileDeployer.kt | 53 ++++++++++--------- 4 files changed, 56 insertions(+), 45 deletions(-) diff --git a/src/main/kotlin/com/mkring/wildlydeplyplugin/CliExecutioner.kt b/src/main/kotlin/com/mkring/wildlydeplyplugin/CliExecutioner.kt index 98f0d94..b086425 100644 --- a/src/main/kotlin/com/mkring/wildlydeplyplugin/CliExecutioner.kt +++ b/src/main/kotlin/com/mkring/wildlydeplyplugin/CliExecutioner.kt @@ -1,6 +1,9 @@ package com.mkring.wildlydeplyplugin import org.jboss.`as`.cli.scriptsupport.CLI +import org.slf4j.LoggerFactory + +private val log = LoggerFactory.getLogger(CliExecutioner::class.java) object CliExecutioner { fun execute( @@ -10,23 +13,23 @@ object CliExecutioner { password: String?, commands: List ) { - println("deploy(): " + this) + log.debug("deploy(): " + this) checkHostDns(host) checkSocket(host, port) CLI.newInstance().let { cli -> - println("wildfly connect with $user on $host:$port") + log.debug("wildfly connect with $user on $host:$port") connect(cli, host, port, user, password) commands.forEach { - println("going to execute `$it`") + log.debug("going to execute `$it`") val result: CLI.Result? = cli.cmd(it) - println("result: ${result?.isSuccess}") + log.debug("result: ${result?.isSuccess}") try { result?.response?.get("result")?.asString()?.let { - println("result string:\n$it") + log.debug("result string:\n$it") } ?: run { println("result or response null") } } catch (e: Exception) { - println("cmd might have failed: ${e.message}") + log.debug("cmd might have failed: ${e.message}") } } diff --git a/src/main/kotlin/com/mkring/wildlydeplyplugin/DeployWildflyPlugin.kt b/src/main/kotlin/com/mkring/wildlydeplyplugin/DeployWildflyPlugin.kt index 3342783..8733ab2 100644 --- a/src/main/kotlin/com/mkring/wildlydeplyplugin/DeployWildflyPlugin.kt +++ b/src/main/kotlin/com/mkring/wildlydeplyplugin/DeployWildflyPlugin.kt @@ -5,14 +5,17 @@ import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.tasks.Input import org.gradle.api.tasks.TaskAction +import org.slf4j.LoggerFactory open class DeployWildflyPlugin : Plugin { + val log = LoggerFactory.getLogger(DeployWildflyPlugin::class.java) override fun apply(project: Project) { - println("DeployWildflyPlugin applied") + log.debug("DeployWildflyPlugin applied") } } open class DeployWildflyTask : DefaultTask() { + val log = LoggerFactory.getLogger(DeployWildflyTask::class.java) @Input var file: String? = null @@ -57,24 +60,24 @@ open class DeployWildflyTask : DefaultTask() { @TaskAction fun deployWildfly() { if (file == null || user == null || password == null) { - println("DeployWildflyTask: missing configuration") + log.error("DeployWildflyTask: missing configuration") return } if (reload && restart) { - println("reload && restart are mutually exclusive!") + log.error("reload && restart are mutually exclusive!") return } if (awaitReload && awaitRestart) { - println("awaitReload && awaitRestart are mutually exclusive!") + log.error("awaitReload && awaitRestart are mutually exclusive!") return } if (awaitReload && reload.not()) { - println("awaitReload is pointless if no reload is set") + log.warn("awaitReload is pointless if no reload is set") } if (awaitRestart && restart.not()) { - println("awaitRestart is pointless if no restart is set") + log.warn("awaitRestart is pointless if no restart is set") } - println("deployWildfly: going to deploy $file to $host:$port") + log.info("deployWildfly: going to deploy $file to $host:$port") try { FileDeployer( file, @@ -92,8 +95,8 @@ open class DeployWildflyTask : DefaultTask() { awaitRestart ).deploy() } catch (e: Exception) { - println("deployWildfly task failed: ${e.message}") - e.printStackTrace() + log.error("deployWildfly task failed: ${e.message}", e) + throw e } } } \ No newline at end of file diff --git a/src/main/kotlin/com/mkring/wildlydeplyplugin/ExecuteWildflyTask.kt b/src/main/kotlin/com/mkring/wildlydeplyplugin/ExecuteWildflyTask.kt index 5f7cf67..21b75d7 100644 --- a/src/main/kotlin/com/mkring/wildlydeplyplugin/ExecuteWildflyTask.kt +++ b/src/main/kotlin/com/mkring/wildlydeplyplugin/ExecuteWildflyTask.kt @@ -3,8 +3,10 @@ package com.mkring.wildlydeplyplugin import org.gradle.api.DefaultTask import org.gradle.api.tasks.Input import org.gradle.api.tasks.TaskAction +import org.slf4j.LoggerFactory open class ExecuteWildflyTask : DefaultTask() { + val log = LoggerFactory.getLogger(ExecuteWildflyTask::class.java) @Input var host: String = "localhost" @Input @@ -26,18 +28,18 @@ open class ExecuteWildflyTask : DefaultTask() { @TaskAction fun executeWildfly() { if (user == null || password == null || commands == null || commands?.isEmpty() == true) { - println("ExecuteWildflyTask: missing configuration") + log.error("ExecuteWildflyTask: missing configuration") return } - println("ExecuteWildflyTask: on $host:$port going to execute:") + log.info("ExecuteWildflyTask: on $host:$port going to execute:") commands?.forEach { - println("`$it`") + log.debug("command: `$it`") } try { CliExecutioner.execute(host, port, user, password, commands ?: emptyList()) } catch (e: Exception) { - println("ExecuteWildflyTask task failed: ${e.message}") - e.printStackTrace() + log.error("ExecuteWildflyTask task failed: ${e.message}", e) + throw e } } } \ No newline at end of file diff --git a/src/main/kotlin/com/mkring/wildlydeplyplugin/FileDeployer.kt b/src/main/kotlin/com/mkring/wildlydeplyplugin/FileDeployer.kt index 886ba05..ee51c63 100644 --- a/src/main/kotlin/com/mkring/wildlydeplyplugin/FileDeployer.kt +++ b/src/main/kotlin/com/mkring/wildlydeplyplugin/FileDeployer.kt @@ -3,6 +3,7 @@ package com.mkring.wildlydeplyplugin import org.jboss.`as`.cli.CommandLineException import org.jboss.`as`.cli.scriptsupport.CLI import org.jboss.dmr.ModelNode +import org.slf4j.LoggerFactory import java.io.File import java.net.InetAddress import java.net.InetSocketAddress @@ -10,6 +11,8 @@ import java.net.Socket import java.time.LocalDateTime import java.time.temporal.ChronoUnit +private val log = LoggerFactory.getLogger(FileDeployer::class.java) + class FileDeployer( private val file: String?, private val host: String, @@ -25,12 +28,13 @@ class FileDeployer( private val restart: Boolean, private val awaitRestart: Boolean ) { + fun deploy() { - println("deploy(): " + this) + log.debug("deploy(): " + this) checkHostDns(host) checkSocket(host, port) CLI.newInstance().let { cli -> - println("wildfly connect with $user on $host:$port") + log.debug("wildfly connect with $user on $host:$port") connect(cli, host, port, user, password) val force = if (force) { "--force" @@ -47,17 +51,17 @@ class FileDeployer( } else { "" } - println("connected successfully") + log.debug("connected successfully") val deploymentExists = File(file).isFile - println("given $file existent: $deploymentExists") + log.debug("given $file existent: $deploymentExists") if (deploymentExists.not()) throw IllegalStateException("couldn't find given deployment") if (undeployBeforehand) { - println("\nundeploying existing deployment with same name if preset...") + log.debug("\nundeploying existing deployment with same name if preset...") val shouldUndeploy = blockingCmd("deployment-info", 2, ChronoUnit.MINUTES).response.get("result").asList() .any { it.asProperty().name == name.removePrefix("--name=") } - println("shouldUndeploy=$shouldUndeploy") + log.debug("shouldUndeploy=$shouldUndeploy") if (shouldUndeploy) { blockingCmd("undeploy $name", 2, ChronoUnit.MINUTES).response.also { println("undeploy response: $it\n") @@ -67,26 +71,26 @@ class FileDeployer( // deploy val deploySuccess = cli.cmd("deploy $force $name $runtimeName $file").isSuccess - println("deploy success: $deploySuccess") + log.debug("deploy success: $deploySuccess") enableDeploymentIfNecessary(name) if (reload) { try { - println("going to reload wildfly") + log.debug("going to reload wildfly") val reloadSuccess = cli.cmd("reload").isSuccess - println("reload success: $reloadSuccess") + log.debug("reload success: $reloadSuccess") } catch (e: CommandLineException) { - println("looks like reload timed out: ${e.message}") + log.debug("looks like reload timed out: ${e.message}") } } if (restart) { try { - println("going to restart wildfly") + log.debug("going to restart wildfly") val restartSuccess = cli.cmd("shutdown --restart=true").isSuccess - println("restart success: $restartSuccess") + log.debug("restart success: $restartSuccess") } catch (e: CommandLineException) { - println("looks like restart timed out: ${e.message}") + log.debug("looks like restart timed out: ${e.message}") } } cli.disconnect() @@ -101,15 +105,15 @@ class FileDeployer( } fun blockTillCliIsBack() { - println("going to block until the reload/restart finished...\n") + log.debug("going to block until the reload/restart finished...\n") Thread.sleep(1000) val postReloadDeploymentInfoPrettyPrint = blockingCmd("deployment-info", 1, ChronoUnit.MINUTES).response.responsePrettyPrint() - println("\n\nPOST reload/restart deployment info:\n$postReloadDeploymentInfoPrettyPrint") + log.debug("\n\nPOST reload/restart deployment info:\n$postReloadDeploymentInfoPrettyPrint") } private fun enableDeploymentIfNecessary(name: String) { - println("\nchecking if deployment is enabled...") + log.debug("\nchecking if deployment is enabled...") val deploymentEnabled = blockingCmd("deployment-info", 2, ChronoUnit.MINUTES).response.get("result").asList().map { it.asProperty().name to it.getParam("enabled").removePrefix("enabled: ") @@ -118,9 +122,9 @@ class FileDeployer( }?.second?.toBoolean() if (deploymentEnabled == false) { - println("not enabled! going to enable now!") + log.debug("not enabled! going to enable now!") blockingCmd("deploy $name", 2, ChronoUnit.MINUTES).response.also { - println("enable response: $it\n") + log.debug("enable response: $it\n") } } } @@ -140,14 +144,14 @@ class FileDeployer( } return cmd } catch (e: Exception) { - println("connect + cmd exception: ${e::class.java.simpleName}:${e.message}") + log.debug("connect + cmd exception: ${e::class.java.simpleName}:${e.message}") Thread.sleep(500) continue } finally { try { cli.disconnect() } catch (e: Exception) { - println("disconnect exception: ${e::class.java.simpleName}:${e.message}") + log.debug("disconnect exception: ${e::class.java.simpleName}:${e.message}") throw e } } @@ -181,7 +185,7 @@ fun connect( } fun checkHostDns(host: String) { - println("$host DNS: ${InetAddress.getAllByName(host).joinToString(";")}") + log.debug("$host DNS: ${InetAddress.getAllByName(host).joinToString(";")}") } fun checkSocket(host: String, port: Int) { @@ -189,11 +193,10 @@ fun checkSocket(host: String, port: Int) { try { it.connect(InetSocketAddress(host, port), 2000) it.close() - println("socket connect worked") + log.debug("socket connect worked") } catch (e: Exception) { - println("looks like we can't connect?!") - println("${e.message}") - e.printStackTrace() + log.debug("looks like we can't connect?!") + log.debug("${e.message}") } } } \ No newline at end of file