Package com.sun.tools.attach

Examples of com.sun.tools.attach.VirtualMachine


  public static void loadAgent(String jarFilePath, String params) {
    log.info("dynamically loading javaagent for {}", jarFilePath);

    try {
      VirtualMachine vm;

      String pid = discoverPid();

      if (AttachProvider.providers().isEmpty()) {
        vm = getVirtualMachineImplementationFromEmbeddedOnes(pid);
      }
      else {
        vm = VirtualMachine.attach(pid);
      }

      vm.loadAgent(jarFilePath, params);
      vm.detach();
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
View Full Code Here


        "display name: " + virtualMachineDescriptor.displayName() + ", " +
        "main class: " + extractMainClassNameFromDisplayName(virtualMachineDescriptor.displayName()) + ", " +
        "command line: " + virtualMachineDescriptor.displayName());

      // to get system properties (or to load java agent) we need to attach to VM
      VirtualMachine virtualMachine = VirtualMachine.attach(virtualMachineDescriptor);
      if (virtualMachine != null) {
        Properties targetSystemProperties = virtualMachine.getSystemProperties();
        System.out.print(", ");
        System.out.print("command (from sysprops): " + targetSystemProperties.getProperty("sun.java.command"));
      }
      virtualMachine.detach();

      System.out.println();
    }
  }
View Full Code Here

        }

    }

    public static void loadAgent(String pid, String args) throws IOException {
        VirtualMachine vm = null;
        try {
            vm = VirtualMachine.attach(pid);
        } catch (AttachNotSupportedException x) {
            IOException ioe = new IOException(x.getMessage());
            ioe.initCause(x);
            throw ioe;
        }

        try {
            String agent = ApmAgentLauncher.class.getProtectionDomain().getCodeSource().getLocation().getPath();
            System.err.println("Trying to load agent " + agent);
            vm.loadAgent(agent, args);
            System.out.println("Agent successfully loaded");
        } catch (AgentLoadException x) {
            IOException ioe = new IOException(x.getMessage());
            ioe.initCause(x);
            throw ioe;
        } catch (AgentInitializationException x) {
            IOException ioe = new IOException(x.getMessage());
            ioe.initCause(x);
            throw ioe;
        } finally {
            if (vm != null) {
                vm.detach();
            }
        }
    }
