if -jar, try to get Main-Class from jar manifest

This commit is contained in:
Doug MacEachern 2005-12-16 21:37:26 +00:00
parent 430962a988
commit 54105b5eb3
1 changed files with 56 additions and 16 deletions

View File

@ -8,11 +8,16 @@ import net.hyperic.sigar.ProcMem;
import net.hyperic.sigar.ProcTime; import net.hyperic.sigar.ProcTime;
import net.hyperic.sigar.ProcState; import net.hyperic.sigar.ProcState;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
/** /**
* Show process status. * Show process status.
@ -89,6 +94,51 @@ public class Ps extends SigarCommandBase {
return true; return true;
} }
//try to guess classname for java programs
public static String getJavaMainClass(SigarProxy sigar, long pid)
throws SigarException {
String[] args = sigar.getProcArgs(pid);
for (int i=1; i<args.length; i++) {
String arg = args[i];
if (isClassName(arg)) {
//example: "java:weblogic.Server"
return arg;
}
else if (arg.equals("-jar")) {
File file = new File(args[i+1]);
if (!file.isAbsolute()) {
try {
String cwd =
sigar.getProcExe(pid).getCwd();
file = new File(cwd + File.separator + file);
} catch (SigarException e) {}
}
if (file.exists()) {
JarFile jar = null;
try {
jar = new JarFile(file);
return
jar.getManifest().
getMainAttributes().
getValue(Attributes.Name.MAIN_CLASS);
} catch (IOException e) {
} finally {
if (jar != null) {
try { jar.close(); }
catch (IOException e){}
}
}
}
return file.toString();
}
}
return null;
}
public static List getInfo(SigarProxy sigar, long pid) public static List getInfo(SigarProxy sigar, long pid)
throws SigarException { throws SigarException {
@ -132,24 +182,14 @@ public class Ps extends SigarCommandBase {
} }
String name = state.getName(); String name = state.getName();
if (name.equals("java")) { if (name.equals("java") || name.equals("javaw")) {
//try to guess classname for java programs String className = null;
try { try {
String[] args = sigar.getProcArgs(pid); className = getJavaMainClass(sigar, pid);
for (int i=1; i<args.length; i++) {
String arg = args[i];
if (isClassName(arg)) {
//example: "java:weblogic.Server"
name += ":" + arg;
break;
}
else if (arg.equals("-jar")) {
name += ":" + args[i+1];
break;
}
}
} catch (SigarException e) {} } catch (SigarException e) {}
if (className != null) {
name += ":" + className;
}
} }
else { else {
try { try {