KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > corba > idl2java > Redirector


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2004 INRIA & USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Christophe Demarey.
23 Contributor(s): ________________________________________.
24
25 ====================================================================*/

26
27 package org.objectweb.openccm.corba.idl2java;
28
29 //Package dependencies
30
import java.io.BufferedReader JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.io.InputStreamReader JavaDoc;
33
34 /**
35  * This class redirect output and error output of a process.
36  *
37  * @author <a HREF="mailto:Christophe.Demarey@lifl.fr">Christophe Demarey</a>
38  *
39  * @version 0.1
40  */

41 public class Redirector
42 {
43     // ==================================================================
44
//
45
// Internal states.
46
//
47
// ==================================================================
48

49     /** The Process to redirect. */
50     private Process JavaDoc process_;
51
52     // ==================================================================
53
//
54
// Constructor.
55
//
56
// ==================================================================
57

58     /**
59      * The default constructor.
60      *
61      * @param process - The process to redirect.
62      */

63     public Redirector(Process JavaDoc process)
64     {
65         process_ = process;
66     }
67     
68     // ==================================================================
69
//
70
// Inner classes.
71
//
72
// ==================================================================
73

74     /**
75      * Redirect the standard output.
76      */

77     private class OutputRedirectorThread
78        implements Runnable JavaDoc
79     {
80         /** The input stream to redirect. */
81         private InputStream JavaDoc inputstream_;
82
83         /**
84          * The default constructor
85          *
86          * @param inputstream - The input stream to redirect.
87          */

88         public
89         OutputRedirectorThread(InputStream JavaDoc inputstream)
90         {
91             inputstream_ = inputstream;
92         }
93         
94         /**
95          * Run the output redirection.
96          */

97         public void
98         run()
99         {
100             BufferedReader JavaDoc bufferedreader = null;
101             String JavaDoc line = null;
102             
103             try
104             {
105                 // put a BufferedReader on the process output
106
bufferedreader =
107                     new BufferedReader JavaDoc( new InputStreamReader JavaDoc(inputstream_) );
108     
109                 // read the process output
110
while ( (line = bufferedreader.readLine()) != null)
111                 {
112                     System.out.println(line);
113                 }
114             }catch(java.io.IOException JavaDoc e){
115                 e.printStackTrace();
116             }
117         }
118     }
119
120     /**
121      * Redirect the standard error.
122      */

123     private class ErrorRedirectorThread
124        implements Runnable JavaDoc
125     {
126         /** The input stream to redirect. */
127         private InputStream JavaDoc inputstream_;
128
129         /**
130          * The default constructor
131          *
132          * @param inputstream - The input stream to redirect.
133          */

134         public
135         ErrorRedirectorThread(InputStream JavaDoc inputstream)
136         {
137             inputstream_ = inputstream;
138         }
139         
140         /**
141          * Run the output redirection.
142          */

143         public void
144         run()
145         {
146             BufferedReader JavaDoc bufferedreader = null;
147             String JavaDoc line = null;
148
149             try
150             {
151                 // put a BufferedReader on the process output
152
bufferedreader =
153                     new BufferedReader JavaDoc( new InputStreamReader JavaDoc(inputstream_) );
154     
155                 // read the process output
156
while ( (line = bufferedreader.readLine()) != null)
157                 {
158                     System.err.println(line);
159                 }
160             }catch(java.io.IOException JavaDoc e){
161                 e.printStackTrace();
162             }
163         }
164     }
165
166     // ==================================================================
167
//
168
// Internal methods.
169
//
170
// ==================================================================
171

172     // ==================================================================
173
//
174
// Public methods.
175
//
176
// ==================================================================
177

178     /**
179      * Redirect standard output and standard error to
180      * standard output and standard error of the current Thread.
181      */

182     public void
183     redirectOutput()
184     {
185         Thread JavaDoc t1 = null,
186                t2 = null;
187         OutputRedirectorThread outThread = null;
188         ErrorRedirectorThread errThread = null;
189                
190                
191         // Redirect Output Stream
192
try
193         {
194             outThread = new OutputRedirectorThread(process_.getInputStream());
195             errThread = new ErrorRedirectorThread(process_.getErrorStream());
196             
197             t1 = new Thread JavaDoc( outThread );
198             t2 = new Thread JavaDoc( errThread );
199
200             t1.start();
201             t2.start();
202         }catch (Exception JavaDoc e){
203             e.printStackTrace();
204         }
205     }
206
207 }
208
Popular Tags