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, 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.setup.CmsUpdateBean;
031import org.opencms.ui.report.CmsStreamReportWidget;
032
033import java.io.PrintStream;
034
035public class CmsVaadinUpdateDBThread extends Thread {
036
037    /** Saves the System.err stream so it can be restored. */
038    public PrintStream m_tempErr;
039
040    /** System.out and System.err are redirected to this stream. */
041    private PrintStream m_out;
042
043    /** The additional shell commands, i.e. the update bean. */
044    private CmsUpdateBean m_updateBean;
045
046    /** Saves the System.out stream so it can be restored. */
047    private PrintStream m_tempOut;
048
049    private CmsStreamReportWidget m_reportWidget;
050
051    /**
052     * Constructor.<p>
053     *
054     * @param updateBean the initialized update bean
055     */
056    public CmsVaadinUpdateDBThread(CmsUpdateBean updateBean, CmsStreamReportWidget reportWidget) {
057
058        super("OpenCms: Database Update");
059
060        // store setup bean
061        m_updateBean = updateBean;
062        // init stream and logging thread
063        m_out = reportWidget.getStream();
064        m_reportWidget = reportWidget;
065    }
066
067    /**
068     * @see java.lang.Runnable#run()
069     */
070    @Override
071    public void run() {
072
073        // save the original out and err stream
074        m_tempOut = System.out;
075        m_tempErr = System.err;
076        try {
077            // redirect the streams
078            System.setOut(m_out);
079            System.setErr(m_out);
080
081            System.out.println("Starting DB Update... ");
082
083            CmsUpdateDBManager dbMan = new CmsUpdateDBManager();
084            try {
085                dbMan.initialize(m_updateBean);
086                dbMan.run();
087            } catch (Exception e) {
088                e.printStackTrace();
089            }
090
091            System.out.println("... DB Update finished.");
092        } finally {
093            // restore to the old streams
094            System.setOut(m_tempOut);
095            System.setErr(m_tempErr);
096            m_reportWidget.finish();
097        }
098    }
099
100}