1 21 22 package org.apache.derbyTesting.unitTests.harness; 23 24 import org.apache.derby.iapi.services.property.PropertyUtil; 25 import org.apache.derby.iapi.services.context.ContextService; 26 import org.apache.derby.iapi.services.monitor.Monitor; 27 28 import org.apache.derby.iapi.services.context.Context; 29 30 import java.util.Enumeration ; 31 import java.util.Vector ; 32 33 public class T_Bomb implements Runnable { 34 public static String BOMB_DELAY_PN="derby.testing.BombDelay"; 35 private static int DEFAULT_BOMB_DELAY=3600000; 37 private static T_Bomb me; 38 39 private Thread t; 40 private Vector v; 41 private long delay; 42 private boolean armed = false; 43 44 private T_Bomb() 45 { 46 delay = 47 PropertyUtil.getSystemInt(BOMB_DELAY_PN,0, 48 Integer.MAX_VALUE, 49 DEFAULT_BOMB_DELAY); 50 v = new Vector (); 51 t = new Thread (this); 52 t.setDaemon(true); 53 t.start(); 54 } 55 56 59 public synchronized static void makeBomb() { 60 if (me==null) me = new T_Bomb(); 61 me.armBomb(); 62 } 63 64 68 public synchronized void armBomb() { 69 if (me == null) me = new T_Bomb(); 70 me.armed = true; 71 } 72 73 77 public synchronized static void explodeBomb() { 78 if (me == null) me = new T_Bomb(); 79 me.armed = true; 80 me.blowUp(); 81 } 82 83 public synchronized static void registerBombable(T_Bombable b) 84 { 85 if (me == null) me = new T_Bomb(); 86 me.v.addElement(b); 87 } 88 89 public synchronized static void unRegisterBombable(T_Bombable b) 90 { 91 if (null == me || null == b ) 92 return; 93 me.v.removeElement(b); 94 if( me.v.isEmpty()) 95 { 96 me.armed = false; 97 me.t.interrupt(); 98 me = null; 99 } 100 } 101 102 public void run() { 103 104 try { 105 Thread.sleep(delay); 106 } 107 108 catch (InterruptedException e) { 109 } 110 111 if (armed) 112 { 113 me.blowUp(); 114 } 115 } 116 117 private void blowUp() 118 { 119 performLastGasp(); 120 ContextService csf = ContextService.getFactory(); 121 if (csf != null) 122 { 123 System.out.println("ran out of time"); 124 csf.notifyAllActiveThreads 125 ((Context) null); 126 } 127 128 try { 129 Thread.currentThread().sleep(30*1000); } 131 catch (InterruptedException ie) {} 132 System.out.println("Exit due to time bomb"); 133 Runtime.getRuntime().exit(1234); 134 } 135 136 private void performLastGasp() 137 { 138 for (Enumeration e = v.elements() ; e.hasMoreElements() ;) { 139 try{ 140 T_Bombable b = (T_Bombable)e.nextElement(); 141 b.lastChance(); 142 } 143 144 145 catch (Exception exc) { 146 System.out.println("Last Gasp exception"); 147 exc.printStackTrace(); 148 } 149 } 151 } 152 } 153 | Popular Tags |