KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > test > server > util > VmStat


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tc.test.server.util;
6
7 import com.tc.util.runtime.Os;
8
9 import java.io.File JavaDoc;
10 import java.io.FileOutputStream JavaDoc;
11 import java.io.IOException JavaDoc;
12
13 public class VmStat {
14
15   private static Process JavaDoc process;
16   private static File JavaDoc scriptFile;
17   private static final String JavaDoc scriptName = "capture-vmstat.sh";
18   private static final String JavaDoc scriptContents = "#!/bin/sh\nexec vmstat 1 > vmstat.txt\n";
19
20   public static synchronized void start(File JavaDoc workingDir) throws IOException JavaDoc {
21     if (process != null) {
22       stop();
23       throw new IllegalStateException JavaDoc("VmStat is already running. Stopping VmStat...");
24     }
25     // win* and mac not supported
26
if (Os.isWindows() || Os.isMac()) return;
27
28     String JavaDoc[] commandLine = new String JavaDoc[2];
29     commandLine[0] = "/bin/bash";
30     commandLine[1] = createScriptFile(workingDir).toString();
31
32     Runtime JavaDoc runtime = Runtime.getRuntime();
33     process = runtime.exec(commandLine, null, workingDir);
34
35     String JavaDoc msg = "\n";
36     msg += "*****************************\n";
37     msg += "* Running vmstat in [" + workingDir + "]\n";
38     msg += "*****************************\n";
39     System.out.println(msg);
40   }
41
42   public static synchronized void stop() {
43     if (process != null) {
44       process.destroy();
45       process = null;
46       deleteScriptFile();
47     }
48   }
49
50   private static synchronized File JavaDoc createScriptFile(File JavaDoc baseDir) throws IOException JavaDoc {
51     if (scriptFile != null) return scriptFile;
52     File JavaDoc script = new File JavaDoc(baseDir + File.separator + scriptName);
53     script.createNewFile();
54     FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(script);
55     out.write(scriptContents.getBytes());
56     out.flush();
57     out.close();
58     return scriptFile = script;
59   }
60
61   private static synchronized void deleteScriptFile() {
62     if (scriptFile != null) {
63       scriptFile.delete();
64       scriptFile = null;
65     }
66   }
67 }
68
Popular Tags