001/*
002 * IzPack - Copyright 2001-2005 Julien Ponge, All Rights Reserved.
003 * 
004 * http://www.izforge.com/izpack/
005 * http://developer.berlios.de/projects/izpack/
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
022package com.izforge.izpack.util;
023
024import 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/*---------------------------------------------------------------------------*/
041public class Housekeeper
042{
043
044    // ------------------------------------------------------------------------
045    // Variable Declarations
046    // ------------------------------------------------------------------------
047    private static Housekeeper me = null;
048
049    private Vector cleanupClients = new Vector();
050
051    /*--------------------------------------------------------------------------*/
052    /**
053     * This class is implemented as a 'Singleton'. Therefore the constructor is private to prevent
054     * instantiation of this class. Use <code>getInstance()</code> to obtain an instance for use.
055     * <br>
056     * <br>
057     * For more information about the 'Singleton' pattern I highly recommend the book Design
058     * Patterns by Gamma, Helm, Johnson and Vlissides ISBN 0-201-63361-2.
059     */
060    /*--------------------------------------------------------------------------*/
061    private Housekeeper()
062    {
063    }
064
065    /*--------------------------------------------------------------------------*/
066    /**
067     * Returns an instance of <code>Housekeeper</code> to use.
068     * 
069     * @return an instance of <code>Housekeeper</code>.
070     */
071    /*--------------------------------------------------------------------------*/
072    public static Housekeeper getInstance()
073    {
074        if (me == null)
075        {
076            me = new Housekeeper();
077        }
078
079        return (me);
080    }
081
082    /*--------------------------------------------------------------------------*/
083    /**
084     * Use to register objects that need to perform cleanup operations before the application shuts
085     * down.
086     * 
087     * @param client reference of to an object that needs to perform cleanup operations.
088     */
089    /*--------------------------------------------------------------------------*/
090    public void registerForCleanup(CleanupClient client)
091    {
092        cleanupClients.add(client);
093    }
094
095    /*--------------------------------------------------------------------------*/
096    /**
097     * This methods shuts the application down. First, it will call all clients that have registered
098     * for cleanup operations. Once this has been accomplished, the application will be forceably
099     * terminated. <br>
100     * <br>
101     * <b>THIS METHOD DOES NOT RETURN!</b>
102     * 
103     * @param exitCode the exit code that should be returned to the calling process.
104     */
105    /*--------------------------------------------------------------------------*/
106    public void shutDown(int exitCode)
107    {
108        for (int i = 0; i < cleanupClients.size(); i++)
109        {
110            try
111            {
112                ((CleanupClient) cleanupClients.elementAt(i)).cleanUp();
113            }
114            catch (Throwable exception)
115            {
116                // At this point we can not afford to treat exceptions. Cleanup
117                // that
118                // can not be completed might unfortunately leave some garbage
119                // behind.
120                // If we have a logging module, any exceptions received here
121                // should
122                // be written to the log.
123            }
124        }
125
126        System.exit(exitCode);
127    }
128}
129/*---------------------------------------------------------------------------*/