001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/report/A_CmsReportThread.java,v $
003: * Date : $Date: 2008-02-27 12:05:41 $
004: * Version: $Revision: 1.25 $
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.report;
033:
034: import org.opencms.file.CmsObject;
035: import org.opencms.main.OpenCms;
036: import org.opencms.util.CmsUUID;
037:
038: import java.util.List;
039: import java.util.Locale;
040:
041: /**
042: * Provides a common Thread class for the reports.<p>
043: *
044: * @author Alexander Kandzior
045: *
046: * @version $Revision: 1.25 $
047: *
048: * @since 6.0.0
049: */
050: public abstract class A_CmsReportThread extends Thread implements
051: I_CmsReportThread {
052:
053: /** The OpenCms request context to use. */
054: private CmsObject m_cms;
055:
056: /** Indicates if the thread was already checked by the grim reaper. */
057: private boolean m_doomed;
058:
059: /** The id of this report. */
060: private CmsUUID m_id;
061:
062: /** The report that belongs to the thread. */
063: private I_CmsReport m_report;
064:
065: /** The time this report is running. */
066: private long m_starttime;
067:
068: /**
069: * Constructs a new report Thread with the given name.<p>
070: *
071: * @param cms the current OpenCms context object
072: * @param name the name of the Thread
073: */
074: protected A_CmsReportThread(CmsObject cms, String name) {
075:
076: super (OpenCms.getThreadStore().getThreadGroup(), name);
077: // report Threads are never daemon Threads
078: setDaemon(false);
079: // the session in the cms context must not be updated when it is used in a report
080: m_cms = cms;
081: m_cms.getRequestContext().setUpdateSessionEnabled(false);
082: // generate the report Thread id
083: m_id = new CmsUUID();
084: setName(name + " [" + m_id + "]");
085: // new Threads are not doomed
086: m_doomed = false;
087: // set start time
088: m_starttime = System.currentTimeMillis();
089: // add this Thread to the main Thread store
090: OpenCms.getThreadStore().addThread(this );
091: }
092:
093: /**
094: * Adds an error object to the list of errors that occured during the report.<p>
095: *
096: * @param obj the error object
097: */
098: public void addError(Object obj) {
099:
100: if (getReport() != null) {
101: getReport().addError(obj);
102: }
103: }
104:
105: /**
106: * Returns the error exception in case there was an error during the execution of
107: * this Thread, null otherwise.<p>
108: *
109: * @return the error exception in case there was an error, null otherwise
110: */
111: public Throwable getError() {
112:
113: return null;
114: }
115:
116: /**
117: * Returns a list of all errors that occured during the report.<p>
118: *
119: * @return an error list that occured during the report
120: */
121: public List getErrors() {
122:
123: if (getReport() != null) {
124: return getReport().getErrors();
125: } else {
126: return null;
127: }
128: }
129:
130: /**
131: * Returns the part of the report that is ready for output.<p>
132: *
133: * @return the part of the report that is ready for output
134: */
135: public abstract String getReportUpdate();
136:
137: /**
138: * Returns the time this report has been running.<p>
139: *
140: * @return the time this report has been running
141: */
142: public synchronized long getRuntime() {
143:
144: if (m_doomed) {
145: return m_starttime;
146: } else {
147: return System.currentTimeMillis() - m_starttime;
148: }
149: }
150:
151: /**
152: * Returns the OpenCms UUID of this report thread.<p>
153: *
154: * @return the OpenCms UUID of this report thread
155: */
156: public CmsUUID getUUID() {
157:
158: return m_id;
159: }
160:
161: /**
162: * Returns if the report generated an error output.<p>
163: *
164: * @return true if the report generated an error, otherwise false
165: */
166: public boolean hasError() {
167:
168: if (getReport() != null) {
169: return (getReport().getErrors().size() > 0);
170: } else {
171: return false;
172: }
173: }
174:
175: /**
176: * Returns true if this thread is already "doomed" to be deleted.<p>
177: *
178: * A OpenCms deamon Thread (the "Grim Reaper") will collect all
179: * doomed Threads, i.e. threads that are not longer active for some
180: * time.<p>
181: *
182: * @return true if this thread is already "doomed" to be deleted
183: */
184: public synchronized boolean isDoomed() {
185:
186: if (isAlive()) {
187: // as long as the Thread is still active it is never doomed
188: return false;
189: }
190: if (m_doomed) {
191: // not longer active, and already doomed, so rest in peace...
192: return true;
193: }
194: // condemn the Thread to be collected by the grim reaper next time
195: m_starttime = getRuntime();
196: m_doomed = true;
197: return false;
198: }
199:
200: /**
201: * Returns the OpenCms context object this Thread is initialized with.<p>
202: *
203: * @return the OpenCms context object this Thread is initialized with
204: */
205: protected CmsObject getCms() {
206:
207: return m_cms;
208: }
209:
210: /**
211: * Returns the report where the output of this Thread is written to.<p>
212: *
213: * @return the report where the output of this Thread is written to
214: */
215: protected I_CmsReport getReport() {
216:
217: return m_report;
218: }
219:
220: /**
221: * Initialize a HTML report for this Thread.<p>
222: *
223: * @param locale the locale for the report output messages
224: */
225: protected void initHtmlReport(Locale locale) {
226:
227: m_report = new CmsHtmlReport(locale, m_cms.getRequestContext()
228: .getSiteRoot());
229: }
230:
231: /**
232: * Initialize a HTML report for this Thread.<p>
233: *
234: * This method is reserved for older report threads that still use
235: * XML templates to generate their output.<p>
236: *
237: * @param locale the locale for the report output messages
238: */
239: protected void initOldHtmlReport(Locale locale) {
240:
241: m_report = new CmsHtmlReport(locale, m_cms.getRequestContext()
242: .getSiteRoot(), true, false);
243: }
244: }
|