1 23 24 package org.infoglue.deliver.util; 25 26 import java.io.*; 27 import java.util.*; 28 29 import javax.servlet.http.HttpServletRequest ; 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 40 41 public class ThreadMonitor implements Runnable 42 { 43 44 private final static Logger logger = Logger.getLogger(ThreadMonitor.class.getName()); 45 46 private Thread targetThread; 47 private long millis; 48 private Thread watcherThread; 49 private boolean loop; 50 private boolean enabled; 51 private HttpServletRequest request; 52 private String message; 53 private boolean kill = false; 54 55 public ThreadMonitor(Thread targetThread, long millis, HttpServletRequest request, String message, boolean kill) 59 { 60 this.targetThread = targetThread; 61 this.millis = millis; 62 watcherThread = new Thread (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 81 } 82 83 public ThreadMonitor(long millis, HttpServletRequest request, String message, boolean kill) 85 { 86 this(Thread.currentThread(), millis, request, message, kill); 87 } 88 89 public synchronized void done() 91 { 92 loop = false; 93 enabled = false; 94 notify(); 95 } 96 97 public synchronized void reset() 99 { 100 loop = true; 101 notify(); 102 } 103 104 public synchronized void reset(long millis) 106 { 107 this.millis = millis; 108 reset(); 109 } 110 111 public synchronized void run() 115 { 116 Thread 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 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 [] el = targetThread.getStackTrace(); 146 147 StringBuffer stackString = new StringBuffer ("\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 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 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 e) 171 { 172 logger.error("Could not send mail:" + e.getMessage(), e); 173 } 174 } 175 } 176 177 181 182 public String getOriginalFullURL() 183 { 184 String originalRequestURL = this.request.getParameter("originalRequestURL"); 185 if(originalRequestURL == null || originalRequestURL.length() == 0) 186 originalRequestURL = this.request.getRequestURL().toString(); 187 188 String 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 |