Subversion Repositories DevTools

Rev

Rev 7033 | Rev 7048 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.erggroup.buildtool.ripple;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.erggroup.buildtool.ripple.BuildFile.BuildFileState;
import com.erggroup.buildtool.ripple.ReleaseManager.BuildReason;
import com.erggroup.buildtool.smtp.CreateUrls;
import com.erggroup.buildtool.smtp.Smtpsend;
import com.erggroup.buildtool.utilities.StringAppender;
import com.erggroup.buildtool.utilities.XmlBuilder;

/**Plans release impact by generating a set of Strings containing build file content.
 */
public class RippleEngine
{

    /**configured mail server
     * @attribute
     */
    private String mMailServer = "";

    /**configured mail sender user
     * @attribute
     */
    private String mMailSender = "";

    /**configured global email target
     * @attribute
     */
    private String mMailGlobalTarget = "";

    /** Vector of email addresses for global and project wide use
     *  Most emails will be sent to this list of email recipients
     */
    public List<String> mMailGlobalCollection = new ArrayList<String>();

    /**name associated with the baseline
     * @attribute
     */
    public String mBaselineName = "";

    /**collection of released pv_ids associated with the release
     * @attribute
     */
    public List<Integer> mReleasedPvIDCollection = new ArrayList<Integer>();

    /**Collection of build exceptions associated with the baseline
     * Used to determine (and report) what change in build exceptions happens as part of planRelease
     * Daemon centric
     * @aggregation shared
     * @attribute
     */
    ArrayList<BuildExclusion> mBuildExclusionCollection = new ArrayList<BuildExclusion>();

    /**Logger
     * @attribute
     */
    private static final Logger mLogger = LoggerFactory.getLogger(RippleEngine.class);

    /** Escrow information - commands to set up escrow
     * @attribute
     */
    private String mEscrowSetup;

    /** Escrow information - Raw data (May not be used)
     * @attribute
     */
    private String mEscrowRawData;

    /**package versions representing the baseline
     * escrow centric
     * @aggregation shared
     * @attribute
     */
    private ArrayList<Package> mPackageCollection = new ArrayList<Package>();

    /**index to current String item
     * @attribute
     */
    private int mBuildIndex;

    /**Database abstraction
     * @attribute
     */
    ReleaseManager mReleaseManager;

    /**Baseline identifier (rtag_id for a release manager baseline, bom_id for deployment manager baseline)
     * @attribute
     */
    private int mBaseline;

    /** Escrow Only: SBOM_ID
     */
    private int mSbomId;

    /** RTAG_ID
     *  Set from mBaseline
     */
    private int mRtagId;

    /**When true, mBuildCollection contains one item based on a release manager rtag_id and contains a daemon property
     * When false, mBuildCollection contains at least one item based on a deployment manager bom_id
     * Will be accessed by the Package class to calculate its mAlias
     * @attribute
     */
    public boolean mDaemon;

    /**collection of build file content in String form
     * @attribute
     */
    private ArrayList<BuildFile> mBuildCollection = new ArrayList<BuildFile>();

    /** List of packages that we plan to build
     * Used to provide feedback into RM
     * Only the first entry is about to be built as we re-plan every cycle
     */
    private ArrayList<Package> mBuildOrder = new ArrayList  <Package>();
    /**Warning message
     * @attribute
     */
    private static final String mAnyBuildPlatforms = "Warning. The following package versions are not reproducible on any build platform: ";

    /**Flag to control output to standard out
     * @attribute
     */
    private boolean mAnyBuildPlatformsFlag = true;

    /**Warning message
     * @attribute
     */
    private static final String mAssocBuildPlatforms = "Warning. The following package versions are not reproducible on the build platforms associated with this baseline: ";

    /**Flag to control output to standard out
     * @attribute
     */
    private boolean mAssocBuildPlatformsFlag = true;

    /**Warning message
     * @attribute
     */
    private static final String mNotInBaseline = "Warning. The following package versions are not reproducible as they are directly dependent upon package versions not in the baseline: ";

    /**Flag to control output to standard out
     * @attribute
     */
    private boolean mNotInBaselineFlag = true;

    /**Warning message
     * @attribute
     */
    private static final String mDependent = "Warning. The following package versions are not reproducible as they are directly/indirectly dependent upon not reproducible package versions: ";

    /**Flag to control output to standard out
     * @attribute
     */
    private boolean mDependentFlag = true;

    /**Warning message
     * @attribute
     */
    private static final String mCircularDependency = "Warning. The following package versions are not reproducible as they have circular dependencies: ";

    /**Flag to control output to standard out
     * @attribute
     */
    private boolean mCircularDependencyFlag = true;

    /** String used to terminate lines
     * @attribute
     */
    private static final  String mlf = System.getProperty("line.separator");

    /** XML File Prefix
     */
    private static final String mXmlHeader = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>" + mlf;

    /**RippleEngine constructor
     * @param releaseManager  - Associated releaseManager instance
     * @param rtagId         - Release Identifier
     * @param isDaemon        - Mode of operation. False: Escrow, True: Daemon
     */
    public RippleEngine(ReleaseManager releaseManager, int rtagId, boolean isDaemon)
    {
        mLogger.debug("RippleEngine rtag_id " + rtagId + " isDaemon " + isDaemon);
        mReleaseManager = releaseManager;
        mBaseline = rtagId;
        mRtagId = rtagId;
        mDaemon = isDaemon;
        mReleaseManager.setDaemonMode(mDaemon);
    }

    /**
     * getRtagId
     */
    public int getRtagId()
    {
        return mRtagId;
    }

    /**Plan what is to be built
     *  <br>Discards all build file content
     *  <br>Generates new build file content
     * 
     * @param lastBuildActive           - False. Daemon Mode. The last build was a dummy. 
     *                                This planning session may not result in a build
     *                              - True. Daemon Mode. The last build was not a dummy.
     *                                There is a very good chance that this planning session
     *                                will result in a build, so it is given priority to connect 
     *                                to the database.  
     */
    public void planRelease(final boolean lastBuildActive) throws SQLException, Exception
    {
        mLogger.warn("planRelease mDaemon " + mDaemon);

        mBuildCollection.clear();
        mPackageCollection.clear();
        mReleasedPvIDCollection.clear();
        mBuildOrder.clear();
        mEscrowRawData = "";
        mEscrowSetup = "";
        Phase phase = new Phase("Plan");

        // use finally block in planRelease to ensure the connection is released
        try
        {
            phase.setPhase("connectForPlanning");
            mReleaseManager.connectForPlanning(lastBuildActive);

            if ( mDaemon )
            {
                // claim the mutex
                mLogger.warn("planRelease claimMutex");
                phase.setPhase("claimMutex");
                mReleaseManager.claimMutex();

                // Populate the mBuildExclusionCollection
                //
                // Builds are either 'Directly excluded' or 'Indirectly excluded'
                // Direct excludes result from:
                //      User request
                //      Build failure
                //      Inability to build package
                // Indirectly excluded packages result from have a build dependency on a package
                // that is directly excluded.
                //  
                // In the following code we will extract from the Database all build exclusions
                // We will then add 'Relevant' entries to mBuildExclusionCollection
                // and delete, from the database those that are no longer relevant - ie indirectly excluded
                // items where the root cause is no longer in the set.
                phase.setPhase("mBuildExclusionCollection");
                mBuildExclusionCollection.clear();
                ArrayList<BuildExclusion> tempBuildExclusionCollection = new ArrayList<BuildExclusion>();

                mLogger.debug("planRelease queryBuildExclusions");
                mReleaseManager.queryBuildExclusions(tempBuildExclusionCollection, mBaseline);

                // only populate mBuildExclusionCollection with tempBuildExclusionCollection entries which have a relevant root_pv_id
                // The entry is relevant if:
                //     It is for a directly excluded package,other than a RippleStop
                //     It is for an indirectly excluded package AND the reason for exclusion still exists
                //

                for (Iterator<BuildExclusion> it = tempBuildExclusionCollection.iterator(); it.hasNext(); )
                {
                    BuildExclusion buildExclusion = it.next();

                    if ( buildExclusion.isRelevant(tempBuildExclusionCollection) )
                    {
                        mBuildExclusionCollection.add(buildExclusion);
                    }
                    else
                    {
                        // Remove the indirectly excluded entry as its root cause
                        // is no longer present.
                        buildExclusion.includeToBuild(mReleaseManager, mBaseline);
                    }
                }
            }

            //-----------------------------------------------------------------------
            //  Query package versions
            //
            phase.setPhase("queryPackageVersions");
            mLogger.debug("planRelease queryPackageVersions");
            mReleaseManager.queryPackageVersions(this, mPackageCollection, mBaseline);

            //------------------------------------------------------------------------
            //    Process packages collected
            //    Determine and tag those we can't build
            phase.setPhase("processPackages");
            processPackages();

            //-----------------------------------------------------------------------
            //    At this point we have tagged all the packages that we cannot build
            //    Now we can determine what we are building
            phase.setPhase("planBuildOrder");
            mLogger.debug("planRelease process Remaining");
            planBuildOrder();

            //  Report excluded packages and the build plan
            //  This is being done with the MUTEX being held, but the 
            //  trade off is the cost of getting a connection.
            //
            if ( mDaemon )
            {
                phase.setPhase("Report Change");
                reportChange();
                phase.setPhase("Report Plan");            
                reportPlan();
            }
            
            //
            //  Generate the build Files
            //
            phase.setPhase("generateBuildFiles");
            generateBuildFiles();
        }
        finally
        {
            mLogger.debug("planRelease finally");
            // this block is executed regardless of what happens in the try block
            // even if an exception is thrown
            // ensure the SELECT FOR UPDATE is released
            try
            {
                if ( mDaemon )
                {
                    // attempt to release the SELECT FOR UPDATE through a commit
                    // a commit must be done in the normal case
                    // a commit may as well be done in the Exception case
                    // in the case of a SQLException indicating database connectivity has been lost
                    // having a go at the commit is superfluous
                    // as the SELECT FOR UPDATE will have been released upon disconnection
                    phase.setPhase("releaseMutex");
                    mReleaseManager.releaseMutex();
                }
            }
            finally
            {
                // ensure disconnect under all error conditions
                phase.setPhase("disconnectForPlanning");
                mReleaseManager.disconnectForPlanning(lastBuildActive);
            }
        }

        mLogger.warn("planRelease mDaemon " + mDaemon + " returned");
        phase.setPhase("EndPlan");
    }
    
