2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.maven.plugin.util;
11 import java.io.BufferedReader;
13 import java.io.InputStreamReader;
14 import java.util.ArrayList;
15 import java.util.List;
18 * Uses JPS tool to monitor java local processes
20 public class JpsProcessMonitor extends ProcessMonitor {
21 private final String jpsTool;
23 public JpsProcessMonitor() {
24 String jh = System.getProperty("java.home");
25 File jps = new File(jh + SEP + "bin" + SEP + "jps");
28 jps = new File(jh + SEP + ".." + SEP + "bin" + SEP + "jps");
30 throw new IllegalStateException("jps tool cannot be located.");
33 jpsTool = jps.getAbsolutePath();
37 public List<JavaProcess> getProcesses() {
38 if (jpsTool == null) return super.getProcesses();
39 List<JavaProcess> jvms = new ArrayList<JavaProcess>();
41 ProcessBuilder pb = new ProcessBuilder();
42 pb.command(new String[] { jpsTool, "-mlvV"} );
43 pb.redirectErrorStream(true);
44 Process process = pb.start();
45 BufferedReader br = new BufferedReader(
46 new InputStreamReader(process.getInputStream()));
48 while ( (line = br.readLine()) != null) {
49 JavaProcess j = parseLine(line);
50 if (j != null) jvms.add(j);
52 } catch (Exception e) {
58 public static JavaProcess parseLine(String line) {
59 String[] tokens = line.split("\\s");
60 if (tokens.length < 2) {
61 System.out.println("Unable to parse line: " + line);
65 int pid = Integer.parseInt(tokens[idx++]);
66 String mainClass = "";
67 if (!tokens[idx].startsWith("-")) {
68 mainClass = tokens[idx++];
70 JavaProcess proc = new JavaProcess(pid, mainClass);
71 for (int i=idx; i<tokens.length; i++) {
72 if (tokens[i].startsWith("-D")) {
73 proc.setSystemProperty(tokens[i]);