-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* TomcatExecutionListener * Catch exceptions and allow running * Ignore default Tomcat server port * Changelog entry * Format fix * Environment variable for tomcat server port
- Loading branch information
Showing
8 changed files
with
138 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fixed Tomcat support. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
fun properties(key: String) = project.findProperty(key).toString() | ||
|
||
plugins { | ||
// Java support | ||
id("java") | ||
// Kotlin support | ||
id("org.jetbrains.kotlin.jvm") version "1.8.22" | ||
// Gradle IntelliJ Plugin | ||
id("org.jetbrains.intellij") version "1.+" | ||
} | ||
|
||
tasks { | ||
buildSearchableOptions { | ||
enabled = false | ||
} | ||
} | ||
|
||
intellij { | ||
version.set(properties("platformVersion")) | ||
type.set("IU") | ||
plugins.set(listOf("Tomcat:223.7571.182", "com.intellij.javaee.app.servers.integration")) | ||
} | ||
|
||
dependencies { | ||
implementation(project(":mirrord-core")) | ||
} |
97 changes: 97 additions & 0 deletions
97
...s/tomcat/src/main/kotlin/com/metalbear/mirrord/products/tomcat/TomcatExecutionListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
@file:Suppress("UnstableApiUsage") | ||
|
||
package com.metalbear.mirrord.products.tomcat | ||
|
||
import com.intellij.execution.ExecutionListener | ||
import com.intellij.execution.process.ProcessHandler | ||
import com.intellij.execution.runners.ExecutionEnvironment | ||
import com.intellij.execution.target.createEnvironmentRequest | ||
import com.intellij.execution.util.EnvironmentVariable | ||
import com.intellij.execution.wsl.target.WslTargetEnvironmentRequest | ||
import com.intellij.javaee.appServers.run.configuration.RunnerSpecificLocalConfigurationBit | ||
import com.intellij.notification.NotificationType | ||
import com.intellij.openapi.components.service | ||
import com.metalbear.mirrord.CONFIG_ENV_NAME | ||
import com.metalbear.mirrord.MirrordLogger | ||
import com.metalbear.mirrord.MirrordProjectService | ||
import java.util.concurrent.ConcurrentHashMap | ||
|
||
private const val DEFAULT_TOMCAT_SERVER_PORT: String = "8005" | ||
|
||
private fun getTomcatServerPort(): String { | ||
return System.getenv("MIRRORD_TOMCAT_SERVER_PORT") ?: DEFAULT_TOMCAT_SERVER_PORT | ||
} | ||
|
||
class TomcatExecutionListener : ExecutionListener { | ||
private val savedEnvs: ConcurrentHashMap<String, List<EnvironmentVariable>> = ConcurrentHashMap() | ||
|
||
private fun getConfig(env: ExecutionEnvironment): RunnerSpecificLocalConfigurationBit? { | ||
if (!env.toString().startsWith("Tomcat")) { | ||
return null | ||
} | ||
|
||
val settings = env.configurationSettings ?: return null | ||
|
||
return if (settings is RunnerSpecificLocalConfigurationBit) { | ||
settings | ||
} else { | ||
null | ||
} | ||
} | ||
|
||
override fun processStartScheduled(executorId: String, env: ExecutionEnvironment) { | ||
getConfig(env)?.let { config -> | ||
val envVars = config.envVariables | ||
|
||
val service = env.project.service<MirrordProjectService>() | ||
|
||
MirrordLogger.logger.debug("wsl check") | ||
val wsl = when (val request = createEnvironmentRequest(env.runProfile, env.project)) { | ||
is WslTargetEnvironmentRequest -> request.configuration.distribution!! | ||
else -> null | ||
} | ||
|
||
try { | ||
val mirrordEnv = service.execManager.wrapper("idea").apply { | ||
this.wsl = wsl | ||
configFromEnv = envVars.find { e -> e.name == CONFIG_ENV_NAME }?.VALUE | ||
}.start()?.first?.let { it + mapOf(Pair("MIRRORD_DETECT_DEBUGGER_PORT", "javaagent")) }.orEmpty().toMutableMap() | ||
|
||
// This should allow clean shutdown of the app even if `outgoing` feature is enabled. | ||
mirrordEnv["MIRRORD_IGNORE_DEBUGGER_PORTS"] = getTomcatServerPort() | ||
|
||
savedEnvs[executorId] = envVars.toList() | ||
envVars.addAll(mirrordEnv.map { (k, v) -> EnvironmentVariable(k, v, false) }) | ||
config.setEnvironmentVariables(envVars) | ||
} catch (_: Throwable) { | ||
// Error notifications were already fired. | ||
// We can't abort the execution here, so we let the app run without mirrord. | ||
service.notifier.notifySimple( | ||
"Cannot abort run due to platform limitations, running without mirrord", | ||
NotificationType.WARNING | ||
) | ||
} | ||
} | ||
|
||
super.processStartScheduled(executorId, env) | ||
} | ||
|
||
private fun restoreEnv(executorId: String, config: RunnerSpecificLocalConfigurationBit) { | ||
val saved = savedEnvs.remove(executorId) ?: return | ||
config.setEnvironmentVariables(saved) | ||
} | ||
|
||
override fun processNotStarted(executorId: String, env: ExecutionEnvironment) { | ||
getConfig(env)?.let { | ||
restoreEnv(executorId, it) | ||
} | ||
super.processNotStarted(executorId, env) | ||
} | ||
|
||
override fun processStarted(executorId: String, env: ExecutionEnvironment, handler: ProcessHandler) { | ||
getConfig(env)?.let { | ||
restoreEnv(executorId, it) | ||
} | ||
super.processStarted(executorId, env, handler) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<idea-plugin> | ||
<projectListeners> | ||
<listener class="com.metalbear.mirrord.products.tomcat.TomcatExecutionListener" | ||
topic="com.intellij.execution.ExecutionListener"/> | ||
</projectListeners> | ||
</idea-plugin> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters