KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jbatchengine > batch > util > LogRedirectionThread


1 /*
2  jBatchEngine - jbatchengine.sourceforge.net
3  Copyright (C) 2006 by Simon Martinelli, Gampelen, Switzerland
4
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */

19 package net.sf.jbatchengine.batch.util;
20
21 import java.io.BufferedReader JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25
26 import org.apache.log4j.Logger;
27
28 /**
29  * Thread to catch STDOUT und STDERR of a process and redirect to Log4j.
30  *
31  * @author $Author: simas_ch $
32  * @version $Revision: 1.6 $, $Date: 2006/10/21 16:37:06 $
33  */

34 public class LogRedirectionThread extends Thread JavaDoc {
35
36   /** The log. */
37   private static transient Logger log = Logger.getLogger(LogRedirectionThread.class);
38
39   /** The proc. */
40   private Process JavaDoc proc;
41
42   /** The usage. */
43   private int usage;
44
45   /** The Constant OUT. */
46   public final static int OUT = 1;
47
48   /** The Constant ERR. */
49   public final static int ERR = 2;
50
51   /**
52    * The Constructor.
53    *
54    * @param proc the proc
55    * @param usage the usage
56    */

57   public LogRedirectionThread(Process JavaDoc proc, int usage) {
58     this.proc = proc;
59     this.usage = usage;
60   }
61
62   /**
63    * Run.
64    */

65   public void run() {
66     try {
67       InputStream JavaDoc is = null;
68
69       if (this.usage == OUT) {
70         is = proc.getInputStream();
71       }
72       else {
73         is = proc.getErrorStream();
74       }
75
76       BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is));
77       String JavaDoc msg = br.readLine();
78       while (msg != null) {
79         if (this.usage == OUT) {
80           log.info(msg);
81         }
82         else {
83           log.error(msg);
84         }
85         msg = br.readLine();
86       }
87     }
88     catch (IOException JavaDoc e) {
89       e.printStackTrace();
90     }
91   }
92 }
93
Popular Tags