KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > util > ThreadMonitor


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.deliver.util;
25
26 import java.io.*;
27 import java.util.*;
28
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30
31 import org.apache.log4j.Logger;
32 import org.infoglue.cms.controllers.kernel.impl.simple.CmsJDOCallback;
33 import org.infoglue.cms.exception.SystemException;
34 import org.infoglue.cms.util.CmsPropertyHandler;
35 import org.infoglue.cms.util.mail.MailServiceFactory;
36
37 /*
38  * Kill a thread after a given timeout has elapsed
39  */

40
41 public class ThreadMonitor implements Runnable JavaDoc
42 {
43
44     private final static Logger logger = Logger.getLogger(ThreadMonitor.class.getName());
45
46     private Thread JavaDoc targetThread;
47     private long millis;
48     private Thread JavaDoc watcherThread;
49     private boolean loop;
50     private boolean enabled;
51     private HttpServletRequest JavaDoc request;
52     private String JavaDoc message;
53     private boolean kill = false;
54     
55     /// Constructor. Give it a thread to watch, and a timeout in milliseconds.
56
// After the timeout has elapsed, the thread gets killed. If you want
57
// to cancel the kill, just call done().
58
public ThreadMonitor(Thread JavaDoc targetThread, long millis, HttpServletRequest JavaDoc request, String JavaDoc message, boolean kill)
59     {
60         this.targetThread = targetThread;
61         this.millis = millis;
62         watcherThread = new Thread JavaDoc(this);
63         enabled = true;
64         this.request = request;
65         this.message = message;
66         this.kill = kill;
67         
68         if(millis > 0)
69             watcherThread.start();
70
71         // Hack - pause a bit to let the watcher thread get started.
72
/*
73         try
74         {
75             Thread.sleep(100);
76         }
77         catch (InterruptedException e)
78         {
79         }
80         */

81     }
82
83     /// Constructor, current thread.
84
public ThreadMonitor(long millis, HttpServletRequest JavaDoc request, String JavaDoc message, boolean kill)
85     {
86         this(Thread.currentThread(), millis, request, message, kill);
87     }
88
89     /// Call this when the target thread has finished.
90
public synchronized void done()
91     {
92         loop = false;
93         enabled = false;
94         notify();
95     }
96
97     /// Call this to restart the wait from zero.
98
public synchronized void reset()
99     {
100         loop = true;
101         notify();
102     }
103
104     /// Call this to restart the wait from zero with a different timeout value.
105
public synchronized void reset(long millis)
106     {
107         this.millis = millis;
108         reset();
109     }
110
111     /// The watcher thread - from the Runnable interface.
112
// This has to be pretty anal to avoid monitor lockup, lost
113
// threads, etc.
114
public synchronized void run()
115     {
116         Thread JavaDoc me = Thread.currentThread();
117         me.setPriority(Thread.MAX_PRIORITY);
118         if (enabled)
119         {
120             do
121             {
122                 loop = false;
123                 try
124                 {
125                     wait(millis);
126                 }
127                 catch (InterruptedException JavaDoc e)
128                 {
129                 }
130             }
131             while (enabled && loop);
132         }
133         
134         if (enabled && targetThread.isAlive())
135         {
136             printThread();
137             if(kill)
138                 targetThread.stop();
139         }
140     }
141
142     
143     private void printThread()
144     {
145         StackTraceElement JavaDoc[] el = targetThread.getStackTrace();
146         
147         StringBuffer JavaDoc stackString = new StringBuffer JavaDoc("\n\n" + message + ":\n\n");
148         stackString.append("\nThread before killed:\n");
149         stackString.append("\nOriginal url:" + getOriginalFullURL() + "\n");
150         if (el != null && el.length != 0)
151         {
152             for (int j = 0; j < el.length; j++)
153             {
154                 StackTraceElement JavaDoc frame = el[j];
155                 if (frame == null)
156                     stackString.append(" null stack frame" + "\n");
157                 else
158                     stackString.append(" " + frame.toString() + "\n");
159             }
160         }
161         logger.warn(stackString);
162         
163         String JavaDoc warningEmailReceiver = CmsPropertyHandler.getWarningEmailReceiver();
164         if(warningEmailReceiver != null && !warningEmailReceiver.equals("") && warningEmailReceiver.indexOf("@warningEmailReceiver@") == -1)
165         {
166             try
167             {
168                 MailServiceFactory.getService().sendEmail(warningEmailReceiver, warningEmailReceiver, null, message, stackString.toString().replaceAll("\n", "<br/>"), "utf-8");
169             }
170             catch (Exception JavaDoc e)
171             {
172                 logger.error("Could not send mail:" + e.getMessage(), e);
173             }
174         }
175     }
176     
177     /**
178      * This method returns the exact full url from the original request - not modified
179      * @return
180      */

181     
182     public String JavaDoc getOriginalFullURL()
183     {
184         String JavaDoc originalRequestURL = this.request.getParameter("originalRequestURL");
185         if(originalRequestURL == null || originalRequestURL.length() == 0)
186             originalRequestURL = this.request.getRequestURL().toString();
187
188         String JavaDoc originalQueryString = this.request.getParameter("originalQueryString");
189         if(originalQueryString == null || originalQueryString.length() == 0)
190             originalQueryString = this.request.getQueryString();
191
192         return originalRequestURL + "?" + originalQueryString;
193     }
194
195 }
196
Popular Tags