001: /*
002: * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
003: *
004: * http://izpack.org/
005: * http://izpack.codehaus.org/
006: *
007: * Copyright 2002 Elmar Grom
008: *
009: * Licensed under the Apache License, Version 2.0 (the "License");
010: * you may not use this file except in compliance with the License.
011: * You may obtain a copy of the License at
012: *
013: * http://www.apache.org/licenses/LICENSE-2.0
014: *
015: * Unless required by applicable law or agreed to in writing, software
016: * distributed under the License is distributed on an "AS IS" BASIS,
017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: * See the License for the specific language governing permissions and
019: * limitations under the License.
020: */
021:
022: package com.izforge.izpack.util;
023:
024: import java.util.Vector;
025:
026: /*---------------------------------------------------------------------------*/
027: /**
028: * This class performs housekeeping and cleanup tasks. There can only be one instance of
029: * <code>Housekeeper</code> per Java runtime, therefore this class is implemented as a
030: * 'Singleton'. <br>
031: * <br>
032: * It is VERY important to perform pre-shutdown cleanup operations through this class. Do NOT rely
033: * on operations like <code>deleteOnExit()</code> shutdown hooks or <code>finalize()</code>for
034: * cleanup. Because <code>shutDown()</code> uses <code>System.exit()</code> to terminate, these
035: * methods will not work at all or will not work reliably.
036: *
037: * @version 0.0.1 / 2/9/02
038: * @author Elmar Grom
039: */
040: /*---------------------------------------------------------------------------*/
041: public class Housekeeper {
042:
043: // ------------------------------------------------------------------------
044: // Variable Declarations
045: // ------------------------------------------------------------------------
046: private static Housekeeper me = null;
047:
048: private Vector<CleanupClient> cleanupClients = new Vector<CleanupClient>();
049:
050: /*--------------------------------------------------------------------------*/
051: /**
052: * This class is implemented as a 'Singleton'. Therefore the constructor is private to prevent
053: * instantiation of this class. Use <code>getInstance()</code> to obtain an instance for use.
054: * <br>
055: * <br>
056: * For more information about the 'Singleton' pattern I highly recommend the book Design
057: * Patterns by Gamma, Helm, Johnson and Vlissides ISBN 0-201-63361-2.
058: */
059: /*--------------------------------------------------------------------------*/
060: private Housekeeper() {
061: }
062:
063: /*--------------------------------------------------------------------------*/
064: /**
065: * Returns an instance of <code>Housekeeper</code> to use.
066: *
067: * @return an instance of <code>Housekeeper</code>.
068: */
069: /*--------------------------------------------------------------------------*/
070: public static Housekeeper getInstance() {
071: if (me == null) {
072: me = new Housekeeper();
073: }
074:
075: return (me);
076: }
077:
078: /*--------------------------------------------------------------------------*/
079: /**
080: * Use to register objects that need to perform cleanup operations before the application shuts
081: * down.
082: *
083: * @param client reference of to an object that needs to perform cleanup operations.
084: */
085: /*--------------------------------------------------------------------------*/
086: public void registerForCleanup(CleanupClient client) {
087: cleanupClients.add(client);
088: }
089:
090: /*--------------------------------------------------------------------------*/
091: /**
092: * This methods shuts the application down. First, it will call all clients that have registered
093: * for cleanup operations. Once this has been accomplished, the application will be forceably
094: * terminated. <br>
095: * <br>
096: * <b>THIS METHOD DOES NOT RETURN!</b>
097: *
098: * @param exitCode the exit code that should be returned to the calling process.
099: */
100: /*--------------------------------------------------------------------------*/
101: public void shutDown(int exitCode) {
102: for (int i = 0; i < cleanupClients.size(); i++) {
103: try {
104: (cleanupClients.elementAt(i)).cleanUp();
105: } catch (Throwable exception) {
106: // At this point we can not afford to treat exceptions. Cleanup
107: // that
108: // can not be completed might unfortunately leave some garbage
109: // behind.
110: // If we have a logging module, any exceptions received here
111: // should
112: // be written to the log.
113: }
114: }
115:
116: System.exit(exitCode);
117: }
118: }
119: /*---------------------------------------------------------------------------*/
|