001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.setup;
029
030import java.io.BufferedReader;
031import java.io.File;
032import java.io.FileWriter;
033import java.io.IOException;
034import java.io.InputStreamReader;
035import java.io.LineNumberReader;
036import java.io.PipedInputStream;
037import java.io.PipedOutputStream;
038import java.util.ArrayList;
039import java.util.List;
040
041/**
042 * Logging Thread which collects the output from CmsSetupThread and
043 * stores it in a Vector that the OpenCms setup wizard can read via
044 * the getMessages() method.<p>
045 *
046 * @since 6.0.0
047 */
048public class CmsSetupLoggingThread extends Thread {
049
050    /** The line number reader to print out the report. */
051    private LineNumberReader m_lineReader;
052
053    /** The file writer for logging. */
054    private FileWriter m_logWriter;
055
056    /** A list of messages. */
057    private List<String> m_messages;
058
059    /** The piped input. */
060    private PipedInputStream m_pipedIn;
061
062    /** The piped output. */
063    private PipedOutputStream m_pipedOut;
064
065    /** Signals if the logging thread is finished. */
066    private boolean m_stopThread;
067
068    /**
069     * Constructor.<p>
070     *
071     * @param pipedOut the output stream to write to
072     * @param log the file name to write the log to (if null, no log is written)
073     */
074    public CmsSetupLoggingThread(PipedOutputStream pipedOut, String log) {
075
076        super("OpenCms: Setup logging");
077
078        m_pipedOut = pipedOut;
079        m_messages = new ArrayList<String>();
080        m_stopThread = false;
081
082        if (log != null) {
083            try {
084                File logFile = new File(log);
085                if (logFile.exists()) {
086                    logFile.delete();
087                }
088                m_logWriter = new FileWriter(logFile);
089            } catch (Throwable t) {
090                m_logWriter = null;
091            }
092        } else {
093            m_logWriter = null;
094        }
095
096        try {
097            m_pipedIn = new PipedInputStream();
098            m_pipedIn.connect(m_pipedOut);
099            m_lineReader = new LineNumberReader(new BufferedReader(new InputStreamReader(m_pipedIn)));
100        } catch (Exception e) {
101            m_messages.add(e.toString());
102        }
103    }
104
105    /**
106     * Returns a Vector with the last collected log messages.<p>
107     *
108     * @return a Vector with the last collected log messages
109     */
110    public List<String> getMessages() {
111
112        return m_messages;
113    }
114
115    /**
116     * Returns <code>"true"</code> if the logging is finished.<p>
117     *
118     * @return <code>"true"</code> if the logging is finished
119     */
120    public boolean isFinished() {
121
122        return m_stopThread;
123    }
124
125    /**
126     * @see java.lang.Runnable#run()
127     */
128    @Override
129    public void run() {
130
131        int lineNr = 0;
132        int lastLineNr = -1;
133        String line = null;
134        while (!m_stopThread) {
135            lineNr = m_lineReader.getLineNumber();
136            try {
137                line = m_lineReader.readLine();
138            } catch (IOException e) {
139                // "Write end dead" IO exceptions can be ignored
140            }
141            if (line != null) {
142                if (lineNr > lastLineNr) {
143                    // supress multiple output of the same line after "Write end dead" IO exception
144                    String content = (lineNr + 1) + ":\t" + line;
145                    m_messages.add(content);
146                    lastLineNr = lineNr;
147                    if (m_logWriter != null) {
148                        try {
149                            m_logWriter.write(content + "\n");
150                        } catch (IOException e) {
151                            m_logWriter = null;
152                        }
153                    }
154                }
155            }
156        }
157        try {
158            m_pipedIn.close();
159        } catch (IOException e) {
160            // ignore
161        }
162        if (m_logWriter != null) {
163            try {
164                m_logWriter.close();
165            } catch (IOException e) {
166                m_logWriter = null;
167            }
168        }
169    }
170
171    /**
172     * Used to break the loop in the run() method.<p>
173     */
174    public void stopThread() {
175
176        try {
177            // give the logging thread a chance to read all remaining messages
178            Thread.sleep(1000);
179        } catch (Throwable t) {
180            // ignore
181        }
182        m_stopThread = true;
183    }
184
185}