svn-gvsig-desktop / branches / v2_0_0_prep / build / distribution / IzPack / src / lib / com / izforge / izpack / util / Housekeeper.java @ 23393
History | View | Annotate | Download (4.85 KB)
1 |
/*
|
---|---|
2 |
* $Id: Housekeeper.java 5819 2006-06-14 07:29:09Z cesar $
|
3 |
* IzPack
|
4 |
* Copyright (C) 2002 by Elmar Grom
|
5 |
*
|
6 |
* File : Housekeeper.java
|
7 |
* Description : Performs housekeeping and cleanup tasks for IzForge
|
8 |
* Author's email : elmar@grom.net
|
9 |
* Website : http://www.izforge.com
|
10 |
*
|
11 |
* This program is free software; you can redistribute it and/or
|
12 |
* modify it under the terms of the GNU General Public License
|
13 |
* as published by the Free Software Foundation; either version 2
|
14 |
* of the License, or any later version.
|
15 |
*
|
16 |
* This program is distributed in the hope that it will be useful,
|
17 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
19 |
* GNU General Public License for more details.
|
20 |
*
|
21 |
* You should have received a copy of the GNU General Public License
|
22 |
* along with this program; if not, write to the Free Software
|
23 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
24 |
*/
|
25 |
|
26 |
package com.izforge.izpack.util; |
27 |
|
28 |
import java.util.Vector; |
29 |
|
30 |
/*---------------------------------------------------------------------------*/
|
31 |
/**
|
32 |
* This class performs housekeeping and cleanup tasks. There can only be one
|
33 |
* instance of <code>Housekeeper</code> per Java runtime, therefore this class
|
34 |
* is implemented as a 'Singleton'.
|
35 |
* <br><br>
|
36 |
* It is VERY important to perform pre-shutdown cleanup operations through
|
37 |
* this class. Do NOT rely on operations like <code>deleteOnExit()</code>
|
38 |
* shutdown hooks or <code>finalize()</code>for cleanup. Because
|
39 |
* <code>shutDown()</code> uses <code>System.exit()</code> to terminate,
|
40 |
* these methods will not work at all or will not work reliably.
|
41 |
*
|
42 |
* @version 0.0.1 / 2/9/02
|
43 |
* @author Elmar Grom
|
44 |
*/
|
45 |
/*---------------------------------------------------------------------------*/
|
46 |
public class Housekeeper |
47 |
{ |
48 |
// ------------------------------------------------------------------------
|
49 |
// Variable Declarations
|
50 |
// ------------------------------------------------------------------------
|
51 |
private static Housekeeper me = null; |
52 |
|
53 |
private Vector cleanupClients = new Vector (); |
54 |
|
55 |
/*--------------------------------------------------------------------------*/
|
56 |
/**
|
57 |
* This class is implemented as a 'Singleton'. Therefore the constructor is
|
58 |
* private to prevent instantiation of this class. Use <code>getInstance()</code>
|
59 |
* to obtain an instance for use.
|
60 |
* <br><br>
|
61 |
* For more information about the 'Singleton' pattern I highly recommend
|
62 |
* the book Design Patterns by Gamma, Helm, Johnson and Vlissides
|
63 |
* ISBN 0-201-63361-2.
|
64 |
*/
|
65 |
/*--------------------------------------------------------------------------*/
|
66 |
private Housekeeper () {}
|
67 |
/*--------------------------------------------------------------------------*/
|
68 |
/**
|
69 |
* Returns an instance of <code>Housekeeper</code> to use.
|
70 |
*
|
71 |
* @return an instance of <code>Housekeeper</code>.
|
72 |
*/
|
73 |
/*--------------------------------------------------------------------------*/
|
74 |
public static Housekeeper getInstance () |
75 |
{ |
76 |
if (me == null) |
77 |
{ |
78 |
me = new Housekeeper ();
|
79 |
} |
80 |
|
81 |
return (me);
|
82 |
} |
83 |
/*--------------------------------------------------------------------------*/
|
84 |
/**
|
85 |
* Use to register objects that need to perform cleanup operations before the
|
86 |
* application shuts down.
|
87 |
*
|
88 |
* @param client reference of to an object that needs to perform
|
89 |
* cleanup operations.
|
90 |
*/
|
91 |
/*--------------------------------------------------------------------------*/
|
92 |
public void registerForCleanup (CleanupClient client) |
93 |
{ |
94 |
cleanupClients.add (client); |
95 |
} |
96 |
/*--------------------------------------------------------------------------*/
|
97 |
/**
|
98 |
* This methods shuts the application down. First, it will call all clients
|
99 |
* that have registered for cleanup operations. Once this has been
|
100 |
* accomplished, the application will be forceably terminated.
|
101 |
* <br><br>
|
102 |
* <b>THIS METHOD DOES NOT RETURN!</b>
|
103 |
*
|
104 |
* @param exitCode the exit code that should be returned to the
|
105 |
* calling process.
|
106 |
*/
|
107 |
/*--------------------------------------------------------------------------*/
|
108 |
public void shutDown (int exitCode) |
109 |
{ |
110 |
for (int i = 0; i < cleanupClients.size (); i++) |
111 |
{ |
112 |
try
|
113 |
{ |
114 |
((CleanupClient)cleanupClients.elementAt (i)).cleanUp (); |
115 |
} |
116 |
catch (Throwable exception) |
117 |
{ |
118 |
// At this point we can not afford to treat exceptions. Cleanup that
|
119 |
// can not be completed might unfortunately leave some garbage behind.
|
120 |
// If we have a logging module, any exceptions received here should
|
121 |
// be written to the log.
|
122 |
} |
123 |
} |
124 |
|
125 |
System.exit (exitCode);
|
126 |
} |
127 |
} |
128 |
/*---------------------------------------------------------------------------*/
|