buildframework/helium/sf/java/core/src/com/nokia/helium/core/ant/HeliumExecutor.java
author wbernard
Fri, 13 Aug 2010 14:59:05 +0300
changeset 628 7c4a911dc066
parent 587 85df38eb4012
permissions -rw-r--r--
helium_11.0.0-e00f171ca185
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     1
/*
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     2
 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     3
 * All rights reserved.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     4
 * This component and the accompanying materials are made available
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     5
 * under the terms of the License "Eclipse Public License v1.0"
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     6
 * which accompanies this distribution, and is available
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     7
 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     8
 *
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     9
 * Initial Contributors:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    10
 * Nokia Corporation - initial contribution.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    11
 *
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    12
 * Contributors:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    13
 *
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    14
 * Description:  
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    15
 *
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    16
 */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    17
package com.nokia.helium.core.ant;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    18
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    19
import java.io.File;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    20
import java.io.IOException;
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    21
import java.net.MalformedURLException;
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    22
import java.net.URL;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    23
import java.util.ArrayList;
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    24
import java.util.Enumeration;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    25
import java.util.Hashtable;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    26
import java.util.Iterator;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    27
import java.util.List;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    28
import java.util.Map;
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    29
import java.util.jar.JarEntry;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    30
import java.util.jar.JarFile;
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    31
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    32
import org.apache.tools.ant.BuildException;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    33
import org.apache.tools.ant.BuildListener;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    34
import org.apache.tools.ant.BuildLogger;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    35
import org.apache.tools.ant.Project;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    36
import org.apache.tools.ant.ProjectHelper;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    37
import org.apache.tools.ant.helper.DefaultExecutor;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    38
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    39
import com.nokia.helium.core.MultiCauseBuildException;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    40
import com.nokia.helium.core.ant.types.HlmDefList;
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    41
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    42
/**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    43
 * This class implements a flexible Ant Executor which allows dynamic discovery
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    44
 * and automatic loading of new features. It also supports pre/post actions to
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    45
 * be executed.
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    46
 * 
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    47
 */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    48
