001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/publish/CmsPublishListenerCollection.java,v $
003: * Date : $Date: 2008-02-27 12:05:27 $
004: * Version: $Revision: 1.6 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.publish;
033:
034: import org.opencms.main.CmsLog;
035: import org.opencms.main.OpenCms;
036: import org.opencms.staticexport.CmsAfterPublishStaticExportHandler;
037: import org.opencms.util.CmsUUID;
038:
039: import java.util.Iterator;
040: import java.util.Vector;
041:
042: import org.apache.commons.logging.Log;
043:
044: /**
045: * Publish job information bean.<p>
046: *
047: * @author Michael Moossen
048: *
049: * @version $Revision: 1.6 $
050: *
051: * @since 6.5.5
052: */
053: public final class CmsPublishListenerCollection extends Vector {
054:
055: /** The log object for this class. */
056: private static final Log LOG = CmsLog
057: .getLog(CmsPublishListenerCollection.class);
058:
059: /** serializable version id. */
060: private static final long serialVersionUID = -4945973010986412449L;
061:
062: /** Publish engine. */
063: private transient CmsPublishEngine m_publishEngine;
064:
065: /**
066: * Default constructor.<p>
067: *
068: * @param publishEngine the publish engine
069: */
070: protected CmsPublishListenerCollection(
071: CmsPublishEngine publishEngine) {
072:
073: m_publishEngine = publishEngine;
074: }
075:
076: /**
077: * Fires an abort event to all listeners.<p>
078: *
079: * @param userId the id of the user that aborted the job
080: * @param publishJob the publish job that is going to be aborted.
081: */
082: protected void fireAbort(CmsUUID userId,
083: CmsPublishJobEnqueued publishJob) {
084:
085: if (LOG.isDebugEnabled()) {
086: LOG.debug(Messages.get().getBundle().key(
087: Messages.LOG_PUBLISH_JOB_ABORT_0));
088: }
089: for (Iterator it = iterator(); it.hasNext();) {
090: I_CmsPublishEventListener listener = (I_CmsPublishEventListener) it
091: .next();
092: try {
093: listener.onAbort(userId, publishJob);
094: } catch (Throwable t) {
095: // catch every thing including runtime exceptions
096: if (LOG.isErrorEnabled()) {
097: LOG.error(Messages.get().getBundle().key(
098: Messages.ERR_PUBLISH_JOB_ABORT_ERROR_1,
099: listener.getClass().getName()), t);
100: }
101: if (publishJob.m_publishJob.getPublishReport() != null) {
102: publishJob.m_publishJob.getPublishReport().println(
103: t);
104: }
105: }
106: }
107: if ((userId != null) && userId.equals(publishJob.getUserId())) {
108: // prevent showing messages if the owner aborted the job by himself
109: return;
110: }
111: // popup the abort message
112: String msgText = Messages.get().getBundle(
113: publishJob.getLocale()).key(
114: Messages.GUI_PUBLISH_JOB_ABORTED_2,
115: new Long(publishJob.getEnqueueTime()),
116: m_publishEngine.getUser(userId).getName());
117: m_publishEngine.sendMessage(publishJob.getUserId(), msgText,
118: true);
119: }
120:
121: /**
122: * Fires an enqueue event to all listeners.<p>
123: *
124: * @param publishJob the publish job that is going to be enqueued.
125: */
126: protected void fireEnqueued(CmsPublishJobBase publishJob) {
127:
128: if (LOG.isDebugEnabled()) {
129: LOG.debug(Messages.get().getBundle().key(
130: Messages.LOG_PUBLISH_JOB_ENQUEUE_0));
131: }
132: for (Iterator it = iterator(); it.hasNext();) {
133: I_CmsPublishEventListener listener = (I_CmsPublishEventListener) it
134: .next();
135: try {
136: listener.onEnqueue(publishJob);
137: } catch (Throwable t) {
138: // catch every thing including runtime exceptions
139: if (LOG.isErrorEnabled()) {
140: LOG.error(Messages.get().getBundle().key(
141: Messages.ERR_PUBLISH_JOB_ENQUEUE_ERROR_1,
142: listener.getClass().getName()), t);
143: }
144: if (publishJob.m_publishJob.getPublishReport() != null) {
145: publishJob.m_publishJob.getPublishReport().println(
146: t);
147: }
148: }
149: }
150: }
151:
152: /**
153: * Fires a finish event to all listeners.<p>
154: *
155: * @param publishJob the publish job that has been finished.
156: */
157: protected void fireFinish(CmsPublishJobRunning publishJob) {
158:
159: if (LOG.isDebugEnabled()) {
160: LOG.debug(Messages.get().getBundle().key(
161: Messages.LOG_PUBLISH_JOB_FINISH_0));
162: }
163: for (Iterator it = iterator(); it.hasNext();) {
164: I_CmsPublishEventListener listener = (I_CmsPublishEventListener) it
165: .next();
166: try {
167: listener.onFinish(publishJob);
168: } catch (Throwable t) {
169: // catch every thing including runtime exceptions
170: if (LOG.isErrorEnabled()) {
171: LOG.error(Messages.get().getBundle().key(
172: Messages.ERR_PUBLISH_JOB_FINISH_ERROR_1,
173: listener.getClass().getName()), t);
174: }
175: if (publishJob.m_publishJob.getPublishReport() != null) {
176: publishJob.m_publishJob.getPublishReport().println(
177: t);
178: }
179: }
180: }
181: // popup the finish message
182: String msgText;
183: boolean hasError = false;
184: if (!publishJob.getReport().hasError()
185: && !publishJob.getReport().hasWarning()) {
186: msgText = Messages.get().getBundle(publishJob.getLocale())
187: .key(Messages.GUI_PUBLISH_JOB_FINISHED_1,
188: new Long(publishJob.getEnqueueTime()));
189: } else {
190: hasError = true;
191: Object[] params = new Object[] {
192: new Long(publishJob.getEnqueueTime()),
193: new Integer(publishJob.getReport().getErrors()
194: .size()),
195: new Integer(publishJob.getReport().getWarnings()
196: .size()) };
197: msgText = Messages
198: .get()
199: .getBundle(publishJob.getLocale())
200: .key(
201: Messages.GUI_PUBLISH_JOB_FINISHED_WITH_WARNS_3,
202: params);
203: }
204: m_publishEngine.sendMessage(publishJob.getUserId(), msgText,
205: hasError);
206: }
207:
208: /**
209: * Fires a remove event to all listeners.<p>
210: *
211: * @param publishJob the publish job that is going to be removed.
212: */
213: protected void fireRemove(CmsPublishJobFinished publishJob) {
214:
215: if (LOG.isDebugEnabled()) {
216: LOG.debug(Messages.get().getBundle().key(
217: Messages.LOG_PUBLISH_JOB_REMOVE_0));
218: }
219: for (Iterator it = iterator(); it.hasNext();) {
220: I_CmsPublishEventListener listener = (I_CmsPublishEventListener) it
221: .next();
222: try {
223: listener.onRemove(publishJob);
224: } catch (Throwable t) {
225: // catch every thing including runtime exceptions
226: if (LOG.isErrorEnabled()) {
227: LOG.error(Messages.get().getBundle().key(
228: Messages.ERR_PUBLISH_JOB_REMOVE_ERROR_1,
229: listener.getClass().getName()), t);
230: }
231: if (publishJob.m_publishJob.getPublishReport() != null) {
232: publishJob.m_publishJob.getPublishReport().println(
233: t);
234: }
235: }
236: }
237: }
238:
239: /**
240: * Fires a start event to all listeners.<p>
241: *
242: * @param publishJob the publish job that is going to start.
243: */
244: protected void fireStart(CmsPublishJobEnqueued publishJob) {
245:
246: if (LOG.isDebugEnabled()) {
247: LOG.debug(Messages.get().getBundle().key(
248: Messages.LOG_PUBLISH_JOB_START_0));
249: }
250: for (Iterator it = iterator(); it.hasNext();) {
251: I_CmsPublishEventListener listener = (I_CmsPublishEventListener) it
252: .next();
253: try {
254: listener.onStart(publishJob);
255: } catch (Throwable t) {
256: // catch every thing including runtime exceptions
257: if (LOG.isErrorEnabled()) {
258: LOG.error(Messages.get().getBundle().key(
259: Messages.ERR_PUBLISH_JOB_START_ERROR_1,
260: listener.getClass().getName()), t);
261: }
262: if (publishJob.m_publishJob.getPublishReport() != null) {
263: publishJob.m_publishJob.getPublishReport().println(
264: t);
265: }
266: }
267: }
268: // popup the start message
269: boolean busyStart = ((System.currentTimeMillis() - publishJob
270: .getEnqueueTime()) > 2000);
271: boolean bigJob = ((publishJob.getPublishList().size() > 25) || (OpenCms
272: .getStaticExportManager().getHandler() instanceof CmsAfterPublishStaticExportHandler));
273: if (busyStart || bigJob) {
274: String msgText = Messages.get().getBundle(
275: publishJob.getLocale()).key(
276: Messages.GUI_PUBLISH_JOB_STARTED_1,
277: new Long(publishJob.getEnqueueTime()));
278: m_publishEngine.sendMessage(publishJob.getUserId(),
279: msgText, false);
280: }
281: }
282: }
|