1 64 65 package com.jcorporate.expresso.core.security; 66 67 import org.apache.log4j.Logger; 68 69 70 93 public class DelayThread 94 extends Thread { 95 protected static DelayThread theInstance = null; 96 private static Logger log = Logger.getLogger(DelayThread.class); 97 volatile protected Object lockObject = new Object (); 98 99 102 int secondsDelay = 0; 103 104 public DelayThread() { 105 106 } 107 108 113 public DelayThread(int numSeconds) { 114 setDelay(numSeconds); 115 116 } 117 118 123 public void setDelay(int numSeconds) { 124 if (numSeconds >= 0) { 125 secondsDelay = numSeconds; 126 } 127 } 128 129 134 public int getDelay() { 135 return secondsDelay; 136 } 137 138 141 public void run() { 142 java.util.Date startTime = null; 144 java.util.Date endTime = null; 145 146 if (log.isDebugEnabled()) { 147 startTime = new java.util.Date (); 148 log.debug("Entering delay thread. Time=" + startTime.getTime()); 149 } 150 try { 151 synchronized (lockObject) { 152 if (this.isInterrupted()) { 153 return; 154 } 155 if (secondsDelay > 0) { 156 sleep(secondsDelay * 1000); 157 } 158 } 159 } catch (InterruptedException e) { 160 161 } 163 if (log.isDebugEnabled()) { 164 endTime = new java.util.Date (); 165 log.debug("Exiting delay thread. Time=" + endTime.getTime() + 166 " time delta = " + 167 (endTime.getTime() - startTime.getTime()) + " ms."); 168 } 169 } 170 171 175 public static void delay() { 176 DelayThread.delay(2); 177 178 } 179 180 184 public synchronized static void kill() { 185 if (theInstance == null) { 186 return; 187 } 188 189 theInstance.interrupt(); 190 191 try { 192 theInstance.join(1000); 193 } catch (InterruptedException ie) { 194 } 195 196 theInstance = null; 197 log = null; 198 199 } 200 201 204 private synchronized static void instantiate() { 205 if (theInstance == null) { 206 theInstance = new DelayThread(); 207 theInstance.setName("Security Delay Thread"); 208 theInstance.setDaemon(false); 209 } 210 } 211 212 218 public static void delay(int numSeconds) { 219 if (theInstance == null) { 220 instantiate(); 221 } 222 try { 223 theInstance.setDelay(numSeconds); 224 theInstance.run(); 225 theInstance.join(); 226 } catch (java.lang.InterruptedException e) { 227 } 228 } 229 } | Popular Tags |