1 4 package com.tc.test; 5 6 import com.tc.util.runtime.Os; 7 8 import java.io.BufferedReader ; 9 import java.io.File ; 10 import java.io.IOException ; 11 import java.io.InputStreamReader ; 12 13 24 public class Handle { 25 26 private static String runProcess(String [] args) throws IOException { 27 Process p = Runtime.getRuntime().exec(args); 28 BufferedReader reader = new BufferedReader (new InputStreamReader (p.getInputStream())); 29 String line; 30 StringBuffer buffer = new StringBuffer (); 31 while ((line = reader.readLine()) != null) { 32 buffer.append(line + "\n"); 33 } 34 reader.close(); 35 return buffer.toString(); 36 } 37 38 private static String getPathFor(String path, String cmd) { 39 String [] dirs = path.split(File.pathSeparator); 40 String thePath = ""; 41 for (int i = 0; i < dirs.length; i++) { 42 File test = new File (dirs[i] + File.separator + cmd); 43 if (test.exists()) { 44 thePath = dirs[i]; 45 break; 46 } 47 } 48 return thePath; 49 } 50 51 57 public static String getJavaProcessFileHandles(File file) throws IOException { 58 if (!Os.isWindows()) 59 return "Not a Windows box"; 60 61 String searchPath = TestConfigObject.getInstance().executableSearchPath(); 62 String thePath = getPathFor(searchPath, "handle.exe") + File.separator; 63 64 String [] args = new String [] { thePath + "handle.exe", "-p", "java", file.getAbsolutePath() }; 65 String handleResult = runProcess(args); 66 67 String processResult = ""; 68 if (handleResult.indexOf("No matching handles found") < 0) 70 { 71 args = new String [] { thePath + "pv.exe", "-l", "java.exe" }; 72 processResult = runProcess(args); 73 } 74 75 return handleResult + "\n" + processResult; 76 } 77 78 } 79 | Popular Tags |