001: /*
002: * Copyright (c) 2000 Silvere Martin-Michiellot All Rights Reserved.
003: *
004: * Silvere Martin-Michiellot grants you ("Licensee") a non-exclusive,
005: * royalty free, license to use, modify and redistribute this
006: * software in source and binary code form,
007: * provided that i) this copyright notice and license appear on all copies of
008: * the software; and ii) Licensee does not utilize the software in a manner
009: * which is disparaging to Silvere Martin-Michiellot.
010: *
011: * This software is provided "AS IS," without a warranty of any kind. ALL
012: * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
013: * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
014: * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. Silvere Martin-Michiellot
015: * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
016: * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
017: * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
018: * Silvere Martin-Michiellot OR ITS LICENSORS BE LIABLE
019: * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
020: * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
021: * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
022: * OR INABILITY TO USE SOFTWARE, EVEN IF Silvere Martin-Michiellot HAS BEEN
023: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
024: *
025: * This software is not designed or intended for use in on-line control of
026: * aircraft, air traffic, aircraft navigation or aircraft communications; or in
027: * the design, construction, operation or maintenance of any nuclear
028: * facility. Licensee represents and warrants that it will not use or
029: * redistribute the Software for such purposes.
030: *
031: */
032:
033: package com.db.utils;
034:
035: // This code is repackaged after the Autoupdater
036: // Site http://sourceforge.net/projects/autoupdater/
037: // Email wurp@users.sourceforge.net
038:
039: import java.io.File;
040: import java.io.PrintStream;
041: import java.util.*;
042:
043: public class Terminator {
044:
045: protected List jobsToRunOnExit_;
046: protected static Terminator instance_;
047: public static final int KILL_CHECK_INTERVAL = 20000;
048:
049: protected Terminator() {
050:
051: jobsToRunOnExit_ = new LinkedList();
052: Thread thread = new Thread("killer") {
053:
054: public void run() {
055:
056: File file = new File("killme");
057: File file1 = new File("killme.txt");
058: do {
059: if (file.exists() || file1.exists()) {
060: String s = "killme file found; deleting and calling terminator.exit(-13)";
061: System.err.println(s);
062: file.delete();
063: file1.delete();
064: Terminator.instance().exit(-13);
065: }
066: try {
067: Thread.currentThread();
068: Thread.sleep(20000L);
069: } catch (Exception exception) {
070: }
071: } while (true);
072:
073: }
074:
075: };
076:
077: thread.setDaemon(true);
078: thread.start();
079:
080: }
081:
082: public static Terminator instance() {
083:
084: if (instance_ == null)
085: instance_ = new Terminator();
086:
087: return instance_;
088:
089: }
090:
091: public void runOnExit(VoidFunction voidfunction) {
092:
093: jobsToRunOnExit_.add(0, voidfunction);
094:
095: }
096:
097: public void exit(int i) {
098:
099: runExitJobs();
100: System.gc();
101: System.runFinalization();
102: System.exit(i);
103:
104: }
105:
106: public void finalize() {
107:
108: runExitJobs();
109:
110: }
111:
112: public void runExitJobs() {
113:
114: VoidFunction voidfunction;
115: for (Iterator iterator = jobsToRunOnExit_.iterator(); iterator
116: .hasNext(); voidfunction.execute())
117: voidfunction = (VoidFunction) iterator.next();
118:
119: }
120:
121: public static void main(String args[]) {
122: }
123:
124: }
|