Source Code Cross Referenced for OSProcUtils.java in  » IDE » tIDE » tide » execute » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE » tIDE » tide.execute 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.