View Full Code Here

 
  public JMXConnector createJMXConnector(String id) throws IOException,AgentLoadException,
                  AgentInitializationException, AttachNotSupportedException {

    // attach to the target application
    VirtualMachine vm = VirtualMachine.attach(id);

    // get the connector address
    String connectorAddress = vm.getAgentProperties().getProperty(CONNECTOR_ADDRESS);

    // no connector address, so we start the JMX agent
    if (connectorAddress == null) {
      String agent = vm.getSystemProperties()
          .getProperty("java.home")
          + File.separator
          + "lib"
          + File.separator
          + "management-agent.jar";
      vm.loadAgent(agent);

      // agent is started, get the connector address
      connectorAddress = vm.getAgentProperties().getProperty(CONNECTOR_ADDRESS);
    }

    // establish connection to connector server
    JMXServiceURL url = new JMXServiceURL(connectorAddress);
    return JMXConnectorFactory.connect(url);
View Full Code Here

        String nameOfRunningVM = ManagementFactory.getRuntimeMXBean().getName();
        int p = nameOfRunningVM.indexOf('@');
        String pid = nameOfRunningVM.substring(0, p);
       
        try {
            VirtualMachine vm = VirtualMachine.attach(pid);
            CodeSource codeSource = ActiveJpaAgent.class.getProtectionDomain().getCodeSource();
            vm.loadAgent(codeSource.getLocation().toURI().getPath(), "");
            vm.detach();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
View Full Code Here

        }
    }

    @Nonnull
    protected MBeanServerConnection connectToMbeanServer(@Nonnull String pid) throws IOException {
        VirtualMachine vm;
        try {
            Integer.parseInt(pid);
        } catch (Exception e) {
            logger.warn("Exception parsing PID '{}'", pid, e);
        }
        try {
            vm = VirtualMachine.attach(pid);
        } catch (Exception e) {
            throw new IllegalStateException("Exception attaching VM with PID '" + pid + "'", e);
        }


        logger.trace("VM Agent Properties");
        for (String key : new TreeSet<String>(vm.getAgentProperties().stringPropertyNames())) {
            logger.trace("\t {}: {}", key, vm.getAgentProperties().get(key));
        }
        logger.trace("VM System Properties");
        for (String key : new TreeSet<String>(vm.getSystemProperties().stringPropertyNames())) {
            logger.trace("\t {}: {}", key, vm.getSystemProperties().get(key));
        }


        String connectorAddress =
                vm.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress");
        if (connectorAddress == null) {
            String agent = vm.getSystemProperties().getProperty(
                    "java.home") + File.separator + "lib" + File.separator +
                    "management-agent.jar";
            try {
                vm.loadAgent(agent);
            } catch (Exception e) {
                throw new IllegalStateException("Exception loading agent " + agent);
            }
            connectorAddress = vm.getAgentProperties().getProperty(
                    "com.sun.management.jmxremote.localConnectorAddress");
        }

        if (connectorAddress == null) {
            throw new IllegalStateException("Could not attach to pid: " + pid + ". No connector available");
View Full Code Here

        }
    }

    @Nonnull
    protected MBeanServerConnection connectToMbeanServer(@Nonnull String pid) throws IOException {
        VirtualMachine vm;
        try {
            Integer.parseInt(pid);
        } catch (Exception e) {
            logger.warn("Exception parsing PID '{}'", pid, e);
        }
        try {
            vm = VirtualMachine.attach(pid);
        } catch (Exception e) {
            throw new IllegalStateException("Exception attaching VM with PID '" + pid + "'", e);
        }


        logger.trace("VM Agent Properties");
        for (String key : new TreeSet<String>(vm.getAgentProperties().stringPropertyNames())) {
            logger.trace("\t {}: {}", key, vm.getAgentProperties().get(key));
        }
        logger.trace("VM System Properties");
        for (String key : new TreeSet<String>(vm.getSystemProperties().stringPropertyNames())) {
            logger.trace("\t {}: {}", key, vm.getSystemProperties().get(key));
        }


        String connectorAddress =
                vm.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress");
        if (connectorAddress == null) {
            String agent = vm.getSystemProperties().getProperty(
                    "java.home") + File.separator + "lib" + File.separator +
                    "management-agent.jar";
            try {
                vm.loadAgent(agent);
            } catch (Exception e) {
                throw new IllegalStateException("Exception loading agent " + agent);
            }
            connectorAddress = vm.getAgentProperties().getProperty(
                    "com.sun.management.jmxremote.localConnectorAddress");
        }

        if (connectorAddress == null) {
            throw new IllegalStateException("Could not attach to pid: " + pid + ". No connector available");
View Full Code Here

            if (pid < 0) {
                logger.log(Level.WARNING, INVALID_PID);
                return false;
            }

            VirtualMachine vm = VirtualMachine.attach(String.valueOf(pid));
            String ir = System.getProperty(INSTALL_ROOT_PROPERTY);
            File dir = new File(ir, "lib" + File.separator + "monitor");

            if (!dir.isDirectory()) {
                logger.log(Level.WARNING, MISSING_AGENT_JAR_DIR, dir);
                return false;
            }

            File agentJar = new File(dir, "flashlight-agent.jar");

            if (!agentJar.isFile()) {
                logger.log(Level.WARNING, MISSING_AGENT_JAR, dir);
                return false;
            }

            vm.loadAgent(SmartFile.sanitize(agentJar.getPath()), options);
            isAttached = true;
        }
        catch (Throwable t) {
            logger.log(Level.WARNING, ATTACH_AGENT_EXCEPTION, t.getMessage());
            isAttached = false;
View Full Code Here

    }

    private static void loadAgent(String jarpaths, String pid) throws Exception {
        final String[]       ary      = jarpaths.split(File.pathSeparator);
        final String         agentJar = Classpath.findJar(ary, "scalive");
        final VirtualMachine vm       = VirtualMachine.attach(pid);
        final int            port     = Client.getFreePort();

        vm.loadAgent(agentJar, jarpaths + " " + port);
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override public void run() {
                try {
                    vm.detach();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
View Full Code Here

        if(jvmVersion.contains("1.5")) {
            logger.info("The JMX Attach APIs require Java 6 or above. You are running Java {}", jvmVersion);
            return null;
        }

        VirtualMachine vm = VirtualMachine.attach(id);
        String connectorAddr = vm.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress");
        if (connectorAddr == null) {
            String agent = vm.getSystemProperties().getProperty("java.home")+File.separator+"lib"+File.separator+
                           "management-agent.jar";
            vm.loadAgent(agent);
            connectorAddr = vm.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress");
        }
        MBeanServerConnection mbs = null;
        if(connectorAddr!=null) {
            JMXServiceURL serviceURL = new JMXServiceURL(connectorAddr);
            JMXConnector connector = JMXConnectorFactory.connect(serviceURL);
View Full Code Here

TOP

Related Classes of com.sun.tools.attach.VirtualMachine

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.