Source Code Cross Referenced for ProcessOutputHandler.java in  » Testing » abbot-1.0.1 » abbot » util » 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 » Testing » abbot 1.0.1 » abbot.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package abbot.util;
002:
003:        import java.io.*;
004:        import java.util.*;
005:        import abbot.Log;
006:
007:        /** Provides handling of process output/error streams. */
008:
009:        public class ProcessOutputHandler {
010:
011:            public static class ProcessAbnormalExitException extends
012:                    IOException {
013:                private int code;
014:
015:                private ProcessAbnormalExitException(String msg, int code) {
016:                    super (msg);
017:                    this .code = code;
018:                }
019:
020:                public int getExitValue() {
021:                    return code;
022:                }
023:            }
024:
025:            private InputStreamHandler stderr;
026:            private InputStreamHandler stdout;
027:            private StringBuffer err = new StringBuffer();
028:
029:            public ProcessOutputHandler() {
030:            }
031:
032:            public ProcessOutputHandler(Process p) {
033:                setProcess(p);
034:            }
035:
036:            public String getError() {
037:                return err.toString();
038:            }
039:
040:            public synchronized void setProcess(Process p) {
041:                stdout = new InputStreamHandler(p.getInputStream()) {
042:                    public void handleBytes(byte[] buf, int count) {
043:                        handleOutput(buf, count);
044:                    }
045:                };
046:                stdout.start();
047:                stderr = new InputStreamHandler(p.getErrorStream()) {
048:                    public void handleBytes(byte[] buf, int count) {
049:                        err.append(new String(buf, 0, count));
050:                        handleError(buf, count);
051:                    }
052:                };
053:                stderr.start();
054:            }
055:
056:            /** Override this method to handle stdout output.  The default
057:                implementation does nothing. */
058:            protected void handleOutput(byte[] buf, int count) {
059:            }
060:
061:            /** Override this method to handle stderr output.  The default
062:                implementation does nothing. */
063:            protected void handleError(byte[] buf, int count) {
064:            }
065:
066:            public synchronized void waitFor() throws InterruptedException {
067:                if (stderr != null)
068:                    stderr.join();
069:                if (stdout != null)
070:                    stdout.join();
071:            }
072:
073:            /** Returns the output of the given command as a String. */
074:            public static String exec(String[] command) throws IOException {
075:                return exec(command, null);
076:            }
077:
078:            /** Returns the output of the given command as a String. */
079:            public static String exec(String[] command, String[] environment)
080:                    throws IOException {
081:                return exec(command, environment, null);
082:            }
083:
084:            /** Returns the output of the given command as a String. */
085:            public static String exec(String[] command, String[] environment,
086:                    File dir) throws IOException {
087:                final StringBuffer output = new StringBuffer();
088:                Log.debug("Running " + Arrays.asList(command));
089:                Process p = Runtime.getRuntime()
090:                        .exec(command, environment, dir);
091:                ProcessOutputHandler handler = new ProcessOutputHandler(p) {
092:                    public void handleOutput(byte[] buf, int count) {
093:                        output.append(new String(buf, 0, count));
094:                    }
095:                };
096:                try {
097:                    p.waitFor();
098:                } catch (InterruptedException e) {
099:                    Log.debug(e);
100:                }
101:                try {
102:                    handler.waitFor();
103:                } catch (InterruptedException e) {
104:                }
105:                int code = p.exitValue();
106:                if (code != 0) {
107:                    String msg = "Process " + Arrays.asList(command)
108:                            + " exited with " + code;
109:                    String err = handler.getError();
110:                    if (!"".equals(err))
111:                        msg += ":\n" + err;
112:                    Log.debug(msg);
113:                    throw new ProcessAbnormalExitException(msg, code);
114:                }
115:                Log.debug("output=" + output.toString());
116:                return output.toString();
117:            }
118:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.