KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > tools > util > KillJava


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk
22  * Contributor(s): ______________________.
23  */

24 package org.objectweb.cjdbc.scenario.tools.util;
25
26 import java.io.BufferedReader JavaDoc;
27 import java.io.InputStreamReader JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29
30 /**
31  * This class defines a KillJava
32  *
33  * @author <a HREF="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
34  * @version 1.0
35  */

36 public class KillJava
37 {
38
39   private String JavaDoc killTarget;
40   private int totalCount;
41
42   /**
43    *
44    * Creates a new <code>KillJava</code> object
45    *
46    * @param killTarget one of java class name or 'all'
47    */

48   public KillJava(String JavaDoc killTarget)
49   {
50     this.killTarget = killTarget;
51     totalCount = 0;
52   }
53   
54   /**
55    *
56    * Creates a new <code>KillJava</code> object
57    *
58    *
59    */

60   public KillJava()
61   {
62     killTarget = "org.hsqldb.Server";
63     totalCount = 0;
64   }
65
66   
67   /**
68    *
69    * butchery ...
70    *
71    * @throws Exception if fails
72    */

73   public void execute() throws Exception JavaDoc
74   {
75     //System.out.println("Killing target:" + killTarget);
76
String JavaDoc command = "ps -aux";
77     Process JavaDoc p = Runtime.getRuntime().exec(command);
78     BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(p
79         .getInputStream()));
80     String JavaDoc line = "";
81     StringTokenizer JavaDoc tokenizer;
82     int count = 0;
83     while ((line = br.readLine()) != null)
84     {
85       if (line.indexOf("java") != -1 && line.indexOf(killTarget) != -1
86           && line.indexOf("KillJava") == -1)
87       {
88         //System.out.println(line);
89
tokenizer = new StringTokenizer JavaDoc(line);
90         tokenizer.nextToken();
91         String JavaDoc processNumber = tokenizer.nextToken();
92         //System.out.println("Killing process:" + processNumber);
93
Runtime.getRuntime().exec("kill -9 " + processNumber);
94         count++;
95       }
96     }
97     totalCount += count;
98     if (count > 0)
99       execute();
100     else
101       System.out.println("Terminated "+totalCount+" processes");
102   }
103 }
Popular Tags