Subversion Repositories DevTools

Rev

Blame | Last modification | View Log | RSS feed

package com.erggroup.buildtool.ripple;

import java.io.File;

import java.io.FileOutputStream;

import java.io.FileWriter;

import java.sql.SQLException;

import java.util.Iterator;
import java.util.Vector;

import org.apache.log4j.Logger;

/**Plans release impact by generating a set of Strings containing build file content.
 */
public class RippleEngine
{
  /**collection of gbemachtypes in String form associated with the baseline
   * limited to the following items "win32", "sparc", "solaris10_sparc32", "solaris10_x86", "linux_i386"
   * accessed by Package::isLinuxBuilt, isSolarisBuilt, isWin32Built
   * @attribute
   */
  Vector mGbeMachtypeCollection = new Vector();

  /**configured mail server
   * @attribute
   */
  String mMailServer = new String();

  /**configured mail sender user
   * @attribute
   */
  String mMailSender = new String();

  /**name associated with the baseline
   * @attribute
   */
  String mBaselineName = new String();

  /**collection of released pv_ids associated with the release
   * @attribute
   */
  Vector mReleasedPvIDCollection = new Vector();

  /**timestamp associated with build file generation
   * @attribute
   */
  long mTimestamp = 0;

  /**set to "non generic", "generic" or "dummy" to indicate the nature of the package in the build file in daemon mode
   * @attribute
   */
  String mAddendum = new String("dummy");

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

  /**collection of escrow clearcase support file content in String form, win32_set_up etc
   * @attribute
   */
  private Vector mEscrowClearcaseSupportCollection = new Vector();

  /**package versions representing the baseline
   * @aggregation shared
   * @attribute
   */
  private Vector mPackageCollection = new Vector();

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

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

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

  /**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
   */
  boolean mDaemon;

  /**collection of build file content in String form
   * @attribute
   */
  private Vector mBuildCollection = new Vector();

  /**constructor
   */
  public RippleEngine(ReleaseManager releaseManager, int rtag_id, 
                      boolean isDaemon)
  {
    mLogger.debug("RippleEngine rtag_id " + rtag_id + " isDaemon " + isDaemon);
    mReleaseManager = releaseManager;
    mBaseline = rtag_id;
    mDaemon = isDaemon;
  }

  /**discards all build file content
   * plans new build file content
   */
  public void planRelease() throws SQLException, Exception
  {
    mLogger.warn("planRelease mDaemon " + mDaemon);
    mAddendum = "dummy";
    mBuildCollection.removeAllElements();
    mPackageCollection.removeAllElements();
    mReleasedPvIDCollection.removeAllElements();
    
    if ( !mDaemon )
    {
      mEscrowClearcaseSupportCollection.removeAllElements();  
    }

    mReleaseManager.connect();

    if ( mDaemon )
    {
      // claim the mutex
      mReleaseManager.claimMutex();
    }
    
    collectMetaData();
    
    mReleaseManager.queryPackageVersions(this, mPackageCollection, mDaemon, mBaseline);
    
    // set up mPackageDependencyCollection
    for (Iterator it = mPackageCollection.iterator(); it.hasNext(); )
    {
      Package p = (Package) it.next();
       
      for (Iterator it2 = p.mDependencyCollection.iterator(); it2.hasNext(); )
      {
        String alias = (String) 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
          rippleBuildExclude(p);
           
          // take the package out of the build
          p.mBuildFile = -4;
          break;
        }
        
        p.mPackageDependencyCollection.add(dependency);
      }
    }
    
    // process packages which are not reproducible, and all packages dependent upon them      
    for (Iterator it = mPackageCollection.iterator(); it.hasNext(); )
    {
      Package p = (Package) it.next();

      if (p.mBuildFile == 0)
      {
        // package has yet to be processed
        if (!p.isReproducible())
        {
          if ( !mDaemon )
          {
            // for escrow build purposes, exclude all dependent package versions
            mLogger.info("planRelease package not reproducible " + p.mName);
            rippleBuildExclude(p);
          }
          
          // package is not reproducible, discard
          p.mBuildFile = -1;
        }
      }
    }
    
    // process packages which are not reproducible on the build platforms configured for this baseline
    for (Iterator it = mPackageCollection.iterator(); it.hasNext(); )
    {
      Package p = (Package) it.next();
       
      if (p.mBuildFile == 0)
      {
        // package has yet to be processed
        {
          // assume it does not need to be reproduced for this baseline
          p.mBuildFile = -1;
           
          for (Iterator it2 = mGbeMachtypeCollection.iterator(); it2.hasNext(); )
          {
            String machtype = (String) it2.next();
            
            if ( machtype.compareTo("linux_i386") == 0 )
            {
              if ( p.isLinuxBuilt() )
              {
                p.mBuildFile = 0;
                break;
              }
            }
            else if ( machtype.compareTo("win32") == 0 )
            {
              if ( p.isWin32Built() )
              {
                p.mBuildFile = 0;
                break;
              }
            }
            else if ( machtype.compareTo("sparc") == 0
                   || machtype.compareTo("solaris10_x86") == 0
                   || machtype.compareTo("solaris10_sparc32") == 0 )
            {
              if ( p.isSolarisBuilt() )
              {
                p.mBuildFile = 0;
                break;
              }
            }
          }

          if ( p.mBuildFile == -1 )
          {
            if ( !mDaemon )
            {
              // for escrow build purposes, exclude all dependent package versions
              mLogger.info("planRelease package not reproducible on the build platforms configured for this baseline " + p.mName);
              rippleBuildExclude(p);
            }
            
            // package is not reproducible on the build platforms configured for this baseline, discard
            p.mBuildFile = -2;
          }
        }
      }
    }      
      
