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.db;
029
030import org.opencms.main.CmsLog;
031import org.opencms.main.CmsSystemInfo;
032import org.opencms.setup.CmsSetupLoggingThread;
033import org.opencms.setup.CmsUpdateBean;
034
035import java.io.IOException;
036import java.io.OutputStream;
037import java.io.PipedOutputStream;
038import java.io.PrintStream;
039
040/**
041 * Used for the workplace setup in the OpenCms setup wizard.<p>
042 *
043 * @since 6.0.0
044 */
045public class CmsUpdateDBThread extends Thread {
046
047    /** Saves the System.err stream so it can be restored. */
048    public PrintStream m_tempErr;
049
050    /** Logging thread. */
051    private CmsSetupLoggingThread m_loggingThread;
052
053    /** System.out and System.err are redirected to this stream. */
054    private PipedOutputStream m_pipedOut;
055
056    /** The additional shell commands, i.e. the update bean. */
057    private CmsUpdateBean m_updateBean;
058
059    /** Saves the System.out stream so it can be restored. */
060    private PrintStream m_tempOut;
061
062    /**
063     * Constructor.<p>
064     *
065     * @param updateBean the initialized update bean
066     */
067    public CmsUpdateDBThread(CmsUpdateBean updateBean) {
068
069        super("OpenCms: Database Update");
070
071        // store setup bean
072        m_updateBean = updateBean;
073        // init stream and logging thread
074        m_pipedOut = new PipedOutputStream();
075        m_loggingThread = new CmsSetupLoggingThread(
076            m_pipedOut,
077            m_updateBean.getWebAppRfsPath() + CmsSystemInfo.FOLDER_WEBINF + CmsLog.FOLDER_LOGS + "db-update.log");
078    }
079
080    /**
081     * Returns the logging thread.<p>
082     *
083     * @return the logging thread
084     */
085    public CmsSetupLoggingThread getLoggingThread() {
086
087        return m_loggingThread;
088    }
089
090    public OutputStream getOut() {
091
092        return m_pipedOut;
093    }
094
095    /**
096     * Returns the status of the logging thread.<p>
097     *
098     * @return the status of the logging thread
099     */
100    public boolean isFinished() {
101
102        return m_loggingThread.isFinished();
103    }
104
105    /**
106     * Kills this Thread as well as the included logging Thread.<p>
107     */
108    public void kill() {
109
110        if (m_loggingThread != null) {
111            m_loggingThread.stopThread();
112        }
113        m_updateBean = null;
114    }
115
116    /**
117     * @see java.lang.Runnable#run()
118     */
119    @Override
120    public void run() {
121
122        run(new PrintStream(m_pipedOut));
123    }
124
125    /**
126     * @see java.lang.Runnable#run()
127     */
128
129    public void run(PrintStream out) {
130
131        // save the original out and err stream
132        m_tempOut = System.out;
133        m_tempErr = System.err;
134        try {
135            // redirect the streams
136            System.setOut(out);
137            System.setErr(out);
138
139            // start the logging thread
140            m_loggingThread.start();
141
142            System.out.println("Starting DB Update... ");
143
144            CmsUpdateDBManager dbMan = new CmsUpdateDBManager();
145            try {
146                dbMan.initialize(m_updateBean);
147                dbMan.run();
148            } catch (Exception e) {
149                e.printStackTrace();
150            }
151
152            System.out.println("... DB Update finished.");
153        } finally {
154            kill();
155            if (m_pipedOut != null) {
156                try {
157                    m_pipedOut.close();
158                } catch (IOException e) {
159                    // ignore
160                }
161            }
162            // restore to the old streams
163            System.setOut(m_tempOut);
164            System.setErr(m_tempErr);
165        }
166    }
167
168}