KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > setup > CmsSetupLoggingThread


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/setup/CmsSetupLoggingThread.java,v $
3  * Date : $Date: 2006/03/27 14:52:51 $
4  * Version: $Revision: 1.13 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.setup;
33
34 import java.io.BufferedReader JavaDoc;
35 import java.io.File JavaDoc;
36 import java.io.FileWriter JavaDoc;
37 import java.io.IOException JavaDoc;
38 import java.io.InputStreamReader JavaDoc;
39 import java.io.LineNumberReader JavaDoc;
40 import java.io.PipedInputStream JavaDoc;
41 import java.io.PipedOutputStream JavaDoc;
42 import java.util.ArrayList JavaDoc;
43 import java.util.List JavaDoc;
44
45 /**
46  * Logging Thread which collects the output from CmsSetupThread and
47  * stores it in a Vector that the OpenCms setup wizard can read via
48  * the getMessages() method.<p>
49  *
50  * @author Alexander Kandzior
51  *
52  * @version $Revision: 1.13 $
53  *
54  * @since 6.0.0
55  */

56 public class CmsSetupLoggingThread extends Thread JavaDoc {
57
58     private LineNumberReader JavaDoc m_lineReader;
59     private FileWriter JavaDoc m_logWriter;
60     private List JavaDoc m_messages;
61     private PipedInputStream JavaDoc m_pipedIn;
62     private PipedOutputStream JavaDoc m_pipedOut;
63     private boolean m_stopThread;
64
65     /**
66      * Constructor.<p>
67      *
68      * @param pipedOut the output stream to write to
69      * @param log the file name to write the log to (if null, no log is written)
70      */

71     public CmsSetupLoggingThread(PipedOutputStream JavaDoc pipedOut, String JavaDoc log) {
72
73         super("OpenCms: Setup logging");
74
75         m_pipedOut = pipedOut;
76         m_messages = new ArrayList JavaDoc();
77         m_stopThread = false;
78
79         if (log != null) {
80             try {
81                 File JavaDoc logFile = new File JavaDoc(log);
82                 if (logFile.exists()) {
83                     logFile.delete();
84                 }
85                 m_logWriter = new FileWriter JavaDoc(logFile);
86             } catch (Throwable JavaDoc t) {
87                 m_logWriter = null;
88             }
89         } else {
90             m_logWriter = null;
91         }
92
93         try {
94             m_pipedIn = new PipedInputStream JavaDoc();
95             m_pipedIn.connect(m_pipedOut);
96             m_lineReader = new LineNumberReader JavaDoc(new BufferedReader JavaDoc(new InputStreamReader JavaDoc(m_pipedIn)));
97         } catch (Exception JavaDoc e) {
98             m_messages.add(e.toString());
99         }
100     }
101
102     /**
103      * Returns a Vector with the last collected log messages.<p>
104      *
105      * @return a Vector with the last collected log messages
106      */

107     public List JavaDoc getMessages() {
108
109         return m_messages;
110     }
111
112     /**
113      * Returns <code>"true"</code> if the logging is finished.<p>
114      *
115      * @return <code>"true"</code> if the logging is finished
116      */

117     public boolean isFinished() {
118
119         return m_stopThread;
120     }
121
122     /**
123      * @see java.lang.Runnable#run()
124      */

125     public void run() {
126
127         int lineNr = 0;
128         int lastLineNr = -1;
129         String JavaDoc line = null;
130         while (!m_stopThread) {
131             lineNr = m_lineReader.getLineNumber();
132             try {
133                 line = m_lineReader.readLine();
134             } catch (IOException JavaDoc e) {
135                 // "Write end dead" IO exceptions can be ignored
136
}
137             if (line != null) {
138                 if (lineNr > lastLineNr) {
139                     // supress multiple output of the same line after "Write end dead" IO exception
140
String JavaDoc content = (lineNr + 1) + ":\t" + line;
141                     m_messages.add(content);
142                     lastLineNr = lineNr;
143                     if (m_logWriter != null) {
144                         try {
145                             m_logWriter.write(content + "\n");
146                         } catch (IOException JavaDoc e) {
147                             m_logWriter = null;
148                         }
149                     }
150                 }
151             }
152         }
153         try {
154             m_pipedIn.close();
155         } catch (IOException JavaDoc e) {
156             // ignore
157
}
158         if (m_logWriter != null) {
159             try {
160                 m_logWriter.close();
161             } catch (IOException JavaDoc e) {
162                 m_logWriter = null;
163             }
164         }
165     }
166
167     /**
168      * Used to break the loop in the run() method.<p>
169      */

170     public void stopThread() {
171
172         try {
173             // give the logging thread a chance to read all remaining messages
174
Thread.sleep(1000);
175         } catch (Throwable JavaDoc t) {
176             // ignore
177
}
178         m_stopThread = true;
179     }
180
181 }
Popular Tags