protected fun reloadPluginInRuntime()

in src/main/kotlin/org/jetbrains/teamcity/maven/sdk/AbstractTeamCityMojo.kt [199:278]


    protected fun reloadPluginInRuntime(): Boolean {
        if (username.isEmpty()) {
            log.debug("No username provided, looking for maintenance token")
            val tokenFile = File(getDataDir(), "system${File.separator}pluginData${File.separator}superUser${File.separator}token.txt")
            if (!tokenFile.isFile) {
                log.warn("Neither username provider nor maintenance token file exists. Cannot send plugin reload request. Check that server has already started with '-Dteamcity.superUser.token.saveToFile=true' parameter or provide username and password.")
                return false
            } else {
                try {
                    password = tokenFile.readText().toLong().toString()
                    log.debug("Using $password maintenance token to authenticate")
                } catch (ex: NumberFormatException) {
                    log.warn("Malformed maintenance token: should contain a number")
                    return false
                }
            }
        }

        val authToken = "Basic " + String(Base64.getEncoder().encode("$username:$password".toByteArray(Charset.forName("UTF-8"))))

        val disableAction = getPluginReloadURL(false)
        log.debug("Sending " + disableAction.toString() + "...")
        val disableRequest = disableAction.openConnection()
        (disableRequest as HttpURLConnection).requestMethod = "POST"
        disableRequest.setRequestProperty ("Authorization", authToken)

        try {
            disableRequest.getInputStream().use {
                val result = it.buffered().bufferedReader(Charset.defaultCharset()).readLine()
                if (!result.contains("Plugin unloaded successfully")) {
                    if (result.contains("Plugin unloaded partially")) {
                        log.warn("Plugin unloaded partially - some parts could still be running. Server restart could be needed. Check all resources are released and threads are stopped on server shutdown. ")
                    } else {
                        log.warn(result)
                        return false
                    }
                } else {
                    log.info("Plugin successfully unloaded")
                }
            }
        } catch (ex: ConnectException) {
            log.warn("Cannot find running server on http://$serverAddress. Is server started?")
            return false
        } catch (ex: IOException) {
            when (disableRequest.responseCode) {
                401 -> log.warn("Cannot authenticate server on http://$serverAddress with " +
                        if (username.isEmpty())
                            "maintenance token $password. Check that server has already started with '-Dteamcity.superUser.token.saveToFile=true' parameter or provide username and password."
                        else
                            "provided credentials.")
            }
            log.warn("Cannot connect to the server on http://$serverAddress: ${disableRequest.responseCode}", ex)
            return false
        }

        uploadPluginAgentZip()

        val enableAction = getPluginReloadURL(true)
        log.debug("Sending " + enableAction.toString() + "...")
        val enableRequest = enableAction.openConnection()
        (enableRequest as HttpURLConnection).requestMethod = "POST"
        enableRequest.setRequestProperty ("Authorization", authToken)
        try {
            enableRequest.getInputStream().use {
                val result = it.buffered().bufferedReader(Charset.defaultCharset()).readLine()

                if (!result.contains("Plugin loaded successfully")) {
                    log.warn(result)
                    return false
                } else {
                    log.info("Plugin successfully loaded")
                }
            }
        } catch(ex: IOException) {
            log.warn("Cannot connect to the server on http://$serverAddress: ${disableRequest.responseCode}", ex)
            return false
        }

        return true
    }