    if (mDaemon)
    {
      // process packages which are not ripple buildable, and all packages dependent upon them      
      for (Iterator it = mPackageCollection.iterator(); it.hasNext(); )
      {
        Package p = (Package) it.next();

        if (p.mBuildFile == 0)
        {
          // package has yet to be processed
          if (p.mDoNotRipple)
          {
            // exclude all dependent package versions
            mLogger.info("planRelease do not ripple " + p.mName);
            rippleBuildExclude(p);
            
            // package is not reproducible, discard
            p.mBuildFile = -3;
          }
        }
      }
      
      // process packages which need to be ripple built
      for (Iterator it = mPackageCollection.iterator(); it.hasNext(); )
      {
        Package p = (Package) it.next();

        if (p.mBuildFile == 0)
        {
          // package has yet to be processed
          if (p.mDirectlyPlanned)
          {
            // a WIP exists on the package
            // exclude all dependent package versions
            mLogger.info("planRelease package has WIP " + p.mName);
            rippleIndirectlyPlanned(p);
          }
          else
          {
            Iterator it2 = p.mDependencyIDCollection.iterator();
            Iterator it3 = p.mPackageDependencyCollection.iterator();
            for ( ; it2.hasNext() && it3.hasNext(); )
            {
              Integer dpv_id = (Integer) it2.next();
              Package dependency = (Package) it3.next();

              if ( !dependency.mAdvisoryRipple )
              {
                // not advisory, ie has ripple build impact
                boolean found = false;
                 
                for ( Iterator it4 = mReleasedPvIDCollection.iterator(); it4.hasNext(); )
                {
                  Integer pv_id = (Integer) it4.next();
                   
                  if ( pv_id.compareTo(dpv_id) == 0 )
                  {
                    found = true;
                    break;
                  }
                }
                 
                if ( !found )
                {
                  // the package is out of date
                  // exclude all dependent package versions
                  mLogger.info("planRelease package out of date " + p.mName);
                  rippleIndirectlyPlanned(p);                 
                  break;
                }
              }
            }
          }
        }
      }
      
      // process packages which do not exist in the archive
      for (Iterator it = mPackageCollection.iterator(); it.hasNext(); )
      {
        Package p = (Package) it.next();

        if (p.mBuildFile == 0)
        {
          // package has yet to be processed
          // for unit test purposes, assume all packages exist in the archive if released
          if ( mReleaseManager.mUseDatabase )
          {
            // only check existence outside the unit test
            if (!p.mDirectlyPlanned && !p.mIndirectlyPlanned)
            {
              // check package version archive existence
              if (!p.exists())
              {
                mLogger.info("planRelease package not found in archive " + p.mName);
                rippleIndirectlyPlanned(p);
              }
            }
          }
        }
      }
    }
    
    // process remaining packages which 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
    // 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
    boolean allProcessed = false;
    int buildFile = 1;

    // delete the file <rtagId>official
    Integer rtag = new Integer(mBaseline);
    File rtagIdOfficial = new File(rtag + "official");
    
    if (rtagIdOfficial.exists())
    {
      rtagIdOfficial.delete();
    }
    
    do
    {
      boolean allDependenciesProcessed = true;
      
      do
      {
        // assume all dependencies have been processed
        allDependenciesProcessed = true;
        
        for (Iterator it = mPackageCollection.iterator(); it.hasNext(); )
        {
          Package p = (Package) it.next();
          
          if ( mDaemon && ( ( !p.mDirectlyPlanned && !p.mIndirectlyPlanned ) || p.mBuildFile < 0 ) )
          {
            // flag packages with no build requirement as processed in daemon mode
            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
              // in daemon mode has a build requirement or
              // in escrow mode
              boolean canBeBuiltNow = true;
              boolean allDependenciesForThisPackageProcessed = true;
              
              for ( Iterator it2 = p.mPackageDependencyCollection.iterator(); it2.hasNext(); )
              {
                Package dependency = (Package) it2.next();
                
                if ( !dependency.mProcessed )
                {
                  // cannot determine 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.isLinuxBuilt() && !dependency.isLinuxBuilt() ) ||
                                  ( p.isWin32Built() && !dependency.isWin32Built() ) ||
                                  ( p.isSolarisBuilt() && !dependency.isSolarisBuilt() ) ) ) ) ) )
                {
                  // in daemon mode this processed dependency has a build requirement or
                  // in escrow mode...
                  // this processed dependency has not been assigned to a build iteration or
                  // this processed dependency has been assigned to this build iteration and does not build on this platform
                  canBeBuiltNow = false;
                  mLogger.info("planRelease package cannot be built in this iteration " + p.mName);
                  break;
                }
              }
              
