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 org.opencms.main.CmsLog;
031import org.opencms.main.CmsShell;
032
033import java.io.File;
034import java.io.FileInputStream;
035import java.io.FileNotFoundException;
036import java.io.PipedOutputStream;
037import java.io.PrintStream;
038
039/**
040 * Used for the OpenCms workplace update wizard.<p>
041 *
042 * @since 6.0.0
043 */
044public class CmsUpdateThread extends Thread {
045
046    /** Saves the System.err stream so it can be restored. */
047    public PrintStream m_tempErr;
048
049    /** Logging thread. */
050    private CmsSetupLoggingThread m_loggingThread;
051
052    /** System.out and System.err are redirected to this stream. */
053    private PipedOutputStream m_pipedOut;
054
055    /** The cms shell to import the workplace with. */
056    private CmsShell m_shell;
057
058    /** Saves the System.out stream so it can be restored. */
059    private PrintStream m_tempOut;
060
061    /** The additional shell commands, i.e. the setup bean. */
062    private CmsUpdateBean m_updateBean;
063
064    /**
065     * Constructor.<p>
066     *
067     * @param updateBean the initialized update bean
068     */
069    public CmsUpdateThread(CmsUpdateBean updateBean) {
070
071        super("OpenCms: Workplace update");
072
073        // store setup bean
074        m_updateBean = updateBean;
075        // init stream and logging thread
076        m_pipedOut = new PipedOutputStream();
077        m_loggingThread = new CmsSetupLoggingThread(m_pipedOut, m_updateBean.getLogName());
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    /**
091     * Returns the status of the logging thread.<p>
092     *
093     * @return the status of the logging thread
094     */
095    public boolean isFinished() {
096
097        return m_loggingThread.isFinished();
098    }
099
100    /**
101     * Kills this Thread as well as the included logging Thread.<p>
102     */
103    public void kill() {
104
105        if (m_shell != null) {
106            m_shell.exit();
107        }
108        if (m_loggingThread != null) {
109            m_loggingThread.stopThread();
110        }
111        m_shell = null;
112        m_updateBean = null;
113    }
114
115    /**
116     * Write somthing to System.out during setup.<p>
117     *
118     * @param str the string to write
119     */
120    public void printToStdOut(String str) {
121
122        m_tempOut.println(str);
123    }
124
125    /**
126     * @see java.lang.Runnable#run()
127     */
128    @Override
129    public void run() {
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(new PrintStream(m_pipedOut));
137            System.setErr(new PrintStream(m_pipedOut));
138
139            // start the logging thread
140            m_loggingThread.start();
141
142            // create a shell that will start importing the workplace
143            m_shell = new CmsShell(
144                m_updateBean.getWebAppRfsPath() + "WEB-INF" + File.separator,
145                m_updateBean.getServletMapping(),
146                m_updateBean.getDefaultWebApplication(),
147                "${user}@${project}>",
148                m_updateBean);
149
150            try {
151                try {
152                    if (CmsLog.INIT.isInfoEnabled()) {
153                        // log welcome message, the full package name is required because
154                        // two different Message classes are used
155                        CmsLog.INIT.info(
156                            org.opencms.main.Messages.get().getBundle().key(org.opencms.main.Messages.INIT_DOT_0));
157                        CmsLog.INIT.info(
158                            org.opencms.main.Messages.get().getBundle().key(org.opencms.main.Messages.INIT_DOT_0));
159                        CmsLog.INIT.info(
160                            org.opencms.main.Messages.get().getBundle().key(org.opencms.main.Messages.INIT_DOT_0));
161                        CmsLog.INIT.info(
162                            org.opencms.setup.Messages.get().getBundle().key(
163                                org.opencms.setup.Messages.INIT_WELCOME_UPDATE_0));
164                        CmsLog.INIT.info(
165                            org.opencms.setup.Messages.get().getBundle().key(
166                                org.opencms.setup.Messages.INIT_UPDATE_WORKPLACE_START_0));
167                        CmsLog.INIT.info(
168                            org.opencms.main.Messages.get().getBundle().key(org.opencms.main.Messages.INIT_DOT_0));
169                        CmsLog.INIT.info(
170                            org.opencms.main.Messages.get().getBundle().key(org.opencms.main.Messages.INIT_DOT_0));
171                        for (int i = 0; i < org.opencms.main.Messages.COPYRIGHT_BY_ALKACON.length; i++) {
172                            CmsLog.INIT.info(". " + org.opencms.main.Messages.COPYRIGHT_BY_ALKACON[i]);
173                        }
174                        CmsLog.INIT.info(
175                            org.opencms.main.Messages.get().getBundle().key(org.opencms.main.Messages.INIT_DOT_0));
176                        CmsLog.INIT.info(
177                            org.opencms.main.Messages.get().getBundle().key(org.opencms.main.Messages.INIT_DOT_0));
178                        CmsLog.INIT.info(
179                            org.opencms.main.Messages.get().getBundle().key(org.opencms.main.Messages.INIT_LINE_0));
180
181                    }
182                    m_shell.execute(
183                        new FileInputStream(
184                            new File(m_updateBean.getWebAppRfsPath() + CmsUpdateBean.FOLDER_UPDATE + "cmsupdate.txt")));
185                    if (CmsLog.INIT.isInfoEnabled()) {
186                        CmsLog.INIT.info(
187                            org.opencms.setup.Messages.get().getBundle().key(
188                                org.opencms.setup.Messages.INIT_UPDATE_WORKPLACE_FINISHED_0));
189                    }
190                } catch (FileNotFoundException e) {
191                    e.printStackTrace();
192                }
193                // stop the logging thread
194                kill();
195                m_pipedOut.close();
196            } catch (Throwable t) {
197                // ignore
198            }
199        } finally {
200            // restore to the old streams
201            System.setOut(m_tempOut);
202            System.setErr(m_tempErr);
203        }
204    }
205}