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 */
021
022package com.izforge.izpack.event;
023
024import java.io.File;
025import java.io.FileOutputStream;
026import java.io.IOException;
027import java.io.PrintWriter;
028
029import com.izforge.izpack.installer.AutomatedInstallData;
030import com.izforge.izpack.util.AbstractUIProgressHandler;
031import com.izforge.izpack.util.Debug;
032import com.izforge.izpack.util.IoHelper;
033import com.izforge.izpack.util.SummaryProcessor;
034import com.izforge.izpack.util.VariableSubstitutor;
035
036/**
037 * Installer listener which writes the summary of all panels into the logfile which is defined by
038 * info.summarylogfilepath. Default is $INSTALL_PATH/Uninstaller/InstallSummary.htm
039 * 
040 * @author Klaus Bartz
041 * 
042 */
043public class SummaryLoggerInstallerListener extends SimpleInstallerListener
044{
045
046    /**
047     * Default constructor.
048     */
049    public SummaryLoggerInstallerListener()
050    {
051        super(false);
052    }
053
054    /*
055     * (non-Javadoc)
056     * 
057     * @see com.izforge.izpack.compiler.InstallerListener#afterPacks(com.izforge.izpack.installer.AutomatedInstallData,
058     * com.izforge.izpack.util.AbstractUIProgressHandler)
059     */
060    public void afterPacks(AutomatedInstallData idata, AbstractUIProgressHandler handler)
061            throws Exception
062    {
063        if (!getInstalldata().installSuccess) return;
064        // No logfile at automated installation because panels are not
065        // involved.
066        if (getInstalldata().panels == null || getInstalldata().panels.size() < 1) return;
067        String path = getInstalldata().info.getSummaryLogFilePath();
068        if (path == null) return;
069        VariableSubstitutor vs = new VariableSubstitutor(getInstalldata().getVariables());
070        path = IoHelper.translatePath(path, vs);
071        File parent = new File(path).getParentFile();
072
073        if (!parent.exists())
074        {
075            parent.mkdirs();
076        }
077        PrintWriter logfile = null;
078        try
079        {
080            logfile = new PrintWriter(new FileOutputStream(path), true);
081        }
082        catch (IOException e)
083        {
084            Debug.error(e);
085        }
086        String summary = SummaryProcessor.getSummary(getInstalldata());
087        logfile.print(summary);
088        logfile.close();
089    }
090
091}