EMMA Coverage Report (generated Tue May 16 15:34:38 CDT 2006)
[all classes][com.mysql.management.util]

COVERAGE SUMMARY FOR SOURCE FILE [ProcessUtil.java]

nameclass, %method, %block, %line, %
ProcessUtil.java100% (3/3)100% (16/16)86%  (260/303)92%  (56/61)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ProcessUtil100% (1/1)100% (12/12)85%  (239/282)91%  (50/55)
launchShell (String, String [], int): Shell 100% (1/1)49%  (40/81)67%  (8/12)
isRunning (): boolean 100% (1/1)94%  (29/31)80%  (4/5)
ProcessUtil (String, PrintStream, PrintStream, File): void 100% (1/1)100% (10/10)100% (2/2)
ProcessUtil (String, PrintStream, PrintStream, File, Utils): void 100% (1/1)100% (36/36)100% (10/10)
forceKill (): void 100% (1/1)100% (10/10)100% (3/3)
getWindowsKillFile (): File 100% (1/1)100% (24/24)100% (5/5)
isRunningArgs (): String [] 100% (1/1)100% (17/17)100% (1/1)
kill (): void 100% (1/1)100% (4/4)100% (2/2)
kill (boolean): void 100% (1/1)100% (30/30)100% (5/5)
killArgs (boolean): String [] 100% (1/1)100% (26/26)100% (6/6)
killNoThrow (): void 100% (1/1)100% (10/10)100% (3/3)
pid (): String 100% (1/1)100% (3/3)100% (1/1)
     
class ProcessUtil$1100% (1/1)100% (2/2)100% (11/11)100% (3/3)
ProcessUtil$1 (ProcessUtil): void 100% (1/1)100% (6/6)100% (1/1)
inner (): void 100% (1/1)100% (5/5)100% (2/2)
     
class ProcessUtil$2100% (1/1)100% (2/2)100% (10/10)100% (3/3)
ProcessUtil$2 (ProcessUtil): void 100% (1/1)100% (6/6)100% (1/1)
inner (): void 100% (1/1)100% (4/4)100% (2/2)

1/*
2 Copyright (C) 2004 MySQL AB
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License version 2 as 
6 published by the Free Software Foundation.
7 
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 GNU General Public License for more details.
12 
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16 
17 */
18package com.mysql.management.util;
19 
20import java.io.File;
21import java.io.PrintStream;
22import java.util.ArrayList;
23import java.util.List;
24 
25public final class ProcessUtil {
26 
27    private String pid;
28 
29    private PrintStream out;
30 
31    private PrintStream err;
32 
33    private String killCommand;
34 
35    private Utils utils;
36 
37    private File installDir;
38 
39    public ProcessUtil(String pid, PrintStream out, PrintStream err,
40            File installDir) {
41        this(pid, out, err, installDir, new Utils());
42    }
43 
44    public ProcessUtil(String pid, PrintStream out, PrintStream err,
45            File installDir, Utils utils) {
46        this.installDir = installDir;
47        this.pid = (pid != null) ? pid.trim() : "-1";
48        this.out = out;
49        this.err = err;
50        this.utils = utils;
51        if (utils.files().isWindows()) {
52            this.killCommand = getWindowsKillFile().getPath();
53        } else {
54            this.killCommand = "kill";
55        }
56    }
57 
58    /* called from constructor */
59    final File getWindowsKillFile() {
60        File parent = new File(installDir, "c-mxj-utils");
61        File kill = new File(parent, "kill.exe");
62        if (!kill.exists()) {
63            utils.streams().createFileFromResource("kill.exe", kill);
64        }
65        return kill;
66    }
67 
68    String pid() {
69        return pid;
70    }
71 
72    public void kill() {
73        kill(false);
74    }
75 
76    public void forceKill() {
77        Exceptions.VoidBlock block = new Exceptions.VoidBlock() {
78            public void inner() {
79                kill(true);
80            }
81        };
82        block.execNotThrowingExceptions(err);
83    }
84 
85    /**
86     * @param force
87     */
88    private void kill(boolean force) {
89        String threadName = "killing process " + pid;
90        if (force) {
91            threadName = "force " + threadName;
92        }
93        launchShell(threadName, killArgs(force), 10);
94    }
95 
96    String[] killArgs(boolean force) {
97        List args = new ArrayList();
98        args.add(killCommand);
99        if (force) {
100            args.add("-9");
101        }
102        args.add(pid);
103        return utils.str().toStringArray(args);
104    }
105 
106    public boolean isRunning() {
107        String threadName = "is_process_" + pid + "_running";
108        Shell shell = launchShell(threadName, isRunningArgs(), 5);
109        if (!shell.hasReturned()) {
110            return false;
111        }
112        return shell.returnCode() == 0;
113    }
114 
115    private Shell launchShell(String threadName, String[] args, int seconds) {
116        Shell shell = utils.shellFactory().newShell(args, threadName, out, err);
117        shell.start();
118        int fraction = 20;
119        int loops = (fraction * seconds);
120        do {
121            utils.threads().pause((1000 / fraction));
122        } while (!shell.hasReturned() && loops-- > 0);
123 
124        if (!shell.hasReturned()) {
125            err.println("Thread \"" + threadName + "\" may be hung");
126            err.println("(did not return after " + seconds + " seconds)");
127            err.println("command line used: ");
128            err.println(new ListToString("", " ", "").toString(args));
129        }
130        return shell;
131    }
132 
133    String[] isRunningArgs() {
134        return new String[] { killCommand, "-0", pid };
135    }
136 
137    public void killNoThrow() {
138        Exceptions.VoidBlock block = new Exceptions.VoidBlock() {
139            public void inner() {
140                kill();
141            }
142        };
143        block.execNotThrowingExceptions(err);
144    }
145}

[all classes][com.mysql.management.util]
EMMA 2.0.5312 (C) Vladimir Roubtsov