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.publish;
029
030import org.opencms.main.CmsLog;
031import org.opencms.main.OpenCms;
032import org.opencms.staticexport.CmsAfterPublishStaticExportHandler;
033import org.opencms.util.CmsUUID;
034
035import java.util.Iterator;
036import java.util.Vector;
037
038import org.apache.commons.logging.Log;
039
040/**
041 * Publish job information bean.<p>
042 *
043 * @since 6.5.5
044 */
045public final class CmsPublishListenerCollection extends Vector<I_CmsPublishEventListener> {
046
047    /** The log object for this class. */
048    private static final Log LOG = CmsLog.getLog(CmsPublishListenerCollection.class);
049
050    /** serializable version id. */
051    private static final long serialVersionUID = -4945973010986412449L;
052
053    /** Publish engine. */
054    private transient CmsPublishEngine m_publishEngine;
055
056    /**
057     * Default constructor.<p>
058     *
059     * @param publishEngine the publish engine
060     */
061    protected CmsPublishListenerCollection(CmsPublishEngine publishEngine) {
062
063        m_publishEngine = publishEngine;
064    }
065
066    /**
067     * Fires an abort event to all listeners.<p>
068     *
069     * @param userId the id of the user that aborted the job
070     * @param publishJob the publish job that is going to be aborted.
071     */
072    protected void fireAbort(CmsUUID userId, CmsPublishJobEnqueued publishJob) {
073
074        if (LOG.isDebugEnabled()) {
075            LOG.debug(Messages.get().getBundle().key(Messages.LOG_PUBLISH_JOB_ABORT_0));
076        }
077        for (Iterator<I_CmsPublishEventListener> it = iterator(); it.hasNext();) {
078            I_CmsPublishEventListener listener = it.next();
079            try {
080                listener.onAbort(userId, publishJob);
081            } catch (Throwable t) {
082                // catch every thing including runtime exceptions
083                if (LOG.isErrorEnabled()) {
084                    LOG.error(
085                        Messages.get().getBundle().key(
086                            Messages.ERR_PUBLISH_JOB_ABORT_ERROR_1,
087                            listener.getClass().getName()),
088                        t);
089                }
090                if (publishJob.m_publishJob.getPublishReport() != null) {
091                    publishJob.m_publishJob.getPublishReport().println(t);
092                }
093            }
094        }
095        if ((userId != null) && userId.equals(publishJob.getUserId())) {
096            // prevent showing messages if the owner aborted the job by himself
097            return;
098        }
099        // popup the abort message
100        String msgText = Messages.get().getBundle(publishJob.getLocale()).key(
101            Messages.GUI_PUBLISH_JOB_ABORTED_2,
102            Long.valueOf(publishJob.getEnqueueTime()),
103            m_publishEngine.getUser(userId).getName());
104        m_publishEngine.sendMessage(publishJob.getUserId(), msgText, true);
105    }
106
107    /**
108     * Fires an enqueue event to all listeners.<p>
109     *
110     * @param publishJob the publish job that is going to be enqueued.
111     */
112    protected void fireEnqueued(CmsPublishJobBase publishJob) {
113
114        if (LOG.isDebugEnabled()) {
115            LOG.debug(Messages.get().getBundle().key(Messages.LOG_PUBLISH_JOB_ENQUEUE_0));
116        }
117        for (Iterator<I_CmsPublishEventListener> it = iterator(); it.hasNext();) {
118            I_CmsPublishEventListener listener = it.next();
119            try {
120                listener.onEnqueue(publishJob);
121            } catch (Throwable t) {
122                // catch every thing including runtime exceptions
123                if (LOG.isErrorEnabled()) {
124                    LOG.error(
125                        Messages.get().getBundle().key(
126                            Messages.ERR_PUBLISH_JOB_ENQUEUE_ERROR_1,
127                            listener.getClass().getName()),
128                        t);
129                }
130                if (publishJob.m_publishJob.getPublishReport() != null) {
131                    publishJob.m_publishJob.getPublishReport().println(t);
132                }
133            }
134        }
135    }
136
137    /**
138     * Fires a finish event to all listeners.<p>
139     *
140     * @param publishJob the publish job that has been finished.
141     */
142    protected void fireFinish(CmsPublishJobRunning publishJob) {
143
144        if (LOG.isDebugEnabled()) {
145            LOG.debug(Messages.get().getBundle().key(Messages.LOG_PUBLISH_JOB_FINISH_0));
146        }
147        for (Iterator<I_CmsPublishEventListener> it = iterator(); it.hasNext();) {
148            I_CmsPublishEventListener listener = it.next();
149            try {
150                listener.onFinish(publishJob);
151            } catch (Throwable t) {
152                // catch every thing including runtime exceptions
153                if (LOG.isErrorEnabled()) {
154                    LOG.error(
155                        Messages.get().getBundle().key(
156                            Messages.ERR_PUBLISH_JOB_FINISH_ERROR_1,
157                            listener.getClass().getName()),
158                        t);
159                }
160                if (publishJob.m_publishJob.getPublishReport() != null) {
161                    publishJob.m_publishJob.getPublishReport().println(t);
162                }
163            }
164        }
165        // popup the finish message
166        String msgText;
167        boolean hasError = false;
168        if (!publishJob.getReport().hasError() && !publishJob.getReport().hasWarning()) {
169            msgText = Messages.get().getBundle(publishJob.getLocale()).key(
170                Messages.GUI_PUBLISH_JOB_FINISHED_1,
171                Long.valueOf(publishJob.getEnqueueTime()));
172        } else {
173            hasError = true;
174            Object[] params = new Object[] {
175                Long.valueOf(publishJob.getEnqueueTime()),
176                Integer.valueOf(publishJob.getReport().getErrors().size()),
177                Integer.valueOf(publishJob.getReport().getWarnings().size())};
178            msgText = Messages.get().getBundle(publishJob.getLocale()).key(
179                Messages.GUI_PUBLISH_JOB_FINISHED_WITH_WARNS_3,
180                params);
181        }
182        m_publishEngine.sendMessage(publishJob.getUserId(), msgText, hasError);
183    }
184
185    /**
186     * Fires a remove event to all listeners.<p>
187     *
188     * @param publishJob the publish job that is going to be removed.
189     */
190    protected void fireRemove(CmsPublishJobFinished publishJob) {
191
192        if (LOG.isDebugEnabled()) {
193            LOG.debug(Messages.get().getBundle().key(Messages.LOG_PUBLISH_JOB_REMOVE_0));
194        }
195        for (Iterator<I_CmsPublishEventListener> it = iterator(); it.hasNext();) {
196            I_CmsPublishEventListener listener = it.next();
197            try {
198                listener.onRemove(publishJob);
199            } catch (Throwable t) {
200                // catch every thing including runtime exceptions
201                if (LOG.isErrorEnabled()) {
202                    LOG.error(
203                        Messages.get().getBundle().key(
204                            Messages.ERR_PUBLISH_JOB_REMOVE_ERROR_1,
205                            listener.getClass().getName()),
206                        t);
207                }
208                if (publishJob.m_publishJob.getPublishReport() != null) {
209                    publishJob.m_publishJob.getPublishReport().println(t);
210                }
211            }
212        }
213    }
214
215    /**
216     * Fires a start event to all listeners.<p>
217     *
218     * @param publishJob the publish job that is going to start.
219     */
220    protected void fireStart(CmsPublishJobEnqueued publishJob) {
221
222        if (LOG.isDebugEnabled()) {
223            LOG.debug(Messages.get().getBundle().key(Messages.LOG_PUBLISH_JOB_START_0));
224        }
225        for (Iterator<I_CmsPublishEventListener> it = iterator(); it.hasNext();) {
226            I_CmsPublishEventListener listener = it.next();
227            try {
228                listener.onStart(publishJob);
229            } catch (Throwable t) {
230                // catch every thing including runtime exceptions
231                if (LOG.isErrorEnabled()) {
232                    LOG.error(
233                        Messages.get().getBundle().key(
234                            Messages.ERR_PUBLISH_JOB_START_ERROR_1,
235                            listener.getClass().getName()),
236                        t);
237                }
238                if (publishJob.m_publishJob.getPublishReport() != null) {
239                    publishJob.m_publishJob.getPublishReport().println(t);
240                }
241            }
242        }
243        // popup the start message
244        boolean busyStart = ((System.currentTimeMillis() - publishJob.getEnqueueTime()) > 2000);
245        boolean bigJob = ((publishJob.getPublishList().size() > 25)
246            || (OpenCms.getStaticExportManager().getHandler() instanceof CmsAfterPublishStaticExportHandler));
247        if (busyStart || bigJob) {
248            String msgText = Messages.get().getBundle(publishJob.getLocale()).key(
249                Messages.GUI_PUBLISH_JOB_STARTED_1,
250                Long.valueOf(publishJob.getEnqueueTime()));
251            m_publishEngine.sendMessage(publishJob.getUserId(), msgText, false);
252        }
253    }
254}