This repository has been archived by the owner on Mar 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
272 additions
and
140 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,35 @@ | ||
# projector-agent | ||
This subproject allows draw application window and send it with server at the same time. | ||
This subproject allows draw application window and send it with projector-server at the same time. | ||
|
||
## Building | ||
This will give you a jar: | ||
|
||
This will give you a jar in the `projector-agent/build/libs` dir: | ||
```shell script | ||
./gradlew :projector-agent:jar | ||
``` | ||
|
||
This command creates a jar in the `projector-agent/build/libs` dir. | ||
|
||
## How to run my application using this? | ||
### Not modifying your application code | ||
To launch your app, change your run script like this: | ||
```Shell Script | ||
java \ | ||
-javaagent:PATH_TO_AGENT_JAR=PATH_TO_AGENT_JAR \ | ||
YOUR_USUAL_JAVA_ARGUMENTS | ||
-Djdk.attach.allowAttachSelf=true \ | ||
-Dswing.bufferPerWindow=false \ | ||
-Dorg.jetbrains.projector.agent.path=PATH_TO_AGENT_JAR \ | ||
-Dorg.jetbrains.projector.agent.classToLaunch=FQN_OF_YOUR_MAIN_CLASS \ | ||
-classpath YOUR_CLASSPATH:PATH_TO_AGENT_JAR \ | ||
org.jetbrains.projector.agent.AgentLauncher \ | ||
YOUR_MAIN_ARGUMENTS | ||
``` | ||
|
||
### Modifying your application code | ||
You can launch sharing of your application dynamically from your code, just call `AgentLauncher.attachAgent` method. | ||
|
||
### Run with Gradle tasks | ||
There are two gradle tasks for running app with server: | ||
There are two gradle tasks for running server. They are handy when developing. To enable them, you should set some properties in `local.properties` file in the project root. Use [local.properties.example](../local.properties.example) as a reference. | ||
|
||
1. `runWithAgent` — launch your app with Projector Server. Required properties: | ||
* `projectorLauncher.targetClassPath` — classPath of your application; | ||
* `projectorLauncher.classToLaunch` — your application main class. | ||
|
||
1. `runWithAgent` - launch your app with projector server. For enabling this task set properties in `local.properties` file in the project root: | ||
* `projectorLauncher.targetClassPath` - classPath of your application; | ||
* `projectorLauncher.classToLaunch` - your application main class. | ||
|
||
2. `runIdeaWithAgent` - launch IDEA with projector server. For enabling this task set properties in `local.properties` file in the project root: | ||
* `projectorLauncher.ideaPath` - path to IDEA root directory. | ||
2. `runIdeaWithAgent` — launch IntelliJ IDEA with Projector Server. Required property: | ||
* `projectorLauncher.ideaPath` — path to IDEA's root directory. |
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
74 changes: 74 additions & 0 deletions
74
projector-agent/src/main/kotlin/org/jetbrains/projector/agent/AgentLauncher.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,74 @@ | ||
/* | ||
* GNU General Public License version 2 | ||
* | ||
* Copyright (C) 2019-2020 JetBrains s.r.o. | ||
* | ||
* This program is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along | ||
* with this program; if not, write to the Free Software Foundation, Inc., | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
*/ | ||
package org.jetbrains.projector.agent | ||
|
||
import com.sun.tools.attach.VirtualMachine | ||
import java.lang.management.ManagementFactory | ||
import java.lang.reflect.Method | ||
|
||
public object AgentLauncher { | ||
|
||
private fun checkProperty(name: String, expected: String) { | ||
val actual = System.getProperty(name) | ||
if (actual != expected) { | ||
println("System property `$name` is incorrect: expected <$expected>, got <$actual>") | ||
} | ||
} | ||
|
||
private fun getNotNullProperty(name: String): String { | ||
return requireNotNull(System.getProperty(name)) { "Can't launch: no system property `$name` defined..." } | ||
} | ||
|
||
private fun getMainMethodOf(canonicalClassName: String): Method { | ||
val mainClass = Class.forName(canonicalClassName) | ||
return mainClass.getMethod("main", Array<String>::class.java) | ||
} | ||
|
||
@JvmStatic | ||
public fun attachAgent(agentJar: String) { | ||
println("dynamically attaching agent...") | ||
|
||
checkProperty("swing.bufferPerWindow", false.toString()) | ||
checkProperty("jdk.attach.allowAttachSelf", true.toString()) | ||
|
||
val nameOfRunningVM = ManagementFactory.getRuntimeMXBean().name | ||
val pid = nameOfRunningVM.substringBefore('@') | ||
|
||
try { | ||
val vm = VirtualMachine.attach(pid)!! | ||
vm.loadAgent(agentJar, agentJar) | ||
vm.detach() | ||
} | ||
catch (e: Exception) { | ||
throw RuntimeException(e) | ||
} | ||
|
||
println("dynamically attaching agent... - done") | ||
} | ||
|
||
@JvmStatic | ||
public fun main(args: Array<String>) { | ||
val classToLaunch = getNotNullProperty("org.jetbrains.projector.agent.classToLaunch") | ||
val agentJar = getNotNullProperty("org.jetbrains.projector.agent.path") | ||
|
||
attachAgent(agentJar) | ||
|
||
getMainMethodOf(classToLaunch).invoke(null, args) | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.