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