    /** Process packages that have been collected as a part of the plan
     * @param phase
     * @throws SQLException
     * @throws Exception
     */
    private void processPackages() throws SQLException, Exception {

        // Deal with test builds here as they may impact upon package attributes
        //    eg dependency collection and build standard differences
        //    This gives test builds preferential treatment
        //    Note: Done before mPackageDependencyCollection is setup
        //
        if ( mDaemon )
        {
            // process test builds
            for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
            {
                Package p = it.next();

                if (p.mBuildFile == 0)
                {
                    // package has yet to be processed
                    if (  p.mTestBuildInstruction > 0 )
                    {
                        mLogger.info("planRelease package test build " + p.mName);

                        //
                        //    Cannot test build an SDK based package or a Pegged Package
                        //        Remove the test build request from the database
                        //        Send a warning email
                        //
                        if(p.mIsPegged || p.mIsSdk)
                        {
                            String reason;
                            reason = (p.mIsPegged) ? "Pegged" : "SDK Based";

                            mLogger.error("planRelease Daemon Instruction of " + reason + " package deleted: " + p.mName);
                            mReleaseManager.markDaemonInstCompleted( p.mTestBuildInstruction );
                            emailRejectedDaemonInstruction("Cannot 'Test Build' a " + reason + " package",p);
                        }

                        // force patch for test build numbering
                        p.mDirectlyPlanned = true;
                        p.mChangeType.setPatch();
                        p.mRequiresSourceControlInteraction = false;
                        p.mBuildReason = BuildReason.Test;
                        rippleIndirectlyPlanned(p);

                        // put the mTestBuildAttributes to work
                        p.mVcsTag = p.mTestBuildVcsTag;
                        p.mId = p.mTestBuildPvId;
                        p.setTestEmail();
                        p.setTestDependencyCollection();
                        p.setTestBuildStandardCollection();
                        p.mHasAutomatedUnitTests = p.mTestBuildHasAutomatedUnitTests;
                    }
                }
            }
        }

        // Set up mPackageDependencyCollection on each package
        //    Examine the dependencies by alias and convert this to a 'package' selected from the released package set
        mLogger.debug("planRelease setup mPackageDependencyCollection");
        for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
        {
            Package p = it.next();

            for (Iterator<String> it2 = p.mDependencyCollection.iterator(); it2.hasNext(); )
            {
                String alias = it2.next();
                Package dependency = findPackage(alias);

                p.mPackageDependencyCollection.add(dependency);
            }
        }

        // DEVI 56479 detect and deal with circular dependencies
        mLogger.debug("planRelease deal with circular dependencies");
        for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
        {
            Package p = it.next();

            if ( p.hasCircularDependency( this ) )
            {
                mLogger.info("planRelease circular dependency detected " + p.mAlias);
                
                //  Force this package to be marked as having a circular dependency - even if its been excluded
                p.mBuildFile = 0;
                
                // exclude all dependent packages
                // max 50 chars
                rippleBuildExclude(p, p.mId, "Package has circular dependency", null, null, true);

                // take the package out of the build
                p.mBuildFile = -6;
                mLogger.info("planRelease set mBuildFile to -6 for package " + p.mAlias );
                standardOut(mCircularDependency, p.mAlias, mCircularDependencyFlag);
                mCircularDependencyFlag = false;
            }
        }

        // Scan for packages with missing dependencies
        //    ie: The dependent package is not in the package Collection
        //        DEVI 55483 now use the fully built mPackageDependencyCollection (in rippleBuildExclude)
        mLogger.debug("planRelease use the fully built mPackageDependencyCollection");
        for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
        {
            Package p = it.next();

            if ( mDaemon )
            {
                //  Daemon Mode Only
                //  Not interested in the dependencies of a pegged package or a package provided from an SDK.
                //  Such packages will be deemed to not have missing dependencies
                if (p.mIsPegged || p.mIsSdk)
                {
                    continue;
                }
            }

            for (Iterator<String> it2 = p.mDependencyCollection.iterator(); it2.hasNext(); )
            {
                String alias = it2.next();
                Package dependency = findPackage(alias);

                if (dependency == ReleaseManager.NULL_PACKAGE)
                {
                    mLogger.info("planRelease dependency is not in the baseline " + alias);
                    // exclude all dependent packages
                    // max 50 chars
                    rippleBuildExclude(p, p.mId, "Package build dependency not in the release", null, null, true);

                    // take the package out of the build
                    p.mBuildFile = -4;
                    mLogger.info("planRelease set mBuildFile to -4 for package " + p.mAlias );
                    standardOut(mNotInBaseline, p.mAlias, mNotInBaselineFlag);
                    mNotInBaselineFlag = false;
                    break;
                }
            }
        }

        // Process packages which are not reproducible, and all packages dependent upon them
        //    ie: Have no build standard      
        mLogger.debug("planRelease process packages which are not reproducible");
        for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
        {
            Package p = it.next();

            if (p.mBuildFile == 0)
            {
                if ( mDaemon )
                {
                    //  Daemon Mode Only
                    //  Not interested in the reproducibility of a pegged package or a package provided from an SDK.
                    //  Such packages will be deemed to be reproducible - but out side of this release
                    if (p.mIsPegged || p.mIsSdk)
                    {
                        continue;
                    }
                }

                // package has yet to be processed
                if (!p.isReproducible())
                {
                    // for escrow build purposes, exclude all dependent package versions
                    mLogger.info("planRelease package not reproducible " + p.mName);
                    // max 50 chars
                    rippleBuildExclude(p, p.mId, "Package has no build environment", null, null, true);

                    // package is not reproducible, discard
                    p.mBuildFile = -1;
                    mLogger.info("planRelease set mBuildFile to -1 for package " + p.mAlias );
                    standardOut(mAnyBuildPlatforms, p.mAlias, mAnyBuildPlatformsFlag);
                    mAnyBuildPlatformsFlag = false;
                }
            }
        }

        //    Process packages which are not reproducible on the configured set of  build machines.
        //
        //    Test each package and determine if the package contains a buildStandard that
        //    can be processed by one of the machines in the build set
        //
        //    ie: Package: Win32:Production and we have a BM with a class of 'Win32'
        //
        //    July-2014 
        //    Only exclude the failing package and not its dependents
        //    May be legitimate in the release
        //
        mLogger.debug("planRelease process packages which are not reproducible2");
        for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
        {
            Package p = it.next();

            if (p.mBuildFile == 0)
            {
                if ( mDaemon )
                {
                    //  Daemon Mode Only
                    //  Not interested in the reproducibility of a pegged package or a package provided from an SDK.
                    //  Such packages will be deemed to be reproducible - but out side of this release
                    if (p.mIsPegged || p.mIsSdk)
                    {
                        continue;
                    }
                }

                // package has yet to be processed
                // assume it does not need to be reproduced for this baseline
                //
                //    For each machineClass in the buildset
                boolean reproduce = false;

                for (Iterator<String> it2 = mReleaseManager.mReleaseConfigCollection.mMachineClasses.iterator(); it2.hasNext(); )
                {
                    String machineClass = it2.next();

                    if ( p.canBeBuildby(machineClass))
                    {
                        reproduce = true;
                        mLogger.info("planRelease package built on " + machineClass + " " + p.mAlias );
                        break;
                    }
                }

                if ( !reproduce )
                {
                    mLogger.info("planRelease package not reproducible on the build platforms configured for this baseline " + p.mName);

                    if (mDaemon)
                    {
                        // DEVI 54816
                        // for escrow build purposes, do not exclude all dependent package versions
                        // max 50 chars
                        rippleBuildExclude(p, p.mId, "Package not built for configured platforms", null, null, false);
                    }

                    // package is not reproducible on the build platforms configured for this baseline, discard
                    p.mBuildFile = -2;
                    mLogger.info("planRelease set mBuildFile to -2 for package " + p.mAlias );
                    standardOut(mAssocBuildPlatforms, p.mAlias, mAssocBuildPlatformsFlag);
                    mAssocBuildPlatformsFlag = false;
                }
            }
        }      

        if (mDaemon)
        {
            // Daemon Mode Only
            // Process packages which are not ripple buildable, and all packages dependent upon them
            mLogger.debug("planRelease process packages which are not ripple buildable");
            for (ListIterator<BuildExclusion> it = mBuildExclusionCollection.listIterator(); it.hasNext(); )
            {
                BuildExclusion be = it.next();

                for (Iterator<Package> it1 = mPackageCollection.iterator(); it1.hasNext(); )
                {
                    Package p = it1.next();

                    // ensure only root cause, non test build, build exclusions are excluded
                    // mBuildExclusionCollection is at this point based on
                    // relevant (direct and indirect) excluded pv's in the database
                    if ( be.compare(p.mId) && be.isARootCause() && p.mTestBuildInstruction == 0 && p.mForcedRippleInstruction == 0 )
                    {
                        // package is not reproducible, discard
                        rippleBuildExclude( p, p.mId, null, it, be, true );
                        p.mBuildFile = -3;
                        mLogger.info("planRelease set mBuildFile to -3 for package " + p.mAlias );
                        break;
                    }
                }
            }

            //  Daemon Mode Only
            //  Process packages which need to be ripple built
            mLogger.debug("planRelease process packages which need to be ripple built");
            for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
            {
                Package p = it.next();

                if (p.mBuildFile == 0)
                {
                    //  Not interested in a pegged package or a package provided from an SDK.
                    //  Such packages are not rippled
                    if (p.mIsPegged || p.mIsSdk)
                    {
                        continue;
                    }

                    // package has yet to be processed
                    if (p.mDirectlyPlanned)
                    {
                        // a WIP exists on the package, or it is a Test Build
                        // exclude all dependent package versions
                        mLogger.info("planRelease package has WIP " + p.mName);
                        if ( p.mBuildReason == null)
                            p.mBuildReason = BuildReason.NewVersion;
                        rippleIndirectlyPlanned(p);
                    }
                    else
                    {
                        //    Examine this packages dependencies
                        //    If one of them does not exist in the 'official release' set then
                        //    the package needs to be built against the official release set
                        //    and we can't build any of its dependent packages
                        Iterator<Integer> it2 = p.mDependencyIDCollection.iterator();
                        Iterator<Package> it3 = p.mPackageDependencyCollection.iterator();
                        while ( it2.hasNext() && it3.hasNext() )
                        {
                            Integer dpvId = it2.next();
                            Package dependency = it3.next();

                            if ( !dependency.mAdvisoryRipple )
                            {
                                // not advisory, ie: has ripple build impact
                                if ( !isInRelease(dpvId) )
                                {
                                    // the package is out of date
                                    // exclude all dependent package versions
                                    mLogger.info("planRelease package out of date " + p.mName);
                                    p.mBuildReason = BuildReason.Ripple;
                                    rippleIndirectlyPlanned(p);
                                    
                                    //  This package needs to be rippled
                                    //  If this package has a rippleStop marker of 's', then we cannot
                                    //  build this package at the moment.
                                    //  Packages that depend on this package have been excluded
                                    //  We need to exclude this one too
                                    //
                                    if (p.mRippleStop == 's' || p.mRippleStop == 'w') 
                                    {
                                        // Package marked as a rippleStop
                                        // max 50 chars
                                        rippleBuildExclude(p, -2, "Ripple Required." + " Waiting for user", null, null, false);

                                        // package is unBuildable, mark reason and discard
                                        p.mBuildFile = -11;
                                        
                                        if (p.mRippleStop == 's' ) {
                                            // Need to flag to users that the package build is waiting user action
                                            mLogger.info("planRelease " + "Ripple Required." + " Stopped by flag" + p.mName);
                                            mReleaseManager.setRippleStopWait(mRtagId,p);
                                        }
                                    }
                                    
                                    
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            //  Daemon Mode Only
            //  Process packages which do not exist in the archive
            //  For unit test purposes, assume all packages exist in the archive if released
            mLogger.debug("planRelease process packages which do not exist in the archive");
            if ( ReleaseManager.mUseDatabase )
            {
                for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
                {
                    Package p = it.next();

                    if (p.mBuildFile == 0)
                    {
                        // package has yet to be processed
                        if (!p.mDirectlyPlanned && !p.mIndirectlyPlanned && p.mForcedRippleInstruction == 0)
                        {
                            // check package version archive existence
                            if (!p.existsInDpkgArchive())
                            {
                                if (! p.mIsBuildable)
                                {
                                    //  Package does not exist in dpkg_archive and it has been flagged as unbuildable
                                    //  This may be because is Unbuildable or Manually built
                                    mLogger.info("planRelease " + "Unbuildable" + " package not found in archive " + p.mName);
                                    // max 50 chars
                                    rippleBuildExclude(p, p.mId, "Unbuildable" + " package not found in archive", null, null, true);

                                    // package is unBuildable, mark reason and discard
                                    p.mBuildFile = -10;
                                    
                                }
                                //  Not interested in a pegged package or a package provided from an SDK.
                                //  Such packages are not rippled
                                else if (p.mIsPegged || p.mIsSdk)
                                {
                                    String reason;
                                    reason = (p.mIsPegged) ? "Pegged" : "SDK";

                                    //  Pegged packages or packages provided from an SDK MUST exist in dpkg_archive
                                    //  They will not be built within the context of this release. It is the responsibility
                                    //  of another release to build them.
                                    mLogger.info("planRelease " + reason + " package not found in archive " + p.mName);
                                    // max 50 chars
                                    rippleBuildExclude(p, p.mId, reason + " package not found in archive", null, null, true);

                                    // package is not reproducible, mark reason and discard
                                    p.mBuildFile = -7;

                                }
                                else if (p.mForcedRippleInstruction == 0)
                                {
                                    //  [JATS-331] Unable to rebuild package with Advisory Ripple dependencies
                                    //    Examine this packages dependencies
                                    //    If one of them does not exist in the 'official release' set then
                                    //    the package cannot be rebuilt in this release.
                                    for (Iterator<Integer> it2 = p.mDependencyIDCollection.iterator() ; it2.hasNext() ;)
                                    {
                                        Integer dpvId = it2.next();
                                        if ( !isInRelease(dpvId) )
                                        {
                                            // This package cannot be rebuilt as one of its dependents is NOT in this release
                                            // exclude all dependent package versions
                                            
                                            mLogger.info("planRelease package not found in archive. Cannot be rebuilt du to  " + p.mName);
                                            // max 50 chars
                                            rippleBuildExclude(p, p.mId, "Package cannot be rebuilt in this release", null, null, true);

                                            // package is not reproducible, mark reason and discard
                                            p.mBuildFile = -4;
                                            break;
                                        }
                                    }
                                    
                                    //  The package has not been excluded from the build
                                    if (p.mBuildFile == 0)
                                    {
                                        mLogger.info("planRelease package not found in archive " + p.mName);
                                        // DEVI 47395 the cause of this build is not WIP or ripple induced,
                                        // it simply does not exist in the archive (has been removed)
                                        // prevent source control interaction
                                        p.mRequiresSourceControlInteraction = false;
                                        p.mBuildReason = BuildReason.Restore;
                                        rippleIndirectlyPlanned(p);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            //  Daemon Mode Only
            //  Process forced ripples
            mLogger.debug("planRelease process forced ripples");
            for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
            {
                Package p = it.next();

                if (p.mBuildFile == 0)
                {
                    // package has yet to be processed
                    if ( p.mForcedRippleInstruction > 0 )
                    {
                        //
                        //    Cannot force a ripple on an SDK based package or a Pegged Package
                        //        Remove the daemon instruction from the database
                        //        Send a warning email
                        //
                        if(p.mIsPegged || p.mIsSdk)
                        {
                            String reason;
                            reason = (p.mIsPegged) ? "Pegged" : "SDK Based";

                            mLogger.error("planRelease Daemon Instruction of " + reason + " package deleted: " + p.mName);
                            mReleaseManager.markDaemonInstCompleted( p.mForcedRippleInstruction );
                            emailRejectedDaemonInstruction("Cannot 'Ripple' a " + reason + " package",p);

                            p.mBuildFile = -8; 
                        }
                        else
                        {
                            mLogger.info("planRelease package forced ripple " + p.mName);
                            p.mBuildReason = BuildReason.Ripple;
                            rippleIndirectlyPlanned(p);
                        }
                    }
                }
            }

            //  Daemon Mode Only
            //  Mark Pegged and SDK packages as not to be built
            //  Have previously detected conflicts between pegged/sdk packages and daemon instructions
            //
            mLogger.debug("planRelease remove pegged and SDK packages from the build set");
            for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
            {
                Package p = it.next();

                if (p.mBuildFile == 0)
                {
                    //  Not interested in a pegged package or a package provided from an SDK.
                    //  Such packages are not built
                    if (p.mIsPegged || p.mIsSdk)
                    {
                        String reason;
                        reason = (p.mIsPegged) ? "Pegged" : "SDK";

                        mLogger.info("planRelease " + reason + " not built in this release " + p.mName);
                        p.mBuildFile = -8;
                    }
                }
            }

            //  Daemon Mode Only
            //  Locate Test Build Requests that cannot be satisfied - will not be built due to
            //  errors in dependent packages. Report the error to the user and remove the request
            //
            for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
            {
                Package p = it.next();

                if (p.mTestBuildInstruction > 0)
                {
                    if (p.mBuildFile < 0)
                    {
                        String reason;
                        switch (p.mBuildFile)
                        {
                        case -1: reason = "Not reproducible"; break;
                        case -2: reason = "Not reproducible on configured build platforms"; break;
                        case -3: reason = "Marked as 'Do not ripple'"; break;
                        case -4: reason = "Dependent on a package not in the release"; break;
                        case -5: reason = "Indirectly dependent on a package not reproducible in the release"; break;
                        case -6: reason = "Has a circular dependency"; break;
                        case -7: reason = "Pegged or SDK package not in dpkg_archive"; break;
                        case -8: reason = "Is a Pegged or SDK package"; break;
                        case -9: reason = "Rejected Daemon Instruction"; break;
                        case -10: reason = "Unbuildable package not in dpkg_archive"; break;
                        case -11: reason = "Marked as 'RippleStop'"; break;
                        default: reason = "Unknown reason. Code:" + p.mBuildFile; break;
                        }
                        mLogger.error("planRelease Test Build of an unbuildable of package deleted: " + p.mName);
                        mReleaseManager.markDaemonInstCompleted( p.mTestBuildInstruction );
                        emailRejectedDaemonInstruction(reason,p);
                    }
                }
            }
        }
        else
        {
            // escrow reporting only
            // Report packages that are not reproducible
            //
            //  Note: I don't believe this code can be executed
            //        The -3 is only set in daemon mode
            for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
            {
                Package p = it.next();

                if (p.mBuildFile == -3)
                {
                    standardOut(mDependent, p.mAlias, mDependentFlag);
                    mDependentFlag = false;
                }
            }
        }
    }

    /** Plan the build order.
     *  Assumes that a great deal of work has been done.
     *  This is a stand alone method to contain the work
     *  
     * @throws Exception
     * @throws SQLException
     */
    private void planBuildOrder() throws Exception, SQLException {

        // Process remaining packages which are need to be reproduced for this baseline.
        //    Determine the build file for each package
        //    For daemon builds:
        //      Determine the first package that can be built now, this means the first package 
        //      not dependent upon packages also to be built.
        //      Set its mBuildNumber to 1, all remaining reproducible packages to 2
        //      Add buildable packages to mBuildableCollection
        //    For escrow builds:
        //      Determine the package versions that can be built in the build iteration
        //      Set their mBuildNumber to the build iteration
        //      Increment the build iteration and repeat until all package versions 
        //      that need to be reproduced have been assigned a build iteration        
        
        if ( mDaemon )
        {
            // Sort the build order such that the unit tests are sorted by instruction id 
            //  and placed at the head of the list. This is an attempt to have the oldest 
            //  unit tests at the head of the list.
            //
            //  The order of all other elements is preserved
            //  The order of the other elements is basically
            //      planned - ordered by modified date-time
            //      others  - order by pv_id
            //
            Package.setSequence(mPackageCollection);
            Collections.sort(mPackageCollection, Package.UnitTestComparator);
        }
        
        boolean allProcessed = false;
        int buildFile = 1;

        do
        {
            boolean allDependenciesProcessed = true;
            do
            {
                // assume all dependencies have been processed
                allDependenciesProcessed = true;

                for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
                {
                    Package p = it.next();

                    if ( (  mDaemon && ( ( !p.mDirectlyPlanned && !p.mIndirectlyPlanned ) || p.mBuildFile < 0 ) ) ||
                         ( !mDaemon && p.mBuildFile == -2 ) )
                    {
                        // Daemon: Flag packages with no build requirement as processed
                        // Escrow: Flag packages with a foreign build environment as processed
                        p.mProcessed = true;
                        mLogger.info("planRelease package has no build requirement " + p.mName);            
                    }
                    else if ( ( p.mBuildFile == 0 ) && ( (mDaemon && ( p.mDirectlyPlanned || p.mIndirectlyPlanned ) ) || ( !mDaemon ) ) )
                    {
                        // package yet to be processed and
                        //  Daemon mode: Has a build requirement
                        //  Escrow mode: Can be reproduced
                        boolean canBeBuiltNow = true;
                        boolean allDependenciesForThisPackageProcessed = true;

                        //  Scan the packages dependencies
                        for ( Iterator<Package> it2 = p.mPackageDependencyCollection.iterator(); it2.hasNext(); )
                        {
                            Package dependency = it2.next();

                            if ( !dependency.mProcessed )
                            {
                                //  If all the dependencies have been processed, then we can potentially build this package
                                //  If any of them have not been processed, then cannot calculate canBeBuiltNow until this 
                                //  dependency has been processed
                                allDependenciesForThisPackageProcessed = false;
                                allDependenciesProcessed = false;
                            }
                            else if (  (  mDaemon && ( dependency.mDirectlyPlanned ) || ( dependency.mIndirectlyPlanned ) ) 
                                    || ( !mDaemon && ( ( dependency.mBuildFile == 0 ) ||
                                                           ( dependency.mBuildFile == buildFile &&
                                                           ( !p.haveSameBuildStandards(dependency)  ) ) ) ) )
                            {
                                // Daemon mode:
                                //    This processed dependency has a build requirement - so can't build
                                // Escrow mode: This package cannot be built now if
                                //    This processed dependency has not been assigned to a build iteration or
                                //    This processed dependency has been assigned to this build iteration, but the 
                                //    build standards of the package and this dependency prevent the package being
                                //    built in this iteration.   
                                canBeBuiltNow = false;
                                mLogger.info("planRelease package cannot be built in this iteration " + p.mName);
                                break;
                            }
                        }

                        if (allDependenciesForThisPackageProcessed)
                        {
                            p.mProcessed = true;

                            if ( mDaemon )
                            {
                                mBuildOrder.add(p);
                                if ( canBeBuiltNow )
                                {
                                    // flag package with build requirement, may get down graded to future build requirement
                                    p.mBuildFile = 1;
                                    mLogger.info("planRelease set mBuildFile to 1 for package " + p.mAlias );
                                }
                                else
                                {
                                    // flag package with future build requirement
                                    p.mBuildFile = 2;
                                    mLogger.info("planRelease set mBuildFile to 2 for package " + p.mAlias );
                                }
                            }
                            else
                            {
                                // Escrow Mode
                                if ( canBeBuiltNow )
                                {
                                    mBuildOrder.add(p);
                                    p.mBuildFile = buildFile;
                                    mLogger.info("planRelease set mBuildFile to " + buildFile + " for package " + p.mAlias );
                                }
                            }
                        }
                    }
                }
            } while( !allDependenciesProcessed );

            //
            //  Daemon Mode:
            //  Locate all packages that we would like to build and
            //      Calculate new version - report errors
            //      Determine a package we would like to build
            //
            if ( mDaemon )
            {
                for (Iterator<Package> it = mBuildOrder.iterator(); it.hasNext(); )
                {
                    Package p = it.next();

                    if ( p.mProcessed && p.mBuildFile == 1 )
                    {
                        p.mBuildFile = buildFile;
                        mLogger.info("planRelease 2 set mBuildFile to " + buildFile + " for package " + p.mAlias );

                        if ( buildFile == 1 )
                        {
                            int pvApplied = p.applyPV(mReleaseManager, mBaseline);

                            if ( pvApplied == 1 )
                            {
                                // max 50 chars
                                rippleBuildExclude(p, p.mId, "Package has non standard versioning", null, null, true);
                            }
                            else if ( pvApplied == 2 )
                            {
                                // max 50 chars
                                rippleBuildExclude(p, p.mId, "Package has reached ripple field limitations", null, null, true);
                            }
                            else if ( pvApplied == 3 )
                            {
                                // max 50 chars
                                rippleBuildExclude(p, p.mId, "Package has invalid change type", null, null, true);
                            }
                            else
                            {
                                //    Have found a package to build
                                //        Prevent finding others
                                buildFile = 2;

                                if ( p.mForcedRippleInstruction > 0 )
                                {
                                    mReleaseManager.markDaemonInstCompleted( p.mForcedRippleInstruction );
                                }

                                if ( p.mTestBuildInstruction > 0 )
                                {
                                    mReleaseManager.markDaemonInstInProgress( p.mTestBuildInstruction );
                                }
                            }
                        }
                        else
                        {
                            mLogger.info("planRelease package has future (downgraded) build requirement " + p.mName + " " + buildFile);              
                        }
                    }
                }
            }

            // are more build files required
            allProcessed = true;

            if (mDaemon)
            {
                //  Recalculate mBuildOrder
                //  Remove any packages that cannot be built due to errors encountered during
                //  the use of applyPV()
                //
                ArrayList<Package> mTempBuildOrder = new ArrayList<Package>();

                //  Seed mBuildOrder with packages that were prime buildable candidates
                //  This includes test builds
                //      Insert the active build at the head of the list - should only be one
                //
                for (Iterator<Package> it = mBuildOrder.iterator(); it.hasNext(); )
                {
                    Package p = it.next();
                    if(p.mBuildFile > 0)
                    {
                        mTempBuildOrder.add(p);
                    }
                }
                mBuildOrder = mTempBuildOrder;


                for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
                {
                    Package p = it.next();

                    if ( p.mBuildFile < 0 || ( !p.mDirectlyPlanned && !p.mIndirectlyPlanned ) )
                    {
                        // at this point...
                        // only 1 package with a build requirement has a mBuildFile of 1,
                        // all other packages with a build requirement have an mBuildFile of 2
                        // give packages with no build requirement, reproducible or not, an mBuildFile of 3
                        p.mNoBuildReason = p.mBuildFile;
                        p.mBuildFile = 3;
                        mLogger.info("planRelease 1 set mBuildFile to 3 for package " + p.mAlias );
                    }
                }
            }
            else
            {
                // this is escrow mode centric
                for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
                {
                    Package p = it.next();

                    if ( p.mBuildFile == 0 )
                    {
                        // more build files are required
                        allProcessed = false;
                        mLogger.info("planRelease more build files are required for " + p.mName);
                        break;
                    }
                }

                buildFile++;
            }
        } while( !allProcessed );
    }


    /** Determine if a given PVID is a member of the current release.
     *  Used to determine if a package dependency is out of date
     * 
     * @param dpvId
     * @return true - specified pvid is a full member of the Release
     */
    private boolean isInRelease(Integer dpvId) {
        
        boolean found = false;

        for ( Iterator<Integer> it4 = mReleasedPvIDCollection.iterator(); it4.hasNext(); )
        {
            Integer pvId = it4.next();

            if ( pvId.compareTo(dpvId) == 0 )
            {
                found = true;
                break;
            }
        }
        return found;
    }


    /**reports what change in build exceptions happens as part of planRelease
     */
    public void reportChange() throws SQLException, Exception
    {
        int counter = 0;
        for (Iterator<BuildExclusion> it = mBuildExclusionCollection.iterator(); it.hasNext(); )
        {
            BuildExclusion buildExclusion = it.next();

            if ( !buildExclusion.isProcessed() )
            {
                if (buildExclusion.isImported() && ! buildExclusion.isARootCause() ) {
                    // Remove from the exclusion list
                    buildExclusion.includeToBuild(mReleaseManager, mBaseline);
                    mLogger.error("reportChange remove unused exclusion: " + buildExclusion.info());
                } else {
                    // Exclude and notify
                    buildExclusion.excludeFromBuild(mReleaseManager, mBaseline);
                    buildExclusion.email(this, mPackageCollection);
                    counter++;
                }
            }
        }
        mLogger.error("reportChange exclusion count: " + counter);
    }

    /**reports the build plan
     */
    public void reportPlan() throws SQLException, Exception
    {
        mReleaseManager.reportPlan(mRtagId, mBuildOrder);
    }

    /**Returns the first build file from the collection
     * The build file will be flagged as empty if none exists
     */
    public BuildFile getFirstBuildFileContent()
    {
        mLogger.debug("getFirstBuildFileContent");

        mBuildIndex = -1;
        return getNextBuildFileContent();
    }

    /**Returns next build file from the collection
     * The build file will be flagged as empty if none exists
     */
    public BuildFile getNextBuildFileContent()
    {
        mLogger.debug("getNextBuildFileContent");
        BuildFile retVal = null;

        try
        {
            mBuildIndex++;
            retVal = mBuildCollection.get( mBuildIndex );
        }
        catch( IndexOutOfBoundsException e )
        {
            // Ignore exception. retVal is still null.
        }

        if (retVal == null)
        {
            retVal = new BuildFile();
        }

        mLogger.debug("getNextBuildFileContent returned " + retVal.state.name() );
        return retVal;
    }


    /**collects meta data associated with the baseline
     * this is sufficient to send an indefinite pause email notification 
     *  
     * Escrow: Used once to collect information about the SBOM and associated Release 
     *         mBaseline is an SBOMID 
     * Daemon: Used each build cycle to refresh the information 
     *          mBaseline is an RTAGID
     */
    public void collectMetaData() throws SQLException, Exception
    {
        Phase phase = new Phase("cmd");
        mLogger.debug("collectMetaData mDaemon " + mDaemon);

        try
        {
            phase.setPhase("connect");
            mReleaseManager.connect();

            if (! mDaemon)
            {
                mSbomId = mBaseline;
                phase.setPhase("queryRtagIdForSbom");
                mRtagId = mReleaseManager.queryRtagIdForSbom(mBaseline);
                if (mRtagId == 0)
                {
                    mLogger.error("SBOM does not have a matching Release. Cannot be used as a base for an Escrow"); 
                    throw new Exception("rtagIdForSbom show stopper. SBOM does not have an associated Release");
                }
            }

            phase.setPhase("queryReleaseConfig");
            mReleaseManager.queryReleaseConfig(mRtagId);

            if (mDaemon)
            {
                phase.setPhase("queryMailServer");
                setMailServer(mReleaseManager.queryMailServer());
                phase.setPhase("queryMailSender");
                setMailSender(mReleaseManager.queryMailSender());
                phase.setPhase("queryGlobalAddresses");
                setMailGlobalTarget(mReleaseManager.queryGlobalAddresses());
                phase.setPhase("queryProjectEmail");
                mMailGlobalCollection = mReleaseManager.queryProjectEmail(mBaseline);
                phase.setPhase("mMailGlobalTarget");
                mMailGlobalCollection.add(0,getMailGlobalTarget());
            }
            phase.setPhase("queryBaselineName");
            mBaselineName = mReleaseManager.queryBaselineName(mBaseline);
            phase.setPhase("Done");
        }
        finally
        {
            // this block is executed regardless of what happens in the try block
            // even if an exception is thrown
            // ensure disconnect
            mReleaseManager.disconnect();
        }
    }

    /**
     * Find Package by package alias
     * Searches the complete package collection
     * @param   alias               - alias of package to locate
     * @return  Package with the matching mAlias or NULL_PACKAGE if no package has the mAlias
     */
    public Package findPackage(String alias)
    {
        mLogger.debug("findPackage");

        Package retVal = mReleaseManager.findPackage(alias, mPackageCollection);

        mLogger.info("findPackage returned " + retVal.mName);
        return retVal;
    }

    /**
     * Sets the mBuildFile to -5 (indirectly dependent on package versions which are not reproducible) 
     * for the package and all dependent packages 
     * @param p             The package being excluded 
     * @param rootPvId      The PVID of the package that is causing the exclusion. Null or -ve values are special
     *                      This package is the root cause, -2: Excluded by Ripple Stop 
     *                   
     * @param rootCause     Text message. Max 50 characters imposed by RM database 
     * @param list          If not null, add build exclusion to specified list, else use default(mBuildExclusionCollection) list. Allows insertions while iterating the list.
     * @param be            If not null, process provided BuildExclusion element, otherwise find-existing/create a BuildExclusion element
     * @param dependentToo  True: Exclude all packages that depend on the excluded package 
     */
    private void rippleBuildExclude(Package p, int rootPvId, String rootCause, ListIterator<BuildExclusion> list, BuildExclusion be, boolean dependentToo )
    {
        mLogger.debug("rippleBuildExclude");
        if ( p.mBuildFile == 0 || p.mBuildFile == 1 )
        {
            p.mBuildFile = -5;
            mLogger.info("rippleBuildExclude set mBuildFile to -5 for package " + p.mAlias );

            if ( be != null )
            {
                be.setProcessed();
            }
            else
            {
                //  If no be item has been provided, then scan the complete collection looking for a matching item
                //  If found, process it, else add it (unprocessed)
                boolean found = false;
                for (Iterator<BuildExclusion> it = mBuildExclusionCollection.iterator(); it.hasNext(); )
                {
                    BuildExclusion buildExclusion = it.next();

                    if ( buildExclusion.compare(p.mId, rootPvId, rootCause))
                    {
                        buildExclusion.setProcessed();
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    // Entry not found in the mBuildExclusionCollection
                    // Mark all occurrences for this package as processed
                    // These will be superseded by a new build exclusion entry
                    for (Iterator<BuildExclusion> it = mBuildExclusionCollection.iterator(); it.hasNext(); )
                    {
                        BuildExclusion buildExclusion = it.next();

                        if ( buildExclusion.compare(p.mId))
                        {
                            buildExclusion.setProcessed();
                        }
                    }

                    BuildExclusion buildExclusion = new BuildExclusion(p.mId, rootPvId, rootCause, p.mTestBuildInstruction);

                    //
                    //  Add the new build exclusion to a list
                    //    Either the default list or a user-specified list
                    if ( list == null )
                    {
                        mBuildExclusionCollection.add(buildExclusion);
                    }
                    else
                    {
                        // must use the ListIterator interface to add to the collection whilst iterating through it
                        list.add(buildExclusion);
                    }
                }
            }

            //  Locate ALL packages that depend on this package and exclude them too
            //  This process will be recursive
            //    Not sure that this it is efficient 
            //  Only ripple a build exclusion through for non test builds
            //
            if ( p.mTestBuildInstruction == 0 && dependentToo)
            {
                for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
                {
                    Package pkg = it.next();

                    if ( pkg != p )
                    {
                        for (Iterator<Package> it2 = pkg.mPackageDependencyCollection.iterator(); it2.hasNext(); )
                        {
                            Package dependency = it2.next();

                            if ( dependency == p )
                            {
                                rippleBuildExclude( pkg, rootPvId, null, list, null, dependentToo );
                                break;
                            }
                        }
                    }
                }
            }
        }
        mLogger.info("rippleBuildExclude set " + p.mName + " " + p.mBuildFile);
    }

    /**Simple XML string escaping
     * 
     * @param xml               - String to escape
     * @return          - A copy of the string with XML-unfriendly characters escaped 
     */
    public static String escapeXml( String xml )
    {
        xml = xml.replaceAll("&", "&amp;");
        xml = xml.replaceAll("<", "&lt;");
        xml = xml.replaceAll(">", "&gt;");
        xml = xml.replaceAll("\"","&quot;");
        xml = xml.replaceAll("'", "&apos;");
        xml = xml.replaceAll("\\$", "\\$\\$");

        return xml;
    }

    /** Quote a string or a string pair
     *  If two strings are provided, then they will be joined with a comma.
     *   
     * @param text              - First string to quote
     * @param text2             - Optional, second string
     * @return A string of the form 'text','text2'
     */
    public static String quoteString(String text, String text2)
    {
        String result;
        result =  "\'" + text + "\'";
        if (text2 != null )
        {
            result +=  ",\'" + text2 + "\'";  
        }
        return result;
    }

    /** Generate build file information
     * 
     */
    private void generateBuildFiles() 
    {

        // persist the build files
        boolean allProcessed = false;
        int buildFile = 1;
        StringBuilder rawData = new StringBuilder();
        StringBuilder setUp = new StringBuilder();

        mLogger.debug("generateBuildFiles");

        if ( mDaemon )
        {
            // all interesting packages in daemon mode match the following filter
            buildFile = 3;
        }

        //-----------------------------------------------------------------------
        //    Generate the build file
        do
        {
            BuildFile buildEntry = new  BuildFile();
            buildEntry.state = BuildFileState.Dummy;
            XmlBuilder xml = generateBuildFileHeader();


            //  Generate properties for each package in this build level or lower build levels
            //  The properties link the packageAlias to the PackageName and PackageVersion
            //
            //  [DEVI 54816] In escrow mode all unreproducible packages are included 
            for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
            {
                Package p = it.next();

                if ( ( ( p.mBuildFile > 0 ) && ( p.mBuildFile <= buildFile ) ) || ( !mDaemon && p.mBuildFile == -2 ) )
                {
                    xml.addProperty(p.mAlias, p.mName + " " + p.mVersion + p.mExtension);
                }
            }

            //  UTF Support
            //  Insert additional info into the build file to provide extra checking
            //
            if ( ! ReleaseManager.mUseDatabase )
            {
                // UTF Support
                // Insert per-package planning information
                //
                xml.addComment("mPackageCollection");
                for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
                {
                    Package p = it.next();
                    generatePackageInfo(xml, p);
                }

                // UTF Support
                // Insert build exclusion information
                xml.addComment("mBuildExclusionCollection");
                for (Iterator<BuildExclusion> it = mBuildExclusionCollection.iterator(); it.hasNext(); )
                {
                    BuildExclusion buildExclusion = it.next();
                    if (!buildExclusion.isProcessed())
                    {
                        xml.addComment(buildExclusion.info());
                    }
                }
                
                // UTF Support
                // Insert build Plan
                if (mDaemon)
                {
                    xml.addComment("mBuildOrder");
                    int order = 0;
                    for (Iterator<Package> it = mBuildOrder.iterator(); it.hasNext(); )
                    {
                        Package p = it.next();
                        String comment =
                                "pvid="+ p.mId +
                                " order=" +(++order) +
                                " name=\"" + p.mAlias + "\"";
                        xml.addComment(comment);
                    }
                }
            }

            //  Generate Taskdef information
            generateTaskdef(xml);

            //
            //  Insert known Machine Information
            //  Escrow usage: 
            //      Map machType to machClass
            //  Also serves as a snapshot of the required build configuration
            //  ie: machine types and buildfilters
            //
            if (!mDaemon)
            {
                generateMachineInfo(xml, null);
            }

            //
            //  Generate target rules for each package to be built within the current build file
            //
            boolean daemonHasTarget = false;

            for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
            {
                Package p = it.next();

                if ( p.mBuildFile > 0 && p.mBuildFile <= buildFile )
                {
                    generateTarget(xml, buildEntry, p, buildFile);

                    if ( p.mBuildFile == 1 )
                    {
                        daemonHasTarget = true;
                        
                        // Retain information about the target package
                        buildEntry.mPkgId = p.mPid;
                        buildEntry.mPvId = p.mId;
                    }
                }

                //      Generate escrow extraction commands
                //      Only done on the first pass though the packages
                //
                if ( !mDaemon && buildFile == 1 )
                {
                    setUp.append("jats jats_vcsrelease -extractfiles"
                                + " \"-label=" + p.mVcsTag + "\""
                                + " \"-view=" + p.mAlias + "\""
                                + " -root=. -noprefix"
                                + mlf );
                }

                //      Generate escrow raw CSV data
                //      Note: I don't think this data is used at all

                if ( !mDaemon && ( p.mBuildFile == buildFile))
                {
                    StringAppender machines = new StringAppender(",");
                    for (Iterator<BuildStandard> it1 = p.mBuildStandardCollection.iterator(); it1.hasNext();)
                    {
                        BuildStandard bs = it1.next();
                        machines.append(bs.mMachClass);
                    }

                    rawData.append(p.mAlias + "," +
                                    buildFile + "," +
                                    machines +
                                    mlf);
                }
            }

            if ( mDaemon && !daemonHasTarget )
            {
                // must have AbtTestPath, AbtSetUp, AbtTearDown, and AbtPublish targets
                XmlBuilder target = xml.addNewElement("target");
                target.addAttribute("name", "AbtTestPath");

                target = xml.addNewElement("target");
                target.addAttribute("name", "AbtSetUp");

                target = xml.addNewElement("target");
                target.addAttribute("name", "AbtTearDown");

                target = xml.addNewElement("target");
                target.addAttribute("name", "AbtPublish");
            }

            generateDefaultTarget( xml, buildFile);

            //  Convert the Xml structure into text and save it in the build file
            //  Add this build file to the mBuildCollection
            buildEntry.content = mXmlHeader + xml.toString();
            mBuildCollection.add(buildEntry);

            // are more build files required
            allProcessed = true;

            if (!mDaemon)
            {
                // this is escrow mode centric
                for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
                {
                    Package p = it.next();

                    if ( p.mBuildFile > buildFile )
                    {
                        // more build files are required
                        allProcessed = false;
                        mLogger.info("planRelease reiterating package has no build requirement " + p.mName + " " + p.mBuildFile + " " + buildFile);
                        break;
                    }
                } 

                buildFile++;
            }

        } while( !allProcessed );

        //      Save additional escrow data
        if ( !mDaemon )
        {
            mEscrowSetup = setUp.toString();
            mEscrowRawData = rawData.toString();
        }
    }

    /**returns a build file header for the mBaseline
     */
    private XmlBuilder generateBuildFileHeader()
    {
        mLogger.debug("generateBuildFileHeader");
        XmlBuilder element = new XmlBuilder("project");

        element.addAttribute("name", "mass");
        element.addAttribute("default", "full");
        element.addAttribute("basedir", ".");

        if ( mDaemon )
        {
            element.addProperty("abt_mail_server", getMailServer());
            element.addProperty("abt_mail_sender", getMailSender()); 
            element.addProperty("abt_rtag_id", mBaseline);
            element.addProperty("abt_daemon", mReleaseManager.currentTimeMillis());
            element.makePropertyTag("abt_packagetarball", true);
            element.makePropertyTag("abt_usetestarchive", !ReleaseManager.getUseMutex());

            for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
            {
                Package p = it.next();

                if ( p.mBuildFile == 1 )
                {
                    element.addProperty("abt_package_name", p.mName);
                    element.addProperty("abt_package_version", p.mVersion + p.mExtension);
                    element.addProperty("abt_package_extension", p.mExtension);
                    element.addProperty("abt_package_location", getBuildLocation(p));
                    element.addProperty("abt_package_ownerlist", p.emailInfoNonAntTask(this));
                    element.addProperty("abt_package_build_info", buildInfoText(p));

                    // depends in the form 'cs','25.1.0000.cr';'Dinkumware_STL','1.0.0.cots'
                    StringAppender depends = new StringAppender(";");

                    for (Iterator<Package> it3 = p.mPackageDependencyCollection.iterator(); it3.hasNext(); )
                    {
                        Package depend = it3.next();
                        depends.append( quoteString(depend.mName, depend.mVersion + depend.mExtension) );
                    }

                    element.addProperty("abt_package_depends", depends.toString());
                    element.addProperty("abt_is_ripple", p.mDirectlyPlanned ? "0" : "1");
                    element.addProperty("abt_build_reason", p.mBuildReason.toString());
                    element.addProperty("abt_package_version_id", p.mId);
                    element.addProperty("abt_does_not_require_source_control_interaction", ! p.mRequiresSourceControlInteraction ? "true" : "false" );
                    element.addProperty("abt_test_build_instruction", p.mTestBuildInstruction);
                }
            }
        }
        else
        {
            //    Escrow Mode
            element.addProperty("abt_rtag_id", mRtagId);
            element.addProperty("abt_sbom_id", mSbomId);
        }

        element.addProperty("abt_release", escapeXml(mBaselineName));
        element.addProperty("abt_buildtool_version", mReleaseManager.getMajorVersionNumber() );

        element.addNewElement("condition")
        .addAttribute("property", "abt_family")
        .addAttribute("value", "windows")
        .addNewElement("os")
        .addAttribute("family", "windows");
        element.addProperty("abt_family", "unix");

        return element;
    }

    /** Add task def XML items taskdef for the abt ant task
     * @param xml       - xml item to extend
     */
    private void generateTaskdef(XmlBuilder xml)
    {
        mLogger.debug("generateTaskdef");
        xml.addNewElement("taskdef")
        .addAttribute("name", "abt")
        .addAttribute("classname", "com.erggroup.buildtool.abt.ABT");

        xml.addNewElement("taskdef")
        .addAttribute("name", "abtdata")
        .addAttribute("classname", "com.erggroup.buildtool.abt.ABTData");
    }

    /** returns the command abtdata items
     *  <br>Common machine information
     *  <br>Common email address
     *  
     *  
     *  @param  xml - Xml element to extend   
     *  @param    p - Package (May be null)
     */
    private void generateMachineInfo(XmlBuilder xml, Package p)
    {
        XmlBuilder element = xml.addNewElement("abtdata");
        element.addAttribute("id", "global-abt-data");

        //
        //  Iterate over all the machines and create a nice entry
        //
        for (Iterator<ReleaseConfig> it = mReleaseManager.mReleaseConfigCollection.mReleaseConfig.iterator(); it.hasNext(); )
        {
            ReleaseConfig rc = it.next();
            element.addElement(rc.getMachineEntry());
        }

        //
        //  Now the email information
        //
        if ( p != null)
        {
            p.emailInfo(this, element);
        }
    }

    /** Generate package information
     *  Only when running unit tests
     *  
     * @param xml       - An xml element to append data to
     * @param p - Package to process
     */
    private void generatePackageInfo (XmlBuilder xml, Package p)
    {

        String comment =
                "pvid="+ p.mId + 
                " name=\"" + p.mAlias + "\""+
                " reason=" + p.mNoBuildReason +" "+ 
                " buildFile=" + p.mBuildFile + " "+
                " directlyPlanned=" + p.mDirectlyPlanned + " "+
                " indirectlyPlanned=" + p.mIndirectlyPlanned;

        xml.addComment(comment);            
    }

    /**returns an ant target for the passed Package
     * in daemon mode:
     *  packages are categorized with one of three mBuildFile values:
     *   1 the package to be built by this buildfile
     *   2 the packages with a future build requirement
     *   3 the packages with no build requirement
     *  the returned target depends on this categorization and will have
     *   1 full abt info
     *   2 full dependency info to determine future build ordering but no abt info (will not build this package)
     *   3 only a name attribute (will not build this package)
     * in escrow mode:
     *  if the passed Package's mBuildFile is different (less than) the passed build file,
     *  the returned target have only a name attribute (will not build this package)
     *   
     * @param xml - Xml element to extend
     * @param buildEntry - Record build type (dummy/generic/not generic)
     * @param p - Package to process
     * @param buildFile - buildfile level being processed
     */
    private void generateTarget(XmlBuilder xml, BuildFile buildEntry, Package p, int buildFile)
    {
        mLogger.debug("generateTarget");

        //---------------------------------------------------------------------
        //  Generate the AbtData - common machine and email information
        //  Only used by the daemon builds
        //
        if ( mDaemon && p.mBuildFile == 1 )
        {
            generateMachineInfo(xml, p );
        }

        //-------------------------------------------------------------------------
        //  Generate the <target name=... /> construct
        //  There are two types
        //      1) Simple dummy place holder. Just has the PackageName.PackageExt
        //      2) Full target. Has all information to build the package including
        //              AbtSetUp, AbtTearDown and AbtPublish targets
        //

        if ( !mDaemon && ( p.mBuildFile < buildFile ) )
        {
            XmlBuilder target = xml.addNewElement("target");
            target.addAttribute("name", p.mAlias);
        }
        else
        {
            if (!mDaemon) 
            {
                //  Escrow Only:
                //  Generate the 'wrapper' target
                //  This is used to ensure that the required dependencies have been built - I think
                //
                StringAppender dependList = new StringAppender(",");
                if ( !p.mPackageDependencyCollection.isEmpty() )
                {
                    for (Iterator<Package> it = p.mPackageDependencyCollection.iterator(); it.hasNext(); )
                    {
                        Package dependency = it.next();
                        if ( !mDaemon && dependency.mBuildFile == -2 )
                        {
                            // ignore targets which build in foreign environments in escrow mode
                            continue;
                        }
                        dependList.append(dependency.mAlias);
                    }
                }
    
                XmlBuilder target = xml.addNewElement("target").isExpanded();
                target.addAttribute("name", p.mAlias + ".wrap");
    
                if (dependList.length() > 0)
                {
                    target.addAttribute("depends", dependList.toString() );
                }
    
                if ( !mDaemon )
                {
                    boolean hasDependenciesBuiltInThisIteration = false;
                    if ( ( !p.mPackageDependencyCollection.isEmpty()) )
                    {
                        for (Iterator<Package> it = p.mPackageDependencyCollection.iterator(); it.hasNext(); )
                        {
                            Package dependency = it.next();
    
                            if ( dependency.mBuildFile == buildFile )
                            {
                                hasDependenciesBuiltInThisIteration = true;
                                break;
                            }
                        }
                    }
    
                    if ( hasDependenciesBuiltInThisIteration )
                    {
                        XmlBuilder condition = target.addNewElement("condition");
                        condition.addAttribute("property",  p.mAlias + ".build");
    
                        XmlBuilder and = condition.addNewElement("and");
    
                        for (Iterator<Package> it = p.mPackageDependencyCollection.iterator(); it.hasNext(); )
                        {
                            Package dependency = it.next();
    
                            if ( dependency.mBuildFile == buildFile )
                            {
                                XmlBuilder or = and.addNewElement("or");
    
                                XmlBuilder equal1 = or.addNewElement("equals");
                                equal1.addAttribute("arg1", "${" + dependency.mAlias + ".res}");
                                equal1.addAttribute("arg2", "0");
    
                                XmlBuilder equal2 = or.addNewElement("equals");
                                equal2.addAttribute("arg1", "${" + dependency.mAlias + ".res}");
                                equal2.addAttribute("arg2", "257");
                            }
                        }
                    }
                    else
                    {
                        target.addProperty(p.mAlias + ".build", "");
                    }
                }
            }


            //
            //  Generate the 'body' of the target package
            //  Escrow Mode: Always
            //  Daemon Mode: Only for the one target we are building
            //                  Simplifies the XML
            //                  Reduces noise in the logs
            //              Don't add target dependencies. 
            //                  We are only building one target and the 
            //                  dependency management has been done way before now.
            //                  All it does is makes the logs noisy.
            //
            if ( ( mDaemon && p.mBuildFile == 1 ) || !mDaemon )
            {
                XmlBuilder target = xml.addNewElement("target").isExpanded();
                target.addAttribute("name", p.mAlias);
    
                if ( !mDaemon )
                {
                    target.addAttribute("depends",  p.mAlias + ".wrap");
                    target.addAttribute("if",p.mAlias + ".build");
                }
    
                if ( mDaemon && p.mBuildFile == 1 )
                {
                    target.addProperty(p.mAlias + "pkg_id",p.mPid);
                    target.addProperty(p.mAlias + "pv_id",p.mId);
                }
    
                target.addProperty(p.mAlias + "packagename",p.mName);                   
                target.addProperty(p.mAlias + "packageversion",p.mVersion);
                target.addProperty(p.mAlias + "packageextension",p.mExtension);
                target.addProperty(p.mAlias + "packagevcstag",p.mVcsTag);

                target.makePropertyTag(p.mAlias + "directchange", p.mDirectlyPlanned); 
                target.makePropertyTag(p.mAlias + "doesnotrequiresourcecontrolinteraction", ! p.mRequiresSourceControlInteraction);

                buildEntry.state = BuildFile.BuildFileState.NonGeneric;
                if ( p.isGeneric() )
                {
                    buildEntry.state = BuildFile.BuildFileState.Generic;
                    target.makePropertyTag(p.mAlias + "generic", true); 
                }

                target.addProperty(p.mAlias + "loc", getBuildLocation(p));
                target.makePropertyTag(p.mAlias + "unittests", p.mHasAutomatedUnitTests && mDaemon);

                //    Add our own task and associated information
                //
                XmlBuilder abt = target.addNewElement("abt").isExpanded();
    
                for (Iterator<Package> it = p.mPackageDependencyCollection.iterator(); it.hasNext(); )
                {
                    Package dependency = it.next();
                    XmlBuilder depend = abt.addNewElement("depend");
                    depend.addAttribute("package_alias", "${" + dependency.mAlias + "}");
                }

                buildInfo(abt,p);
    
                if ( mDaemon && p.mBuildFile == 1 )
                {
                    //    AbtTestPath
                    target = xml.addNewElement("target").isExpanded();
                    target.addAttribute("name", "AbtTestPath");
                    target.addProperty("AbtTestPathpackagevcstag", p.mVcsTag);
                    target.addProperty("AbtTestPathpackagename", p.mName);
                    abt = target.addNewElement("abt").isExpanded();
                    buildInfo(abt, p);
 
                    
                    //    AbtSetUp
                    target = xml.addNewElement("target").isExpanded();
                    target.addAttribute("name", "AbtSetUp");
                    target.addProperty("AbtSetUppackagevcstag", p.mVcsTag);
                    target.addProperty("AbtSetUppackagename", p.mName);
    
                    abt = target.addNewElement("abt").isExpanded();
                    buildInfo(abt, p);
    
                    //    AbtTearDown
                    target = xml.addNewElement("target").isExpanded();
                    target.addAttribute("name", "AbtTearDown");
                    target.addProperty("AbtTearDownpackagevcstag", p.mVcsTag);
                    target.addProperty("AbtTearDownpackagename", p.mName);
                    target.addProperty("AbtTearDownpackageversion", p.mVersion);
                    target.addProperty("AbtTearDownpackageextension", p.mExtension);
                    target.makePropertyTag(p.mAlias + "generic", p.isGeneric());
    
                    abt = target.addNewElement("abt").isExpanded();
                    buildInfo(abt, p);
    
    
                    //  AbtPublish
                    target = xml.addNewElement("target").isExpanded();
                    target.addAttribute("name", "AbtPublish");
    
                    target.addProperty("AbtPublishpackagevcstag", p.mVcsTag);
                    target.addProperty("AbtPublishpackagename", p.mName);
                    target.addProperty("AbtPublishpackageversion", p.mVersion);
                    target.addProperty("AbtPublishpackageextension", p.mExtension);
                    target.makePropertyTag("AbtPublishdirectchange", p.mDirectlyPlanned);
                    target.makePropertyTag("AbtPublishdoesnotrequiresourcecontrolinteraction", ! p.mRequiresSourceControlInteraction);
                    target.makePropertyTag("AbtPublishgeneric", p.isGeneric());
                    target.addProperty("AbtPublishloc", getBuildLocation(p));
    
                    abt = target.addNewElement("abt").isExpanded();
                    buildInfo(abt, p);
    
                }
            }
        }
    }

    /** Extends the xml object. Adds ant default target for the current build iteration
     * @param xml - The XmlBuilder Object to extend
     * @param buildFile - The current build file level. This differs for Daemon and Escrow mode. In Daemon mode it will not be a '1' 
     */
    private void generateDefaultTarget(XmlBuilder xml, int buildFile)
    {
        mLogger.debug("generateDefaultTarget");

        XmlBuilder target = xml.addNewElement("target").isExpanded();
        target.addAttribute("name", "fullstart");

        if (buildFile == 1)
        {
            antEcho(target, "${line.separator}" + mAnyBuildPlatforms + "${line.separator}${line.separator}");
            for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
            {
                Package p = it.next();

                if ( p.mBuildFile == -1 )
                {
                    antEcho(target, "${line.separator}" + p.mAlias + "${line.separator}");
                }
            }

            antEcho(target, "${line.separator}" + mAssocBuildPlatforms + "${line.separator}${line.separator}");
            for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
            {
                Package p = it.next();

                if ( p.mBuildFile == -2 )
                {
                    antEcho(target, "${line.separator}" + p.mAlias + "${line.separator}");
                }
            }

            antEcho(target, "${line.separator}" + mNotInBaseline + "${line.separator}${line.separator}");
            for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
            {
                Package p = it.next();

                if ( p.mBuildFile == -4 )
                {
                    antEcho(target, "${line.separator}" + p.mAlias + "${line.separator}");
                }
            }

            antEcho(target, "${line.separator}" + mDependent + "${line.separator}${line.separator}");
            for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
            {
                Package p = it.next();

                if ( p.mBuildFile == -5 )
                {
                    antEcho(target, "${line.separator}" + p.mAlias + "${line.separator}");
                }
            }
        }

        if ( !mDaemon )
        {
            antEcho(target, "${line.separator}Build Started:${line.separator}${line.separator}");
        }

        //
        //      Create a comma separated list of all the required targets
        //      Escrow : All packages
        //      Daemon : Just the package being built
        //
        StringAppender dependList = new StringAppender(",");
        dependList.append("fullstart");
        for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
        {
            Package p = it.next();

            if ( ( p.mBuildFile > 0 ) && ( p.mBuildFile <= buildFile ) )
            {
                if ((mDaemon && p.mBuildFile == 1) || !mDaemon)
                {
                    dependList.append(p.mAlias);                    
                }
            }
        }

        target = xml.addNewElement("target").isExpanded();
        target.addAttribute("name", "full");
        target.addAttribute("depends", dependList.toString());

        if ( !mDaemon )
        {
            antEcho(target, "${line.separator}Build Finished${line.separator}");
        }
    }

    /** Internal helper function to create an ant 'echo statement
     *  Many of the parameters are fixed
     */
    private void antEcho( XmlBuilder xml, String message)
    {
        XmlBuilder msg = xml.addNewElement("echo");
        msg.addAttribute("message", message);
        msg.addAttribute("file", "publish.log");
        msg.addAttribute("append", "true");
    }

    /**sets the mIndirectlyPlanned true for the package and all dependent packages
     */
    private void rippleIndirectlyPlanned(Package p)
    {
        mLogger.debug("rippleIndirectlyPlanned");
        if ( !p.mIndirectlyPlanned && p.mBuildFile == 0 )
        {
            p.mIndirectlyPlanned = true;

            for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
            {
                Package pkg = it.next();

                if ( pkg != p )
                {
                    for (Iterator<Package> it2 = pkg.mPackageDependencyCollection.iterator(); it2.hasNext(); )
                    {
                        Package dependency = it2.next();

                        if ( dependency == p )
                        {
                            rippleIndirectlyPlanned( pkg );
                            break;
                        }
                    }
                }
            }
        }
        mLogger.info("rippleIndirectlyPlanned set " + p.mName + " " + p.mIndirectlyPlanned);    
    }

    /**accessor method
     */
    public String getEscrowSetUp()
    {
        mLogger.debug("getEscrowSetUp");
        String retVal = mEscrowSetup;

        mLogger.debug("getEscrowSetUp returned " + retVal);
        return retVal;
    }

    /**accessor method
     */
    public String getRawData()
    {
        mLogger.debug("getRawData");
        String retVal = mEscrowRawData;

        mLogger.debug("getRawData returned " + retVal);
        return retVal;
    }

    /**Get the build loc (location)
     * This is package specific and will depend on the build mode (Escrow/Daemon)
     * 
     * @param   p - Package being built
     * @return A string that describes the build location
     */
    private String getBuildLocation(Package p)
    {
        mLogger.debug("locationProperty");
        String location = "";

        if (mDaemon)
        {
            // Daemon: Start in root of view/workspace
            location += mBaseline;
        }
        else
        {
            // Escrow: mAlias used with jats -extractfiles -view
            location += p.mAlias;
        }

        //
        //  Always use '/' as a path separator - even if user has specified '\'
        //  Ant can handle it.
        //
        location = location.replace('\\', '/');
        return location;
    }

    /**Adds package build into as XML elements 
     * @param     xml   - Xml element to extend
     * @param   p       - Package to process
     */
    private void buildInfo(XmlBuilder xml, Package p)
    {
        mLogger.debug("buildInfo");

        //
        // Create the xml build information
        // <platform gbe_machtype="linux_i386" type="jats" arg="all"/>
        //
        for (Iterator<BuildStandard> it = p.mBuildStandardCollection.iterator(); it.hasNext();)
        {
            BuildStandard bs = it.next();
            bs.getBuildStandardXml(xml);
        }
    }

    /**returns the buildInfo as a single line of text
     * Used for reporting purposes only
     * @param   p       - Package to process
     * 
     */
    public String buildInfoText(Package p)
    {
        mLogger.debug("buildInfoText");

        StringAppender result = new StringAppender (";");

        //
        //  Create platform:standards
        //      
        for (Iterator<BuildStandard> it = p.mBuildStandardCollection.iterator(); it.hasNext(); )
        {
            BuildStandard bs = it.next();

            if ( bs.isActive() )
            {
                String info = bs.getBuildStandardText();
                result.append(info);
            }
        }

        mLogger.info("buildInfoText returned " + result);
        return result.toString();
    }

    /**prints to standard out in escrow mode only
     * <br>Prints a title and information. The title is only printed once.
     * 
     * @param header - The message title to display, if printMessage is true
     * @param text - A package name. Really just the 2nd line of the message
     * @param printHeader -  Controls the printing of the message argument
     */
    private void standardOut(final String header, final String text, boolean printHeader)
    {
        mLogger.debug("standardOut");
        if (!mDaemon)
        {
            if ( printHeader )
            {
                System.out.println(header);
            }

            System.out.println(text);
        }
    }


    /**
     *  Email users about a rejected daemon instruction
     *  @param  reason  - Reason for the rejection
     *  @param  pkg     - Package to be affected by the instruction
     *  
     */
    public void emailRejectedDaemonInstruction(String reason, Package p)
    {
        mLogger.debug("emailRejectedDaemonInstruction");

        //  Email Subject
        String subject = "BUILD FAILURE of Daemon Instruction on package " + p.mAlias;

        // Email Body
        String mailBody = "The build system reject the the Daemon Instruction";
        mailBody += "<br>Reason: " + reason; 

        mailBody += "<p>Release: " + mBaselineName 
                +  "<br>Package: " + p.mAlias 
                +  "<br>Rm Ref: " + CreateUrls.generateRmUrl(getRtagId(), p.mId); 

        mailBody += "<p><hr>";

        //    Generate the list of email recipients
        //        Transfer the daemon instruction email list into the package before use

        p.setTestEmail();
        String target = p.emailInfoNonAntTask(this);

        mLogger.error("emailRejectedDaemonInstruction Server: " + getMailServer());
        mLogger.error("emailRejectedDaemonInstruction Sender: " + getMailSender());
        mLogger.error("emailRejectedDaemonInstruction Target: " + target);

        try
        {
            //    
            Smtpsend.send(getMailServer(), // mailServer
                    getMailSender(),       // source
                    target,            // target
                    getMailSender(),       // cc
                    null,              // bcc
                    subject,           // subject
                    mailBody,          // body
                    null               // attachment
                    );
        } catch (Exception e)
        {
            mLogger.warn("Email Failure: emailRejectedDaemonInstruction:" + e.getMessage());
        }
    }

    /**
     * @return the mMailServer
     */
    public String getMailServer() {
        return mMailServer;
    }

    /**
     * @param mMailServer the mMailServer to set
     */
    public void setMailServer(String mMailServer) {
        this.mMailServer = mMailServer;
    }

    /**
     * @return the mMailSender
     */
    public String getMailSender() {
        return mMailSender;
    }

    /**
     * @param mMailSender the mMailSender to set
     */
    public void setMailSender(String mMailSender) {
        this.mMailSender = mMailSender;
    }

    /**
     * @return the mMailGlobalTarget
     */
    public String getMailGlobalTarget() {
        return mMailGlobalTarget;
    }

    /**
     * @param mMailGlobalTarget the mMailGlobalTarget to set
     */
    public void setMailGlobalTarget(String mMailGlobalTarget) {
        this.mMailGlobalTarget = mMailGlobalTarget;
    }

}