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.report;
029
030import org.opencms.i18n.CmsMessageContainer;
031import org.opencms.main.CmsShell;
032
033import java.io.PrintStream;
034import java.util.Locale;
035
036/**
037 * Report class used for the shell.<p>
038 *
039 * It stores nothing. It just prints everything to <code>{@link System#out}</code><p>.
040 *
041 * @since 6.0.0
042 */
043public class CmsShellReport extends CmsPrintStreamReport {
044
045    /** Flag indicating if the job is still running. */
046    private boolean m_stillRunning;
047
048    /**
049     * Constructs a new report using the provided locale for the output language.<p>
050     *
051     * @param locale the locale to use for the output language
052     */
053    public CmsShellReport(Locale locale) {
054
055        super(getOutputStream(), locale, false);
056    }
057
058    /**
059     * Retrieves the appropriate output stream to write the report to.<p>
060     *
061     * If we are running in a shell context, this will return the shell's assigned output stream, otherwise System.out is returned.<p>
062     *
063     * @return the output stream to write the report to
064     */
065    public static PrintStream getOutputStream() {
066
067        CmsShell shell = CmsShell.getTopShell();
068        if (shell != null) {
069            return shell.getOut();
070        } else {
071            return System.out;
072        }
073
074    }
075
076    /**
077     * @see org.opencms.report.A_CmsReport#addError(java.lang.Object)
078     */
079    @Override
080    public void addError(Object obj) {
081
082        super.addError(obj);
083        CmsShell.setReportError();
084    }
085
086    /**
087     * @see org.opencms.report.I_CmsReport#getReportUpdate()
088     */
089    @Override
090    public synchronized String getReportUpdate() {
091
092        // to avoid premature interruption of the reporting thread (@see org.opencms.main.CmsThreadStore),
093        // a not empty string is returned, if there have been any print outs since the last check
094        if (m_stillRunning) {
095            m_stillRunning = false;
096            return "*";
097        }
098        return "";
099    }
100
101    /**
102     * @see org.opencms.report.A_CmsReport#print(org.opencms.i18n.CmsMessageContainer)
103     */
104    @Override
105    public void print(CmsMessageContainer container) {
106
107        super.print(container);
108        m_stillRunning = true;
109    }
110
111    /**
112     * @see org.opencms.report.A_CmsReport#print(org.opencms.i18n.CmsMessageContainer, int)
113     */
114    @Override
115    public void print(CmsMessageContainer container, int format) {
116
117        super.print(container, format);
118        m_stillRunning = true;
119    }
120
121    /**
122     * @see org.opencms.report.CmsPrintStreamReport#println()
123     */
124    @Override
125    public void println() {
126
127        super.println();
128        m_stillRunning = true;
129    }
130
131    /**
132     * @see org.opencms.report.A_CmsReport#println(org.opencms.i18n.CmsMessageContainer)
133     */
134    @Override
135    public void println(CmsMessageContainer container) {
136
137        super.println(container);
138        m_stillRunning = true;
139    }
140
141    /**
142     * @see org.opencms.report.A_CmsReport#println(org.opencms.i18n.CmsMessageContainer, int)
143     */
144    @Override
145    public void println(CmsMessageContainer container, int format) {
146
147        super.println(container, format);
148        m_stillRunning = true;
149    }
150
151    /**
152     * @see org.opencms.report.CmsPrintStreamReport#println(java.lang.Throwable)
153     */
154    @Override
155    public void println(Throwable t) {
156
157        super.println(t);
158        m_stillRunning = true;
159    }
160
161    /**
162     * @see org.opencms.report.A_CmsReport#printMessageWithParam(org.opencms.i18n.CmsMessageContainer, java.lang.Object)
163     */
164    @Override
165    public void printMessageWithParam(CmsMessageContainer container, Object param) {
166
167        super.printMessageWithParam(container, param);
168        m_stillRunning = true;
169    }
170
171    /**
172     * @see org.opencms.report.A_CmsReport#printMessageWithParam(int, int, org.opencms.i18n.CmsMessageContainer, java.lang.Object)
173     */
174    @Override
175    public void printMessageWithParam(int m, int n, CmsMessageContainer container, Object param) {
176
177        super.printMessageWithParam(m, n, container, param);
178        m_stillRunning = true;
179    }
180
181    /**
182     * @see org.opencms.report.CmsPrintStreamReport#start()
183     */
184    @Override
185    public void start() {
186
187        super.start();
188        m_stillRunning = true;
189    }
190
191}