svn-gvsig-desktop / trunk / build / distribution / IzPack / src / lib / com / izforge / izpack / uninstaller / Destroyer.java @ 21757
History | View | Annotate | Download (8.83 KB)
1 |
/*
|
---|---|
2 |
* $Id: Destroyer.java 5819 2006-06-14 07:29:09Z cesar $
|
3 |
* IzPack
|
4 |
* Copyright (C) 2001-2004 Julien Ponge
|
5 |
*
|
6 |
* File : Destroyer.java
|
7 |
* Description : The destroyer.
|
8 |
* Author's email : julien@izforge.com
|
9 |
* Author's 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 |
package com.izforge.izpack.uninstaller; |
26 |
|
27 |
import java.io.BufferedReader; |
28 |
import java.io.File; |
29 |
import java.io.InputStream; |
30 |
import java.io.InputStreamReader; |
31 |
import java.io.ObjectInputStream; |
32 |
import java.util.ArrayList; |
33 |
import java.util.Collections; |
34 |
import java.util.Iterator; |
35 |
import java.util.List; |
36 |
import java.util.TreeSet; |
37 |
|
38 |
import com.izforge.izpack.ExecutableFile; |
39 |
import com.izforge.izpack.event.UninstallerListener; |
40 |
import com.izforge.izpack.util.AbstractUIProgressHandler; |
41 |
import com.izforge.izpack.util.FileExecutor; |
42 |
|
43 |
/**
|
44 |
* The files destroyer class.
|
45 |
*
|
46 |
* @author Julien Ponge
|
47 |
*/
|
48 |
public class Destroyer extends Thread |
49 |
{ |
50 |
/** True if the destroyer must force the recursive deletion. */
|
51 |
private boolean forceDestroy; |
52 |
|
53 |
/** The installation path. */
|
54 |
private String installPath; |
55 |
|
56 |
/** the destroyer listener. */
|
57 |
private AbstractUIProgressHandler handler;
|
58 |
|
59 |
/**
|
60 |
* The constructor.
|
61 |
*
|
62 |
* @param installPath The installation path.
|
63 |
* @param forceDestroy Shall we force the recursive deletion.
|
64 |
* @param handler The destroyer listener.
|
65 |
*/
|
66 |
public Destroyer(
|
67 |
String installPath,
|
68 |
boolean forceDestroy,
|
69 |
AbstractUIProgressHandler handler) |
70 |
{ |
71 |
super("IzPack - Destroyer"); |
72 |
|
73 |
this.installPath = installPath;
|
74 |
this.forceDestroy = forceDestroy;
|
75 |
this.handler = handler;
|
76 |
} |
77 |
|
78 |
/** The run method. */
|
79 |
public void run() |
80 |
{ |
81 |
try
|
82 |
{ |
83 |
// We get the list of uninstaller listeners
|
84 |
List [] listeners = getListenerLists(); |
85 |
// We get the list of the files to delete
|
86 |
ArrayList executables = getExecutablesList();
|
87 |
|
88 |
|
89 |
FileExecutor executor = new FileExecutor(executables);
|
90 |
executor.executeFiles(ExecutableFile.UNINSTALL, this.handler);
|
91 |
|
92 |
ArrayList files = getFilesList();
|
93 |
int size = files.size();
|
94 |
|
95 |
// Custem action listener stuff --- beforeDeletion ----
|
96 |
informListeners(listeners[0], UninstallerListener.BEFORE_DELETION,
|
97 |
files, handler); |
98 |
|
99 |
handler.startAction("destroy", size);
|
100 |
|
101 |
// We destroy the files
|
102 |
for (int i = 0; i < size; i++) |
103 |
{ |
104 |
File file = (File) files.get(i); |
105 |
// Custem action listener stuff --- beforeDelete ----
|
106 |
informListeners(listeners[1], UninstallerListener.BEFORE_DELETE,
|
107 |
file, handler); |
108 |
|
109 |
file.delete(); |
110 |
|
111 |
// Custem action listener stuff --- afterDelete ----
|
112 |
informListeners(listeners[1], UninstallerListener.AFTER_DELETE,
|
113 |
file, handler); |
114 |
|
115 |
handler.progress(i, file.getAbsolutePath()); |
116 |
} |
117 |
|
118 |
// Custem action listener stuff --- afterDeletion ----
|
119 |
informListeners(listeners[0], UninstallerListener.AFTER_DELETION,
|
120 |
files, handler); |
121 |
|
122 |
// We make a complementary cleanup
|
123 |
handler.progress(size, "[ cleanups ]");
|
124 |
cleanup(new File(installPath)); |
125 |
|
126 |
handler.stopAction(); |
127 |
} catch (Exception err) |
128 |
{ |
129 |
handler.stopAction(); |
130 |
err.printStackTrace(); |
131 |
handler.emitError("exception caught", err.toString());
|
132 |
} |
133 |
} |
134 |
|
135 |
/**
|
136 |
* Asks the JVM for the uninstaller deletion.
|
137 |
*
|
138 |
* @exception Exception Description of the Exception
|
139 |
*/
|
140 |
private void askUninstallerRemoval() throws Exception |
141 |
{ |
142 |
// Initialisations
|
143 |
InputStream in = Destroyer.class.getResourceAsStream("/jarlocation.log"); |
144 |
InputStreamReader inReader = new InputStreamReader(in); |
145 |
BufferedReader reader = new BufferedReader(inReader); |
146 |
|
147 |
// We delete
|
148 |
File jar = new File(reader.readLine()); |
149 |
File path = new File(reader.readLine()); |
150 |
File inst = new File(installPath); |
151 |
jar.deleteOnExit(); |
152 |
path.deleteOnExit(); |
153 |
inst.deleteOnExit(); |
154 |
} |
155 |
|
156 |
/**
|
157 |
* Returns an ArrayList of the files to delete.
|
158 |
*
|
159 |
* @return The files list.
|
160 |
* @exception Exception Description of the Exception
|
161 |
*/
|
162 |
private ArrayList getFilesList() throws Exception |
163 |
{ |
164 |
// Initialisations
|
165 |
TreeSet files = new TreeSet(Collections.reverseOrder()); |
166 |
InputStream in = Destroyer.class.getResourceAsStream("/install.log"); |
167 |
InputStreamReader inReader = new InputStreamReader(in); |
168 |
BufferedReader reader = new BufferedReader(inReader); |
169 |
|
170 |
// We skip the first line (the installation path)
|
171 |
reader.readLine(); |
172 |
|
173 |
// We read it
|
174 |
String read = reader.readLine();
|
175 |
while (read != null) |
176 |
{ |
177 |
files.add(new File(read)); |
178 |
read = reader.readLine(); |
179 |
} |
180 |
|
181 |
// We return it
|
182 |
return new ArrayList(files); |
183 |
} |
184 |
|
185 |
private ArrayList getExecutablesList() throws Exception |
186 |
{ |
187 |
ArrayList executables = new ArrayList(); |
188 |
ObjectInputStream in =
|
189 |
new ObjectInputStream(Destroyer.class.getResourceAsStream("/executables")); |
190 |
int num = in.readInt();
|
191 |
for (int i = 0; i < num; i++) |
192 |
{ |
193 |
ExecutableFile file = (ExecutableFile) in.readObject(); |
194 |
executables.add(file); |
195 |
} |
196 |
return executables;
|
197 |
} |
198 |
|
199 |
/**
|
200 |
* Makes some reccursive cleanups.
|
201 |
*
|
202 |
* @param file The file to wipe.
|
203 |
* @exception Exception Description of the Exception
|
204 |
*/
|
205 |
private void cleanup(File file) throws Exception |
206 |
{ |
207 |
if (file.isDirectory())
|
208 |
{ |
209 |
File[] files = file.listFiles(); |
210 |
int size = files.length;
|
211 |
for (int i = 0; i < size; i++) |
212 |
cleanup(files[i]); |
213 |
file.delete(); |
214 |
} else if (forceDestroy) |
215 |
file.delete(); |
216 |
|
217 |
} |
218 |
|
219 |
// CUSTOM ACTION STUFF -------------- start -----------------
|
220 |
|
221 |
/**
|
222 |
* Load the defined uninstall listener objects.
|
223 |
* @return a list with the defined uninstall listeners
|
224 |
* @throws Exception
|
225 |
*/
|
226 |
private List [] getListenerLists() throws Exception |
227 |
{ |
228 |
ArrayList [] uninstaller = new ArrayList[] {new ArrayList(),new ArrayList()}; |
229 |
// Load listeners if exist
|
230 |
InputStream in;
|
231 |
ObjectInputStream objIn;
|
232 |
in = Destroyer.class.getResourceAsStream("/uninstallerListeners");
|
233 |
if( in != null ) |
234 |
{ |
235 |
objIn = new ObjectInputStream(in); |
236 |
List listeners = (List)objIn.readObject(); |
237 |
objIn.close(); |
238 |
Iterator iter = listeners.iterator();
|
239 |
while( iter != null && iter.hasNext()) |
240 |
{ |
241 |
Class clazz = Class.forName(((String) iter.next())); |
242 |
UninstallerListener ul = (UninstallerListener) clazz.newInstance(); |
243 |
if( ul.isFileListener())
|
244 |
uninstaller[1].add( ul );
|
245 |
uninstaller[0].add( ul );
|
246 |
} |
247 |
} |
248 |
return uninstaller;
|
249 |
} |
250 |
/**
|
251 |
* Informs all listeners.
|
252 |
* @param listeners list with the listener objects
|
253 |
* @param action identifier which callback should be called
|
254 |
* @param param parameter for the call
|
255 |
* @param handler the current progress handler
|
256 |
*/
|
257 |
|
258 |
private void informListeners(List listeners, int action, |
259 |
Object param, AbstractUIProgressHandler handler)
|
260 |
{ |
261 |
// Iterate the action list.
|
262 |
Iterator iter = listeners.iterator();
|
263 |
UninstallerListener il = null;
|
264 |
while( iter.hasNext())
|
265 |
{ |
266 |
try
|
267 |
{ |
268 |
il = (UninstallerListener) iter.next(); |
269 |
switch( action )
|
270 |
{ |
271 |
case UninstallerListener.BEFORE_DELETION:
|
272 |
il.beforeDeletion( (List) param, handler );
|
273 |
break;
|
274 |
case UninstallerListener.AFTER_DELETION:
|
275 |
il.afterDeletion( (List) param, handler );
|
276 |
break;
|
277 |
case UninstallerListener.BEFORE_DELETE:
|
278 |
il.beforeDelete( (File) param, handler );
|
279 |
break;
|
280 |
case UninstallerListener.AFTER_DELETE:
|
281 |
il.afterDelete( (File) param, handler );
|
282 |
break;
|
283 |
} |
284 |
} |
285 |
catch(Throwable e) |
286 |
{ // Catch it to prevent for a block of uninstallation.
|
287 |
handler.emitError("Skipping custom action because exception caught during "
|
288 |
+ il.getClass().getName(), e.toString()); |
289 |
} |
290 |
} |
291 |
} |
292 |
|
293 |
// CUSTOM ACTION STUFF -------------- end -----------------
|
294 |
|
295 |
} |