KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > unitTests > harness > T_Bomb


1 /*
2
3    Derby - Class org.apache.derbyTesting.unitTests.harness.T_Bomb
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

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 JavaDoc;
31 import java.util.Vector JavaDoc;
32
33 public class T_Bomb implements Runnable JavaDoc {
34     public static String JavaDoc BOMB_DELAY_PN="derby.testing.BombDelay";
35     private static int DEFAULT_BOMB_DELAY=3600000; //1 hour
36

37     private static T_Bomb me;
38     
39     private Thread JavaDoc t;
40     private Vector JavaDoc 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 JavaDoc();
51         t = new Thread JavaDoc(this);
52         t.setDaemon(true);
53         t.start();
54     }
55
56     /**
57       Make an armed bomb set to go off in 1 hour.
58       */

59     public synchronized static void makeBomb() {
60         if (me==null) me = new T_Bomb();
61         me.armBomb();
62     }
63
64     /**
65       Arm a bomb to go off. If the bomb does not exist
66       make it.
67       */

68     public synchronized void armBomb() {
69         if (me == null) me = new T_Bomb();
70         me.armed = true;
71     }
72
73     /**
74       Cause a bomb to explode. If the bomb does not exist
75       make it.
76       */

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 JavaDoc 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); //Give threads 30 sec to shut down.
130
}
131             catch (InterruptedException JavaDoc 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 JavaDoc e = v.elements() ; e.hasMoreElements() ;) {
139             try{
140              T_Bombable b = (T_Bombable)e.nextElement();
141              b.lastChance();
142             }
143
144
145             catch (Exception JavaDoc exc) {
146                 System.out.println("Last Gasp exception");
147                 exc.printStackTrace();
148             }
149         } //end for
150

151     }
152 }
153
Popular Tags