              if (allDependenciesForThisPackageProcessed)
              {
                p.mProcessed = true;
                
                if ( mDaemon )
                {
                  if ( canBeBuiltNow )
                  {
                    // flag package with build requirement, may get downgraded to future build requirement
                    p.mBuildFile = 1;
                    mLogger.info("planRelease package has current build requirement " + p.mName);
                  }
                  else
                  {
                    // flag package with future build requirement
                    p.mBuildFile = 2;
                    mLogger.info("planRelease package has future build requirement " + p.mName);
                  }
                }
                else
                {
                  if ( canBeBuiltNow )
                  {
                    p.mBuildFile = buildFile;
                    mLogger.info("planRelease package can be built in this iteration " + p.mName);
                  }
                }
              }
            }
          }
        }
      } while( !allDependenciesProcessed );

      if ( mDaemon )
      {
        for (Iterator it = mPackageCollection.iterator(); it.hasNext(); )
        {
          Package p = (Package) it.next();
          
          if ( p.mProcessed && p.mBuildFile == 1 )
          {
            p.mBuildFile = buildFile;
            
            if ( buildFile == 1 )
            {
              p.applyPV(mReleaseManager, mBaseline);
              // write <rtagId>offical
              String lf = new String( System.getProperty("line.separator") );
              rtagIdOfficial.createNewFile();
              FileWriter rtagIdOfficialFileWriter = new FileWriter(rtagIdOfficial);
              rtagIdOfficialFileWriter.append("packageName=" + p.mName + lf);
              String extension = p.mExtension;
              String version = p.mVersion;

              if ( extension.length() > 0 )
              {
                version += extension;
              }
              else
              {
                extension = ".";
              }

              rtagIdOfficialFileWriter.append("packageExtension=" + extension + lf);
              rtagIdOfficialFileWriter.append("packageVersion=" + version + lf);
              
              // depends in the form 'cs','25.1.0000.cr';'Dinkumware_STL','1.0.0.cots'
              String depends = new String();
              
              for (Iterator it3=p.mPackageDependencyCollection.iterator(); it3.hasNext(); )
              {
                Package depend = (Package)it3.next();
                
                if ( depends.compareTo( "" ) != 0 )
                {
                  depends += ";";
                }
                depends += "\'" + depend.mName + "\'";
                depends += ",";
                String dependsExtension = depend.mExtension;
                String dependsVersion = depend.mVersion;
                
                if ( dependsExtension.length() > 0 )
                {
                  dependsVersion += dependsExtension;
                }
                else
                {
                  dependsExtension = ".";
                }
                depends += "\'" + dependsVersion + "\'";
              }
                            
              rtagIdOfficialFileWriter.append("packageDepends=" + depends + lf);
              
              if ( p.mDirectlyPlanned )
              {
                // a WIP
                rtagIdOfficialFileWriter.append("packageRipple=0" + lf);
              }
              else
              {
                // a ripple
                rtagIdOfficialFileWriter.append("packageRipple=1" + lf);
              }
              
              rtagIdOfficialFileWriter.append("packageVersionID=" + p.mId);
              
              rtagIdOfficialFileWriter.close();
            }
            else
            {
              mLogger.info("planRelease package has future (downgraded) build requirement " + p.mName + " " + buildFile);              
            }
            
            buildFile = 2;
          }
        }
      }
      
      // are more build files required
      allProcessed = true;
      
      if (mDaemon)
      {
        for (Iterator it = mPackageCollection.iterator(); it.hasNext(); )
        {
          Package p = (Package) 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.mBuildFile = 3;
            mLogger.info("planRelease reiterating package has no build requirement " + p.mName);
          }
        }
      }
      else
      {
        // this is escrow mode centric
        for (Iterator it = mPackageCollection.iterator(); it.hasNext(); )
        {
          Package p = (Package) 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 );
    
    // persist the build files
    allProcessed = false;
    buildFile = 1;
    
    if ( mDaemon )
    {
      // all interesting packages in daemon mode match the following filter
      buildFile = 3;
    }

    mTimestamp = System.currentTimeMillis();

    if ( !mReleaseManager.mUseDatabase )
    {
      mTimestamp = 123456789;
    }

    do
    {
      String buildFileContent = new String( generateBuildFileHeader() );
      
      for (Iterator it = mPackageCollection.iterator(); it.hasNext(); )
      {
        Package p = (Package) it.next();
      
        if ( ( p.mBuildFile > 0 ) && ( p.mBuildFile <= buildFile ) )
        {
          buildFileContent += generatePackageProperty(p);
        }
      }
      
      buildFileContent += generateTaskdef();
      
      String win32_set_up = new String("");
      String win32_tear_down = new String("");
      String unix_set_up = new String("");
      String unix_tear_down = new String("");
      String lf = new String( System.getProperty("line.separator") );
      
      for (Iterator it = mPackageCollection.iterator(); it.hasNext(); )
      {
        Package p = (Package) it.next();
      
        if ( p.mBuildFile > 0 && p.mBuildFile <= buildFile )
        {
          buildFileContent += generateTarget(p, buildFile);
        }
        
        if ( !mDaemon && buildFile == 1 )
        {
          for (Iterator it2 = mGbeMachtypeCollection.iterator(); it2.hasNext(); )
          {
            String machtype = (String) it2.next();
            
            if ( machtype.compareTo("win32") == 0 )
            {
              if ( p.isWin32Built() )
              {
                win32_set_up +=
                "jats release -extract " + p.mLabel + " -path=" + p.mLocation + " -view=" + p.mAlias + " -root=. -noprefix" +lf;
                win32_tear_down +=
                "cleartool rmview " + p.mAlias + lf;
                break;
              }
            }
          }

          for (Iterator it2 = mGbeMachtypeCollection.iterator(); it2.hasNext(); )
          {
            String machtype = (String) it2.next();
            
            if ( machtype.compareTo("sparc") == 0
              || machtype.compareTo("solaris10_x86") == 0
              || machtype.compareTo("solaris10_sparc32") == 0
              || machtype.compareTo("linux_i386") == 0 )
            {
              if ( p.isLinuxBuilt() || p.isSolarisBuilt() )
              {
                unix_set_up +=
                "jats release -extract " + p.mLabel + " -view=" + p.mAlias + " -root=. -noprefix" +lf;
                unix_tear_down +=
                "cleartool rmview " + p.mAlias + lf;
                break;
              }
            }
          }
        }
      }
      
      if ( !mDaemon && buildFile == 1 )
      {
        mEscrowClearcaseSupportCollection.add(win32_set_up);  
        mEscrowClearcaseSupportCollection.add(win32_tear_down);  
        mEscrowClearcaseSupportCollection.add(unix_set_up);  
        mEscrowClearcaseSupportCollection.add(unix_tear_down);  
      }
      
      buildFileContent += generateDefaultTarget( buildFile);
      buildFileContent += generateBuildFileFooter();

      mBuildCollection.add(buildFileContent);
      
      // are more build files required
      allProcessed = true;
      
      if (!mDaemon)
      {
        // this is escrow mode centric
        for (Iterator it = mPackageCollection.iterator(); it.hasNext(); )
        {
          Package p = (Package) 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 );
      
    mReleaseManager.disconnect();
    mLogger.warn("planRelease mDaemon " + mDaemon + " returned");
  }

  /**returns first build file content
   * returns false if no build file content exists
   */
  public boolean getFirstBuildFileContent(MutableString content)
  {
    mLogger.debug("getFirstBuildFileContent");
    boolean retVal = true;
    
    try
    {
      mBuildIndex = 0;
      content.value = (String)mBuildCollection.get( mBuildIndex );
    }
    catch( ArrayIndexOutOfBoundsException e )
    {
      retVal = false;
    }
    
    mLogger.info("getFirstBuildFileContent returned " + retVal);
    return retVal;
  }

  /**returns next build file content
   * returns false if no next build file content exists
   */
  public boolean getNextBuildFileContent(MutableString content)
  {
    mLogger.debug("getNextBuildFileContent");
    boolean retVal = true;
    
    try
    {
      mBuildIndex++;
      content.value = (String)mBuildCollection.get( mBuildIndex );
    }
    catch( ArrayIndexOutOfBoundsException e )
    {
      retVal = false;
    }
    
    mLogger.debug("getNextBuildFileContent returned " + retVal);
    return retVal;
  }

  /**collects meta data associated with the baseline
   */
  private void collectMetaData() throws SQLException, Exception
  {
    mLogger.debug("collectMetaData mDaemon " + mDaemon);
    mGbeMachtypeCollection.removeAllElements();
    mReleaseManager.queryMachtypes(mGbeMachtypeCollection, mDaemon, mBaseline);

    if (mDaemon)
    {
      mMailServer = mReleaseManager.queryMailServer();
      mMailSender = mReleaseManager.queryMailSender();
    }
    mBaselineName = mReleaseManager.queryBaselineName(mDaemon, mBaseline);
  }

  /**returns the Package with the matching mAlias or NULL_PACKAGE if no package has the mID
   */
  private Package findPackage(String alias)
  {
    mLogger.debug("findPackage");
    Package retVal = ReleaseManager.NULL_PACKAGE;

    for (Iterator it = mPackageCollection.iterator(); it.hasNext(); )
    {
      Package p = (Package) it.next();
      
      if ( p.mAlias.compareTo( alias ) == 0 )
      {
        retVal = p;
        break;
      }
    }
    
    mLogger.info("findPackage returned " + retVal.mName);
    return retVal;
  }

  /**sets the mBuildFile to -5 for the package and all dependent packages
   */
  private void rippleBuildExclude(Package p)
  {
    mLogger.debug("rippleBuildExclude");
    if ( p.mBuildFile == 0 )
    {
      p.mBuildFile = -5;
    
      for (Iterator it = mPackageCollection.iterator(); it.hasNext(); )
      {
        Package pkg = (Package) it.next();
        
        if ( pkg != p )
        {
          for (Iterator it2 = pkg.mPackageDependencyCollection.iterator(); it2.hasNext(); )
          {
            Package dependency = (Package) it2.next();
            
            if ( dependency == p )
            {
              rippleBuildExclude( pkg );
              break;
            }
          }
        }
      }
    }
    mLogger.info("rippleBuildExclude set " + p.mName + " " + p.mBuildFile);
  }

  /**returns a build file header for the mBaseline
   */
  private String generateBuildFileHeader()
  {
    mLogger.debug("generateBuildFileHeader");
    String lf = new String( System.getProperty("line.separator") );
    String retVal = new String("");
    retVal +=
    "<?xml version=\"1.0\"?>" + lf +
    "<project name=\"mass\" default=\"full\" basedir=\".\">" + lf;
    
    if ( mDaemon )
    {
      retVal +=
      "<property name=\"abt_mail_server\" value=\"" + mMailServer + "\"/>" + lf +
      "<property name=\"abt_mail_sender\" value=\"" + mMailSender + "\"/>" + lf +
      "<property name=\"abt_rtag_id\" value=\"" + mBaseline + "\"/>" + lf +
      "<property name=\"abt_daemon\" value=\"" + mTimestamp + "\"/>" + lf;
    }
    else
    {
      retVal +=
      "<property name=\"abt_rtag_id\" value=\"-1\"/>" + lf;
    }
    
    retVal +=
    "<property name=\"abt_release\" value=\"" + mBaselineName + "\"/>" + lf +
    "<property name=\"abt_buildtool_version\" value=\"11\"/>" + lf +
    "<condition property=\"abt_family\" value=\"windows\">" + lf +
    "  <os family=\"windows\"/>" + lf +
    "</condition>" + lf +
    "<property name=\"abt_family\" value=\"unix\"/>" + lf;
    mLogger.info("generateBuildFileHeader returned " + retVal);
    return retVal;
  }

  /**returns an ant property for the passed Package
   */
  private String generatePackageProperty(Package p)
  {
    mLogger.debug("generatePackageProperty");
    String lf = new String( System.getProperty("line.separator") );
    String retVal = new String("");
    retVal +=
    "<property name=\"" + p.mAlias + "\" value=\"" + p.mName + " " + p.mVersion + p.mExtension + "\"/>" + lf;
    mLogger.info("generatePackageProperty returned " + retVal);
    return retVal;
  }

  /**returns an ant taskdef for the abt ant task
   */
  private String generateTaskdef()
  {
    mLogger.debug("generateTaskdef");
    String lf = new String( System.getProperty("line.separator") );
    String retVal = new String("");
    retVal +=
    "<taskdef name=\"abt\" classname=\"com.erggroup.buildtool.ABT\"/>" + lf;
    return retVal;
  }

  /**returns an ant target for the passed Package
   * in daemon mode:
   *  packages are categorised 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 categorisation 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) 
   */
  private String generateTarget(Package p, int buildFile)
  {
    mLogger.debug("generateTarget");
    
    if ( mDaemon && p.mBuildFile == 1 )
    {
      // populate 'missing' BuildStandards
      boolean solaris = false;
      boolean linux = false;
      boolean win32 = false;
      boolean jats = false;
      boolean determinedBuildStandard = false;
      
      for (Iterator it = p.mBuildStandardCollection.iterator(); it.hasNext(); )
      {
        BuildStandard bs = (BuildStandard)it.next();
        
        if ( bs.getSolaris() )
        {
          solaris = true;
        }
        else
        if ( bs.getLinux() )
        {
          linux = true;
        }
        else
        if ( bs.getWin32() )
        {
          win32 = true;
        }

        if ( !determinedBuildStandard && bs.getBuildStandard(!mReleaseManager.mUseDatabase).contains("<jats") )
        {
          jats = true;
          determinedBuildStandard = true;
        }
      }
      
      if ( !solaris )
      {
        BuildStandard bs = new BuildStandard(this);
        bs.setSolaris();
        
        if ( jats )
        {
          bs.setJatsNone();
        }
        else
        {
          bs.setAntNone();
        }
        
        p.mBuildStandardCollection.add(bs);
      }
      
      if ( !linux )
      {
        BuildStandard bs = new BuildStandard(this);
        bs.setLinux();
        
        if ( jats )
        {
          bs.setJatsNone();
        }
        else
        {
          bs.setAntNone();
        }
        
        p.mBuildStandardCollection.add(bs);
      }
      
      if ( !win32 )
      {
        BuildStandard bs = new BuildStandard(this);
        bs.setWin32();
        
        if ( jats )
        {
          bs.setJatsNone();
        }
        else
        {
          bs.setAntNone();
        }
        
        p.mBuildStandardCollection.add(bs);
      }
    }
    
    String lf = new String( System.getProperty("line.separator") );
    String retVal = new String("");
    
    if ( ( mDaemon && p.mBuildFile == 3 ) ||
         ( !mDaemon && ( p.mBuildFile < buildFile ) ) )
    {
      retVal +=
      "<target name=\"" + p.mAlias + "\"/>" + lf;
    }
    else
    {
      retVal +=
      "<target name=\"" + p.mAlias + ".wrap\"";
      
      if ( p.mPackageDependencyCollection.size() > 0 )
      {
        retVal +=" depends=\"";
        boolean comma = false;
        
        for (Iterator it = p.mPackageDependencyCollection.iterator(); it.hasNext(); )
        {
          if (comma)
          {
            retVal += ",";
          }
          comma = true;
          
          Package dependency = (Package) it.next();
          retVal += dependency.mAlias;
        }
        
        retVal += "\"";
      }
      retVal += ">" + lf;

      if ( !mDaemon )
      {
        boolean hasDependenciesBuiltInThisIteration = false;
        if ( ( p.mPackageDependencyCollection.size() > 0 ) )
        {
          for (Iterator it = p.mPackageDependencyCollection.iterator(); it.hasNext(); )
          {
            Package dependency = (Package) it.next();
            
            if ( dependency.mBuildFile == buildFile )
            {
              hasDependenciesBuiltInThisIteration = true;
              break;
            }
          }
        }
        
        if ( hasDependenciesBuiltInThisIteration )
        {
          retVal +=
          "  <condition property=\"" + p.mAlias + ".build\">" + lf +
          "    <and>" + lf;
          
          for (Iterator it = p.mPackageDependencyCollection.iterator(); it.hasNext(); )
          {
            Package dependency = (Package) it.next();
            
            if ( dependency.mBuildFile == buildFile )
            {
              retVal +=
              "      <or>" + lf +
              "        <equals arg1=\"${" + dependency.mAlias + ".res}\" arg2=\"0\"/>" + lf +
              "        <equals arg1=\"${" + dependency.mAlias + ".res}\" arg2=\"257\"/>" + lf +
              "      </or>" + lf;
            }
          }
          
          retVal +=
          "    </and>" + lf +
          "  </condition>" + lf;
        }
        else
        {
          retVal += "  <property name=\"" + p.mAlias + ".build\" value=\"\"/>" + lf;
        }
      }
      
      retVal +=
      "</target>" + lf +
      "<target name=\"" + p.mAlias + "\" depends=\"" + p.mAlias + ".wrap\"";
      
      if ( !mDaemon )
      {
        retVal += " if=\"" + p.mAlias + ".build\"";
      }
      
      retVal += ">" + lf;
      
      if ( mDaemon && p.mBuildFile == 1 )
      {
        retVal +=
        "<property name=\"" + p.mAlias + "pkg_id\" value=\"" + p.mPid + "\"/>" + lf +
        "<property name=\"" + p.mAlias + "pv_id\" value=\"" + p.mId + "\"/>" + lf;
      }
      
      if ( ( mDaemon && p.mBuildFile == 1 ) || !mDaemon )
      {
        retVal +=
        "<property name=\"" + p.mAlias + "packagename\" value=\"" + p.mName + "\"/>" + lf +
        "<property name=\"" + p.mAlias + "packageversion\" value=\"" + p.mVersion + "\"/>" + lf +
        "<property name=\"" + p.mAlias + "packageextension\" value=\"";
        
        if ( p.mExtension.length() > 0 )
        {
          // drop the .
          retVal += p.mExtension.substring(1);
        }
        else
        {
          retVal += p.mExtension;
        }
        
        retVal += "\"/>" + lf +
        "<property name=\"" + p.mAlias + "packagelabel\" value=\"" + p.mLabel + "\"/>" + lf;
        
        if ( p.mDirectlyPlanned )
        {
          retVal += "<property name=\"" + p.mAlias + "directchange\" value=\"\"/>" + lf;
        }
        
        mAddendum = "non generic";
  
        if ( p.isGeneric() )
        {
          mAddendum = "generic";
          retVal += "<property name=\"" + p.mAlias + "generic\" value=\"\"/>" + lf;
        }
  
        retVal += loc(p, p.mAlias + "loc", lf);
        
        if ( p.mHasAutomatedUnitTests )
        {
          retVal += 
          "<property name=\"" + p.mAlias + "unittests\" value=\"\"/>" + lf;
        }
      }
      
      retVal += "<abt>" + lf;
      
      if ( ( mDaemon && p.mBuildFile == 1 ) || !mDaemon )
      {
        for (Iterator it = p.mPackageDependencyCollection.iterator(); it.hasNext(); )
        {
          Package dependency = (Package) it.next();
          retVal +=
          "  <depend package_alias=\"${" + dependency.mAlias + "}\"/>" + lf;
        }
    
        String platforms = new String();
        String standards = new String();
        
        for (Iterator it = p.mBuildStandardCollection.iterator(); it.hasNext(); )
        {
          BuildStandard bs = (BuildStandard)it.next();

          String platform = bs.getPlatform(!mReleaseManager.mUseDatabase);
          
          if ( platform.length() > 0 )
          {
            platforms += platform + lf;
          }
          
          String standard = bs.getBuildStandard(!mReleaseManager.mUseDatabase);
          
          if ( standard.length() > 0 )
          {
            standards += standard + lf;
          }
        }
        
        retVal += platforms + standards;
      }
      
      if ( mDaemon && p.mBuildFile == 1 )
      {
       for (Iterator it = p.mBuildFailureEmailCollection.iterator(); it.hasNext(); )
       {
         String email = (String)it.next();
         retVal +=
         "  <owner email=\"" + email +"\"/>" + lf;
       }
      }
      
      retVal += "</abt>" + lf +
      "</target>" + lf;
      
      if ( mDaemon && p.mBuildFile == 1 )
      {
        retVal +=
        "<target name=\"AbtSetUp\">" + lf +
        "<property name=\"AbtSetUppackagelabel\" value=\"" + p.mLabel + "\"/>" + lf;
        
        retVal += loc(p, "AbtSetUppackagelocation", lf);

        retVal +=
        "<abt>" + lf +
        "</abt>" + lf +
        "</target>" + lf +
        "<target name=\"AbtTearDown\">" + lf +
        "<property name=\"AbtTearDownpackagelabel\" value=\"" + p.mLabel + "\"/>" + lf +
        "<property name=\"AbtTearDownpackagename\" value=\"" + p.mName + "\"/>" + lf +
        "<property name=\"AbtTearDownpackageversion\" value=\"" + p.mVersion + "\"/>" + lf +
        "<property name=\"AbtTearDownpackageextension\" value=\"";
        
        if ( p.mExtension.length() > 0 )
        {
          // drop the .
          retVal += p.mExtension.substring(1);
        }
        else
        {
          retVal += p.mExtension;
        }
        
        retVal += "\"/>" + lf;

        if ( p.isGeneric() )
        {
          retVal += "<property name=\"" + p.mAlias + "generic\" value=\"\"/>" + lf;
        }

        retVal +=        
        "<abt>" + lf +
        "</abt>" + lf +
        "</target>" + lf +
        "<target name=\"AbtPublish\">" + lf +
        "<property name=\"AbtPublishpackagelabel\" value=\"" + p.mLabel + "\"/>" + lf +
        "<property name=\"AbtPublishpackagename\" value=\"" + p.mName + "\"/>" + lf +
        "<property name=\"AbtPublishpackageversion\" value=\"" + p.mVersion + "\"/>" + lf +
        "<property name=\"AbtPublishpackageextension\" value=\"";
        
        if ( p.mExtension.length() > 0 )
        {
          // drop the .
          retVal += p.mExtension.substring(1);
        }
        else
        {
          retVal += p.mExtension;
        }
        
        retVal += "\"/>" + lf;
        
        if ( p.mDirectlyPlanned )
        {
          retVal += "<property name=\"AbtPublishdirectchange\" value=\"\"/>" + lf;
        }
        
        if ( p.isGeneric() )
        {
          retVal += "<property name=\"AbtPublishgeneric\" value=\"\"/>" + lf;
        }

        retVal += loc(p, "AbtPublishloc", lf);
        retVal +=
        "<abt>" + lf;
        
        String platforms = new String();
        
        for (Iterator it = p.mBuildStandardCollection.iterator(); it.hasNext(); )
        {
          BuildStandard bs = (BuildStandard)it.next();
          
          if ( !bs.getBuildStandard(!mReleaseManager.mUseDatabase).contains("\"none\"") )
          {

            String platform = bs.getPlatform(!mReleaseManager.mUseDatabase);
            
            if ( platform.length() > 0 )
            {
              platforms += platform + lf;
            }
          }
          
        }
        
        retVal += platforms +
        "</abt>" + lf +
        "</target>" + lf;
      }
    }
    mLogger.info("generateTarget returned " + retVal);
    return retVal;
  }

  /**returns an ant default target for the current build iteration
   */
  private String generateDefaultTarget(int buildFile)
  {
    mLogger.debug("generateDefaultTarget");
    String lf = new String( System.getProperty("line.separator") );
    String retVal = new String("");
    retVal +=
    "<target name=\"fullstart\">" + lf;
    
    if (buildFile == 1)
    {
      retVal +=
      "<echo message=\"${line.separator}The following package versions are not reproducible on any build platform:${line.separator}${line.separator}\" file=\"publish.log\" append=\"true\"/>" + lf;
      
      for (Iterator it = mPackageCollection.iterator(); it.hasNext(); )
      {
        Package p = (Package)it.next();
        
        if ( p.mBuildFile == -1 )
        {
          retVal +=
          "<echo message=\"${line.separator}" + p.mAlias + "${line.separator}\" file=\"publish.log\" append=\"true\"/>" + lf;
        }
      }

      retVal +=
      "<echo message=\"${line.separator}The following package versions are not reproducible on the build platforms associated with this baseline:${line.separator}${line.separator}\" file=\"publish.log\" append=\"true\"/>" + lf;
      
      for (Iterator it = mPackageCollection.iterator(); it.hasNext(); )
      {
        Package p = (Package)it.next();
        
        if ( p.mBuildFile == -2 )
        {
          retVal +=
          "<echo message=\"${line.separator}" + p.mAlias + "${line.separator}\" file=\"publish.log\" append=\"true\"/>" + lf;
        }
      }

      retVal +=
      "<echo message=\"${line.separator}The following package versions are not reproducible as they are directly dependent upon package versions not in the baseline:${line.separator}${line.separator}\" file=\"publish.log\" append=\"true\"/>" + lf;
      
      for (Iterator it = mPackageCollection.iterator(); it.hasNext(); )
      {
        Package p = (Package)it.next();
        
        if ( p.mBuildFile == -4 )
        {
          retVal +=
          "<echo message=\"${line.separator}" + p.mAlias + "${line.separator}\" file=\"publish.log\" append=\"true\"/>" + lf;
        }
      }

      retVal +=
      "<echo message=\"${line.separator}The following package versions are not reproducible as they are directly/indirectly dependent upon not reproducible package versions:${line.separator}${line.separator}\" file=\"publish.log\" append=\"true\"/>" + lf;
      
      for (Iterator it = mPackageCollection.iterator(); it.hasNext(); )
      {
        Package p = (Package)it.next();
        
        if ( p.mBuildFile == -5 )
        {
          retVal +=
          "<echo message=\"${line.separator}" + p.mAlias + "${line.separator}\" file=\"publish.log\" append=\"true\"/>" + lf;
        }
      }
    }
    if ( !mDaemon )
    {
      retVal +=
      "<echo message=\"${line.separator}Build Started:${line.separator}${line.separator}\" file=\"publish.log\" append=\"true\"/>" + lf;
    }
    
    retVal +=
    "</target>" + lf +
    "<target name=\"full\" depends=\"fullstart";
    
    for (Iterator it = mPackageCollection.iterator(); it.hasNext(); )
    {
      Package p = (Package)it.next();
      
      if ( ( p.mBuildFile > 0 ) && ( p.mBuildFile <= buildFile ) )
      {
        retVal += "," + p.mAlias;
      }
    }
    
    retVal +=
    "\">" + lf;
    
    if ( !mDaemon )
    {
      retVal +=
      "<echo message=\"${line.separator}Build Finished${line.separator}\" file=\"publish.log\" append=\"true\"/>" + lf;
    }
    
    retVal +=
    "</target>" + lf;
    return retVal;
  }

  /**returns a build file footer
   */
  private String generateBuildFileFooter()
  {
    mLogger.debug("generateBuildFileFooter");
    String retVal = new String("</project>");
    return retVal;
  }

  /**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 it = mPackageCollection.iterator(); it.hasNext(); )
      {
        Package pkg = (Package) it.next();
        
        if ( pkg != p )
        {
          for (Iterator it2 = pkg.mPackageDependencyCollection.iterator(); it2.hasNext(); )
          {
            Package dependency = (Package) it2.next();
            
            if ( dependency == p )
            {
              rippleIndirectlyPlanned( pkg );
              break;
            }
          }
        }
      }
    }
    mLogger.info("rippleIndirectlyPlanned set " + p.mName + " " + p.mIndirectlyPlanned);    
  }

  /**accessor method
   */
  public String getWin32SetUp()
  {
    mLogger.debug("getWin32SetUp");
    String retVal = new String("");
    
    try
    {
      if ( mEscrowClearcaseSupportCollection.size() >= 4 )
      {
        retVal = (String)mEscrowClearcaseSupportCollection.get(0);
      }
    }
    catch( ArrayIndexOutOfBoundsException e )
    {
    }
    
    mLogger.info("getWin32SetUp returned " + retVal);
    return retVal;
  }

  /**accessor method
   */
  public String getWin32TearDown()
  {
    mLogger.debug("getWin32TearDown");
    String retVal = new String("");
    
    try
    {
      if ( mEscrowClearcaseSupportCollection.size() >= 4 )
      {
        retVal = (String)mEscrowClearcaseSupportCollection.get(1);
      }
    }
    catch( ArrayIndexOutOfBoundsException e )
    {
    }
    
    mLogger.info("getWin32TearDown returned " + retVal);
    return retVal;
  }

  /**accessor method
   */
  public String getUnixSetUp()
  {
    mLogger.debug("getUnixSetUp");
    String retVal = new String("");
    
    try
    {
      if ( mEscrowClearcaseSupportCollection.size() >= 4 )
      {
        retVal = (String)mEscrowClearcaseSupportCollection.get(2);
      }
    }
    catch( ArrayIndexOutOfBoundsException e )
    {
    }
    
    mLogger.info("getUnixSetUp returned " + retVal);
    return retVal;
  }

  /**accessor method
   */
  public String getUnixTearDown()
  {
    mLogger.debug("getUnixTearDown");
    String retVal = new String("");
    
    try
    {
      if ( mEscrowClearcaseSupportCollection.size() >= 4 )
      {
        retVal = (String)mEscrowClearcaseSupportCollection.get(3);
      }
    }
    catch( ArrayIndexOutOfBoundsException e )
    {
    }
    
    mLogger.info("getUnixTearDown returned " + retVal);
    return retVal;
  }

  /**returns first build file content and addendum
   * the addendum value is one of "non generic", "generic" or "dummy"
   */
  public void getFirstBuildFileContent(MutableString content, 
                                MutableString addendum)
  {
    mLogger.debug("getFirstBuildFileContent");
    try
    {
      mBuildIndex = 0;
      content.value = (String)mBuildCollection.get( mBuildIndex );
      addendum.value = mAddendum;
    }
    catch( ArrayIndexOutOfBoundsException e )
    {
    }
    mLogger.info("getFirstBuildFileContent passed " + content.value + addendum.value);
  }

  /**returns the built loc
   */
  private String loc(Package p, String target, String lf)
  {
    mLogger.debug("loc");
    String retVal = new String();
    String loc = new String("\\");
    
    for (Iterator it = mGbeMachtypeCollection.iterator(); it.hasNext(); )
    {
      String machtype = (String) it.next();
      
      if ( machtype.compareTo("win32") == 0 )
      {
        //if ( p.isWin32Built() )
        {
          if ( target.compareTo("AbtSetUppackagelocation") != 0 )
          {
            if (mDaemon)
            {
              loc += mBaseline + "\\" + mTimestamp + "\\";
            }
            
            loc += p.mLabel + p.mLocation;
          }
          else
          {
            loc = p.mLocation;
          }
          break;
        }
      }
    }
    
    loc = loc.replace('/', '\\');
    retVal =
    "<condition property=\"" + target + "\" value=\"" + loc + "\">" + lf +
    "  <os family=\"windows\"/>" + lf +
    "</condition>" + lf;
    
    loc = "/";
    
    for (Iterator it = mGbeMachtypeCollection.iterator(); it.hasNext(); )
    {
      String machtype = (String) it.next();
      
      if ( machtype.compareTo("linux_i386") == 0 )
      {
        //if ( p.isLinuxBuilt() )
        {
          if ( target.compareTo("AbtSetUppackagelocation") != 0 )
          {
            if (mDaemon)
            {
              loc += mBaseline + "/" + mTimestamp + "/";
            }
          
            loc += p.mLabel + "/vobs" + p.mLocation;
          }
          else
          {
            loc = p.mLocation;
          }
          break;
        }
      }
      else if ( machtype.compareTo("sparc") == 0
             || machtype.compareTo("solaris10_x86") == 0
             || machtype.compareTo("solaris10_sparc32") == 0 )
      {
        //if ( p.isSolarisBuilt() )
        {
          if ( target.compareTo("AbtSetUppackagelocation") != 0 )
          {
            if (mDaemon)
            {
              loc += mBaseline + "/" + mTimestamp + "/";
            }

            loc += p.mLabel + "/vobs" + p.mLocation;
          }
          else
          {
            loc = p.mLocation;
          }
          break;
        }       
      }
    }
    loc = loc.replace('\\', '/');
    retVal += 
    "<property name=\"" + target + "\" value=\"" + loc + "\"/>" + lf;

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