01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.test;
05:
06: import com.tc.util.runtime.Os;
07:
08: import java.io.BufferedReader;
09: import java.io.File;
10: import java.io.IOException;
11: import java.io.InputStreamReader;
12:
13: /*
14: * To be used for Windows only.
15: *
16: * Get java processes that are holding locks on a given file.
17: *
18: * This class depends on 2 free programs
19: * - Handle from http://www.sysinternals.com and
20: * - PrcView from http://www.prcview.com
21: *
22: * Author: Hung Huynh
23: */
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(
29: new InputStreamReader(p.getInputStream()));
30: String line;
31: StringBuffer buffer = new StringBuffer();
32: while ((line = reader.readLine()) != null) {
33: buffer.append(line + "\n");
34: }
35: reader.close();
36: return buffer.toString();
37: }
38:
39: /**
40: * @param file observed file
41: * @param path global path that contains path to handle.exe and pv.exe
42: * @return java processes that holding lock on the given file.
43: * @throws IOException
44: */
45: public static String getJavaProcessFileHandles(File file)
46: throws IOException {
47: if (!Os.isWindows())
48: return "Not a Windows box";
49:
50: String nativeLibPath = TestConfigObject.getInstance()
51: .executableSearchPath();
52:
53: String[] args = new String[] {
54: nativeLibPath + File.separator + "handle.exe", "-p",
55: "java", file.getAbsolutePath() };
56: String handleResult = runProcess(args);
57:
58: String processResult = "";
59: // if "No matching handles found" from handle.exe, no need to display java processes
60: if (handleResult.indexOf("No matching handles found") < 0) {
61: args = new String[] {
62: nativeLibPath + File.separator + "pv.exe", "-l",
63: "java.exe" };
64: processResult = runProcess(args);
65: }
66:
67: return handleResult + "\n" + processResult;
68: }
69:
70: }
|