/*
 * *
 *  Copyright 2015 Yusuke Yamamoto
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  Distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package samurai.remotedump;

import sun.jvmstat.monitor.*;

import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

public class ProcessUtil {
    public static List<VM> getVMs(String host) throws URISyntaxException, MonitorException {
        List<VM> vms = new ArrayList<>();
        HostIdentifier hi = new HostIdentifier(host);
        MonitoredHost mh = MonitoredHost.getMonitoredHost(hi);
        Set<Integer> jvms = mh.activeVms();
        for (Integer pidInteger : jvms) {
            try {
                int pid = pidInteger;
                MonitoredVm mv = mh.getMonitoredVm(new VmIdentifier("//" + pid + "?mode=r"), 0);
                StringMonitor sm = (StringMonitor) mv.findByName("sun.rt.javaCommand");
                String fullCommandLine = "";
                if (sm != null) {
                    fullCommandLine = sm.stringValue();
                }
                vms.add(new VM(pid, fullCommandLine));
            }catch(MonitorException me){
                // target process is no longer available
            }
        }
        return Collections.unmodifiableList(vms);
    }

    public static void main(String... args) throws URISyntaxException, MonitorException {
        List<VM> vms = getVMs("localhost");
        vms.forEach(e -> System.out.printf("%s %s%n", e.getPid(), e.getFqcn()));
    }

}
@@ -924,32 +924,91 @@
 itorException %7B%0A
+        int localPid = (int)ProcessHandle.current().pid();%0A
         List%3CVM%3E
@@ -1292,16 +1292,114 @@
 nteger;%0A
+                // exclude local process from the list%0A                if (pid != localPid) %7B%0A    
         
@@ -1499,24 +1499,28 @@
             
+ 
+   
 StringMonito
@@ -1591,24 +1591,28 @@
             
+    
 String fullC
@@ -1640,24 +1640,28 @@
             
+  
+  
 if (sm != nu
@@ -1662,24 +1662,28 @@
  != null) %7B%0A
+    
             
@@ -1734,34 +1734,42 @@
                 
+   
+ 
 %7D%0A
+    
                 
@@ -1803,24 +1803,28 @@
 mandLine));%0A
+    
             
@@ -1828,13 +1828,29 @@
    %7D
+%0A            %7D 
 catch
+ 
 (Mon
@@ -1866,16 +1866,17 @@
 tion me)
+ 
 %7B%0A      

exclude local process from the list