diff options
author | Diego <diegolparra@gmail.com> | 2016-06-22 15:32:23 -0300 |
---|---|---|
committer | Diego <diegolparra@gmail.com> | 2016-06-22 15:32:23 -0300 |
commit | 8516613ad2ffd5b29f2e611a0a9e9006c5613ea4 (patch) | |
tree | 30b4eabd7f23dc9a9e0d321351e41440c902db61 /kamon-autoweave/src/main | |
parent | 5776b3c27bcb9223ae77b596b32b69d4006e0698 (diff) | |
download | Kamon-8516613ad2ffd5b29f2e611a0a9e9006c5613ea4.tar.gz Kamon-8516613ad2ffd5b29f2e611a0a9e9006c5613ea4.tar.bz2 Kamon-8516613ad2ffd5b29f2e611a0a9e9006c5613ea4.zip |
+ kamon-autoweave: add support Attach on IBM J9 and closes #354
Diffstat (limited to 'kamon-autoweave/src/main')
-rw-r--r-- | kamon-autoweave/src/main/scala/kamon/autoweave/loader/AgentLoader.scala | 5 | ||||
-rw-r--r-- | kamon-autoweave/src/main/scala/kamon/autoweave/loader/AttachmentProviders.scala | 41 |
2 files changed, 33 insertions, 13 deletions
diff --git a/kamon-autoweave/src/main/scala/kamon/autoweave/loader/AgentLoader.scala b/kamon-autoweave/src/main/scala/kamon/autoweave/loader/AgentLoader.scala index 2f26a39d..5f865cb3 100644 --- a/kamon-autoweave/src/main/scala/kamon/autoweave/loader/AgentLoader.scala +++ b/kamon-autoweave/src/main/scala/kamon/autoweave/loader/AgentLoader.scala @@ -99,10 +99,9 @@ object AgentLoader { AttachmentProviders.resolve() match { case Some(virtualMachine) ⇒ val virtualMachineInstance = virtualMachine.getDeclaredMethod("attach", classOf[String]).invoke(null, getPidFromRuntimeMBean) - virtualMachine.getDeclaredMethod("loadAgent", classOf[String], classOf[String]) - .invoke(virtualMachineInstance, generateAgentJar(agent, resources).getAbsolutePath, "") + virtualMachine.getDeclaredMethod("loadAgent", classOf[String], classOf[String]).invoke(virtualMachineInstance, generateAgentJar(agent, resources).getAbsolutePath, "") virtualMachine.getDeclaredMethod("detach").invoke(virtualMachineInstance) - case None ⇒ throw new RuntimeException(s"Error trying to use Attach API") with NoStackTrace + case None ⇒ throw new IllegalStateException("Cannot read the virtual machine type...") with NoStackTrace } } diff --git a/kamon-autoweave/src/main/scala/kamon/autoweave/loader/AttachmentProviders.scala b/kamon-autoweave/src/main/scala/kamon/autoweave/loader/AttachmentProviders.scala index ebb112e4..fff23735 100644 --- a/kamon-autoweave/src/main/scala/kamon/autoweave/loader/AttachmentProviders.scala +++ b/kamon-autoweave/src/main/scala/kamon/autoweave/loader/AttachmentProviders.scala @@ -17,13 +17,20 @@ package kamon.autoweave.loader import java.io.File import java.net.{ URL, URLClassLoader } -import java.security.{ PrivilegedAction, AccessController } +import java.security.{ AccessController, PrivilegedAction } -object AttachmentProviders extends { +import scala.util.control.NonFatal +import scala.util.{ Failure, Success, Try } - val VirtualMachineTyeName = "com.sun.tools.attach.VirtualMachine" +object AttachmentProviders { sealed trait AttachmentProvider { + def resolve(): Option[Class[_]] + } + + trait DefaultAttachmentProvider extends AttachmentProvider { + val VirtualMachineTyeName = "com.sun.tools.attach.VirtualMachine" + def toolsJarPath: String /** @@ -34,17 +41,31 @@ object AttachmentProviders extends { */ def resolve(): Option[Class[_]] = { val toolsJar = new File(System.getProperty("java.home").replace('\\', '/') + "/../" + toolsJarPath) - if (toolsJar.isFile && toolsJar.canRead) - Some(AccessController.doPrivileged(new ClassLoaderCreationAction(toolsJar)).loadClass(VirtualMachineTyeName)) - else None + if (toolsJar.isFile && toolsJar.canRead) { + Try(AccessController.doPrivileged(new ClassLoaderCreationAction(toolsJar)).loadClass(VirtualMachineTyeName)) match { + case Success(vm) ⇒ Some(vm) + case Failure(NonFatal(_)) ⇒ None + } + } else None } } - case object JVM extends AttachmentProvider { val toolsJarPath = "../lib/tools.jar" } - case object JDK extends AttachmentProvider { val toolsJarPath = "lib/tools.jar" } - case object MAC extends AttachmentProvider { val toolsJarPath = "../Classes/classes.jar" } + case object JVM extends DefaultAttachmentProvider { val toolsJarPath = "../lib/tools.jar" } + case object JDK extends DefaultAttachmentProvider { val toolsJarPath = "lib/tools.jar" } + case object MAC extends DefaultAttachmentProvider { val toolsJarPath = "../Classes/classes.jar" } + case object IBM extends AttachmentProvider { + + val IBMVirtualMachineTyeName = "com.ibm.tools.attach.VirtualMachine" + + override def resolve(): Option[Class[_]] = { + Try(ClassLoader.getSystemClassLoader.loadClass(IBMVirtualMachineTyeName)) match { + case Success(vm) ⇒ Some(vm) + case Failure(NonFatal(_)) ⇒ None + } + } + } - private val providers = Seq(JVM, JDK, MAC) + private val providers = Seq(JVM, JDK, MAC, IBM) private final class ClassLoaderCreationAction(toolsJar: File) extends PrivilegedAction[ClassLoader] { override def run(): ClassLoader = new URLClassLoader(Array[URL](toolsJar.toURI.toURL), null) |