-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#47: Initial POC of connecting to another VM
- Loading branch information
1 parent
aca3df4
commit 06b8723
Showing
7 changed files
with
257 additions
and
44 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 |
---|---|---|
|
@@ -92,4 +92,6 @@ gradle-app.setting | |
# VSCode Project | ||
.classpath | ||
.project | ||
.settings | ||
.settings | ||
|
||
*.hprof |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* This file is part of OMJ. | ||
* | ||
* OMJ is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* OMJ 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 OMJ. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package com.octogonapus.omj.ui | ||
|
||
import com.octogonapus.omj.util.JarExtractor | ||
import com.sun.tools.attach.AttachNotSupportedException | ||
import com.sun.tools.attach.VirtualMachine | ||
import java.io.IOException | ||
import java.lang.management.ManagementFactory | ||
|
||
object AgentLoader { | ||
|
||
fun loadAgent() { | ||
val name = ManagementFactory.getRuntimeMXBean().name | ||
val pid = name.substring(0, name.indexOf('@')) | ||
try { | ||
val virtualMachine = VirtualMachine.attach(pid) | ||
val agentJar = JarExtractor.extractJar("agent-all") | ||
virtualMachine.loadAgent(agentJar.absolutePath, null) | ||
virtualMachine.detach() | ||
} catch (e: AttachNotSupportedException) { | ||
e.printStackTrace() | ||
} catch (e: IOException) { | ||
e.printStackTrace() | ||
} | ||
} | ||
} | ||
|
||
fun main() { | ||
AgentLoader.loadAgent() | ||
} |
71 changes: 71 additions & 0 deletions
71
util/src/main/java/com/octogonapus/omj/util/JarExtractor.java
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,71 @@ | ||
/* | ||
* This file is part of OMJ. | ||
* | ||
* OMJ is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* OMJ 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 OMJ. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package com.octogonapus.omj.util; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.file.Files; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class JarExtractor { | ||
|
||
private static final Map<String, File> resourceMap = new HashMap<>(); | ||
|
||
/** | ||
* Extracts the agent-lib jar from our jar and into a file. | ||
* | ||
* @return The file the agent-lib jar is extracted into. | ||
* @throws IOException Any IO problems along the way. All fatal. | ||
*/ | ||
public static File extractJar(final String resourceName) throws IOException { | ||
try (final var agentLib = ClassLoader.getSystemResourceAsStream(resourceName)) { | ||
if (agentLib == null) { | ||
throw new IOException("Could not locate resource: " + resourceName); | ||
} | ||
|
||
// Extract the Jar and save it so that subsequent calls do not try to extract the Jar again. | ||
if (!resourceMap.containsKey(resourceName)) { | ||
final var file = | ||
Files.createTempFile(Util.getAgentLibJarDir(), resourceName + '_', ".jar").toFile(); | ||
file.deleteOnExit(); | ||
copyStreamToFile(file, agentLib); | ||
resourceMap.put(resourceName, file); | ||
} | ||
|
||
return resourceMap.get(resourceName); | ||
} | ||
} | ||
|
||
private static void copyStreamToFile(File file, InputStream lib) throws IOException { | ||
try (final var os = Files.newOutputStream(file.toPath())) { | ||
final var buffer = new byte[0xFFFF]; | ||
int readBytes; | ||
while (true) { | ||
readBytes = lib.read(buffer); | ||
|
||
// -1 means end of stream | ||
if (readBytes == -1) { | ||
break; | ||
} | ||
|
||
os.write(buffer, 0, readBytes); | ||
} | ||
} | ||
} | ||
} |