001: /*
002:
003: Derby - Class org.apache.derbyTesting.unitTests.harness.T_Bomb
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.unitTests.harness;
023:
024: import org.apache.derby.iapi.services.property.PropertyUtil;
025: import org.apache.derby.iapi.services.context.ContextService;
026: import org.apache.derby.iapi.services.monitor.Monitor;
027:
028: import org.apache.derby.iapi.services.context.Context;
029:
030: import java.util.Enumeration;
031: import java.util.Vector;
032:
033: public class T_Bomb implements Runnable {
034: public static String BOMB_DELAY_PN = "derby.testing.BombDelay";
035: private static int DEFAULT_BOMB_DELAY = 3600000; //1 hour
036:
037: private static T_Bomb me;
038:
039: private Thread t;
040: private Vector v;
041: private long delay;
042: private boolean armed = false;
043:
044: private T_Bomb() {
045: delay = PropertyUtil.getSystemInt(BOMB_DELAY_PN, 0,
046: Integer.MAX_VALUE, DEFAULT_BOMB_DELAY);
047: v = new Vector();
048: t = new Thread(this );
049: t.setDaemon(true);
050: t.start();
051: }
052:
053: /**
054: Make an armed bomb set to go off in 1 hour.
055: */
056: public synchronized static void makeBomb() {
057: if (me == null)
058: me = new T_Bomb();
059: me.armBomb();
060: }
061:
062: /**
063: Arm a bomb to go off. If the bomb does not exist
064: make it.
065: */
066: public synchronized void armBomb() {
067: if (me == null)
068: me = new T_Bomb();
069: me.armed = true;
070: }
071:
072: /**
073: Cause a bomb to explode. If the bomb does not exist
074: make it.
075: */
076: public synchronized static void explodeBomb() {
077: if (me == null)
078: me = new T_Bomb();
079: me.armed = true;
080: me.blowUp();
081: }
082:
083: public synchronized static void registerBombable(T_Bombable b) {
084: if (me == null)
085: me = new T_Bomb();
086: me.v.addElement(b);
087: }
088:
089: public synchronized static void unRegisterBombable(T_Bombable b) {
090: if (null == me || null == b)
091: return;
092: me.v.removeElement(b);
093: if (me.v.isEmpty()) {
094: me.armed = false;
095: me.t.interrupt();
096: me = null;
097: }
098: }
099:
100: public void run() {
101:
102: try {
103: Thread.sleep(delay);
104: }
105:
106: catch (InterruptedException e) {
107: }
108:
109: if (armed) {
110: me.blowUp();
111: }
112: }
113:
114: private void blowUp() {
115: performLastGasp();
116: ContextService csf = ContextService.getFactory();
117: if (csf != null) {
118: System.out.println("ran out of time");
119: csf.notifyAllActiveThreads((Context) null);
120: }
121:
122: try {
123: Thread.currentThread().sleep(30 * 1000); //Give threads 30 sec to shut down.
124: } catch (InterruptedException ie) {
125: }
126: System.out.println("Exit due to time bomb");
127: Runtime.getRuntime().exit(1234);
128: }
129:
130: private void performLastGasp() {
131: for (Enumeration e = v.elements(); e.hasMoreElements();) {
132: try {
133: T_Bombable b = (T_Bombable) e.nextElement();
134: b.lastChance();
135: }
136:
137: catch (Exception exc) {
138: System.out.println("Last Gasp exception");
139: exc.printStackTrace();
140: }
141: } //end for
142:
143: }
144: }
|