|
Line 0
Link Here
|
|
|
1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2009 SAP AG. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* SAP AG - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.mat.hprof.acquire; |
| 12 |
|
| 13 |
import java.io.BufferedReader; |
| 14 |
import java.io.IOException; |
| 15 |
import java.io.InputStream; |
| 16 |
import java.io.InputStreamReader; |
| 17 |
import java.util.ArrayList; |
| 18 |
import java.util.List; |
| 19 |
import java.util.StringTokenizer; |
| 20 |
import java.util.logging.Level; |
| 21 |
import java.util.logging.Logger; |
| 22 |
|
| 23 |
import org.eclipse.mat.hprof.Messages; |
| 24 |
import org.eclipse.mat.snapshot.acquire.VmInfo; |
| 25 |
|
| 26 |
public class LocalJavaProcessesUtils |
| 27 |
{ |
| 28 |
static List<VmInfo> getLocalVMsUsingJPS() |
| 29 |
{ |
| 30 |
try |
| 31 |
{ |
| 32 |
Process p = Runtime.getRuntime().exec("jps"); //$NON-NLS-1$ |
| 33 |
StreamCollector error = new StreamCollector(p.getErrorStream()); |
| 34 |
error.start(); |
| 35 |
StreamCollector output = new StreamCollector(p.getInputStream()); |
| 36 |
output.start(); |
| 37 |
|
| 38 |
int exitVal = p.waitFor(); |
| 39 |
|
| 40 |
if (exitVal != 0) return null; |
| 41 |
|
| 42 |
List<VmInfo> vms = new ArrayList<VmInfo>(); |
| 43 |
StringTokenizer tok = new StringTokenizer(output.buf.toString(), "\r\n"); //$NON-NLS-1$ |
| 44 |
while (tok.hasMoreTokens()) |
| 45 |
{ |
| 46 |
String token = tok.nextToken(); |
| 47 |
|
| 48 |
// System.err.println(token); |
| 49 |
VmInfo info = parseJPSLine(token); |
| 50 |
if (info != null) vms.add(info); |
| 51 |
} |
| 52 |
|
| 53 |
return vms; |
| 54 |
} |
| 55 |
catch (Throwable t) |
| 56 |
{ |
| 57 |
// $JL-EXC$ |
| 58 |
return null; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
private static VmInfo parseJPSLine(String line) |
| 63 |
{ |
| 64 |
int firstSpaceIdx = line.indexOf(' '); |
| 65 |
if (firstSpaceIdx == -1) return null; |
| 66 |
int pid = Integer.parseInt(line.substring(0, firstSpaceIdx)); |
| 67 |
String description = line.substring(firstSpaceIdx); |
| 68 |
return new VmInfo(pid, description, false, null, null); |
| 69 |
} |
| 70 |
|
| 71 |
static class StreamCollector extends Thread |
| 72 |
{ |
| 73 |
InputStream is; |
| 74 |
StringBuilder buf; |
| 75 |
|
| 76 |
StreamCollector(InputStream is) |
| 77 |
{ |
| 78 |
this.is = is; |
| 79 |
this.buf = new StringBuilder(); |
| 80 |
} |
| 81 |
|
| 82 |
public void run() |
| 83 |
{ |
| 84 |
try |
| 85 |
{ |
| 86 |
InputStreamReader isr = new InputStreamReader(is); |
| 87 |
BufferedReader br = new BufferedReader(isr); |
| 88 |
String line = null; |
| 89 |
while ((line = br.readLine()) != null) |
| 90 |
buf.append(line).append("\r\n"); //$NON-NLS-1$ |
| 91 |
} |
| 92 |
catch (IOException ioe) |
| 93 |
{ |
| 94 |
Logger.getLogger(getClass().getName()).log(Level.SEVERE, Messages.LocalJavaProcessesUtils_ErrorGettingProcesses, ioe); |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
} |