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