001: package tide.execute;
002:
003: import snow.utils.gui.Icons;
004: import java.util.regex.*;
005: import java.io.*;
006: import snow.utils.*;
007: import java.util.*;
008: import java.awt.event.*;
009: import tide.editor.MainEditorFrame;
010: import javax.swing.*;
011:
012: /** Some Os utils related to processes.
013: */
014: public final class OSProcUtils {
015: private OSProcUtils() {
016: }
017:
018: public static void createOSMenu(JPopupMenu pop,
019: final ProcessesManager pm) {
020: JMenuItem mi = new JMenuItem("List all java processes");
021: pop.add(mi);
022: mi.addActionListener(new ActionListener() {
023: public void actionPerformed(ActionEvent ae) {
024: File jpst = MainEditorFrame.instance.getActualProject()
025: .getJPS_TOOL();
026: if (!jpst.exists()) {
027: JOptionPane.showMessageDialog(
028: MainEditorFrame.instance,
029: "JPS tool not found (Java5+ tool)",
030: "Error", JOptionPane.ERROR_MESSAGE);
031: return;
032: }
033:
034: List<JProcess> allJPs = JProcess
035: .getAllRunningJavaProcesses(jpst);
036: pm.refreshALLJavaProcesses(allJPs);
037:
038: }
039: });
040:
041: JMenuItem mis = new JMenuItem("List all system processes");
042: pop.add(mis);
043: mis.addActionListener(new ActionListener() {
044: public void actionPerformed(ActionEvent ae) {
045: try {
046: List<SProcess> allJPs = getTaskList();
047: pm.refreshALL(allJPs);
048: } catch (Exception ex) {
049: ex.printStackTrace();
050: }
051:
052: }
053: });
054:
055: JMenuItem rem = new JMenuItem("Remove non tIDE processes",
056: Icons.sharedCross);
057: pop.addSeparator();
058: pop.add(rem);
059: rem.addActionListener(new ActionListener() {
060: public void actionPerformed(ActionEvent ae) {
061: pm.removeNonTIDEProcs();
062: }
063: });
064: }
065:
066: /** Use system tasklist or ps (linux) to populate the running processes.
067: */
068: public static List<SProcess> getTaskList() throws Exception {
069: List<SProcess> tasks = new ArrayList<SProcess>();
070: if (SysUtils.is_Windows_OS()) //XP?
071: {
072: // /v verbose
073: // /SVC shows services, but no more memory.
074: String fs = ProcessUtils.readWholeProcessStack("cmd", "/C",
075: "tasklist", "/FO", "CSV", "/NH"); // csv list without header
076: BufferedReader sr = new BufferedReader(new StringReader(fs));
077: String line = null;
078: Pattern p = Pattern.compile("\"(.*?)\"");
079: Matcher matcher = p.matcher("");
080: while ((line = sr.readLine()) != null) {
081: line = line.trim(); // "smss.exe","1096","Console","0","364 K"
082:
083: if (line.length() == 0)
084: continue;
085:
086: //System.out.println(">"+line);
087:
088: matcher.reset(line);
089:
090: matcher.find(); // image name
091: String name = matcher.group(1);
092:
093: if (name.equalsIgnoreCase("tasklist.exe"))
094: continue; // ignore it !!
095: //if(name.equals("cmd.exe")) continue; // ignore it !! (only if the last...), maybe several other cmds !!
096:
097: matcher.find(); // pid
098: String id = matcher.group(1);
099:
100: matcher.find(); // session name
101: String sn = matcher.group(1);
102:
103: matcher.find(); //?
104:
105: matcher.find(); // mem
106: long mem = parseMemFromWinTaskList(matcher.group(1));
107:
108: SProcess sp = new SProcess(name, id, mem);
109: tasks.add(sp);
110: }
111:
112: // remove the last cmd.exe if so.
113: if (tasks.size() > 0) {
114: SProcess lp = tasks.get(tasks.size() - 1);
115: if ("cmd.exe".equalsIgnoreCase(lp.name)) {
116: tasks.remove(lp);
117: }
118: }
119:
120: // second call to fetch the services
121: Map<String, SProcess> dp = getDetailledTaskList();
122: for (SProcess pi : tasks) {
123: if (dp.containsKey(pi.getPid())) {
124: pi.services = dp.get(pi.getPid()).services;
125: }
126: }
127: } else {
128: //String fs = ProcessUtils.readWholeProcessStack("ps", "-a"); // "pid tty time cmd"
129: // Suselinux: special case, only keep distinct ppids
130: String fs = ProcessUtils.readWholeProcessStack("ps", "-a",
131: "-f"); // "uid pid ppid c stime tty time cmd"
132: BufferedReader sr = new BufferedReader(new StringReader(fs));
133: String line = sr.readLine(); // ignore first line = header
134: Pattern pa = Pattern.compile("\\s*(\\S*)\\s*");
135: Matcher ma = pa.matcher("");
136:
137: Set<String> alreadyPPID = new HashSet<String>();
138:
139: while ((line = sr.readLine()) != null) {
140: line = line.trim(); // "pid tty time cmd"
141: if (line.length() == 0)
142: continue;
143:
144: ma.reset(line);
145: ma.find(); // uid
146: ma.find(); // pid
147: ma.find(); // ppid
148: String ppid = ma.group(1);
149: if (alreadyPPID.contains(ppid))
150: continue;
151: alreadyPPID.add(ppid);
152:
153: ma.find(); //c
154: ma.find(); //stime
155: ma.find(); // tty
156: ma.find(); // time
157: ma.find(); // cmd
158:
159: String cmd = ma.group(1);
160:
161: SProcess sp = new SProcess(cmd, ppid, -1);
162: tasks.add(sp);
163:
164: //System.out.println(">"+line);
165:
166: }
167:
168: // TODO !
169: }
170: return tasks;
171: }
172:
173: /** -1 if not ok
174: */
175: private static long parseMemFromWinTaskList(String mem) {
176: long fact = 1;
177: mem = mem.replace("'", "").trim();
178: if (mem.toLowerCase().endsWith("k")) {
179: fact = 1000;
180: mem = mem.substring(0, mem.length() - 1).trim();
181: }
182:
183: long b = -1;
184: try {
185: //System.out.println(""+mem+" "+fact);
186: b = Long.parseLong(mem);
187: } catch (Exception e) {
188: return -1;
189: }
190:
191: return fact * b;
192:
193: }
194:
195: /** Windows special tasklist mode.
196: */
197: public static Map<String, SProcess> getDetailledTaskList()
198: throws Exception {
199: Map<String, SProcess> tasks = new HashMap<String, SProcess>();
200: if (SysUtils.is_Windows_OS()) {
201: // /v verbose
202: // /SVC shows services, but no more memory.
203: String fs = ProcessUtils.readWholeProcessStack("cmd", "/C",
204: "tasklist", "/FO", "CSV", "/SVC", "/NH"); // csv list without header
205: BufferedReader sr = new BufferedReader(new StringReader(fs));
206: String line = null;
207: Pattern p = Pattern.compile("\"(.*?)\"");
208: Matcher matcher = p.matcher("");
209: while ((line = sr.readLine()) != null) {
210: line = line.trim(); // "smss.exe","1096","Console","0","364 K"
211:
212: if (line.length() == 0)
213: continue;
214:
215: // System.out.println(">"+line);
216:
217: matcher.reset(line);
218:
219: matcher.find();
220: String nameExe = matcher.group(1);
221:
222: matcher.find();
223: String id = matcher.group(1);
224:
225: matcher.find();
226: String serv = matcher.group(1);
227:
228: SProcess sp = new SProcess(nameExe, id, -1);
229: sp.services = serv;
230:
231: tasks.put(id, sp);
232: }
233: }
234: return tasks;
235: }
236:
237: public static void main(String[] args) throws Exception {
238: Pattern pa = Pattern.compile("\\s*(\\S*)\\s*");
239: Matcher m = pa.matcher("122 hello\tHHH");
240: m.find();
241: System.out.println("" + m.group(1));
242: m.find();
243: System.out.println("" + m.group(1));
244: m.find();
245: System.out.println("" + m.group(1));
246:
247: System.out.println("" + getTaskList());
248: }
249:
250: }
|