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 2004 Klaus Bartz
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 */
021package com.izforge.izpack.event;
022
023import java.io.File;
024import java.io.InputStream;
025import java.io.ObjectInputStream;
026import java.util.ArrayList;
027import java.util.Iterator;
028import java.util.List;
029
030import com.izforge.izpack.util.AbstractUIProgressHandler;
031import com.izforge.izpack.util.IoHelper;
032
033/**
034 * Uninstaller listener for performing ANT actions at uninstall time. The definition of what should
035 * be done here will be made in a specification file that is referenced by the resource id
036 * "AntActionsSpec.xml". There should be an entry in the install.xml file in the sub ELEMENT "res"
037 * of ELEMENT "resources" that references it. The specification of the xml file is done in the DTD
038 * antaction.dtd. The xml file may contain an ELEMENT "uninstall_target" that should be performed
039 * for uninstalling purposes.
040 * 
041 * @author Klaus Bartz
042 */
043public class AntActionUninstallerListener extends SimpleUninstallerListener
044{
045
046    /** Ant actions to be performed after deletion */
047    private List antActions = null;
048
049    /**
050     * Default constructor
051     */
052    public AntActionUninstallerListener()
053    {
054        super();
055        // TODO Auto-generated constructor stub
056    }
057
058    /*
059     * (non-Javadoc)
060     * 
061     * @see com.izforge.izpack.uninstaller.UninstallerListener#beforeDeletion(java.util.List,
062     * com.izforge.izpack.util.AbstractUIProgressHandler)
063     */
064    public void beforeDeletion(List files, AbstractUIProgressHandler handler) throws Exception
065    {
066        // Load the defined actions.
067        InputStream in = getClass().getResourceAsStream("/antActions");
068        if (in == null)
069        { // No actions, nothing todo.
070            return;
071        }
072        ObjectInputStream objIn = new ObjectInputStream(in);
073        // The actions are stored at installation time as list of AntAction
074        // objects.
075        // See AntActionInstallerListener.afterPacks.
076        List allActions = (List) objIn.readObject();
077        objIn.close();
078        in.close();
079        ArrayList befDel = new ArrayList();
080        antActions = new ArrayList();
081        Iterator iter = allActions.iterator();
082        // There are two possible orders; before and after deletion.
083        // Now we assign the actions to two different lists, the
084        // local "before" list which we perform after the scan and
085        // the class member "antActions" which should contain the
086        // "afterdeletion" actions. Additionally we should save needed
087        // files like the properties file for this order because if they're
088        // part of the pack the'll be lost after the deletion has been
089        // performed.
090        while (iter.hasNext())
091        {
092            AntAction action = (AntAction) iter.next();
093            // 
094            if (action.getUninstallOrder().equals(ActionBase.BEFOREDELETION))
095                befDel.add(action);
096            else
097            {// We need the build and the properties file(s) outside the
098                // install dir.
099                File tmpFile = IoHelper.copyToTempFile(action.getBuildFile(), ".xml");
100                action.setBuildFile(tmpFile.getCanonicalPath());
101                List props = action.getPropertyFiles();
102                if (props != null)
103                {
104                    Iterator iter2 = props.iterator();
105                    ArrayList newProps = new ArrayList();
106                    while (iter2.hasNext())
107                    {
108                        String propName = (String) iter2.next();
109                        File 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 files, AbstractUIProgressHandler handler) throws Exception
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}