KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > report > A_CmsReportThread


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/report/A_CmsReportThread.java,v $
3  * Date : $Date: 2005/07/28 15:53:10 $
4  * Version: $Revision: 1.22 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.report;
33
34 import org.opencms.file.CmsObject;
35 import org.opencms.main.OpenCms;
36 import org.opencms.util.CmsUUID;
37
38 import java.util.List JavaDoc;
39 import java.util.Locale JavaDoc;
40
41 /**
42  * Provides a common Thread class for the reports.<p>
43  *
44  * @author Alexander Kandzior
45  *
46  * @version $Revision: 1.22 $
47  *
48  * @since 6.0.0
49  */

50 public abstract class A_CmsReportThread extends Thread JavaDoc implements I_CmsReportThread {
51
52     /** The OpenCms request context to use. */
53     private CmsObject m_cms;
54
55     /** Indicates if the thread was already checked by the grim reaper. */
56     private boolean m_doomed;
57
58     /** The id of this report. */
59     private CmsUUID m_id;
60
61     /** The report that belongs to the thread. */
62     private I_CmsReport m_report;
63
64     /** The time this report is running. */
65     private long m_starttime;
66
67     /**
68      * Constructs a new report Thread with the given name.<p>
69      *
70      * @param cms the current OpenCms context object
71      * @param name the name of the Thread
72      */

73     protected A_CmsReportThread(CmsObject cms, String JavaDoc name) {
74
75         super(OpenCms.getThreadStore().getThreadGroup(), name);
76         // report Threads are never daemon Threads
77
setDaemon(false);
78         // the session in the cms context must not be updated when it is used in a report
79
m_cms = cms;
80         m_cms.getRequestContext().setUpdateSessionEnabled(false);
81         // generate the report Thread id
82
m_id = new CmsUUID();
83         setName(name + " [" + m_id + "]");
84         // new Threads are not doomed
85
m_doomed = false;
86         // set start time
87
m_starttime = System.currentTimeMillis();
88         // add this Thread to the main Thread store
89
OpenCms.getThreadStore().addThread(this);
90     }
91
92     /**
93      * Adds an error object to the list of errors that occured during the report.<p>
94      *
95      * @param obj the error object
96      */

97     public void addError(Object JavaDoc obj) {
98
99         if (m_report != null) {
100             m_report.addError(obj);
101         }
102     }
103
104     /**
105      * Returns the error exception in case there was an error during the execution of
106      * this Thread, null otherwise.<p>
107      *
108      * @return the error exception in case there was an error, null otherwise
109      */

110     public Throwable JavaDoc getError() {
111
112         return null;
113     }
114
115     /**
116      * Returns a list of all errors that occured during the report.<p>
117      *
118      * @return an error list that occured during the report
119      */

120     public List JavaDoc getErrors() {
121
122         if (m_report != null) {
123             return m_report.getErrors();
124         } else {
125             return null;
126         }
127     }
128
129     /**
130      * Returns the part of the report that is ready for output.<p>
131      *
132      * @return the part of the report that is ready for output
133      */

134     public abstract String JavaDoc getReportUpdate();
135
136     /**
137      * Returns the time this report has been running.<p>
138      *
139      * @return the time this report has been running
140      */

141     public long getRuntime() {
142
143         if (m_doomed) {
144             return m_starttime;
145         } else {
146             return System.currentTimeMillis() - m_starttime;
147         }
148     }
149
150     /**
151      * Returns the OpenCms UUID of this report thread.<p>
152      *
153      * @return the OpenCms UUID of this report thread
154      */

155     public CmsUUID getUUID() {
156
157         return m_id;
158     }
159
160     /**
161      * Returns if the report generated an error output.<p>
162      *
163      * @return true if the report generated an error, otherwise false
164      */

165     public boolean hasError() {
166
167         if (m_report != null) {
168             return (m_report.getErrors().size() > 0);
169         } else {
170             return false;
171         }
172     }
173
174     /**
175      * Returns true if this thread is already "doomed" to be deleted.<p>
176      *
177      * A OpenCms deamon Thread (the "Grim Reaper") will collect all
178      * doomed Threads, i.e. threads that are not longer active for some
179      * time.<p>
180      *
181      * @return true if this thread is already "doomed" to be deleted
182      */

183     public synchronized boolean isDoomed() {
184
185         if (isAlive()) {
186             // as long as the Thread is still active it is never doomed
187
return false;
188         }
189         if (m_doomed) {
190             // not longer active, and already doomed, so rest in peace...
191
return true;
192         }
193         // condemn the Thread to be collected by the grim reaper next time
194
m_starttime = getRuntime();
195         m_doomed = true;
196         return false;
197     }
198
199     /**
200      * Returns the OpenCms context object this Thread is initialized with.<p>
201      *
202      * @return the OpenCms context object this Thread is initialized with
203      */

204     protected CmsObject getCms() {
205
206         return m_cms;
207     }
208
209     /**
210      * Returns the report where the output of this Thread is written to.<p>
211      *
212      * @return the report where the output of this Thread is written to
213      */

214     protected I_CmsReport getReport() {
215
216         return m_report;
217     }
218
219     /**
220      * Initialize a HTML report for this Thread.<p>
221      *
222      * @param locale the locale for the report output messages
223      */

224     protected void initHtmlReport(Locale JavaDoc locale) {
225
226         m_report = new CmsHtmlReport(locale, m_cms.getRequestContext().getSiteRoot());
227     }
228
229     /**
230      * Initialize a HTML report for this Thread.<p>
231      *
232      * This method is reserved for older report threads that still use
233      * XML templates to generate their output.<p>
234      *
235      * @param locale the locale for the report output messages
236      */

237     protected void initOldHtmlReport(Locale JavaDoc locale) {
238
239         m_report = new CmsHtmlReport(locale, m_cms.getRequestContext().getSiteRoot(), true);
240     }
241 }
242
Popular Tags