KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > main > CmsThreadStore


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/main/CmsThreadStore.java,v $
3  * Date : $Date: 2006/03/27 14:52:27 $
4  * Version: $Revision: 1.16 $
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.main;
33
34 import org.opencms.report.A_CmsReportThread;
35 import org.opencms.util.CmsUUID;
36
37 import java.util.HashSet JavaDoc;
38 import java.util.Hashtable JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.Map JavaDoc;
41 import java.util.Set JavaDoc;
42
43 import org.apache.commons.logging.Log;
44
45 /**
46  * The OpenCms "Grim Reaper" thread store where all system Threads are maintained.<p>
47  *
48  * This thread executes all 60 seconds and checks if report threads are still active.
49  * A report thread usually waits for a user to get the contents written to the report.
50  * However, if the user does not request the reports content (e.g. because the
51  * browser was closed), then the report thread becomes abandoned. This Grim Reaper
52  * will collect all such abandoned report threads and remove them after further
53  * 60 seconds.<p>
54  *
55  * Moreover, the Grim Reaper checks for all invalid user sessions that have times out for
56  * 5 or more minutes, and removes them as well.<p>
57  *
58  * @author Alexander Kandzior
59  *
60  * @version $Revision: 1.16 $
61  *
62  * @since 6.0.0
63  */

64 public class CmsThreadStore extends Thread JavaDoc {
65
66     /** The log object for this class. */
67     private static final Log LOG = CmsLog.getLog(CmsThreadStore.class);
68
69     /** Indicates that this thread store is alive. */
70     private boolean m_alive;
71
72     /** A map to store all system Threads in. */
73     private Map JavaDoc m_threads;
74
75     /**
76      * Hides the public constructor.<p>
77      */

78     protected CmsThreadStore() {
79
80         super(new ThreadGroup JavaDoc("OpenCms Thread Store"), "OpenCms: Grim Reaper");
81         setDaemon(true);
82         // Hashtable is still the most efficient form of a synchronized HashMap
83
m_threads = new Hashtable JavaDoc();
84         m_alive = true;
85         start();
86     }
87
88     /**
89      * Adds a Thread to this Thread store.<p>
90      *
91      * @param thread the Thread to add
92      */

93     public void addThread(A_CmsReportThread thread) {
94
95         m_threads.put(thread.getUUID(), thread);
96         if (LOG.isDebugEnabled()) {
97             dumpThreads();
98         }
99     }
100
101     /**
102      * Retrieves a Thread from this Thread store.<p>
103      *
104      * @param key the key of the Thread to retrieve
105      * @return the Thread form this Thread store that matches the given key
106      */

107     public A_CmsReportThread retrieveThread(CmsUUID key) {
108
109         if (LOG.isDebugEnabled()) {
110             dumpThreads();
111         }
112         return (A_CmsReportThread)m_threads.get(key);
113     }
114
115     /**
116      * @see java.lang.Runnable#run()
117      */

118     public void run() {
119
120         int m_minutesForSessionUpdate = 0;
121         while (m_alive) {
122             // the Grim Reaper is eternal, of course
123
try {
124                 // one minute sleep time
125
sleep(60000);
126             } catch (InterruptedException JavaDoc e) {
127                 // let's go on reaping...
128
}
129             try {
130                 Iterator JavaDoc i;
131                 i = m_threads.keySet().iterator();
132                 Set JavaDoc doomed = new HashSet JavaDoc();
133                 // first collect all doomed Threads
134
while (i.hasNext()) {
135                     CmsUUID key = (CmsUUID)i.next();
136                     A_CmsReportThread thread = (A_CmsReportThread)m_threads.get(key);
137                     if (thread.isDoomed()) {
138                         doomed.add(key);
139                         if (LOG.isDebugEnabled()) {
140                             LOG.debug(Messages.get().getBundle().key(
141                                 Messages.LOG_THREADSTORE_DOOMED_2,
142                                 thread.getName(),
143                                 thread.getUUID()));
144                         }
145                     }
146                 }
147                 i = doomed.iterator();
148                 // no remove all doomed Threads from the Thread store
149
while (i.hasNext()) {
150                     m_threads.remove(i.next());
151                 }
152                 if (LOG.isDebugEnabled()) {
153                     dumpThreads();
154                 }
155             } catch (Throwable JavaDoc t) {
156                 // the Grim Reaper must not be stopped by any error
157
LOG.error(Messages.get().getBundle().key(Messages.LOG_THREADSTORE_CHECK_THREADS_ERROR_0), t);
158             }
159
160             // check the session manager for invalid sessions not removed for whatever reason
161
m_minutesForSessionUpdate++;
162             if (m_minutesForSessionUpdate >= 5) {
163                 // do this every 5 minutes
164
m_minutesForSessionUpdate = 0;
165                 try {
166                     CmsSessionManager sessionInfoManager = OpenCms.getSessionManager();
167                     if (sessionInfoManager != null) {
168                         // will be null if only the shell is running
169
sessionInfoManager.validateSessionInfos();
170                     }
171                 } catch (Throwable JavaDoc t) {
172                     LOG.error(Messages.get().getBundle().key(Messages.LOG_THREADSTORE_CHECK_SESSIONS_ERROR_0), t);
173                 }
174             }
175         }
176     }
177
178     /**
179      * Shut down this thread store.<p>
180      */

181     protected synchronized void shutDown() {
182
183         m_alive = false;
184         interrupt();
185     }
186
187     /**
188      * Method to dump all currently known Threads.<p>
189      */

190     private void dumpThreads() {
191
192         if (LOG.isDebugEnabled()) {
193             StringBuffer JavaDoc b = new StringBuffer JavaDoc(512);
194             Iterator JavaDoc i = m_threads.keySet().iterator();
195             while (i.hasNext()) {
196                 CmsUUID key = (CmsUUID)i.next();
197                 A_CmsReportThread thread = (A_CmsReportThread)m_threads.get(key);
198                 b.append(thread.getName());
199                 b.append(" - ");
200                 b.append(thread.getUUID());
201                 b.append('\n');
202             }
203             LOG.debug(Messages.get().getBundle().key(
204                 Messages.LOG_THREADSTORE_POOL_CONTENT_2,
205                 new Integer JavaDoc(m_threads.size()),
206                 b.toString()));
207         }
208     }
209 }
Popular Tags