Merge "Improve logging of persister and netconf client."
[controller.git] / opendaylight / commons / controller-maven-plugin / src / main / java / org / opendaylight / controller / maven / plugin / util / JpsProcessMonitor.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.maven.plugin.util;
10
11 import java.io.BufferedReader;
12 import java.io.File;
13 import java.io.InputStreamReader;
14 import java.util.ArrayList;
15 import java.util.List;
16
17 /**
18  * Uses JPS tool to monitor java local processes
19  */
20 public class JpsProcessMonitor extends ProcessMonitor {
21     private final String jpsTool;
22
23     public JpsProcessMonitor() {
24         String jh = System.getProperty("java.home");
25         File jps = new File(jh + SEP + "bin" + SEP + "jps");
26         if (!jps.exists()) {
27             // try one dir above
28             jps = new File(jh + SEP + ".." + SEP + "bin" + SEP + "jps");
29             if (!jps.exists()) {
30                 throw new IllegalStateException("jps tool cannot be located.");
31             }
32         }
33         jpsTool = jps.getAbsolutePath();
34     }
35
36     @Override
37     public List<JavaProcess> getProcesses() {
38         if (jpsTool == null) return super.getProcesses();
39         List<JavaProcess> jvms = new ArrayList<JavaProcess>();
40         try {
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()));
47             String line = null;
48             while ( (line = br.readLine()) != null) {
49                 JavaProcess j = parseLine(line);
50                 if (j != null) jvms.add(j);
51             }
52         } catch (Exception e) {
53             e.printStackTrace();
54         }
55         return jvms;
56     }
57
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);
62             return null;
63         }
64         int idx = 0;
65         int pid = Integer.parseInt(tokens[idx++]);
66         String mainClass = "";
67         if (!tokens[idx].startsWith("-")) {
68             mainClass = tokens[idx++];
69         }
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]);
74             }
75         }
76         return proc;
77     }
78
79 }