001: /*
002: * ProcessTable.java
003: *
004: * Copyright (C) 2000-2002 Peter Graves
005: * $Id: ProcessTable.java,v 1.2 2002/10/05 01:04:10 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.io.BufferedReader;
025: import java.io.InputStreamReader;
026: import java.util.ArrayList;
027: import java.util.List;
028: import java.util.StringTokenizer;
029:
030: public final class ProcessTable {
031: private ArrayList entries = new ArrayList();
032:
033: private ProcessTable() {
034: }
035:
036: public static ProcessTable getProcessTable() {
037: if (!Platform.isPlatformUnix())
038: return null;
039: ProcessTable table = new ProcessTable();
040: try {
041: String[] cmdarray = { "/bin/sh", "-c",
042: "ps -o pid,ppid,command" };
043: Process process = Runtime.getRuntime().exec(cmdarray);
044: BufferedReader reader = new BufferedReader(
045: new InputStreamReader(process.getInputStream()));
046: if (reader != null) {
047: String s;
048: while ((s = reader.readLine()) != null) {
049: if (s.length() > 0)
050: table.addEntry(s);
051: }
052: }
053: } catch (Throwable t) {
054: Log.error(t);
055: }
056: return table;
057: }
058:
059: public List findMatchingEntries(String command) {
060: ArrayList results = new ArrayList();
061: if (command != null && command.length() > 0) {
062: // First char of command might be replaced with '-', so we look
063: // for that pattern too.
064: String alternate = "-".concat(command.substring(1));
065: for (int i = 0; i < entries.size(); i++) {
066: ProcessTableEntry entry = (ProcessTableEntry) entries
067: .get(i);
068: if (entry.command.indexOf(command) >= 0
069: || entry.command.indexOf(alternate) >= 0)
070: results.add(entry);
071: }
072: }
073: return results;
074: }
075:
076: public List findChildren(int pid) {
077: ArrayList results = new ArrayList();
078: for (int i = 0; i < entries.size(); i++) {
079: ProcessTableEntry entry = (ProcessTableEntry) entries
080: .get(i);
081: if (entry.ppid == pid)
082: results.add(entry);
083: }
084: return results;
085: }
086:
087: private void addEntry(String s) {
088: StringTokenizer st = new StringTokenizer(s);
089: if (st.countTokens() >= 3) {
090: String pidString = st.nextToken();
091: String ppidString = st.nextToken();
092: String command = st.nextToken("\n").trim();
093: int pid, ppid;
094: try {
095: pid = Integer.parseInt(pidString);
096: ppid = Integer.parseInt(ppidString);
097: entries.add(new ProcessTableEntry(pid, ppid, command));
098: } catch (NumberFormatException e) {
099: }
100: }
101: }
102: }
|