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;
029
030import org.opencms.main.CmsLog;
031import org.opencms.main.CmsShell;
032import org.opencms.ui.report.CmsStreamReportWidget;
033
034import java.io.File;
035import java.io.FileInputStream;
036import java.io.FileNotFoundException;
037import java.io.PipedOutputStream;
038import java.io.PrintStream;
039import java.util.Arrays;
040
041public class CmsVaadinUpdateThread extends Thread {
042
043    /** System.out and System.err are redirected to this stream. */
044    private PipedOutputStream m_pipedOut;
045
046    /** The cms shell to import the workplace with. */
047    private CmsShell m_shell;
048
049    /** The report widget. */
050    private CmsStreamReportWidget m_reportWidget;
051
052    /** The output stream for the CmsShell. */
053    private PrintStream m_out;
054
055    /** The additional shell commands, i.e. the setup bean. */
056    private CmsUpdateBean m_updateBean;
057
058    /**
059     * Constructor.<p>
060     *
061     * @param updateBean the initialized update bean
062     */
063    public CmsVaadinUpdateThread(CmsUpdateBean updateBean, CmsStreamReportWidget reportWidget) {
064
065        super("OpenCms: Workplace update");
066
067        // store setup bean
068        m_updateBean = updateBean;
069        m_out = reportWidget.getStream();
070        m_reportWidget = reportWidget;
071    }
072
073    /**
074     * @see java.lang.Runnable#run()
075     */
076    @Override
077    public void run() {
078
079        try {
080
081            // create a shell that will start importing the workplace
082            m_shell = new CmsShell(
083                m_updateBean.getWebAppRfsPath() + "WEB-INF" + File.separator,
084                m_updateBean.getServletMapping(),
085                m_updateBean.getDefaultWebApplication(),
086                "${user}@${project}>",
087                Arrays.asList(m_updateBean),
088                m_out,
089                m_out,
090                false);
091
092            try {
093                try {
094                    if (CmsLog.INIT.isInfoEnabled()) {
095                        // log welcome message, the full package name is required because
096                        // two different Message classes are used
097                        CmsLog.INIT.info(
098                            org.opencms.main.Messages.get().getBundle().key(org.opencms.main.Messages.INIT_DOT_0));
099                        CmsLog.INIT.info(
100                            org.opencms.main.Messages.get().getBundle().key(org.opencms.main.Messages.INIT_DOT_0));
101                        CmsLog.INIT.info(
102                            org.opencms.main.Messages.get().getBundle().key(org.opencms.main.Messages.INIT_DOT_0));
103                        CmsLog.INIT.info(
104                            org.opencms.setup.Messages.get().getBundle().key(
105                                org.opencms.setup.Messages.INIT_WELCOME_UPDATE_0));
106                        CmsLog.INIT.info(
107                            org.opencms.setup.Messages.get().getBundle().key(
108                                org.opencms.setup.Messages.INIT_UPDATE_WORKPLACE_START_0));
109                        CmsLog.INIT.info(
110                            org.opencms.main.Messages.get().getBundle().key(org.opencms.main.Messages.INIT_DOT_0));
111                        CmsLog.INIT.info(
112                            org.opencms.main.Messages.get().getBundle().key(org.opencms.main.Messages.INIT_DOT_0));
113                        for (int i = 0; i < org.opencms.main.Messages.COPYRIGHT_BY_ALKACON.length; i++) {
114                            CmsLog.INIT.info(". " + org.opencms.main.Messages.COPYRIGHT_BY_ALKACON[i]);
115                        }
116                        CmsLog.INIT.info(
117                            org.opencms.main.Messages.get().getBundle().key(org.opencms.main.Messages.INIT_DOT_0));
118                        CmsLog.INIT.info(
119                            org.opencms.main.Messages.get().getBundle().key(org.opencms.main.Messages.INIT_DOT_0));
120                        CmsLog.INIT.info(
121                            org.opencms.main.Messages.get().getBundle().key(org.opencms.main.Messages.INIT_LINE_0));
122
123                    }
124                    m_shell.execute(
125                        new FileInputStream(
126                            new File(m_updateBean.getWebAppRfsPath() + CmsUpdateBean.FOLDER_UPDATE + "cmsupdate.txt")));
127                    if (CmsLog.INIT.isInfoEnabled()) {
128                        CmsLog.INIT.info(
129                            org.opencms.setup.Messages.get().getBundle().key(
130                                org.opencms.setup.Messages.INIT_UPDATE_WORKPLACE_FINISHED_0));
131                    }
132                } catch (FileNotFoundException e) {
133                    e.printStackTrace();
134                }
135
136                m_pipedOut.close();
137            } catch (Throwable t) {
138                // ignore
139            }
140        } finally {
141            m_reportWidget.finish();
142        }
143    }
144}