public class HeliumExecutor extends DefaultExecutor {
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    49
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    50
    private static final String HELP_TARGET = "help";
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    51
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    52
    private List<HlmDefList> hlmDefCache = new ArrayList<HlmDefList>();
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    53
    private MultiCauseBuildException failure;
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    54
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    55
    /**
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    56
     * Execute the specified Targets for the specified Project.
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    57
     * 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    58
     * @param project
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    59
     *            the Ant Project.
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    60
     * @param targetNames
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    61
     *            String[] of Target names.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    62
     * @throws BuildException
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    63
     *             on error
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    64
     */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    65
    public void executeTargets(Project project, String[] targetNames) {
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    66
        if (targetNames.length > 1 && targetNames[0].equals(HELP_TARGET)) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    67
            displayHelp(project, targetNames);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    68
        } else {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    69
            executeRegularTargets(project, targetNames);
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    70
        }
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    71
    }
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    72
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    73
    /**
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    74
     * Execute the given targets.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    75
     * 
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    76
     * @param project
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    77
     *            is the ant project
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    78
     * @param targetNames
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    79
     *            array of target names to be executed.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    80
     */
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    81
    public void executeRegularTargets(Project project, String[] targetNames) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    82
        project.log("Running executeTargets", Project.MSG_DEBUG);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    83
        loadModules(project);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    84
        handlePreBuildActions(project, targetNames);
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    85
        try {
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    86
            super.executeTargets(project, targetNames);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    87
        } catch (BuildException be) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    88
            recordFailure(be);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    89
        } finally {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    90
            handlePostBuildActions(project, targetNames);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    91
            // Propagating any raised issues.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    92
            handleException(project);
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    93
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    94
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    95
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    96
    /**
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    97
     * Method loads all the available helium modules from the system classpath.
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    98
     * 
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    99
     * @param project
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   100
     *            is the ant project.
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   101
     */
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   102
    @SuppressWarnings("unchecked")
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   103
    private void loadModules(Project project) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   104
        try {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   105
            List<URL> modules = getAvailableModules();
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   106
            project.log("Total no of modules available : " + modules.size(), Project.MSG_DEBUG);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   107
            for (URL module : modules) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   108
                loadModule(project, module);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   109
            }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   110
            Map<String, Object> references = (Hashtable<String, Object>)project.getReferences();
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   111
            for (String key : references.keySet()) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   112
                Object refObj = references.get(key);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   113
                if (refObj instanceof HlmDefList) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   114
                    hlmDefCache.add((HlmDefList)refObj);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   115
                    project.log("Total pre build actions : "
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   116
                            + ((HlmDefList)refObj).getPreBuildActions().size(), Project.MSG_DEBUG);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   117
                    project.log("Total post build actions : "
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   118
                            + ((HlmDefList)refObj).getPostBuildActions().size(), Project.MSG_DEBUG);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   119
                    project.log("Total exception handlers : "
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   120
                            + ((HlmDefList)refObj).getExceptionHandlers().size(), Project.MSG_DEBUG);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   121
                }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   122
            }            
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   123
        } catch (BuildException be) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   124
            recordFailure(be);
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   125
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   126
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   127
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   128
    /**
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   129
     * Returns a list of available helium modules from the system classpath.
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   130
     * 
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   131
     * @return a list of helium module files.
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   132
     */
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   133
    private List<URL> getAvailableModules() {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   134
        List<URL> moduleList = new ArrayList<URL>();
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   135
        String classpathString = System.getProperty("java.class.path");
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   136
        String[] modules = classpathString.split(File.pathSeparator);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   137
        File module = null;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   138
        URL url = null;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   139
        for (String moduleName : modules) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   140
            module = new File(moduleName);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   141
            if (module != null && module.isFile()
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   142
                    && module.getName().endsWith(".jar")) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   143
                try {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   144
                    String hlmAntlibXmlFile = findHeliumAntlibXml(new JarFile(
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   145
                            module));
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   146
                    if (hlmAntlibXmlFile != null) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   147
                        url = new URL("jar:" + module.toURI().toString() + "!/"
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   148
                                + hlmAntlibXmlFile);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   149
                        moduleList.add(url);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   150
                    }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   151
                } catch (MalformedURLException me) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   152
                    throw new BuildException(
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   153
                            "Error occured while getting helium module "
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   154
                                    + module + " : ", me);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   155
                } catch (IOException ioe) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   156
                    throw new BuildException("Error reading file " + module
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   157
                            + ": " + ioe.getMessage(), ioe);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   158
                }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   159
            }
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   160
        }
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   161
        return moduleList;
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   162
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   163
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   164
    /**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   165
     * Search for helium.antlib.xml under the module Jar.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   166
     * 
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   167
     * @param jarFile
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   168
     *            is the jar to be searched in.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   169
     * @return the helium.antlib.xml
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   170
     */
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   171
    private String findHeliumAntlibXml(JarFile jarFile) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   172
        String hlmAntlibXmlFile = null;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   173
        for (Enumeration<JarEntry> jarEntries = jarFile.entries(); jarEntries
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   174
                .hasMoreElements();) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   175
            JarEntry je = jarEntries.nextElement();
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   176
            if (je.getName().endsWith("/helium.antlib.xml")) {
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   177
                hlmAntlibXmlFile = je.getName();
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   178
                break;
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   179
            }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   180
        }
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   181
        return hlmAntlibXmlFile;
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   182
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   183
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   184
    /**
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   185
     * Method loads the specified module .
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   186
     * 
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   187
     * @param project
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   188
     *            is the ant project.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   189
     * @param module
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   190
     *            the helium module to be loaded.
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   191
     */
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   192
    private void loadModule(Project project, URL module) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   193
        project.log("Loading module : " + module.toString(), Project.MSG_DEBUG);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   194
        ProjectHelper helper = (ProjectHelper) project
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   195
                .getReference(ProjectHelper.PROJECTHELPER_REFERENCE);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   196
        helper.parse(project, module);
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   197
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   198
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   199
    /**
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   200
     * Method handles all the pre build events.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   201
     * 
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   202
     * @param project
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   203
     *            is the ant project.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   204
     * @param targets
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   205
     *            an array of target names.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   206
     */
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   207
    private void handlePreBuildActions(Project project, String[] targets) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   208
        for (HlmDefList hlmDefList : hlmDefCache) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   209
            for (PreBuildAction event : hlmDefList.getPreBuildActions()) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   210
                try {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   211
                    event.executeOnPreBuild(project, targets);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   212
                } catch (BuildException be) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   213
                    // Saving current issue
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   214
                    // We are Ignoring the errors as no need to fail the build.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   215
                    recordFailure(be);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   216
                }
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   217
            }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   218
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   219
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   220
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   221
    /**
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   222
     * Method handles all the post build events.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   223
     * 
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   224
     * @param project
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   225
     *            is the ant project.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   226
     * @param targets
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   227
     *            an array of target names.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   228
     */
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   229
    private void handlePostBuildActions(Project project, String[] targets) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   230
        for (HlmDefList hlmDefList : hlmDefCache) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   231
            for (PostBuildAction event : hlmDefList.getPostBuildActions()) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   232
                try {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   233
                    event.executeOnPostBuild(project, targets);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   234
                } catch (BuildException be) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   235
                    // Treating possible new issues...
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   236
                    recordFailure(be);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   237
                }
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   238
            }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   239
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   240
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   241
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   242
    /**
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   243
     * Records a build failure.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   244
     * 
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   245
     * @param be
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   246
     *            a build failure.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   247
     */
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   248
    private void recordFailure(BuildException be) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   249
        if (failure == null) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   250
            failure = new MultiCauseBuildException();
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   251
        }
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   252
        failure.add(be);
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   253
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   254
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   255
    /**
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   256
     * Method handles the recored build failures if any.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   257
     * 
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   258
     * @param project
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   259
     *            is the ant project.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   260
     */
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   261
    private void handleException(Project project) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   262
        if (failure != null) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   263
            for (HlmDefList hlmDefList : hlmDefCache) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   264
                for (HlmExceptionHandler handler : hlmDefList
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   265
                        .getExceptionHandlers()) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   266
                    try {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   267
                        handler.handleException(project, failure);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   268
                    } catch (BuildException be) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   269
                        // Treating possible new issues...
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   270
                        recordFailure(be);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   271
                    }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   272
                }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   273
            }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   274
            throw failure;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   275
        }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   276
    }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   277
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   278
    @SuppressWarnings("unchecked")
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   279
    private void displayHelp(Project project, String[] targetNames) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   280
        if (targetNames.length > 1) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   281
            project.setProperty("help.item", targetNames[1]);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   282
        }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   283
        // Set Emacs mode to true for all listeners, so that help text does
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   284
        // not have [echo] at the start of each line
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   285
        Iterator<BuildListener> iter = project.getBuildListeners().iterator();
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   286
        while (iter.hasNext()) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   287
            BuildListener listener = iter.next();
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   288
            if (listener instanceof BuildLogger) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   289
                BuildLogger logger = (BuildLogger) listener;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   290
                logger.setEmacsMode(true);
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   291
            }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   292
        }
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   293
        // Run the 'help' target
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   294
        project.executeTarget(HELP_TARGET);
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   295
    }
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   296
}