KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > event > AntActionUninstallerListener


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 2004 Klaus Bartz
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 package com.izforge.izpack.event;
22
23 import java.io.File JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.ObjectInputStream JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29
30 import com.izforge.izpack.util.AbstractUIProgressHandler;
31 import com.izforge.izpack.util.IoHelper;
32
33 /**
34  * Uninstaller listener for performing ANT actions at uninstall time. The definition of what should
35  * be done here will be made in a specification file that is referenced by the resource id
36  * "AntActionsSpec.xml". There should be an entry in the install.xml file in the sub ELEMENT "res"
37  * of ELEMENT "resources" that references it. The specification of the xml file is done in the DTD
38  * antaction.dtd. The xml file may contain an ELEMENT "uninstall_target" that should be performed
39  * for uninstalling purposes.
40  *
41  * @author Klaus Bartz
42  */

43 public class AntActionUninstallerListener extends SimpleUninstallerListener
44 {
45
46     /** Ant actions to be performed after deletion */
47     private List JavaDoc antActions = null;
48
49     /**
50      * Default constructor
51      */

52     public AntActionUninstallerListener()
53     {
54         super();
55         // TODO Auto-generated constructor stub
56
}
57
58     /*
59      * (non-Javadoc)
60      *
61      * @see com.izforge.izpack.uninstaller.UninstallerListener#beforeDeletion(java.util.List,
62      * com.izforge.izpack.util.AbstractUIProgressHandler)
63      */

64     public void beforeDeletion(List JavaDoc files, AbstractUIProgressHandler handler) throws Exception JavaDoc
65     {
66         // Load the defined actions.
67
InputStream JavaDoc in = getClass().getResourceAsStream("/antActions");
68         if (in == null)
69         { // No actions, nothing todo.
70
return;
71         }
72         ObjectInputStream JavaDoc objIn = new ObjectInputStream JavaDoc(in);
73         // The actions are stored at installation time as list of AntAction
74
// objects.
75
// See AntActionInstallerListener.afterPacks.
76
List JavaDoc allActions = (List JavaDoc) objIn.readObject();
77         objIn.close();
78         in.close();
79         ArrayList JavaDoc befDel = new ArrayList JavaDoc();
80         antActions = new ArrayList JavaDoc();
81         Iterator JavaDoc iter = allActions.iterator();
82         // There are two possible orders; before and after deletion.
83
// Now we assign the actions to two different lists, the
84
// local "before" list which we perform after the scan and
85
// the class member "antActions" which should contain the
86
// "afterdeletion" actions. Additionally we should save needed
87
// files like the properties file for this order because if they're
88
// part of the pack the'll be lost after the deletion has been
89
// performed.
90
while (iter.hasNext())
91         {
92             AntAction action = (AntAction) iter.next();
93             //
94
if (action.getUninstallOrder().equals(ActionBase.BEFOREDELETION))
95                 befDel.add(action);
96             else
97             {// We need the build and the properties file(s) outside the
98
// install dir.
99
File JavaDoc tmpFile = IoHelper.copyToTempFile(action.getBuildFile(), ".xml");
100                 action.setBuildFile(tmpFile.getCanonicalPath());
101                 List JavaDoc props = action.getPropertyFiles();
102                 if (props != null)
103                 {
104                     Iterator JavaDoc iter2 = props.iterator();
105                     ArrayList JavaDoc newProps = new ArrayList JavaDoc();
106                     while (iter2.hasNext())
107                     {
108                         String JavaDoc propName = (String JavaDoc) iter2.next();
109                         File JavaDoc propFile = IoHelper.copyToTempFile(propName, ".properties");
110                         newProps.add(propFile.getCanonicalPath());
111                     }
112                     action.setPropertyFiles(newProps);
113                 }
114                 antActions.add(action);
115             }
116         }
117         // Perform the actions with the order "beforedeletion".
118
if (befDel.size() > 0)
119         {
120             for (int i = 0; i < befDel.size(); i++)
121             {
122                 AntAction act = (AntAction) befDel.get(i);
123                 act.performUninstallAction();
124             }
125         }
126
127     }
128
129     /*
130      * (non-Javadoc)
131      *
132      * @see com.izforge.izpack.uninstaller.UninstallerListener#afterDeletion(java.util.List,
133      * com.izforge.izpack.util.AbstractUIProgressHandler)
134      */

135     public void afterDeletion(List JavaDoc files, AbstractUIProgressHandler handler) throws Exception JavaDoc
136     {
137         if (antActions != null && antActions.size() > 0)
138         { // There are actions of the order "afterdeletion".
139
for (int i = 0; i < antActions.size(); i++)
140             {
141                 AntAction act = (AntAction) antActions.get(i);
142                 act.performUninstallAction();
143             }
144
145         }
146     }
147
148 }
149
Popular Tags