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