Esempio n. 1
0
  /*
   * Find the VM instance running Cougar based on COUGARVMNAME1 and COUGARVMNAME2
   * fields, and attach, setting the JmxConnector to be used by other methods.
   *
   */
  private void setJMXConnectionFactory() {

    String id = null;
    List<VirtualMachineDescriptor> vms = VirtualMachine.list();
    Boolean foundVM = false;

    for (VirtualMachineDescriptor vmd : vms) {

      String vmName = cleanCommandLineArgs(vmd.displayName());

      id = vmd.id();
      JMXConnector jmxConnector = null;
      try {
        jmxConnector = createJMXConnector(id);
      } catch (Exception e) { // Do Nothing move onto next vm
        continue;
      }
      try {
        if (!makeServerConnection(jmxConnector)) {
          continue;
        }
      } catch (Exception e) { // Do Nothing move onto next vm
        continue;
      }

      // No exceptions thrown so we have our cougar vm
      jmxc = jmxConnector;
      foundVM = true;
      break;
    }
    if (!foundVM) {
      throw new RuntimeException("Unable to find cougar VM");
    }
  }
 public void poll() {
   Set<VirtualMachineDescriptor> current =
       new HashSet<>(com.sun.tools.attach.VirtualMachine.list());
   difference(
       current,
       previous,
       machine -> {
         ProfileListener profileListener = listener.onNewMachine(machine);
         if (machine.isAgentLoaded() && profileListener != null) {
           try {
             conductor.pipeFile(machine.getLogFile(), machine, profileListener);
           } catch (IOException e) {
             logger.error(e.getMessage(), e);
           }
         }
       });
   difference(previous, current, listener::onClosedMachine);
   previous = current;
 }
Esempio n. 3
0
    private JMXServiceURL parseArgs(String[] args) {

      String host = null;
      int port = 0;
      String pid = null;
      JMXServiceURL serviceURL = null;

      for (int i = 0; i < args.length; i++) {

        if (args[i].startsWith("-url")) {
          // The '-url' option will let you specify a JMXServiceURL
          // on the command line. This is an URL that begins with
          // service:jmx:<protocol>
          //
          if (host != null) throwSyntaxError("-url and -host are mutually exclusive");
          if (pid != null) throwSyntaxError("-pid and -url are mutually exclusive");
          if (port > 0) throwSyntaxError("-port and -url are mutually exclusive");
          if (++i >= args.length) throwSyntaxError("missing JMXServiceURL after -url");

          try {
            serviceURL = new JMXServiceURL(args[i]);
          } catch (Exception x) {
            throwSyntaxError("bad JMXServiceURL after -url: " + x);
          }
          continue;

        } else if (args[i].startsWith("-host")) {
          // The '-host' and '-port' options will let you specify a host
          // and port, and from that will construct the JMXServiceURL of
          // the default RMI connector, that is:
          // service:jmx:rmi:///jndi/rmi://<host>:<port>/jmxrmi"
          //
          if (serviceURL != null) throwSyntaxError("-url and -host are mutually exclusive");
          if (pid != null) throwSyntaxError("-pid and -host are mutually exclusive");
          if (++i >= args.length) throwSyntaxError("missing host after -host");
          try {
            InetAddress.getByName(args[i]);
            host = args[i];
          } catch (Exception x) {
            throwSyntaxError("bad host after -url: " + x);
          }

        } else if (args[i].startsWith("-port")) {
          // The '-host' and '-port' options will let you specify a host
          // and port, and from that will construct the JMXServiceURL of
          // the default RMI connector, that is:
          // service:jmx:rmi:///jndi/rmi://<host>:<port>/jmxrmi"
          //
          if (serviceURL != null) throwSyntaxError("-url and -port are mutually exclusive");
          if (pid != null) throwSyntaxError("-pid and -port are mutually exclusive");
          if (++i >= args.length) throwSyntaxError("missing port number after -port");
          try {
            port = Integer.parseInt(args[i]);
            if (port <= 0) throwSyntaxError("bad port number after -port: " + "must be positive");
          } catch (Exception x) {
            throwSyntaxError("bad port number after -port: " + x);
          }
        } else if (args[i].startsWith("-pid")) {
          // The '-pid' and option will let you specify the PID of the
          // target VM you want to connect to. It will then use the
          // attach API to dynamically launch the JMX agent in the target
          // VM (if needed) and to find out the JMXServiceURL of the
          // the default JMX Connector in that VM.
          //
          if (serviceURL != null) throwSyntaxError("-url and -pid are mutually exclusive");
          if (port > 0) throwSyntaxError("-port and -pid are mutually exclusive");
          if (++i >= args.length) throwSyntaxError("missing pid after -pid");
          try {
            pid = args[i];
          } catch (Exception x) {
            throwSyntaxError("bad pid after -pid: " + x);
          }
        } else if (args[i].startsWith("-help")) {
          final List<String> vmlist = new ArrayList();
          for (VirtualMachineDescriptor vd : VirtualMachine.list()) {
            vmlist.add(vd.id());
          }
          System.err.println(SYNTAX);
          System.err.println("Running JVMs are: " + vmlist);
          throw new IllegalArgumentException(SYNTAX);
        } else {
          throwSyntaxError("Unknown argument: " + args[i]);
        }
      }

      // A JMXServiceURL was given on the command line, just use this.
      //
      if (serviceURL != null) return serviceURL;

      // A -host -port info was given on the command line.
      // Construct the default RMI JMXServiceURL from this.
      //
      if (port > 0) {
        if (host == null) host = "localhost";

        try {
          return new JMXServiceURL(
              "service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/jmxrmi");
        } catch (Exception x) {
          throwSyntaxError("Bad host or port number: " + x);
        }
      }

      // A PID was given on the command line.
      // Use the attach API to find the target's connector address, and
      // start it if needed.
      //
      if (pid != null) {
        try {
          return getURLForPid(pid);
        } catch (Exception x) {
          throwSyntaxError("cannot attach to target vm " + pid + ": " + x);
        }
      }

      final List<String> vmlist = new ArrayList();
      for (VirtualMachineDescriptor vd : VirtualMachine.list()) {
        vmlist.add(vd.id());
      }
      throwSyntaxError(
          "missing argument: " + "-port | -url | -pid | -list" + "\n\tRunning VMs are: " + vmlist);

      // Unreachable.
      return null;
    }