001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/publish/CmsPublishHistory.java,v $
003: * Date : $Date: 2008-02-27 12:05:27 $
004: * Version: $Revision: 1.5 $
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.db.CmsDbContext;
035: import org.opencms.db.CmsDriverManager;
036: import org.opencms.main.CmsException;
037: import org.opencms.main.CmsLog;
038: import org.opencms.main.OpenCms;
039:
040: import java.util.ArrayList;
041: import java.util.Collections;
042: import java.util.Iterator;
043: import java.util.List;
044:
045: import org.apache.commons.collections.Buffer;
046: import org.apache.commons.collections.BufferUtils;
047: import org.apache.commons.collections.buffer.CircularFifoBuffer;
048: import org.apache.commons.collections.buffer.TypedBuffer;
049: import org.apache.commons.logging.Log;
050:
051: /**
052: * List of already finished publish jobs.<p>
053: *
054: * @author Michael Moossen
055: *
056: * @version $Revision: 1.5 $
057: *
058: * @since 6.5.5
059: */
060: public class CmsPublishHistory {
061:
062: /** The log object for this class. */
063: protected static final Log LOG = CmsLog
064: .getLog(CmsPublishHistory.class);
065:
066: /** The publish engine. */
067: protected final CmsPublishEngine m_publishEngine;
068:
069: /**
070: * Default constructor.<p>
071: *
072: * @param publishEngine the publish engine instance
073: */
074: protected CmsPublishHistory(final CmsPublishEngine publishEngine) {
075:
076: m_publishEngine = publishEngine;
077: }
078:
079: /**
080: * Returns (and initializes) the queue.<p>
081: *
082: * @param size the history size
083: *
084: * @return the queue buffer
085: */
086: public static Buffer getQueue(int size) {
087:
088: if (CmsLog.INIT.isInfoEnabled()) {
089: CmsLog.INIT.info(Messages.get().getBundle().key(
090: Messages.INIT_PUBLISH_HISTORY_SIZE_SET_1,
091: new Integer(size)));
092: }
093:
094: return BufferUtils.synchronizedBuffer(TypedBuffer.decorate(
095: new CircularFifoBuffer(size) {
096:
097: /** The serialization version id constant. */
098: private static final long serialVersionUID = -6257542123241183114L;
099:
100: /**
101: * Called when the queue is full to remove the oldest element.<p>
102: *
103: * @see org.apache.commons.collections.buffer.BoundedFifoBuffer#remove()
104: */
105: public Object remove() {
106:
107: CmsPublishJobInfoBean publishJob = (CmsPublishJobInfoBean) super
108: .remove();
109: try {
110: OpenCms.getPublishManager().getEngine()
111: .getPublishHistory().remove(
112: publishJob);
113: } catch (CmsException exc) {
114: if (LOG.isErrorEnabled()) {
115: LOG.error(exc.getLocalizedMessage(),
116: exc);
117: }
118: }
119: return publishJob;
120: }
121: }, CmsPublishJobInfoBean.class));
122: }
123:
124: /**
125: * Adds the given publish job to the list.<p>
126: *
127: * @param publishJob the publish job object to add
128: *
129: * @throws CmsException if something goes wrong
130: */
131: protected void add(CmsPublishJobInfoBean publishJob)
132: throws CmsException {
133:
134: OpenCms.getMemoryMonitor().cachePublishJobInHistory(publishJob);
135: // write job to db if necessary
136: if (OpenCms.getMemoryMonitor().requiresPersistency()) {
137: CmsDbContext dbc = m_publishEngine.getDbContextFactory()
138: .getDbContext();
139: try {
140: m_publishEngine.getDriverManager().writePublishJob(dbc,
141: publishJob);
142: // additionally, write the publish report
143: m_publishEngine.getDriverManager().writePublishReport(
144: dbc, publishJob);
145: // delete publish list of started job
146: m_publishEngine.getDriverManager().deletePublishList(
147: dbc, publishJob.getPublishHistoryId());
148: } finally {
149: dbc.clear();
150: }
151: }
152: }
153:
154: /**
155: * Returns an unmodifiable list representation of this list.<p>
156: *
157: * @return a list of {@link CmsPublishJobFinished} objects
158: */
159: protected List asList() {
160:
161: List cachedPublishJobs = OpenCms.getMemoryMonitor()
162: .getAllCachedPublishJobsInHistory();
163: List result = new ArrayList(cachedPublishJobs.size());
164: Iterator it = cachedPublishJobs.iterator();
165: while (it.hasNext()) {
166: CmsPublishJobInfoBean publishJob = (CmsPublishJobInfoBean) it
167: .next();
168: result.add(new CmsPublishJobFinished(publishJob));
169: }
170: return Collections.unmodifiableList(result);
171: }
172:
173: /**
174: * Initializes the internal FIFO queue with publish jobs from the database.<p>
175: */
176: protected void initialize() {
177:
178: CmsDriverManager driverManager = m_publishEngine
179: .getDriverManager();
180: CmsDbContext dbc = m_publishEngine.getDbContextFactory()
181: .getDbContext();
182:
183: try {
184: OpenCms.getMemoryMonitor().flushPublishJobHistory();
185: // read all finished published jobs from the database
186: List publishJobs = driverManager.readPublishJobs(dbc, 1L,
187: Long.MAX_VALUE);
188: for (Iterator i = publishJobs.iterator(); i.hasNext();) {
189: CmsPublishJobInfoBean job = (CmsPublishJobInfoBean) i
190: .next();
191: OpenCms.getMemoryMonitor()
192: .cachePublishJobInHistory(job);
193: }
194: } catch (CmsException exc) {
195: if (LOG.isErrorEnabled()) {
196: LOG.error(exc.getLocalizedMessage(), exc);
197: }
198: } finally {
199: dbc.clear();
200: }
201: }
202:
203: /**
204: * Removes the given job from the list.<p>
205: *
206: * @param publishJob the publish job to remove
207: *
208: * @throws CmsException if something goes wrong
209: */
210: protected void remove(CmsPublishJobInfoBean publishJob)
211: throws CmsException {
212:
213: OpenCms.getMemoryMonitor().uncachePublishJobInHistory(
214: publishJob);
215: // delete job from db if necessary
216: if (OpenCms.getMemoryMonitor().requiresPersistency()) {
217: CmsDbContext dbc = m_publishEngine.getDbContextFactory()
218: .getDbContext();
219: try {
220: OpenCms.getPublishManager().getEngine()
221: .getDriverManager().deletePublishJob(dbc,
222: publishJob.getPublishHistoryId());
223: } finally {
224: dbc.clear();
225: }
226: }
227:
228: m_publishEngine.publishJobRemoved(publishJob);
229: }
230: }
|