KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > ProcessTable


1 /*
2  * ProcessTable.java
3  *
4  * Copyright (C) 2000-2002 Peter Graves
5  * $Id: ProcessTable.java,v 1.2 2002/10/05 01:04:10 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.io.BufferedReader JavaDoc;
25 import java.io.InputStreamReader JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29
30 public final class ProcessTable
31 {
32     private ArrayList JavaDoc entries = new ArrayList JavaDoc();
33
34     private ProcessTable()
35     {
36     }
37
38     public static ProcessTable getProcessTable()
39     {
40         if (!Platform.isPlatformUnix())
41             return null;
42         ProcessTable table = new ProcessTable();
43         try {
44             String JavaDoc[] cmdarray = {"/bin/sh", "-c", "ps -o pid,ppid,command"};
45             Process JavaDoc process = Runtime.getRuntime().exec(cmdarray);
46             BufferedReader JavaDoc reader =
47                 new BufferedReader JavaDoc(new InputStreamReader JavaDoc(process.getInputStream()));
48             if (reader != null) {
49                 String JavaDoc s;
50                 while ((s = reader.readLine()) != null) {
51                     if (s.length() > 0)
52                         table.addEntry(s);
53                 }
54             }
55         }
56         catch (Throwable JavaDoc t) {
57             Log.error(t);
58         }
59         return table;
60     }
61
62     public List JavaDoc findMatchingEntries(String JavaDoc command)
63     {
64         ArrayList JavaDoc results = new ArrayList JavaDoc();
65         if (command != null && command.length() > 0){
66             // First char of command might be replaced with '-', so we look
67
// for that pattern too.
68
String JavaDoc alternate = "-".concat(command.substring(1));
69             for (int i = 0; i < entries.size(); i++) {
70                 ProcessTableEntry entry = (ProcessTableEntry) entries.get(i);
71                 if (entry.command.indexOf(command) >= 0 ||
72                     entry.command.indexOf(alternate) >= 0)
73                     results.add(entry);
74             }
75         }
76         return results;
77     }
78
79     public List JavaDoc findChildren(int pid)
80     {
81         ArrayList JavaDoc results = new ArrayList JavaDoc();
82         for (int i = 0; i < entries.size(); i++) {
83             ProcessTableEntry entry = (ProcessTableEntry) entries.get(i);
84             if (entry.ppid == pid)
85                 results.add(entry);
86         }
87         return results;
88     }
89
90     private void addEntry(String JavaDoc s)
91     {
92         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(s);
93         if (st.countTokens() >= 3) {
94             String JavaDoc pidString = st.nextToken();
95             String JavaDoc ppidString = st.nextToken();
96             String JavaDoc command = st.nextToken("\n").trim();
97             int pid, ppid;
98             try {
99                 pid = Integer.parseInt(pidString);
100                 ppid = Integer.parseInt(ppidString);
101                 entries.add(new ProcessTableEntry(pid, ppid, command));
102             }
103             catch (NumberFormatException JavaDoc e) {}
104         }
105     }
106 }
107
Popular Tags