KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > util > Housekeeper


1 /*
2  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
3  *
4  * http://www.izforge.com/izpack/
5  * http://developer.berlios.de/projects/izpack/
6  *
7  * Copyright 2002 Elmar Grom
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22 package com.izforge.izpack.util;
23
24 import java.util.Vector JavaDoc;
25
26 /*---------------------------------------------------------------------------*/
27 /**
28  * This class performs housekeeping and cleanup tasks. There can only be one instance of
29  * <code>Housekeeper</code> per Java runtime, therefore this class is implemented as a
30  * 'Singleton'. <br>
31  * <br>
32  * It is VERY important to perform pre-shutdown cleanup operations through this class. Do NOT rely
33  * on operations like <code>deleteOnExit()</code> shutdown hooks or <code>finalize()</code>for
34  * cleanup. Because <code>shutDown()</code> uses <code>System.exit()</code> to terminate, these
35  * methods will not work at all or will not work reliably.
36  *
37  * @version 0.0.1 / 2/9/02
38  * @author Elmar Grom
39  */

40 /*---------------------------------------------------------------------------*/
41 public class Housekeeper
42 {
43
44     // ------------------------------------------------------------------------
45
// Variable Declarations
46
// ------------------------------------------------------------------------
47
private static Housekeeper me = null;
48
49     private Vector JavaDoc cleanupClients = new Vector JavaDoc();
50
51     /*--------------------------------------------------------------------------*/
52     /**
53      * This class is implemented as a 'Singleton'. Therefore the constructor is private to prevent
54      * instantiation of this class. Use <code>getInstance()</code> to obtain an instance for use.
55      * <br>
56      * <br>
57      * For more information about the 'Singleton' pattern I highly recommend the book Design
58      * Patterns by Gamma, Helm, Johnson and Vlissides ISBN 0-201-63361-2.
59      */

60     /*--------------------------------------------------------------------------*/
61     private Housekeeper()
62     {
63     }
64
65     /*--------------------------------------------------------------------------*/
66     /**
67      * Returns an instance of <code>Housekeeper</code> to use.
68      *
69      * @return an instance of <code>Housekeeper</code>.
70      */

71     /*--------------------------------------------------------------------------*/
72     public static Housekeeper getInstance()
73     {
74         if (me == null)
75         {
76             me = new Housekeeper();
77         }
78
79         return (me);
80     }
81
82     /*--------------------------------------------------------------------------*/
83     /**
84      * Use to register objects that need to perform cleanup operations before the application shuts
85      * down.
86      *
87      * @param client reference of to an object that needs to perform cleanup operations.
88      */

89     /*--------------------------------------------------------------------------*/
90     public void registerForCleanup(CleanupClient client)
91     {
92         cleanupClients.add(client);
93     }
94
95     /*--------------------------------------------------------------------------*/
96     /**
97      * This methods shuts the application down. First, it will call all clients that have registered
98      * for cleanup operations. Once this has been accomplished, the application will be forceably
99      * 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 JavaDoc 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 /*---------------------------------------------------------------------------*/
130
Popular Tags