Source Code Cross Referenced for ConsoleCmd.java in  » Net » JCGrid » org » homedns » dade » jcgrid » 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 » Net » JCGrid » org.homedns.dade.jcgrid.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         Copyright (C) 2004 David Bucciarelli (davibu@interfree.it)
003:
004:         This program is free software; you can redistribute it and/or
005:         modify it under the terms of the GNU General Public License
006:         as published by the Free Software Foundation; either version 2
007:         of the License, or (at your option) any later version.
008:
009:         This program is distributed in the hope that it will be useful,
010:         but WITHOUT ANY WARRANTY; without even the implied warranty of
011:         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012:         GNU General Public License for more details.
013:
014:         You should have received a copy of the GNU General Public License
015:         along with this program; if not, write to the Free Software
016:         Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
017:         */
018:
019:        package org.homedns.dade.jcgrid.util;
020:
021:        import java.io.*;
022:
023:        import org.apache.log4j.*;
024:
025:        public class ConsoleCmd extends Object {
026:            private final static String className = ConsoleCmd.class.getName();
027:            private static Logger log = Logger.getLogger(className);
028:            private static Logger logDetail = Logger.getLogger("DETAIL."
029:                    + className);
030:
031:            //--------------------------------------------------------------------------
032:
033:            private class WritterThread extends Thread {
034:                private OutputStream os;
035:                private byte[] data;
036:
037:                public WritterThread(OutputStream osVal, byte[] dataVal) {
038:                    os = osVal;
039:                    data = dataVal;
040:                }
041:
042:                public void run() {
043:                    try {
044:                        if (data != null) {
045:                            os.write(data);
046:                            os.flush();
047:                        }
048:
049:                        os.close();
050:                    } catch (Throwable ex) {
051:                        log.warn("Exception in ConsoleCmd.WritterThread.run()",
052:                                ex);
053:                    }
054:                }
055:            }
056:
057:            private class ReaderThread extends Thread {
058:                private InputStream is;
059:                private byte[] data;
060:
061:                public ReaderThread(InputStream isVal) {
062:                    is = new BufferedInputStream(isVal);
063:                }
064:
065:                public void run() {
066:                    try {
067:                        data = ByteVector.readAll(is);
068:                    } catch (Throwable ex) {
069:                        log.warn("Exception in ConsoleCmd.ReaderThread.run()",
070:                                ex);
071:                    }
072:                }
073:
074:                public byte[] getData() {
075:                    return data;
076:                }
077:            }
078:
079:            //--------------------------------------------------------------------------
080:
081:            private String[] cmd;
082:            private Process proc;
083:            private byte[] stdin;
084:            private byte[] stdout;
085:            private byte[] stderr;
086:            private File curDir;
087:
088:            public ConsoleCmd(String[] cmdVal, byte[] stdinVal, String dir) {
089:                stdin = stdinVal;
090:                cmd = cmdVal;
091:                proc = null;
092:
093:                if (dir == null)
094:                    curDir = null;
095:                else
096:                    curDir = new File(dir);
097:            }
098:
099:            public ConsoleCmd(String[] cmdVal, byte[] stdinVal) {
100:                this (cmdVal, stdinVal, null);
101:            }
102:
103:            public ConsoleCmd(String[] cmdVal) {
104:                this (cmdVal, null, null);
105:            }
106:
107:            public String getCmdLine() {
108:                StringBuffer sb = new StringBuffer();
109:
110:                boolean first = true;
111:                for (int i = 0; i < cmd.length; i++) {
112:                    if (first)
113:                        first = false;
114:                    else
115:                        sb.append(" ");
116:
117:                    sb.append(cmd[i]);
118:                }
119:
120:                return sb.toString();
121:            }
122:
123:            public byte[] getStdOut() {
124:                return stdout;
125:            }
126:
127:            public byte[] getStdErr() {
128:                return stderr;
129:            }
130:
131:            public int run() throws IOException, InterruptedException {
132:                if (curDir == null)
133:                    proc = Runtime.getRuntime().exec(cmd);
134:                else
135:                    proc = Runtime.getRuntime().exec(cmd, null, curDir);
136:
137:                OutputStream si = proc.getOutputStream();
138:                InputStream so = proc.getInputStream();
139:                InputStream se = proc.getErrorStream();
140:
141:                // Thread stdin
142:
143:                WritterThread it = new WritterThread(si, stdin);
144:                it.start();
145:
146:                // Thread stdout
147:
148:                ReaderThread ot = new ReaderThread(so);
149:                ot.start();
150:
151:                // Thread stderr
152:
153:                ReaderThread et = new ReaderThread(se);
154:                et.start();
155:
156:                // Wait execution end
157:
158:                int exitVal = proc.waitFor();
159:                it.join();
160:                ot.join();
161:                et.join();
162:
163:                stdout = ot.getData();
164:                stderr = et.getData();
165:
166:                return exitVal;
167:            }
168:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.