KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > backup > backupers > NativeCommandOutputThread


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2005 Emic Networks
4  * Contact: sequoia@continuent.org
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * Initial developer(s): Dylan Hansen.
19  */

20
21 package org.continuent.sequoia.controller.backup.backupers;
22
23 import java.io.BufferedReader JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.InputStreamReader JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.util.ArrayList JavaDoc;
29
30 import org.continuent.sequoia.common.log.Trace;
31
32 /**
33  * Thread to process output of native commands. This class captures certain
34  * errors and buffers up to a hard-coded number of lines of output. To see all
35  * output turn up log4j debugging to DEBUG level.
36  *
37  * @author <a HREF="mailto:dhansen@h2st.com">Dylan Hansen</a>
38  * @version 1.0
39  */

40 public class NativeCommandOutputThread extends Thread JavaDoc
41 {
42   // Buffer this many output lines.
43
private static final int MAX_OUTPUT_LINES = 25;
44
45   ArrayList JavaDoc errors = new ArrayList JavaDoc();
46   ArrayList JavaDoc output = new ArrayList JavaDoc();
47   InputStream JavaDoc inputStream = null;
48   OutputStream JavaDoc outputStream = null;
49
50   // Logger
51
static Trace logger = Trace
52                                                 .getLogger(NativeCommandOutputThread.class
53                                                     .getName());
54
55   /**
56    * Creates a new <code>NativeCommandOutputThread</code> object
57    *
58    * @param inStream input stream for command which is closed by Thread when
59    * finished
60    */

61   public NativeCommandOutputThread(InputStream JavaDoc inStream)
62   {
63     inputStream = inStream;
64   }
65
66   /**
67    * Creates a new <code>NativeCommandOutputThread</code> object that
68    * that transfers all output to the specified output stream.
69    *
70    * @param inStream input stream for command which is closed by thread when
71    * finished
72    * @param outStream Output stream to which output is written; closed by
73    * thread when finished
74    */

75   public NativeCommandOutputThread(InputStream JavaDoc inStream,
76       OutputStream JavaDoc outStream)
77   {
78     inputStream = inStream;
79     outputStream = outStream;
80   }
81
82   /**
83    * Reads from the input stream and outputs to the log. Adds any error message
84    * to the "errors" ArrayList. Up to MAX_OUTPUT_LINES of output are stored.
85    * IOExceptions are logged and result in thread termination.
86    */

87   public void run()
88   {
89     if (logger.isDebugEnabled())
90       logger.debug("Starting NativeCommandOutputThread: " + this.getName());
91
92     if (outputStream == null)
93       doNormalOutput();
94     else
95       doRedirectOutput();
96
97     if (logger.isDebugEnabled())
98       logger.debug("Terminating NativeCommandOutputThread: " + this.toString());
99   }
100
101   /**
102    * Processes normal output.
103    */

104   protected void doNormalOutput()
105   {
106     BufferedReader JavaDoc buffRead = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(inputStream));
107
108     String JavaDoc line = null;
109
110     try
111     {
112       while ((line = buffRead.readLine()) != null)
113       {
114         // Check to see if PostgreSQL output contains "FATAL: " or "ERROR: "
115
if (line.indexOf("FATAL: ") > -1 || line.indexOf("ERROR: ") > -1)
116         {
117           errors.add(line);
118         }
119         if (output.size() < MAX_OUTPUT_LINES)
120         {
121           output.add(line);
122         }
123
124         if (logger.isDebugEnabled())
125           logger.debug(this.getName() + ": " + line);
126       }
127     }
128     catch (IOException JavaDoc ioe)
129     {
130       logger.warn(ioe.getMessage(), ioe);
131     }
132     finally
133     {
134       // Must close stream in this thread to avoid synchronization problems
135
try
136       {
137         buffRead.close();
138       }
139       catch (IOException JavaDoc ioe)
140       {
141         logger.warn(ioe.getMessage(), ioe);
142       }
143     }
144   }
145
146   /**
147    * Processes output redirection when output directly into another
148    * output stream.
149    */

150   protected void doRedirectOutput()
151   {
152     try
153     {
154       byte[] buff = new byte[1024];
155       int len = 0;
156       while ((len = this.inputStream.read(buff)) > 0)
157       {
158         outputStream.write(buff, 0, len);
159       }
160     }
161     catch (IOException JavaDoc ioe)
162     {
163       logger.warn(ioe.getMessage(), ioe);
164     }
165     finally
166     {
167       // Must close stream in this thread to avoid synchronization problems
168
try
169       {
170         inputStream.close();
171         outputStream.close();
172       }
173       catch (IOException JavaDoc ioe)
174       {
175         logger.warn(ioe.getMessage(), ioe);
176       }
177     }
178   }
179
180   /**
181    * Return a list of errors
182    *
183    * @return ArrayList of <code>String</code> objects
184    */

185   public ArrayList JavaDoc getErrors()
186   {
187     return errors;
188   }
189
190   /**
191    * Return buffered output lines.
192    *
193    * @return Output line array.
194    */

195   public ArrayList JavaDoc getOutput()
196   {
197     return output;
198   }
199 }
200
Popular Tags