move getJavaMainClass to ProcUtil class

This commit is contained in:
Doug MacEachern 2006-01-22 21:43:19 +00:00
parent 6bfbfad766
commit 021fb5183e
2 changed files with 70 additions and 63 deletions

View File

@ -0,0 +1,68 @@
package net.hyperic.sigar;
import java.io.File;
import java.io.IOException;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
public class ProcUtil {
private static boolean isClassName(String name) {
int len = name.length();
for (int i=0; i<len; i++) {
char c = name.charAt(i);
if (!((c == '.') || Character.isLetter(c))) {
return false;
}
}
return true;
}
/**
* Try to determina 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;
}
}

View File

@ -7,17 +7,13 @@ import net.hyperic.sigar.ProcCredName;
import net.hyperic.sigar.ProcMem;
import net.hyperic.sigar.ProcTime;
import net.hyperic.sigar.ProcState;
import java.io.File;
import java.io.IOException;
import net.hyperic.sigar.ProcUtil;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
/**
* Show process status.
@ -82,63 +78,6 @@ public class Ps extends SigarCommandBase {
return buf.toString();
}
private static boolean isClassName(String name) {
int len = name.length();
for (int i=0; i<len; i++) {
char c = name.charAt(i);
if (!((c == '.') || Character.isLetter(c))) {
return false;
}
}
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)
throws SigarException {
@ -185,7 +124,7 @@ public class Ps extends SigarCommandBase {
if (name.equals("java") || name.equals("javaw")) {
String className = null;
try {
className = getJavaMainClass(sigar, pid);
className = ProcUtil.getJavaMainClass(sigar, pid);
} catch (SigarException e) {}
if (className != null) {
name += ":" + className;