Subversion Repositories DevTools

Rev

Rev 7050 | Rev 7052 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
6914 dpurdie 1
package com.erggroup.buildtool.ripple;
2
 
3
import java.sql.SQLException;
4
import java.util.ArrayList;
5
import java.util.Collections;
6
import java.util.Iterator;
7
import java.util.List;
8
import java.util.ListIterator;
9
 
7033 dpurdie 10
import org.slf4j.Logger;
11
import org.slf4j.LoggerFactory;
6914 dpurdie 12
 
13
import com.erggroup.buildtool.ripple.BuildFile.BuildFileState;
14
import com.erggroup.buildtool.ripple.ReleaseManager.BuildReason;
15
import com.erggroup.buildtool.smtp.CreateUrls;
16
import com.erggroup.buildtool.smtp.Smtpsend;
17
import com.erggroup.buildtool.utilities.StringAppender;
18
import com.erggroup.buildtool.utilities.XmlBuilder;
19
 
20
/**Plans release impact by generating a set of Strings containing build file content.
21
 */
22
public class RippleEngine
23
{
24
 
25
    /**configured mail server
26
     * @attribute
27
     */
28
    private String mMailServer = "";
29
 
30
    /**configured mail sender user
31
     * @attribute
32
     */
33
    private String mMailSender = "";
34
 
35
    /**configured global email target
36
     * @attribute
37
     */
38
    private String mMailGlobalTarget = "";
39
 
40
    /** Vector of email addresses for global and project wide use
41
     *  Most emails will be sent to this list of email recipients
42
     */
43
    public List<String> mMailGlobalCollection = new ArrayList<String>();
44
 
45
    /**name associated with the baseline
46
     * @attribute
47
     */
48
    public String mBaselineName = "";
49
 
50
    /**Collection of build exceptions associated with the baseline
51
     * Used to determine (and report) what change in build exceptions happens as part of planRelease
52
     * Daemon centric
53
     * @aggregation shared
54
     * @attribute
55
     */
56
    ArrayList<BuildExclusion> mBuildExclusionCollection = new ArrayList<BuildExclusion>();
57
 
58
    /**Logger
59
     * @attribute
60
     */
7033 dpurdie 61
    private static final Logger mLogger = LoggerFactory.getLogger(RippleEngine.class);
6914 dpurdie 62
 
63
    /** Escrow information - commands to set up escrow
64
     * @attribute
65
     */
66
    private String mEscrowSetup;
67
 
68
    /** Escrow information - Raw data (May not be used)
69
     * @attribute
70
     */
71
    private String mEscrowRawData;
72
 
7048 dpurdie 73
    /** Collections of packages
6914 dpurdie 74
     */
75
    private ArrayList<Package> mPackageCollection = new ArrayList<Package>();
7048 dpurdie 76
    private ArrayList<Package> mPackageCollectionWip = new ArrayList<Package>();
77
    private ArrayList<Package> mPackageCollectionTest = new ArrayList<Package>();
78
    private ArrayList<Package> mPackageCollectionRipple = new ArrayList<Package>();
79
    private ArrayList<Package> mPackageCollectionAll = new ArrayList<Package>();
80
 
6914 dpurdie 81
 
82
    /**index to current String item
83
     * @attribute
84
     */
85
    private int mBuildIndex;
86
 
87
    /**Database abstraction
88
     * @attribute
89
     */
90
    ReleaseManager mReleaseManager;
91
 
92
    /**Baseline identifier (rtag_id for a release manager baseline, bom_id for deployment manager baseline)
93
     * @attribute
94
     */
95
    private int mBaseline;
96
 
97
    /** Escrow Only: SBOM_ID
98
     */
99
    private int mSbomId;
100
 
101
    /** RTAG_ID
102
     *  Set from mBaseline
103
     */
104
    private int mRtagId;
105
 
106
    /**When true, mBuildCollection contains one item based on a release manager rtag_id and contains a daemon property
107
     * When false, mBuildCollection contains at least one item based on a deployment manager bom_id
108
     * Will be accessed by the Package class to calculate its mAlias
109
     * @attribute
110
     */
111
    public boolean mDaemon;
112
 
113
    /**collection of build file content in String form
114
     * @attribute
115
     */
116
    private ArrayList<BuildFile> mBuildCollection = new ArrayList<BuildFile>();
117
 
118
    /** List of packages that we plan to build
119
     * Used to provide feedback into RM
120
     * Only the first entry is about to be built as we re-plan every cycle
121
     */
122
    private ArrayList<Package> mBuildOrder = new ArrayList  <Package>();
123
    /**Warning message
124
     * @attribute
125
     */
126
    private static final String mAnyBuildPlatforms = "Warning. The following package versions are not reproducible on any build platform: ";
127
 
128
    /**Flag to control output to standard out
129
     * @attribute
130
     */
131
    private boolean mAnyBuildPlatformsFlag = true;
132
 
133
    /**Warning message
134
     * @attribute
135
     */
136
    private static final String mAssocBuildPlatforms = "Warning. The following package versions are not reproducible on the build platforms associated with this baseline: ";
137
 
138
    /**Flag to control output to standard out
139
     * @attribute
140
     */
141
    private boolean mAssocBuildPlatformsFlag = true;
142
 
143
    /**Warning message
144
     * @attribute
145
     */
146
    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: ";
147
 
148
    /**Flag to control output to standard out
149
     * @attribute
150
     */
151
    private boolean mNotInBaselineFlag = true;
152
 
153
    /**Warning message
154
     * @attribute
155
     */
156
    private static final String mDependent = "Warning. The following package versions are not reproducible as they are directly/indirectly dependent upon not reproducible package versions: ";
157
 
158
    /**Flag to control output to standard out
159
     * @attribute
160
     */
161
    private boolean mDependentFlag = true;
162
 
163
    /**Warning message
164
     * @attribute
165
     */
166
    private static final String mCircularDependency = "Warning. The following package versions are not reproducible as they have circular dependencies: ";
167
 
168
    /**Flag to control output to standard out
169
     * @attribute
170
     */
171
    private boolean mCircularDependencyFlag = true;
172
 
173
    /** String used to terminate lines
174
     * @attribute
175
     */
176
    private static final  String mlf = System.getProperty("line.separator");
177
 
178
    /** XML File Prefix
179
     */
180
    private static final String mXmlHeader = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>" + mlf;
181
 
182
    /**RippleEngine constructor
183
     * @param releaseManager  - Associated releaseManager instance
184
     * @param rtagId         - Release Identifier
185
     * @param isDaemon        - Mode of operation. False: Escrow, True: Daemon
186
     */
187
    public RippleEngine(ReleaseManager releaseManager, int rtagId, boolean isDaemon)
188
    {
7048 dpurdie 189
        mLogger.debug("RippleEngine rtag_id {} isDaemon {}", rtagId, isDaemon);
6914 dpurdie 190
        mReleaseManager = releaseManager;
191
        mBaseline = rtagId;
192
        mRtagId = rtagId;
193
        mDaemon = isDaemon;
194
        mReleaseManager.setDaemonMode(mDaemon);
195
    }
196
 
197
    /**
198
     * getRtagId
7048 dpurdie 199
     * @return The rtagId of the Release attached to this instance of the RippleEngine
6914 dpurdie 200
     */
201
    public int getRtagId()
202
    {
203
        return mRtagId;
204
    }
205
 
206
    /**Plan what is to be built
207
     * 	<br>Discards all build file content
208
     * 	<br>Generates new build file content
209
     * 
210
     * @param lastBuildActive 		- False. Daemon Mode. The last build was a dummy. 
211
     *                                This planning session may not result in a build
212
     *                              - True. Daemon Mode. The last build was not a dummy.
213
     *                                There is a very good chance that this planning session
214
     *                                will result in a build, so it is given priority to connect 
215
     *                                to the database.	
216
     */
217
    public void planRelease(final boolean lastBuildActive) throws SQLException, Exception
218
    {
7048 dpurdie 219
        mLogger.warn("planRelease mDaemon {}", mDaemon);
6914 dpurdie 220
 
221
        mBuildCollection.clear();
222
        mPackageCollection.clear();
7048 dpurdie 223
        mPackageCollectionRipple.clear();
224
        mPackageCollectionTest.clear();
225
        mPackageCollectionWip.clear();
6914 dpurdie 226
        mBuildOrder.clear();
227
        mEscrowRawData = "";
228
        mEscrowSetup = "";
229
        Phase phase = new Phase("Plan");
230
 
231
        // use finally block in planRelease to ensure the connection is released
232
        try
233
        {
234
            phase.setPhase("connectForPlanning");
235
            mReleaseManager.connectForPlanning(lastBuildActive);
236
 
237
            if ( mDaemon )
238
            {
239
                // claim the mutex
240
                mLogger.warn("planRelease claimMutex");
241
                phase.setPhase("claimMutex");
242
                mReleaseManager.claimMutex();
243
 
244
                // Populate the mBuildExclusionCollection
245
                //
246
                // Builds are either 'Directly excluded' or 'Indirectly excluded'
247
                // Direct excludes result from:
248
                //      User request
249
                //      Build failure
250
                //      Inability to build package
251
                // Indirectly excluded packages result from have a build dependency on a package
252
                // that is directly excluded.
253
                //  
254
                // In the following code we will extract from the Database all build exclusions
255
                // We will then add 'Relevant' entries to mBuildExclusionCollection
256
                // and delete, from the database those that are no longer relevant - ie indirectly excluded
257
                // items where the root cause is no longer in the set.
258
                phase.setPhase("mBuildExclusionCollection");
259
                mBuildExclusionCollection.clear();
260
                ArrayList<BuildExclusion> tempBuildExclusionCollection = new ArrayList<BuildExclusion>();
261
 
262
                mLogger.debug("planRelease queryBuildExclusions");
263
                mReleaseManager.queryBuildExclusions(tempBuildExclusionCollection, mBaseline);
264
 
265
                // only populate mBuildExclusionCollection with tempBuildExclusionCollection entries which have a relevant root_pv_id
266
                // The entry is relevant if:
267
                //     It is for a directly excluded package,other than a RippleStop
268
                //     It is for an indirectly excluded package AND the reason for exclusion still exists
269
                //
270
 
271
                for (Iterator<BuildExclusion> it = tempBuildExclusionCollection.iterator(); it.hasNext(); )
272
                {
273
                    BuildExclusion buildExclusion = it.next();
274
 
275
                    if ( buildExclusion.isRelevant(tempBuildExclusionCollection) )
276
                    {
277
                        mBuildExclusionCollection.add(buildExclusion);
278
                    }
279
                    else
280
                    {
281
                        // Remove the indirectly excluded entry as its root cause
282
                        // is no longer present.
283
                        buildExclusion.includeToBuild(mReleaseManager, mBaseline);
284
                    }
285
                }
286
            }
287
 
288
            //-----------------------------------------------------------------------
289
            //	Query package versions
290
            //
291
            phase.setPhase("queryPackageVersions");
292
            mLogger.debug("planRelease queryPackageVersions");
293
            mReleaseManager.queryPackageVersions(this, mPackageCollection, mBaseline);
7048 dpurdie 294
            mReleaseManager.queryWips(this, mPackageCollectionWip, mBaseline);
295
            mReleaseManager.queryTest(this, mPackageCollectionTest, mBaseline);
296
            mReleaseManager.queryRipples(this, mPackageCollectionRipple, mBaseline);
297
            mPackageCollectionAll.addAll(mPackageCollection);
298
            mPackageCollectionAll.addAll(mPackageCollectionWip);
299
            mPackageCollectionAll.addAll(mPackageCollectionTest);
300
            mPackageCollectionAll.addAll(mPackageCollectionRipple);
301
 
302
            // Sort the collection by PVID
303
            //      Unit Test output order is known
304
            //      May assist in creating repeatable build orders
305
            //
306
            Collections.sort(mPackageCollectionAll, Package.SeqComparator);
6914 dpurdie 307
 
308
            //------------------------------------------------------------------------
309
            //    Process packages collected
310
            //    Determine and tag those we can't build
311
            phase.setPhase("processPackages");
312
            processPackages();
313
 
314
            //-----------------------------------------------------------------------
315
            //    At this point we have tagged all the packages that we cannot build
316
            //    Now we can determine what we are building
317
            phase.setPhase("planBuildOrder");
318
            mLogger.debug("planRelease process Remaining");
319
            planBuildOrder();
320
 
321
            //  Report excluded packages and the build plan
322
            //  This is being done with the MUTEX being held, but the 
323
            //  trade off is the cost of getting a connection.
324
            //
325
            if ( mDaemon )
326
            {
327
                phase.setPhase("Report Change");
328
                reportChange();
329
                phase.setPhase("Report Plan");            
330
                reportPlan();
331
            }
332
 
333
            //
334
            //	Generate the build Files
335
            //
336
            phase.setPhase("generateBuildFiles");
337
            generateBuildFiles();
338
        }
339
        finally
340
        {
341
            mLogger.debug("planRelease finally");
342
            // this block is executed regardless of what happens in the try block
343
            // even if an exception is thrown
344
            // ensure the SELECT FOR UPDATE is released
345
            try
346
            {
347
                if ( mDaemon )
348
                {
349
                    // attempt to release the SELECT FOR UPDATE through a commit
350
                    // a commit must be done in the normal case
351
                    // a commit may as well be done in the Exception case
352
                    // in the case of a SQLException indicating database connectivity has been lost
353
                    // having a go at the commit is superfluous
354
                    // as the SELECT FOR UPDATE will have been released upon disconnection
355
                    phase.setPhase("releaseMutex");
356
                    mReleaseManager.releaseMutex();
357
                }
358
            }
359
            finally
360
            {
361
                // ensure disconnect under all error conditions
362
                phase.setPhase("disconnectForPlanning");
363
                mReleaseManager.disconnectForPlanning(lastBuildActive);
364
            }
365
        }
366
 
7048 dpurdie 367
        mLogger.warn("planRelease mDaemon {} returned", mDaemon);
6914 dpurdie 368
        phase.setPhase("EndPlan");
369
    }
370
 
371
    /** Process packages that have been collected as a part of the plan
372
     * @param phase
373
     * @throws SQLException
374
     * @throws Exception
375
     */
376
    private void processPackages() throws SQLException, Exception {
377
 
378
        // Deal with test builds here as they may impact upon package attributes
7048 dpurdie 379
        //    eg: dependency collection and build standard differences
6914 dpurdie 380
        //    Note: Done before mPackageDependencyCollection is setup
381
        //
382
        if ( mDaemon )
383
        {
7048 dpurdie 384
            // Process test builds - they are in their own collection
385
            for (Iterator<Package> it = mPackageCollectionTest.iterator(); it.hasNext(); )
6914 dpurdie 386
            {
387
                Package p = it.next();
7048 dpurdie 388
                mLogger.info("planRelease package test build {}", p.mAlias);
6914 dpurdie 389
 
7048 dpurdie 390
                //
391
                //    Cannot test build an SDK based package or a Pegged Package
392
                //        Remove the test build request from the database
393
                //        Send a warning email
394
                //
395
                if(p.mIsPegged || p.mIsSdk)
6914 dpurdie 396
                {
7048 dpurdie 397
                    String reason;
398
                    reason = (p.mIsPegged) ? "Pegged" : "SDK Based";
6914 dpurdie 399
 
7048 dpurdie 400
                    mLogger.error("planRelease Daemon Instruction (testBuild) of {} package deleted: {}", reason, p.mAlias);
401
                    mReleaseManager.markDaemonInstCompleted( p.mTestBuildInstruction );
402
                    emailRejectedDaemonInstruction("Cannot 'Test Build' a " + reason + " package",p);
403
                }
6914 dpurdie 404
 
7048 dpurdie 405
                // force patch for test build numbering
406
                p.mDirectlyPlanned = true;
407
                p.mChangeType.setPatch();
408
                p.mRequiresSourceControlInteraction = false;
409
                p.mBuildReason = BuildReason.Test;
410
                p.mIndirectlyPlanned = true;
6914 dpurdie 411
            }
412
        }
413
 
414
        // Set up mPackageDependencyCollection on each package
415
        //    Examine the dependencies by alias and convert this to a 'package' selected from the released package set
7048 dpurdie 416
        mLogger.debug("planRelease setup setPackageDependencyCollection");
417
        for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
6914 dpurdie 418
        {
419
            Package p = it.next();
420
 
421
            for (Iterator<String> it2 = p.mDependencyCollection.iterator(); it2.hasNext(); )
422
            {
423
                String alias = it2.next();
424
                Package dependency = findPackage(alias);
425
                p.mPackageDependencyCollection.add(dependency);
426
            }
7048 dpurdie 427
        }    
6914 dpurdie 428
 
7048 dpurdie 429
        // Detect and deal with circular dependencies
430
        // Examine all packages under consideration
431
        //
6914 dpurdie 432
        mLogger.debug("planRelease deal with circular dependencies");
7048 dpurdie 433
        for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
6914 dpurdie 434
        {
435
            Package p = it.next();
436
 
437
            if ( p.hasCircularDependency( this ) )
438
            {
7048 dpurdie 439
                mLogger.info("planRelease circular dependency detected {}", p.mAlias);
6914 dpurdie 440
 
441
                //  Force this package to be marked as having a circular dependency - even if its been excluded
442
                p.mBuildFile = 0;
443
 
444
                // exclude all dependent packages
445
                // max 50 chars
446
                rippleBuildExclude(p, p.mId, "Package has circular dependency", null, null, true);
447
 
448
                // take the package out of the build
449
                p.mBuildFile = -6;
7048 dpurdie 450
                mLogger.info("planRelease set mBuildFile to -6 for package {}", p.mAlias );
6914 dpurdie 451
                standardOut(mCircularDependency, p.mAlias, mCircularDependencyFlag);
452
                mCircularDependencyFlag = false;
453
            }
454
        }
455
 
456
        // Scan for packages with missing dependencies
457
        //    ie: The dependent package is not in the package Collection
458
        //        DEVI 55483 now use the fully built mPackageDependencyCollection (in rippleBuildExclude)
459
        mLogger.debug("planRelease use the fully built mPackageDependencyCollection");
7048 dpurdie 460
        for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
6914 dpurdie 461
        {
462
            Package p = it.next();
463
 
464
            if ( mDaemon )
465
            {
466
                //  Daemon Mode Only
467
                //  Not interested in the dependencies of a pegged package or a package provided from an SDK.
468
                //  Such packages will be deemed to not have missing dependencies
469
                if (p.mIsPegged || p.mIsSdk)
470
                {
471
                    continue;
472
                }
473
            }
474
 
475
            for (Iterator<String> it2 = p.mDependencyCollection.iterator(); it2.hasNext(); )
476
            {
477
                String alias = it2.next();
478
                Package dependency = findPackage(alias);
479
 
480
                if (dependency == ReleaseManager.NULL_PACKAGE)
481
                {
7048 dpurdie 482
                    mLogger.info("planRelease dependency is not in the baseline {}", alias);
6914 dpurdie 483
                    // exclude all dependent packages
484
                    // max 50 chars
485
                    rippleBuildExclude(p, p.mId, "Package build dependency not in the release", null, null, true);
486
 
487
                    // take the package out of the build
488
                    p.mBuildFile = -4;
7048 dpurdie 489
                    mLogger.info("planRelease set mBuildFile to -4 for package {}", p.mAlias );
6914 dpurdie 490
                    standardOut(mNotInBaseline, p.mAlias, mNotInBaselineFlag);
491
                    mNotInBaselineFlag = false;
492
                    break;
493
                }
494
            }
495
        }
496
 
7048 dpurdie 497
        // Detect packages with no build standard and exclude them from the build
6914 dpurdie 498
        mLogger.debug("planRelease process packages which are not reproducible");
7048 dpurdie 499
        for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
6914 dpurdie 500
        {
501
            Package p = it.next();
502
 
503
            if (p.mBuildFile == 0)
504
            {
505
                if ( mDaemon )
506
                {
507
                    //  Daemon Mode Only
508
                    //  Not interested in the reproducibility of a pegged package or a package provided from an SDK.
509
                    //  Such packages will be deemed to be reproducible - but out side of this release
510
                    if (p.mIsPegged || p.mIsSdk)
511
                    {
512
                        continue;
513
                    }
514
                }
515
 
7048 dpurdie 516
                // Does the package have a build standard. If not then we can't reproduce it.
517
                // Escrow - Assume the package is provided
518
                // Daemon - Exclude this package, but not its consumers. If the package is available then we can use it.
6914 dpurdie 519
                if (!p.isReproducible())
520
                {
7048 dpurdie 521
                    mLogger.info("planRelease package not reproducible {}" ,p.mName);
6914 dpurdie 522
                    // max 50 chars
7048 dpurdie 523
                    rippleBuildExclude(p, p.mId, "Package has no build environment", null, null, false);
6914 dpurdie 524
 
525
                    // package is not reproducible, discard
526
                    p.mBuildFile = -1;
7048 dpurdie 527
                    mLogger.info("planRelease set mBuildFile to -1 for package {}", p.mAlias );
6914 dpurdie 528
                    standardOut(mAnyBuildPlatforms, p.mAlias, mAnyBuildPlatformsFlag);
529
                    mAnyBuildPlatformsFlag = false;
530
                }
531
            }
532
        }
533
 
7048 dpurdie 534
        //    Process packages which are not reproducible on the configured set of build machines.
6914 dpurdie 535
        //
536
        //    Test each package and determine if the package contains a buildStandard that
537
        //    can be processed by one of the machines in the build set
538
        //
539
        //    ie: Package: Win32:Production and we have a BM with a class of 'Win32'
540
        //
541
        //    Only exclude the failing package and not its dependents
542
        //    May be legitimate in the release
543
        //
7048 dpurdie 544
//TODO - verify this code. I'm not sure it can be reached. I think that the "Package has no build environment" test above is detecting all these packages
6914 dpurdie 545
        mLogger.debug("planRelease process packages which are not reproducible2");
7048 dpurdie 546
        for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
6914 dpurdie 547
        {
548
            Package p = it.next();
549
 
550
            if (p.mBuildFile == 0)
551
            {
552
                if ( mDaemon )
553
                {
554
                    //  Daemon Mode Only
555
                    //  Not interested in the reproducibility of a pegged package or a package provided from an SDK.
556
                    //  Such packages will be deemed to be reproducible - but out side of this release
557
                    if (p.mIsPegged || p.mIsSdk)
558
                    {
559
                        continue;
560
                    }
561
                }
562
 
563
                // package has yet to be processed
564
                // assume it does not need to be reproduced for this baseline
565
                //
566
                //    For each machineClass in the buildset
567
                boolean reproduce = false;
568
 
569
                for (Iterator<String> it2 = mReleaseManager.mReleaseConfigCollection.mMachineClasses.iterator(); it2.hasNext(); )
570
                {
571
                    String machineClass = it2.next();
572
 
573
                    if ( p.canBeBuildby(machineClass))
574
                    {
575
                        reproduce = true;
7048 dpurdie 576
                        mLogger.info("planRelease package built on {} {}", machineClass, p.mAlias );
6914 dpurdie 577
                        break;
578
                    }
579
                }
580
 
581
                if ( !reproduce )
582
                {
7048 dpurdie 583
                    mLogger.info("planRelease package not reproducible on the build platforms configured for this baseline {}", p.mName);
6914 dpurdie 584
 
7048 dpurdie 585
                    // max 50 chars
586
                    rippleBuildExclude(p, p.mId, "Package not built for configured platforms", null, null, false);
6914 dpurdie 587
 
588
                    // package is not reproducible on the build platforms configured for this baseline, discard
589
                    p.mBuildFile = -2;
7048 dpurdie 590
                    mLogger.info("planRelease set mBuildFile to -2 for package {}", p.mAlias );
6914 dpurdie 591
                    standardOut(mAssocBuildPlatforms, p.mAlias, mAssocBuildPlatformsFlag);
592
                    mAssocBuildPlatformsFlag = false;
593
                }
594
            }
595
        }      
596
 
597
        if (mDaemon)
598
        {
599
            // Daemon Mode Only
600
            // Process packages which are not ripple buildable, and all packages dependent upon them
601
            mLogger.debug("planRelease process packages which are not ripple buildable");
602
            for (ListIterator<BuildExclusion> it = mBuildExclusionCollection.listIterator(); it.hasNext(); )
603
            {
604
                BuildExclusion be = it.next();
605
 
606
                for (Iterator<Package> it1 = mPackageCollection.iterator(); it1.hasNext(); )
607
                {
608
                    Package p = it1.next();
609
 
610
                    // ensure only root cause, non test build, build exclusions are excluded
611
                    // mBuildExclusionCollection is at this point based on
612
                    // relevant (direct and indirect) excluded pv's in the database
613
                    if ( be.compare(p.mId) && be.isARootCause() && p.mTestBuildInstruction == 0 && p.mForcedRippleInstruction == 0 )
614
                    {
615
                        // package is not reproducible, discard
7048 dpurdie 616
 
6914 dpurdie 617
                        rippleBuildExclude( p, p.mId, null, it, be, true );
618
                        p.mBuildFile = -3;
7048 dpurdie 619
                        mLogger.info("planRelease set mBuildFile to -3 for package {}", p.mAlias );
6914 dpurdie 620
                        break;
621
                    }
622
                }
623
            }
624
 
625
            //  Daemon Mode Only
626
            //  Process packages which need to be ripple built
627
            mLogger.debug("planRelease process packages which need to be ripple built");
628
            for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
629
            {
630
                Package p = it.next();
631
 
632
                if (p.mBuildFile == 0)
633
                {
634
                    //  Not interested in a pegged package or a package provided from an SDK.
635
                    //  Such packages are not rippled
636
                    if (p.mIsPegged || p.mIsSdk)
637
                    {
638
                        continue;
639
                    }
640
 
7048 dpurdie 641
                    //    Examine this packages dependencies
642
                    //    If one of them does not exist in the 'official release' set then
643
                    //    the package needs to be built against the official release set
644
                    //    and we can't build any of its dependent packages
645
                    Iterator<Integer> it2 = p.mDependencyIDCollection.iterator();
646
                    Iterator<Package> it3 = p.mPackageDependencyCollection.iterator();
647
                    while ( it2.hasNext() && it3.hasNext() )
6914 dpurdie 648
                    {
7048 dpurdie 649
                        Integer dpvId = it2.next();
650
                        Package dependency = it3.next();
651
 
652
                        if ( !dependency.mAdvisoryRipple )
6914 dpurdie 653
                        {
7048 dpurdie 654
                            // not advisory, ie: has ripple build impact
655
                            if ( !isInRelease(dpvId) )
6914 dpurdie 656
                            {
7048 dpurdie 657
                                // the package is out of date
658
                                // exclude all dependent package versions
659
                                mLogger.info("planRelease package out of date {}", p.mName);
660
                                p.mBuildReason = BuildReason.Ripple;
661
                                rippleIndirectlyPlanned(p);
662
 
663
                                //  This package needs to be rippled
664
                                //  If this package has a rippleStop marker of 's', then we cannot
665
                                //  build this package at the moment.
666
                                //  Packages that depend on this package have been excluded
667
                                //  We need to exclude this one too
668
                                //
669
                                if (p.mRippleStop == 's' || p.mRippleStop == 'w') 
6914 dpurdie 670
                                {
7048 dpurdie 671
                                    // Package marked as a rippleStop
672
                                    // max 50 chars
673
                                    rippleBuildExclude(p, -2, "Ripple Required." + " Waiting for user", null, null, false);
674
 
675
                                    // package is unBuildable, mark reason and discard
676
                                    p.mBuildFile = -11;
6914 dpurdie 677
 
7048 dpurdie 678
                                    if (p.mRippleStop == 's' ) {
679
                                        // Need to flag to users that the package build is waiting user action
680
                                        mLogger.info("planRelease Ripple Required. Stopped by flag {}", p.mName);
681
                                        mReleaseManager.setRippleStopWait(mRtagId,p);
6914 dpurdie 682
                                    }
683
                                }
7048 dpurdie 684
 
685
                                break;
6914 dpurdie 686
                            }
687
                        }
688
                    }
689
                }
690
            }
691
 
692
            //  Daemon Mode Only
693
            //  Process packages which do not exist in the archive
694
            //  For unit test purposes, assume all packages exist in the archive if released
695
            mLogger.debug("planRelease process packages which do not exist in the archive");
7048 dpurdie 696
            if ( mReleaseManager.mUseDatabase )
6914 dpurdie 697
            {
698
                for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
699
                {
700
                    Package p = it.next();
701
 
702
                    if (p.mBuildFile == 0)
703
                    {
704
                        // package has yet to be processed
705
                        if (!p.mDirectlyPlanned && !p.mIndirectlyPlanned && p.mForcedRippleInstruction == 0)
706
                        {
707
                            // check package version archive existence
708
                            if (!p.existsInDpkgArchive())
709
                            {
710
                                if (! p.mIsBuildable)
711
                                {
712
                                    //  Package does not exist in dpkg_archive and it has been flagged as unbuildable
713
                                    //  This may be because is Unbuildable or Manually built
7048 dpurdie 714
                                    mLogger.info("planRelease Unbuildable package not found in archive {}", p.mName);
6914 dpurdie 715
                                    // max 50 chars
716
                                    rippleBuildExclude(p, p.mId, "Unbuildable" + " package not found in archive", null, null, true);
717
 
718
                                    // package is unBuildable, mark reason and discard
719
                                    p.mBuildFile = -10;
720
 
721
                                }
722
                                //  Not interested in a pegged package or a package provided from an SDK.
723
                                //  Such packages are not rippled
724
                                else if (p.mIsPegged || p.mIsSdk)
725
                                {
726
                                    String reason;
727
                                    reason = (p.mIsPegged) ? "Pegged" : "SDK";
728
 
729
                                    //  Pegged packages or packages provided from an SDK MUST exist in dpkg_archive
730
                                    //  They will not be built within the context of this release. It is the responsibility
731
                                    //  of another release to build them.
7048 dpurdie 732
                                    mLogger.info("planRelease {} package not found in archive {}", reason, p.mName);
6914 dpurdie 733
                                    // max 50 chars
734
                                    rippleBuildExclude(p, p.mId, reason + " package not found in archive", null, null, true);
735
 
736
                                    // package is not reproducible, mark reason and discard
737
                                    p.mBuildFile = -7;
738
 
739
                                }
740
                                else if (p.mForcedRippleInstruction == 0)
741
                                {
742
                                    //  [JATS-331] Unable to rebuild package with Advisory Ripple dependencies
743
                                    //    Examine this packages dependencies
744
                                    //    If one of them does not exist in the 'official release' set then
745
                                    //    the package cannot be rebuilt in this release.
746
                                    for (Iterator<Integer> it2 = p.mDependencyIDCollection.iterator() ; it2.hasNext() ;)
747
                                    {
748
                                        Integer dpvId = it2.next();
749
                                        if ( !isInRelease(dpvId) )
750
                                        {
751
                                            // This package cannot be rebuilt as one of its dependents is NOT in this release
752
                                            // exclude all dependent package versions
753
 
7048 dpurdie 754
                                            mLogger.info("planRelease package not found in archive. Cannot be rebuilt due to {}", p.mName);
6914 dpurdie 755
                                            // max 50 chars
756
                                            rippleBuildExclude(p, p.mId, "Package cannot be rebuilt in this release", null, null, true);
757
 
758
                                            // package is not reproducible, mark reason and discard
759
                                            p.mBuildFile = -4;
760
                                            break;
761
                                        }
762
                                    }
763
 
764
                                    //  The package has not been excluded from the build
765
                                    if (p.mBuildFile == 0)
766
                                    {
7048 dpurdie 767
                                        mLogger.info("planRelease package not found in archive {}", p.mName);
6914 dpurdie 768
                                        // DEVI 47395 the cause of this build is not WIP or ripple induced,
769
                                        // it simply does not exist in the archive (has been removed)
770
                                        // prevent source control interaction
771
                                        p.mRequiresSourceControlInteraction = false;
772
                                        p.mBuildReason = BuildReason.Restore;
773
                                        rippleIndirectlyPlanned(p);
774
                                    }
775
                                }
776
                            }
777
                        }
778
                    }
779
                }
780
            }
781
 
782
            //  Daemon Mode Only
7048 dpurdie 783
            //  Detect bad forced ripples requests and reject them
6914 dpurdie 784
            mLogger.debug("planRelease process forced ripples");
7048 dpurdie 785
            for (Iterator<Package> it = mPackageCollectionRipple.iterator(); it.hasNext(); )
6914 dpurdie 786
            {
787
                Package p = it.next();
788
 
789
                if (p.mBuildFile == 0)
790
                {
7048 dpurdie 791
                    //
792
                    //    Cannot force a ripple on an SDK based package or a Pegged Package
793
                    //        Remove the daemon instruction from the database
794
                    //        Send a warning email
795
                    //
796
                    if(p.mIsPegged || p.mIsSdk)
6914 dpurdie 797
                    {
7048 dpurdie 798
                        String reason;
799
                        reason = (p.mIsPegged) ? "Pegged" : "SDK Based";
6914 dpurdie 800
 
7048 dpurdie 801
                        mLogger.error("planRelease Daemon Instruction of {} package deleted: {}", reason, p.mName);
802
                        mReleaseManager.markDaemonInstCompleted( p.mForcedRippleInstruction );
803
                        emailRejectedDaemonInstruction("Cannot 'Ripple' a " + reason + " package",p);
6914 dpurdie 804
 
7048 dpurdie 805
                        p.mBuildFile = -8; 
6914 dpurdie 806
                    }
807
                }
808
            }
809
 
810
            //  Daemon Mode Only
811
            //  Mark Pegged and SDK packages as not to be built
812
            //  Have previously detected conflicts between pegged/sdk packages and daemon instructions
813
            //
814
            mLogger.debug("planRelease remove pegged and SDK packages from the build set");
815
            for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
816
            {
817
                Package p = it.next();
818
 
819
                if (p.mBuildFile == 0)
820
                {
821
                    //  Not interested in a pegged package or a package provided from an SDK.
822
                    //  Such packages are not built
823
                    if (p.mIsPegged || p.mIsSdk)
824
                    {
825
                        String reason;
826
                        reason = (p.mIsPegged) ? "Pegged" : "SDK";
827
 
7048 dpurdie 828
                        mLogger.info("planRelease {} not built in this release {}", reason, p.mName);
6914 dpurdie 829
                        p.mBuildFile = -8;
830
                    }
831
                }
832
            }
833
 
834
            //  Daemon Mode Only
835
            //  Locate Test Build Requests that cannot be satisfied - will not be built due to
836
            //  errors in dependent packages. Report the error to the user and remove the request
837
            //
7048 dpurdie 838
            for (Iterator<Package> it = mPackageCollectionTest.iterator(); it.hasNext(); )
6914 dpurdie 839
            {
840
                Package p = it.next();
841
 
7048 dpurdie 842
                if (p.mBuildFile < 0)
6914 dpurdie 843
                {
7048 dpurdie 844
                    String reason;
845
                    switch (p.mBuildFile)
6914 dpurdie 846
                    {
7048 dpurdie 847
                    case -1:  reason = "Not reproducible"; break;
848
                    case -2:  reason = "Not reproducible on configured build platforms"; break;
849
                    case -3:  reason = "Marked as 'Do not ripple'"; break;
850
                    case -4:  reason = "Dependent on a package not in the release"; break;
851
                    case -5:  reason = "Indirectly dependent on a package not reproducible in the release"; break;
852
                    case -6:  reason = "Has a circular dependency"; break;
853
                    case -7:  reason = "Pegged or SDK package not in dpkg_archive"; break;
854
                    case -8:  reason = "Is a Pegged or SDK package"; break;
855
                    case -9:  reason = "Rejected Daemon Instruction"; break;
856
                    case -10: reason = "Unbuildable package not in dpkg_archive"; break;
857
                    case -11: reason = "Marked as 'RippleStop'"; break;
858
                    default:  reason = "Unknown reason. Code:" + p.mBuildFile; break;
6914 dpurdie 859
                    }
7048 dpurdie 860
                    mLogger.error("planRelease Test Build of an unbuildable of package deleted: {}", p.mName);
861
                    mReleaseManager.markDaemonInstCompleted( p.mTestBuildInstruction );
862
                    emailRejectedDaemonInstruction(reason,p);
6914 dpurdie 863
                }
864
            }
7048 dpurdie 865
 
6914 dpurdie 866
        }
867
        else
868
        {
869
            // escrow reporting only
870
            // Report packages that are not reproducible
871
            //
872
            //  Note: I don't believe this code can be executed
873
            //        The -3 is only set in daemon mode
874
            for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
875
            {
876
                Package p = it.next();
877
 
878
                if (p.mBuildFile == -3)
879
                {
880
                    standardOut(mDependent, p.mAlias, mDependentFlag);
881
                    mDependentFlag = false;
882
                }
883
            }
884
        }
885
    }
886
 
887
    /** Plan the build order.
888
     *  Assumes that a great deal of work has been done.
889
     *  This is a stand alone method to contain the work
890
     *  
891
     * @throws Exception
892
     * @throws SQLException
893
     */
894
    private void planBuildOrder() throws Exception, SQLException {
895
 
896
        // Process remaining packages which are need to be reproduced for this baseline.
897
        //    Determine the build file for each package
898
        //    For daemon builds:
7048 dpurdie 899
        //      Determine the next package that can be built now
900
        //          Sounds simple - doesn't it
6914 dpurdie 901
        //      Set its mBuildNumber to 1, all remaining reproducible packages to 2
902
        //    For escrow builds:
903
        //      Determine the package versions that can be built in the build iteration
904
        //      Set their mBuildNumber to the build iteration
905
        //      Increment the build iteration and repeat until all package versions 
906
        //      that need to be reproduced have been assigned a build iteration        
907
 
908
        boolean allProcessed = false;
909
        int buildFile = 1;
910
 
911
        do
912
        {
7048 dpurdie 913
            //
914
            //  Create the mBuildOrder collection
915
            //      Same for both Daemon and Escrow modes
916
            //      Will have package build order and build level
6914 dpurdie 917
            boolean allDependenciesProcessed = true;
918
            do
919
            {
920
                allDependenciesProcessed = true;
921
                for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
922
                {
923
                    Package p = it.next();
924
 
7048 dpurdie 925
                    if ( p.mBuildFile < 0  ) 
6914 dpurdie 926
                    {
7048 dpurdie 927
                        // Daemon: Flag packages that cannot be built
6914 dpurdie 928
                        // Escrow: Flag packages with a foreign build environment as processed
929
                        p.mProcessed = true;
7048 dpurdie 930
                        mLogger.info("planRelease package not buildable {}", p.mName);            
6914 dpurdie 931
                    }
7048 dpurdie 932
                    else if ( p.mBuildFile == 0 )
6914 dpurdie 933
                    {
934
                        // package yet to be processed and
7048 dpurdie 935
                        //  Daemon mode: Could be built
6914 dpurdie 936
                        //  Escrow mode: Can be reproduced
937
                        boolean canBeBuiltNow = true;
938
                        boolean allDependenciesForThisPackageProcessed = true;
939
 
940
                        //  Scan the packages dependencies
941
                        for ( Iterator<Package> it2 = p.mPackageDependencyCollection.iterator(); it2.hasNext(); )
942
                        {
943
                            Package dependency = it2.next();
944
 
945
                            if ( !dependency.mProcessed )
946
                            {
947
                                //  If all the dependencies have been processed, then we can potentially build this package
948
                                //  If any of them have not been processed, then cannot calculate canBeBuiltNow until this 
949
                                //  dependency has been processed
950
                                allDependenciesForThisPackageProcessed = false;
951
                                allDependenciesProcessed = false;
952
                            }
7048 dpurdie 953
                            else if (  (  mDaemon && (   dependency.mBuildFile == 0 ) ) 
6914 dpurdie 954
                                    || ( !mDaemon && ( ( dependency.mBuildFile == 0 ) ||
955
                                                           ( dependency.mBuildFile == buildFile &&
956
                                                           ( !p.haveSameBuildStandards(dependency)  ) ) ) ) )
957
                            {
958
                                // Daemon mode:
7048 dpurdie 959
                                //    This processed dependency has not been assigned to a build iteration - so can't built yet
6914 dpurdie 960
                                // Escrow mode: This package cannot be built now if
961
                                //    This processed dependency has not been assigned to a build iteration or
962
                                //    This processed dependency has been assigned to this build iteration, but the 
963
                                //    build standards of the package and this dependency prevent the package being
964
                                //    built in this iteration.   
965
                                canBeBuiltNow = false;
7048 dpurdie 966
                                mLogger.info("planRelease package cannot be built in this iteration {}", p.mName);
6914 dpurdie 967
                                break;
968
                            }
969
                        }
970
 
7048 dpurdie 971
                        //  All dependencies have been processed. May be able to build this package
972
                        //      Add the package to the build order list
973
                        //
6914 dpurdie 974
                        if (allDependenciesForThisPackageProcessed)
975
                        {
976
                            p.mProcessed = true;
7048 dpurdie 977
                            if ( canBeBuiltNow )
6914 dpurdie 978
                            {
979
                                mBuildOrder.add(p);
7048 dpurdie 980
                                p.mBuildFile = buildFile;
981
                                mLogger.info("planRelease set mBuildFile to {} for package {}",buildFile, p.mAlias );
6914 dpurdie 982
                            }
983
                        }
984
                    }
985
                }
986
            } while( !allDependenciesProcessed );
7048 dpurdie 987
 
988
            //  Have process all the packages that we can at the moment
989
            //      Examine all the packages in the collection to see if there are more that need to be extracted
990
            //      These will be done at a different build-level
991
            //  
992
            allProcessed = true;
993
            for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
994
            {
995
                Package p = it.next();
996
                if ( p.mBuildFile == 0 )
997
                {
998
                    // more build files are required
999
                    allProcessed = false;
1000
                    mLogger.info("planRelease more build files are required for {}", p.mName);
1001
                    break;
1002
                }
1003
            }
6914 dpurdie 1004
 
7048 dpurdie 1005
            buildFile++;
1006
            if (buildFile % 500 == 0 ) {
1007
                mLogger.error("planRelease. Too many build levels {}", buildFile);
1008
            }
1009
        } while( !allProcessed );
1010
 
1011
 
1012
        if ( mDaemon )
1013
        {
1014
 
6914 dpurdie 1015
            //
1016
            //  Daemon Mode:
7048 dpurdie 1017
            //  Locate all packages that we can build now
1018
            //      Packages that have a build requirement and have no reason we cannot build them
6914 dpurdie 1019
            //
7048 dpurdie 1020
            ArrayList<Package> buildCandidates = new ArrayList<Package>();
1021
 
1022
 
1023
            for (Iterator<Package> it = mBuildOrder.iterator(); it.hasNext(); )
6914 dpurdie 1024
            {
7048 dpurdie 1025
                Package p = it.next();
1026
                if ( p.mProcessed && p.mBuildFile > 0 )
6914 dpurdie 1027
                {
7048 dpurdie 1028
                    if (p.mBuildReason != null)
1029
                    {
1030
                        // Have a reason to build this package
1031
                        buildCandidates.add(p);
1032
                        mLogger.info("planRelease Candidate {}",p.mAlias);
1033
                    }
1034
                }
1035
            }
1036
 
1037
            //  Add in the WIPs, Tests and Ripple Requests so that we can process them as one collections
1038
            //  Now have a collection of packages that we would like to build right now
1039
            //  See if any of them can be excluded due to issues with creating a new package version
1040
            buildCandidates.addAll(mPackageCollectionWip);
1041
            buildCandidates.addAll(mPackageCollectionTest);
1042
            buildCandidates.addAll(mPackageCollectionRipple);
1043
 
1044
            for (Iterator<Package> it = buildCandidates.iterator(); it.hasNext(); )
1045
            {
1046
                Package p = it.next();
6914 dpurdie 1047
 
7048 dpurdie 1048
                if ( p.mBuildFile >= 0 )
1049
                {
7050 dpurdie 1050
                    int pvApplied = p.applyPV(mReleaseManager);
7048 dpurdie 1051
 
1052
                    if ( pvApplied == 1 )
6914 dpurdie 1053
                    {
7048 dpurdie 1054
                        // max 50 chars
1055
                        rippleBuildExclude(p, p.mId, "Package has non standard versioning", null, null, true);
1056
                    }
1057
                    else if ( pvApplied == 2 )
1058
                    {
1059
                        // max 50 chars
1060
                        rippleBuildExclude(p, p.mId, "Package has reached ripple field limitations", null, null, true);
1061
                    }
1062
                    else if ( pvApplied == 3 )
1063
                    {
1064
                        // max 50 chars
1065
                        rippleBuildExclude(p, p.mId, "Package has invalid change type", null, null, true);
1066
                    }
1067
 
1068
                    if ( pvApplied != 0) 
1069
                    {
1070
                        // If this package is not a WIP/TEST/RIPPLE, then remove it from this collection
7051 dpurdie 1071
                        // Want to keep all WIP/TEST/RIPPLE items to simplify reporting
7048 dpurdie 1072
                        if (! p.mIsNotReleased )
6914 dpurdie 1073
                        {
7048 dpurdie 1074
                            it.remove();
6914 dpurdie 1075
                        }
7048 dpurdie 1076
                    }
1077
 
1078
                    //
1079
                    //  Ensure that the mRippleTime is known for each package
1080
                    //  Collection includes: naturalRipples, ForcedRipples, TestBuilds and NewVersions
1081
                    if ( pvApplied == 0)
1082
                    {
1083
                        if (p.mTestBuildInstruction != 0)
6914 dpurdie 1084
                        {
7048 dpurdie 1085
                            // Test Build - there is no ripple effect
1086
                            // The time impact will be the estimated build time of this one package
1087
                            p.mRippleTime = p.mBuildTime;
1088
 
6914 dpurdie 1089
                        }
7048 dpurdie 1090
                        else 
1091
                        {
1092
                            //  Calculate the time to build the package AND all of the packages that depend on it
1093
                            //  Calculate the build plan for the package
1094
                            //
1095
                            calcBuildPlan(p);
1096
                        }
6914 dpurdie 1097
                    }
1098
                }
1099
            }
7048 dpurdie 1100
 
1101
            //
1102
            //  Determine the NON-test package the we will build next
1103
            //
1104
            Package build = ReleaseManager.NULL_PACKAGE; 
1105
 
1106
            for (Iterator<Package> it = buildCandidates.iterator(); it.hasNext(); )
6914 dpurdie 1107
            {
7048 dpurdie 1108
 
1109
                Package p = it.next();
1110
                if ( p.mBuildFile >= 0 )
6914 dpurdie 1111
                {
7048 dpurdie 1112
                    if (p.mTestBuildInstruction == 0)
6914 dpurdie 1113
                    {
7048 dpurdie 1114
                        //  Not a Test Build
1115
                        if (build == ReleaseManager.NULL_PACKAGE || p.mRippleTime < build.mRippleTime)
1116
                        {
1117
                            build = p;
1118
                        }
6914 dpurdie 1119
                    }
1120
                }
7048 dpurdie 1121
            }
6914 dpurdie 1122
 
7048 dpurdie 1123
            //  Determine the first test build package
1124
            //  Assume the collection is in build instruction id order, 
1125
            //      So that the first request will be built first
1126
            //  Add all buildable test-builds to the mBuildOrder collection 
1127
            Package testBuild = ReleaseManager.NULL_PACKAGE;
1128
            mBuildOrder.clear();
6914 dpurdie 1129
 
7048 dpurdie 1130
            for (Iterator<Package> it = buildCandidates.iterator(); it.hasNext(); )
1131
            {
1132
 
1133
                Package p = it.next();
1134
                if ( p.mBuildFile >= 0 )
6914 dpurdie 1135
                {
7048 dpurdie 1136
                    if (p.mTestBuildInstruction != 0)
6914 dpurdie 1137
                    {
7048 dpurdie 1138
                        //  Not a Test Build
1139
                        if (testBuild == ReleaseManager.NULL_PACKAGE)
1140
                        {
7051 dpurdie 1141
                            testBuild = p;
7048 dpurdie 1142
                        }
1143
                        mBuildOrder.add(p);
6914 dpurdie 1144
                    }
1145
                }
1146
            }
7048 dpurdie 1147
 
1148
            //
1149
            //  Extend mBuildOrder with the selected non-test build package and its build plan
1150
            //  Really only for display purposes
1151
            //
1152
            if (build != ReleaseManager.NULL_PACKAGE)
6914 dpurdie 1153
            {
7048 dpurdie 1154
                mBuildOrder.add(build);
1155
                mBuildOrder.addAll(build.mRipplePlan);
1156
            }
1157
 
1158
            //
1159
            //  Mark the first package in the build order as the one to be built
7051 dpurdie 1160
            //  This will give test builds priority, while indicating what will be built after that
7048 dpurdie 1161
            //
1162
 
1163
            if (!mBuildOrder.isEmpty()) {
1164
                build = mBuildOrder.get(0);
1165
                build.mBuildFile = 1;
1166
 
1167
                if ( build.mForcedRippleInstruction > 0 )
6914 dpurdie 1168
                {
7048 dpurdie 1169
                    mReleaseManager.markDaemonInstCompleted( build.mForcedRippleInstruction );
1170
                }
6914 dpurdie 1171
 
7048 dpurdie 1172
                if ( build.mTestBuildInstruction > 0 )
1173
                {
1174
                    mReleaseManager.markDaemonInstInProgress( build.mTestBuildInstruction );
1175
                }
1176
 
1177
                //  Now that we know which package we are building
1178
                //      Set the previously calculated nextVersion as the packages version number
1179
                //      Claim the version number to prevent other builds from using it. Unless doing a test build
1180
                //
1181
                if (build.mTestBuildInstruction == 0 && build.mNextVersion != null)
1182
                {
1183
                    mReleaseManager.claimVersion(build.mPid, build.mNextVersion + build.mExtension, mBaseline);
1184
                    build.mVersion = build.mNextVersion;
1185
                }
1186
            }
1187
 
1188
            //
1189
            //  To fit in with the old algorithm ( ie: could be improved )
1190
            //  Insert marks into all packages
1191
            //  Not sure exactly why - Its used in the generation of the ant build file
1192
            //  Want to set mNoBuildReason, mBuildFile
1193
            //
1194
            //      Package we have selected to build: 0     , 1
1195
            //      Package we could have built      : 2     , 3
1196
            //      Packages we can't build          : reason, 3
1197
            //      Packages that are OK             : 3     , 3
1198
 
1199
            for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
1200
            {
1201
                Package p = it.next();
1202
                if (p == build ) {
1203
                    p.mNoBuildReason = 0;
1204
                    p.mBuildFile = 1;
1205
                } 
1206
                else if ( p.mBuildFile < 0 )
1207
                {
1208
                    p.mNoBuildReason = p.mBuildFile;
1209
                    p.mBuildFile = 3;
1210
                }
1211
                else if (p.mBuildReason != null)
1212
                {
1213
                    p.mNoBuildReason = 2;
1214
                    p.mBuildFile = 3;
1215
                }
1216
                else
1217
                {
1218
                    p.mNoBuildReason = 0;
1219
                    p.mBuildFile = 3;
1220
                }
1221
            }
1222
        }
1223
 
1224
    }
1225
 
1226
    /**
1227
     * Calculate a build plan for the specified package
1228
     * Calculates : mRippleTime - time to build the package and the ripple effect
1229
     * Calculates : mBuildPlan - collection of packages in the plan that can be built at the moment
1230
     * 
1231
     * Needs to determine all packages that consume the package - and all packages that consume them
1232
     *  Note: The package may not be in the mPackageCollection ( it may be a WIP/TEST/RIPPLE )
1233
     *  
1234
     * Need to:
1235
     *      Only include a package once.
1236
     *      Only include packages that can/need to be built at the moment
1237
     * 
1238
     * @param p     - Package to process
1239
     */
1240
 
1241
    private void calcBuildPlan(Package p) {
1242
 
1243
        p.mRipplePlan = usedByPackages(p);
1244
 
1245
        //  Use a ListIterator so that we can add elements to the end of the list while processing
1246
        for (ListIterator<Package> it = p.mRipplePlan.listIterator(); it.hasNext(); )
1247
        {
1248
            Package pkg = it.next();
1249
            ArrayList<Package> usedBy = usedByPackages(pkg);
1250
            for (Iterator<Package> it1 = usedBy.iterator(); it1.hasNext(); )
1251
            {
1252
                Package uPkg = it1.next();
1253
                if ( ! p.mRipplePlan.contains( uPkg ))
1254
                {
1255
                    it.add(uPkg);
1256
                }
1257
            }
1258
        }
1259
 
1260
        //
1261
        //  Now have a plan on what will be affected when we build the package
1262
        //  Calculate the time it will take - include my time
1263
        //
1264
        p.mRippleTime = p.mBuildTime;
1265
        for (ListIterator<Package> it = p.mRipplePlan.listIterator(); it.hasNext(); )
1266
        {
1267
            Package pkg = it.next();
1268
            p.mRippleTime += pkg.mBuildTime;
1269
        }
1270
    }
1271
 
1272
    /** 
1273
     *  Calculate a collection of packages that actively use the named package
1274
     *  A consumer package does NOT use the named package,
1275
     *      If the consumer is an SDK or is Pegged
1276
     *      If the consumer is marked as advisoryRipple
1277
     *      If the consumer cannot be built
1278
     *   
1279
     * @param pkg - Package to process
1280
     * @return A collection of packages that actively 'use' the specified package
1281
 
1282
     */
1283
    private ArrayList<Package> usedByPackages(Package pkg) {
1284
 
1285
        ArrayList<Package> usedBy = new ArrayList<Package>();
1286
 
1287
        for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
1288
        {
1289
            Package p = it.next();
1290
 
1291
            //  Is this package 'actively used' in the current build
1292
            if (p.mBuildFile >= 0)
1293
            {
1294
                for (Iterator<String> it2 = p.mDependencyCollection.iterator(); it2.hasNext(); )
1295
                {
1296
                    String alias = it2.next();
1297
                    if ( alias == pkg.mAlias) {
1298
                        //  Have found a consumer of 'pkg'
1299
                        usedBy.add(p);
6914 dpurdie 1300
                        break;
1301
                    }
1302
                }
1303
            }
7048 dpurdie 1304
        }
1305
 
1306
        return usedBy;
6914 dpurdie 1307
    }
1308
 
1309
    /** Determine if a given PVID is a member of the current release.
1310
     *  Used to determine if a package dependency is out of date
1311
     * 
1312
     * @param dpvId
1313
     * @return true - specified pvid is a full member of the Release
1314
     */
1315
    private boolean isInRelease(Integer dpvId) {
1316
 
7048 dpurdie 1317
        boolean inRelease = false;
6914 dpurdie 1318
 
7048 dpurdie 1319
        for ( Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
6914 dpurdie 1320
        {
7048 dpurdie 1321
            Package p = it.next();
6914 dpurdie 1322
 
7048 dpurdie 1323
            if ( p.mId == dpvId )
6914 dpurdie 1324
            {
7048 dpurdie 1325
                inRelease = ! p.mIsNotReleased;
6914 dpurdie 1326
                break;
1327
            }
1328
        }
7048 dpurdie 1329
        return inRelease;
6914 dpurdie 1330
    }
1331
 
1332
 
1333
    /**reports what change in build exceptions happens as part of planRelease
1334
     */
1335
    public void reportChange() throws SQLException, Exception
1336
    {
1337
        int counter = 0;
1338
        for (Iterator<BuildExclusion> it = mBuildExclusionCollection.iterator(); it.hasNext(); )
1339
        {
1340
            BuildExclusion buildExclusion = it.next();
1341
 
1342
            if ( !buildExclusion.isProcessed() )
1343
            {
7032 dpurdie 1344
                if (buildExclusion.isImported() && ! buildExclusion.isARootCause() ) {
1345
                    // Remove from the exclusion list
1346
                    buildExclusion.includeToBuild(mReleaseManager, mBaseline);
7048 dpurdie 1347
                    mLogger.error("reportChange remove unused exclusion: {}", buildExclusion.info());
7032 dpurdie 1348
                } else {
1349
                    // Exclude and notify
1350
                    buildExclusion.excludeFromBuild(mReleaseManager, mBaseline);
1351
                    buildExclusion.email(this, mPackageCollection);
1352
                    counter++;
1353
                }
6914 dpurdie 1354
            }
1355
        }
7048 dpurdie 1356
        mLogger.error("reportChange exclusion count: {}", counter);
6914 dpurdie 1357
    }
1358
 
1359
    /**reports the build plan
1360
     */
1361
    public void reportPlan() throws SQLException, Exception
1362
    {
1363
        mReleaseManager.reportPlan(mRtagId, mBuildOrder);
1364
    }
1365
 
1366
    /**Returns the first build file from the collection
1367
     * The build file will be flagged as empty if none exists
1368
     */
1369
    public BuildFile getFirstBuildFileContent()
1370
    {
1371
        mLogger.debug("getFirstBuildFileContent");
1372
 
1373
        mBuildIndex = -1;
1374
        return getNextBuildFileContent();
1375
    }
1376
 
1377
    /**Returns next build file from the collection
1378
     * The build file will be flagged as empty if none exists
1379
     */
1380
    public BuildFile getNextBuildFileContent()
1381
    {
1382
        mLogger.debug("getNextBuildFileContent");
1383
        BuildFile retVal = null;
1384
 
1385
        try
1386
        {
1387
            mBuildIndex++;
1388
            retVal = mBuildCollection.get( mBuildIndex );
1389
        }
1390
        catch( IndexOutOfBoundsException e )
1391
        {
1392
            // Ignore exception. retVal is still null.
1393
        }
1394
 
1395
        if (retVal == null)
1396
        {
1397
            retVal = new BuildFile();
1398
        }
1399
 
7048 dpurdie 1400
        mLogger.debug("getNextBuildFileContent returned {}", retVal.state.name() );
6914 dpurdie 1401
        return retVal;
1402
    }
1403
 
1404
 
1405
    /**collects meta data associated with the baseline
1406
     * this is sufficient to send an indefinite pause email notification 
1407
     *  
1408
     * Escrow: Used once to collect information about the SBOM and associated Release 
1409
     *         mBaseline is an SBOMID 
1410
     * Daemon: Used each build cycle to refresh the information 
1411
     *          mBaseline is an RTAGID
1412
     */
1413
    public void collectMetaData() throws SQLException, Exception
1414
    {
1415
        Phase phase = new Phase("cmd");
7048 dpurdie 1416
        mLogger.debug("collectMetaData mDaemon {}", mDaemon);
6914 dpurdie 1417
 
1418
        try
1419
        {
1420
            phase.setPhase("connect");
1421
            mReleaseManager.connect();
1422
 
1423
            if (! mDaemon)
1424
            {
1425
                mSbomId = mBaseline;
1426
                phase.setPhase("queryRtagIdForSbom");
1427
                mRtagId = mReleaseManager.queryRtagIdForSbom(mBaseline);
1428
                if (mRtagId == 0)
1429
                {
7033 dpurdie 1430
                    mLogger.error("SBOM does not have a matching Release. Cannot be used as a base for an Escrow"); 
6914 dpurdie 1431
                    throw new Exception("rtagIdForSbom show stopper. SBOM does not have an associated Release");
1432
                }
1433
            }
1434
 
1435
            phase.setPhase("queryReleaseConfig");
1436
            mReleaseManager.queryReleaseConfig(mRtagId);
1437
 
1438
            if (mDaemon)
1439
            {
1440
                phase.setPhase("queryMailServer");
1441
                setMailServer(mReleaseManager.queryMailServer());
1442
                phase.setPhase("queryMailSender");
1443
                setMailSender(mReleaseManager.queryMailSender());
1444
                phase.setPhase("queryGlobalAddresses");
1445
                setMailGlobalTarget(mReleaseManager.queryGlobalAddresses());
1446
                phase.setPhase("queryProjectEmail");
1447
                mMailGlobalCollection = mReleaseManager.queryProjectEmail(mBaseline);
1448
                phase.setPhase("mMailGlobalTarget");
1449
                mMailGlobalCollection.add(0,getMailGlobalTarget());
1450
            }
1451
            phase.setPhase("queryBaselineName");
1452
            mBaselineName = mReleaseManager.queryBaselineName(mBaseline);
1453
            phase.setPhase("Done");
1454
        }
1455
        finally
1456
        {
1457
            // this block is executed regardless of what happens in the try block
1458
            // even if an exception is thrown
1459
            // ensure disconnect
1460
            mReleaseManager.disconnect();
1461
        }
1462
    }
1463
 
1464
    /**
1465
     * Find Package by package alias
1466
     * Searches the complete package collection
1467
     * @param   alias               - alias of package to locate
1468
     * @return  Package with the matching mAlias or NULL_PACKAGE if no package has the mAlias
1469
     */
1470
    public Package findPackage(String alias)
1471
    {
1472
        mLogger.debug("findPackage");
1473
 
1474
        Package retVal = mReleaseManager.findPackage(alias, mPackageCollection);
1475
 
7048 dpurdie 1476
        mLogger.info("findPackage returned {}", retVal.mName);
6914 dpurdie 1477
        return retVal;
1478
    }
1479
 
1480
    /**
1481
     * Sets the mBuildFile to -5 (indirectly dependent on package versions which are not reproducible) 
7048 dpurdie 1482
     * for the package and all dependent packages that are a part of the release set.
1483
     *  
7032 dpurdie 1484
     * @param p             The package being excluded 
1485
     * @param rootPvId      The PVID of the package that is causing the exclusion. Null or -ve values are special
1486
     *                      This package is the root cause, -2: Excluded by Ripple Stop 
6914 dpurdie 1487
     *                   
7032 dpurdie 1488
     * @param rootCause     Text message. Max 50 characters imposed by RM database 
1489
     * @param list          If not null, add build exclusion to specified list, else use default(mBuildExclusionCollection) list. Allows insertions while iterating the list.
1490
     * @param be            If not null, process provided BuildExclusion element, otherwise find-existing/create a BuildExclusion element
1491
     * @param dependentToo  True: Exclude all packages that depend on the excluded package 
6914 dpurdie 1492
     */
1493
    private void rippleBuildExclude(Package p, int rootPvId, String rootCause, ListIterator<BuildExclusion> list, BuildExclusion be, boolean dependentToo )
1494
    {
1495
        mLogger.debug("rippleBuildExclude");
7048 dpurdie 1496
 
6914 dpurdie 1497
        if ( p.mBuildFile == 0 || p.mBuildFile == 1 )
1498
        {
1499
            p.mBuildFile = -5;
7048 dpurdie 1500
            mLogger.info("rippleBuildExclude set mBuildFile to -5 for package {}", p.mAlias );
6914 dpurdie 1501
 
1502
            if ( be != null )
1503
            {
7032 dpurdie 1504
                be.setProcessed();
6914 dpurdie 1505
            }
1506
            else
1507
            {
1508
                //  If no be item has been provided, then scan the complete collection looking for a matching item
1509
                //  If found, process it, else add it (unprocessed)
1510
                boolean found = false;
1511
                for (Iterator<BuildExclusion> it = mBuildExclusionCollection.iterator(); it.hasNext(); )
1512
                {
1513
                    BuildExclusion buildExclusion = it.next();
1514
 
1515
                    if ( buildExclusion.compare(p.mId, rootPvId, rootCause))
1516
                    {
7032 dpurdie 1517
                        buildExclusion.setProcessed();
6914 dpurdie 1518
                        found = true;
1519
                        break;
1520
                    }
1521
                }
1522
 
1523
                if (!found)
1524
                {
1525
                    // Entry not found in the mBuildExclusionCollection
1526
                    // Mark all occurrences for this package as processed
1527
                    // These will be superseded by a new build exclusion entry
1528
                    for (Iterator<BuildExclusion> it = mBuildExclusionCollection.iterator(); it.hasNext(); )
1529
                    {
1530
                        BuildExclusion buildExclusion = it.next();
1531
 
1532
                        if ( buildExclusion.compare(p.mId))
1533
                        {
7032 dpurdie 1534
                            buildExclusion.setProcessed();
6914 dpurdie 1535
                        }
1536
                    }
1537
 
1538
                    BuildExclusion buildExclusion = new BuildExclusion(p.mId, rootPvId, rootCause, p.mTestBuildInstruction);
1539
 
1540
                    //
1541
                    //  Add the new build exclusion to a list
1542
                    //    Either the default list or a user-specified list
1543
                    if ( list == null )
1544
                    {
1545
                        mBuildExclusionCollection.add(buildExclusion);
1546
                    }
1547
                    else
1548
                    {
1549
                        // must use the ListIterator interface to add to the collection whilst iterating through it
1550
                        list.add(buildExclusion);
1551
                    }
1552
                }
1553
            }
1554
 
1555
            //  Locate ALL packages that depend on this package and exclude them too
1556
            //  This process will be recursive
1557
            //    Not sure that this it is efficient 
7048 dpurdie 1558
            //  Only ripple a build exclusion through for packages that are a part of the full release set
1559
            //  NewWIPs, ForcedRipples and TestBuilds will not force consumer packages to be excluded
6914 dpurdie 1560
            //
7048 dpurdie 1561
            if ( ! p.mIsNotReleased && dependentToo)
6914 dpurdie 1562
            {
7048 dpurdie 1563
                for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
6914 dpurdie 1564
                {
1565
                    Package pkg = it.next();
1566
 
1567
                    if ( pkg != p )
1568
                    {
1569
                        for (Iterator<Package> it2 = pkg.mPackageDependencyCollection.iterator(); it2.hasNext(); )
1570
                        {
1571
                            Package dependency = it2.next();
1572
 
1573
                            if ( dependency == p )
1574
                            {
1575
                                rippleBuildExclude( pkg, rootPvId, null, list, null, dependentToo );
1576
                                break;
1577
                            }
1578
                        }
1579
                    }
1580
                }
1581
            }
1582
        }
7048 dpurdie 1583
        mLogger.info("rippleBuildExclude set {} {}", p.mName, p.mBuildFile);
6914 dpurdie 1584
    }
1585
 
1586
    /**Simple XML string escaping
1587
     * 
1588
     * @param xml		- String to escape
1589
     * @return		- A copy of the string with XML-unfriendly characters escaped 
1590
     */
1591
    public static String escapeXml( String xml )
1592
    {
1593
        xml = xml.replaceAll("&", "&amp;");
1594
        xml = xml.replaceAll("<", "&lt;");
1595
        xml = xml.replaceAll(">", "&gt;");
1596
        xml = xml.replaceAll("\"","&quot;");
1597
        xml = xml.replaceAll("'", "&apos;");
1598
        xml = xml.replaceAll("\\$", "\\$\\$");
1599
 
1600
        return xml;
1601
    }
1602
 
1603
    /** Quote a string or a string pair
1604
     *  If two strings are provided, then they will be joined with a comma.
1605
     *   
1606
     * @param text		- First string to quote
1607
     * @param text2		- Optional, second string
1608
     * @return A string of the form 'text','text2'
1609
     */
1610
    public static String quoteString(String text, String text2)
1611
    {
1612
        String result;
1613
        result =  "\'" + text + "\'";
1614
        if (text2 != null )
1615
        {
1616
            result +=  ",\'" + text2 + "\'";  
1617
        }
1618
        return result;
1619
    }
1620
 
1621
    /** Generate build file information
1622
     * 
1623
     */
1624
    private void generateBuildFiles() 
1625
    {
1626
 
1627
        // persist the build files
1628
        boolean allProcessed = false;
1629
        int buildFile = 1;
1630
        StringBuilder rawData = new StringBuilder();
1631
        StringBuilder setUp = new StringBuilder();
1632
 
1633
        mLogger.debug("generateBuildFiles");
1634
 
1635
        if ( mDaemon )
1636
        {
1637
            // all interesting packages in daemon mode match the following filter
1638
            buildFile = 3;
1639
        }
1640
 
1641
        //-----------------------------------------------------------------------
1642
        //    Generate the build file
1643
        do
1644
        {
1645
            BuildFile buildEntry = new  BuildFile();
1646
            buildEntry.state = BuildFileState.Dummy;
1647
            XmlBuilder xml = generateBuildFileHeader();
1648
 
1649
 
1650
            //	Generate properties for each package in this build level or lower build levels
1651
            //	The properties link the packageAlias to the PackageName and PackageVersion
1652
            //
1653
            //	[DEVI 54816] In escrow mode all unreproducible packages are included 
7048 dpurdie 1654
            for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
6914 dpurdie 1655
            {
1656
                Package p = it.next();
1657
 
1658
                if ( ( ( p.mBuildFile > 0 ) && ( p.mBuildFile <= buildFile ) ) || ( !mDaemon && p.mBuildFile == -2 ) )
1659
                {
1660
                    xml.addProperty(p.mAlias, p.mName + " " + p.mVersion + p.mExtension);
1661
                }
1662
            }
1663
 
1664
            //	UTF Support
1665
            //	Insert additional info into the build file to provide extra checking
1666
            //
7048 dpurdie 1667
            if ( ! mReleaseManager.mUseDatabase )
6914 dpurdie 1668
            {
1669
                // UTF Support
1670
                // Insert per-package planning information
1671
                //
7048 dpurdie 1672
                xml.addComment("mPackageCollectionAll");
1673
                for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
6914 dpurdie 1674
                {
1675
                    Package p = it.next();
1676
                    generatePackageInfo(xml, p);
1677
                }
1678
 
1679
                // UTF Support
1680
                // Insert build exclusion information
1681
                xml.addComment("mBuildExclusionCollection");
1682
                for (Iterator<BuildExclusion> it = mBuildExclusionCollection.iterator(); it.hasNext(); )
1683
                {
1684
                    BuildExclusion buildExclusion = it.next();
1685
                    if (!buildExclusion.isProcessed())
1686
                    {
1687
                        xml.addComment(buildExclusion.info());
1688
                    }
1689
                }
1690
 
1691
                // UTF Support
1692
                // Insert build Plan
1693
                if (mDaemon)
1694
                {
1695
                    xml.addComment("mBuildOrder");
1696
                    int order = 0;
1697
                    for (Iterator<Package> it = mBuildOrder.iterator(); it.hasNext(); )
1698
                    {
1699
                        Package p = it.next();
1700
                        String comment =
1701
                                "pvid="+ p.mId +
7048 dpurdie 1702
                                " order=" + (++order) +
6914 dpurdie 1703
                                " name=\"" + p.mAlias + "\"";
1704
                        xml.addComment(comment);
1705
                    }
1706
                }
1707
            }
1708
 
1709
            //  Generate Taskdef information
1710
            generateTaskdef(xml);
1711
 
1712
            //
1713
            //  Insert known Machine Information
1714
            //  Escrow usage: 
1715
            //      Map machType to machClass
1716
            //  Also serves as a snapshot of the required build configuration
1717
            //  ie: machine types and buildfilters
1718
            //
1719
            if (!mDaemon)
1720
            {
1721
                generateMachineInfo(xml, null);
1722
            }
1723
 
1724
            //
1725
            //	Generate target rules for each package to be built within the current build file
1726
            //
1727
            boolean daemonHasTarget = false;
1728
 
7048 dpurdie 1729
            for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
6914 dpurdie 1730
            {
1731
                Package p = it.next();
1732
 
1733
                if ( p.mBuildFile > 0 && p.mBuildFile <= buildFile )
1734
                {
1735
                    generateTarget(xml, buildEntry, p, buildFile);
1736
 
1737
                    if ( p.mBuildFile == 1 )
1738
                    {
1739
                        daemonHasTarget = true;
1740
 
1741
                        // Retain information about the target package
1742
                        buildEntry.mPkgId = p.mPid;
1743
                        buildEntry.mPvId = p.mId;
1744
                    }
1745
                }
1746
 
1747
                //	Generate escrow extraction commands
1748
                //	Only done on the first pass though the packages
1749
                //
1750
                if ( !mDaemon && buildFile == 1 )
1751
                {
1752
                    setUp.append("jats jats_vcsrelease -extractfiles"
1753
                                + " \"-label=" + p.mVcsTag + "\""
1754
                                + " \"-view=" + p.mAlias + "\""
1755
                                + " -root=. -noprefix"
1756
                                + mlf );
1757
                }
1758
 
1759
                //	Generate escrow raw CSV data
1760
                //	Note: I don't think this data is used at all
1761
 
1762
                if ( !mDaemon && ( p.mBuildFile == buildFile))
1763
                {
1764
                    StringAppender machines = new StringAppender(",");
1765
                    for (Iterator<BuildStandard> it1 = p.mBuildStandardCollection.iterator(); it1.hasNext();)
1766
                    {
1767
                        BuildStandard bs = it1.next();
1768
                        machines.append(bs.mMachClass);
1769
                    }
1770
 
1771
                    rawData.append(p.mAlias + "," +
1772
                                    buildFile + "," +
1773
                                    machines +
1774
                                    mlf);
1775
                }
1776
            }
1777
 
1778
            if ( mDaemon && !daemonHasTarget )
1779
            {
1780
                // must have AbtTestPath, AbtSetUp, AbtTearDown, and AbtPublish targets
1781
                XmlBuilder target = xml.addNewElement("target");
1782
                target.addAttribute("name", "AbtTestPath");
1783
 
1784
                target = xml.addNewElement("target");
1785
                target.addAttribute("name", "AbtSetUp");
1786
 
1787
                target = xml.addNewElement("target");
1788
                target.addAttribute("name", "AbtTearDown");
1789
 
1790
                target = xml.addNewElement("target");
1791
                target.addAttribute("name", "AbtPublish");
1792
            }
1793
 
1794
            generateDefaultTarget( xml, buildFile);
1795
 
1796
            //	Convert the Xml structure into text and save it in the build file
1797
            //	Add this build file to the mBuildCollection
1798
            buildEntry.content = mXmlHeader + xml.toString();
1799
            mBuildCollection.add(buildEntry);
1800
 
1801
            // are more build files required
1802
            allProcessed = true;
1803
 
1804
            if (!mDaemon)
1805
            {
1806
                // this is escrow mode centric
7048 dpurdie 1807
                for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
6914 dpurdie 1808
                {
1809
                    Package p = it.next();
1810
 
1811
                    if ( p.mBuildFile > buildFile )
1812
                    {
1813
                        // more build files are required
1814
                        allProcessed = false;
7048 dpurdie 1815
                        mLogger.info("planRelease reiterating package has no build requirement {} {} {}",p.mName, p.mBuildFile,  buildFile);
6914 dpurdie 1816
                        break;
1817
                    }
1818
                } 
1819
 
1820
                buildFile++;
1821
            }
1822
 
1823
        } while( !allProcessed );
1824
 
1825
        //	Save additional escrow data
1826
        if ( !mDaemon )
1827
        {
1828
            mEscrowSetup = setUp.toString();
1829
            mEscrowRawData = rawData.toString();
1830
        }
1831
    }
1832
 
1833
    /**returns a build file header for the mBaseline
1834
     */
1835
    private XmlBuilder generateBuildFileHeader()
1836
    {
1837
        mLogger.debug("generateBuildFileHeader");
1838
        XmlBuilder element = new XmlBuilder("project");
1839
 
1840
        element.addAttribute("name", "mass");
1841
        element.addAttribute("default", "full");
1842
        element.addAttribute("basedir", ".");
1843
 
1844
        if ( mDaemon )
1845
        {
1846
            element.addProperty("abt_mail_server", getMailServer());
1847
            element.addProperty("abt_mail_sender", getMailSender()); 
1848
            element.addProperty("abt_rtag_id", mBaseline);
1849
            element.addProperty("abt_daemon", mReleaseManager.currentTimeMillis());
1850
            element.makePropertyTag("abt_packagetarball", true);
1851
            element.makePropertyTag("abt_usetestarchive", !ReleaseManager.getUseMutex());
1852
 
7048 dpurdie 1853
            for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
6914 dpurdie 1854
            {
1855
                Package p = it.next();
1856
 
1857
                if ( p.mBuildFile == 1 )
1858
                {
1859
                    element.addProperty("abt_package_name", p.mName);
1860
                    element.addProperty("abt_package_version", p.mVersion + p.mExtension);
1861
                    element.addProperty("abt_package_extension", p.mExtension);
1862
                    element.addProperty("abt_package_location", getBuildLocation(p));
1863
                    element.addProperty("abt_package_ownerlist", p.emailInfoNonAntTask(this));
1864
                    element.addProperty("abt_package_build_info", buildInfoText(p));
1865
 
1866
                    // depends in the form 'cs','25.1.0000.cr';'Dinkumware_STL','1.0.0.cots'
1867
                    StringAppender depends = new StringAppender(";");
1868
 
1869
                    for (Iterator<Package> it3 = p.mPackageDependencyCollection.iterator(); it3.hasNext(); )
1870
                    {
1871
                        Package depend = it3.next();
1872
                        depends.append( quoteString(depend.mName, depend.mVersion + depend.mExtension) );
1873
                    }
1874
 
1875
                    element.addProperty("abt_package_depends", depends.toString());
1876
                    element.addProperty("abt_is_ripple", p.mDirectlyPlanned ? "0" : "1");
1877
                    element.addProperty("abt_build_reason", p.mBuildReason.toString());
1878
                    element.addProperty("abt_package_version_id", p.mId);
1879
                    element.addProperty("abt_does_not_require_source_control_interaction", ! p.mRequiresSourceControlInteraction ? "true" : "false" );
1880
                    element.addProperty("abt_test_build_instruction", p.mTestBuildInstruction);
1881
                }
1882
            }
1883
        }
1884
        else
1885
        {
1886
            //    Escrow Mode
1887
            element.addProperty("abt_rtag_id", mRtagId);
1888
            element.addProperty("abt_sbom_id", mSbomId);
1889
        }
1890
 
1891
        element.addProperty("abt_release", escapeXml(mBaselineName));
1892
        element.addProperty("abt_buildtool_version", mReleaseManager.getMajorVersionNumber() );
1893
 
1894
        element.addNewElement("condition")
1895
        .addAttribute("property", "abt_family")
1896
        .addAttribute("value", "windows")
1897
        .addNewElement("os")
1898
        .addAttribute("family", "windows");
1899
        element.addProperty("abt_family", "unix");
1900
 
1901
        return element;
1902
    }
1903
 
1904
    /** Add task def XML items taskdef for the abt ant task
1905
     * @param xml 	- xml item to extend
1906
     */
1907
    private void generateTaskdef(XmlBuilder xml)
1908
    {
1909
        mLogger.debug("generateTaskdef");
1910
        xml.addNewElement("taskdef")
1911
        .addAttribute("name", "abt")
1912
        .addAttribute("classname", "com.erggroup.buildtool.abt.ABT");
1913
 
1914
        xml.addNewElement("taskdef")
1915
        .addAttribute("name", "abtdata")
1916
        .addAttribute("classname", "com.erggroup.buildtool.abt.ABTData");
1917
    }
1918
 
1919
    /** returns the command abtdata items
1920
     *  <br>Common machine information
1921
     *  <br>Common email address
1922
     *  
1923
     *  
1924
     *  @param	xml - Xml element to extend   
1925
     *  @param    p - Package (May be null)
1926
     */
1927
    private void generateMachineInfo(XmlBuilder xml, Package p)
1928
    {
1929
        XmlBuilder element = xml.addNewElement("abtdata");
1930
        element.addAttribute("id", "global-abt-data");
1931
 
1932
        //
1933
        //  Iterate over all the machines and create a nice entry
1934
        //
1935
        for (Iterator<ReleaseConfig> it = mReleaseManager.mReleaseConfigCollection.mReleaseConfig.iterator(); it.hasNext(); )
1936
        {
1937
            ReleaseConfig rc = it.next();
1938
            element.addElement(rc.getMachineEntry());
1939
        }
1940
 
1941
        //
1942
        //  Now the email information
1943
        //
1944
        if ( p != null)
1945
        {
1946
            p.emailInfo(this, element);
1947
        }
1948
    }
1949
 
1950
    /** Generate package information
1951
     *  Only when running unit tests
1952
     *  
1953
     * @param xml	- An xml element to append data to
1954
     * @param p	- Package to process
1955
     */
1956
    private void generatePackageInfo (XmlBuilder xml, Package p)
1957
    {
7051 dpurdie 1958
        String joiner = "";
1959
 
6914 dpurdie 1960
        String comment =
1961
                "pvid="+ p.mId + 
1962
                " name=\"" + p.mAlias + "\""+
1963
                " reason=" + p.mNoBuildReason +" "+ 
1964
                " buildFile=" + p.mBuildFile + " "+
1965
                " directlyPlanned=" + p.mDirectlyPlanned + " "+
7051 dpurdie 1966
                " indirectlyPlanned=" + p.mIndirectlyPlanned + " depends=[" ;
1967
 
1968
        for (Iterator<String> it2 = p.mDependencyCollection.iterator(); it2.hasNext(); )
1969
        {
1970
            String alias = it2.next();
1971
            comment += joiner + alias;
1972
            joiner = ",";
1973
        }
1974
 
1975
        comment += "]";
6914 dpurdie 1976
 
1977
        xml.addComment(comment);            
1978
    }
1979
 
1980
    /**returns an ant target for the passed Package
1981
     * in daemon mode:
1982
     *  packages are categorized with one of three mBuildFile values:
1983
     *   1 the package to be built by this buildfile
1984
     *   2 the packages with a future build requirement
1985
     *   3 the packages with no build requirement
1986
     *  the returned target depends on this categorization and will have
1987
     *   1 full abt info
1988
     *   2 full dependency info to determine future build ordering but no abt info (will not build this package)
1989
     *   3 only a name attribute (will not build this package)
1990
     * in escrow mode:
1991
     *  if the passed Package's mBuildFile is different (less than) the passed build file,
1992
     *  the returned target have only a name attribute (will not build this package)
1993
     *   
1994
     * @param xml - Xml element to extend
1995
     * @param buildEntry - Record build type (dummy/generic/not generic)
1996
     * @param p - Package to process
1997
     * @param buildFile - buildfile level being processed
1998
     */
1999
    private void generateTarget(XmlBuilder xml, BuildFile buildEntry, Package p, int buildFile)
2000
    {
2001
        mLogger.debug("generateTarget");
2002
 
2003
        //---------------------------------------------------------------------
2004
        //  Generate the AbtData - common machine and email information
2005
        //  Only used by the daemon builds
2006
        //
2007
        if ( mDaemon && p.mBuildFile == 1 )
2008
        {
2009
            generateMachineInfo(xml, p );
2010
        }
2011
 
2012
        //-------------------------------------------------------------------------
2013
        //  Generate the <target name=... /> construct
2014
        //  There are two types
2015
        //      1) Simple dummy place holder. Just has the PackageName.PackageExt
2016
        //      2) Full target. Has all information to build the package including
2017
        //              AbtSetUp, AbtTearDown and AbtPublish targets
2018
        //
2019
 
2020
        if ( !mDaemon && ( p.mBuildFile < buildFile ) )
2021
        {
2022
            XmlBuilder target = xml.addNewElement("target");
2023
            target.addAttribute("name", p.mAlias);
2024
        }
2025
        else
2026
        {
2027
            if (!mDaemon) 
2028
            {
2029
                //  Escrow Only:
2030
                //  Generate the 'wrapper' target
2031
                //  This is used to ensure that the required dependencies have been built - I think
2032
                //
2033
                StringAppender dependList = new StringAppender(",");
2034
                if ( !p.mPackageDependencyCollection.isEmpty() )
2035
                {
2036
                    for (Iterator<Package> it = p.mPackageDependencyCollection.iterator(); it.hasNext(); )
2037
                    {
2038
                        Package dependency = it.next();
2039
                        if ( !mDaemon && dependency.mBuildFile == -2 )
2040
                        {
2041
                            // ignore targets which build in foreign environments in escrow mode
2042
                            continue;
2043
                        }
2044
                        dependList.append(dependency.mAlias);
2045
                    }
2046
                }
2047
 
2048
                XmlBuilder target = xml.addNewElement("target").isExpanded();
2049
                target.addAttribute("name", p.mAlias + ".wrap");
2050
 
2051
                if (dependList.length() > 0)
2052
                {
2053
                    target.addAttribute("depends", dependList.toString() );
2054
                }
2055
 
2056
                if ( !mDaemon )
2057
                {
2058
                    boolean hasDependenciesBuiltInThisIteration = false;
2059
                    if ( ( !p.mPackageDependencyCollection.isEmpty()) )
2060
                    {
2061
                        for (Iterator<Package> it = p.mPackageDependencyCollection.iterator(); it.hasNext(); )
2062
                        {
2063
                            Package dependency = it.next();
2064
 
2065
                            if ( dependency.mBuildFile == buildFile )
2066
                            {
2067
                                hasDependenciesBuiltInThisIteration = true;
2068
                                break;
2069
                            }
2070
                        }
2071
                    }
2072
 
2073
                    if ( hasDependenciesBuiltInThisIteration )
2074
                    {
2075
                        XmlBuilder condition = target.addNewElement("condition");
2076
                        condition.addAttribute("property",  p.mAlias + ".build");
2077
 
2078
                        XmlBuilder and = condition.addNewElement("and");
2079
 
2080
                        for (Iterator<Package> it = p.mPackageDependencyCollection.iterator(); it.hasNext(); )
2081
                        {
2082
                            Package dependency = it.next();
2083
 
2084
                            if ( dependency.mBuildFile == buildFile )
2085
                            {
2086
                                XmlBuilder or = and.addNewElement("or");
2087
 
2088
                                XmlBuilder equal1 = or.addNewElement("equals");
2089
                                equal1.addAttribute("arg1", "${" + dependency.mAlias + ".res}");
2090
                                equal1.addAttribute("arg2", "0");
2091
 
2092
                                XmlBuilder equal2 = or.addNewElement("equals");
2093
                                equal2.addAttribute("arg1", "${" + dependency.mAlias + ".res}");
2094
                                equal2.addAttribute("arg2", "257");
2095
                            }
2096
                        }
2097
                    }
2098
                    else
2099
                    {
2100
                        target.addProperty(p.mAlias + ".build", "");
2101
                    }
2102
                }
2103
            }
2104
 
2105
 
2106
            //
2107
            //  Generate the 'body' of the target package
2108
            //  Escrow Mode: Always
2109
            //  Daemon Mode: Only for the one target we are building
2110
            //                  Simplifies the XML
2111
            //                  Reduces noise in the logs
2112
            //              Don't add target dependencies. 
2113
            //                  We are only building one target and the 
2114
            //                  dependency management has been done way before now.
2115
            //                  All it does is makes the logs noisy.
2116
            //
2117
            if ( ( mDaemon && p.mBuildFile == 1 ) || !mDaemon )
2118
            {
2119
                XmlBuilder target = xml.addNewElement("target").isExpanded();
2120
                target.addAttribute("name", p.mAlias);
2121
 
2122
                if ( !mDaemon )
2123
                {
2124
                    target.addAttribute("depends",  p.mAlias + ".wrap");
2125
                    target.addAttribute("if",p.mAlias + ".build");
2126
                }
2127
 
2128
                if ( mDaemon && p.mBuildFile == 1 )
2129
                {
2130
                    target.addProperty(p.mAlias + "pkg_id",p.mPid);
2131
                    target.addProperty(p.mAlias + "pv_id",p.mId);
2132
                }
2133
 
2134
                target.addProperty(p.mAlias + "packagename",p.mName);        		
2135
                target.addProperty(p.mAlias + "packageversion",p.mVersion);
2136
                target.addProperty(p.mAlias + "packageextension",p.mExtension);
2137
                target.addProperty(p.mAlias + "packagevcstag",p.mVcsTag);
2138
 
2139
                target.makePropertyTag(p.mAlias + "directchange", p.mDirectlyPlanned); 
2140
                target.makePropertyTag(p.mAlias + "doesnotrequiresourcecontrolinteraction", ! p.mRequiresSourceControlInteraction);
2141
 
2142
                buildEntry.state = BuildFile.BuildFileState.NonGeneric;
2143
                if ( p.isGeneric() )
2144
                {
2145
                    buildEntry.state = BuildFile.BuildFileState.Generic;
2146
                    target.makePropertyTag(p.mAlias + "generic", true); 
2147
                }
2148
 
2149
                target.addProperty(p.mAlias + "loc", getBuildLocation(p));
2150
                target.makePropertyTag(p.mAlias + "unittests", p.mHasAutomatedUnitTests && mDaemon);
2151
 
2152
                //    Add our own task and associated information
2153
                //
2154
                XmlBuilder abt = target.addNewElement("abt").isExpanded();
2155
 
2156
                for (Iterator<Package> it = p.mPackageDependencyCollection.iterator(); it.hasNext(); )
2157
                {
2158
                    Package dependency = it.next();
2159
                    XmlBuilder depend = abt.addNewElement("depend");
2160
                    depend.addAttribute("package_alias", "${" + dependency.mAlias + "}");
2161
                }
2162
 
2163
                buildInfo(abt,p);
2164
 
2165
                if ( mDaemon && p.mBuildFile == 1 )
2166
                {
2167
                    //    AbtTestPath
2168
                    target = xml.addNewElement("target").isExpanded();
2169
                    target.addAttribute("name", "AbtTestPath");
2170
                    target.addProperty("AbtTestPathpackagevcstag", p.mVcsTag);
2171
                    target.addProperty("AbtTestPathpackagename", p.mName);
2172
                    abt = target.addNewElement("abt").isExpanded();
2173
                    buildInfo(abt, p);
2174
 
2175
 
2176
                    //    AbtSetUp
2177
                    target = xml.addNewElement("target").isExpanded();
2178
                    target.addAttribute("name", "AbtSetUp");
2179
                    target.addProperty("AbtSetUppackagevcstag", p.mVcsTag);
2180
                    target.addProperty("AbtSetUppackagename", p.mName);
2181
 
2182
                    abt = target.addNewElement("abt").isExpanded();
2183
                    buildInfo(abt, p);
2184
 
2185
                    //    AbtTearDown
2186
                    target = xml.addNewElement("target").isExpanded();
2187
                    target.addAttribute("name", "AbtTearDown");
2188
                    target.addProperty("AbtTearDownpackagevcstag", p.mVcsTag);
2189
                    target.addProperty("AbtTearDownpackagename", p.mName);
2190
                    target.addProperty("AbtTearDownpackageversion", p.mVersion);
2191
                    target.addProperty("AbtTearDownpackageextension", p.mExtension);
2192
                    target.makePropertyTag(p.mAlias + "generic", p.isGeneric());
2193
 
2194
                    abt = target.addNewElement("abt").isExpanded();
2195
                    buildInfo(abt, p);
2196
 
2197
 
2198
                    //  AbtPublish
2199
                    target = xml.addNewElement("target").isExpanded();
2200
                    target.addAttribute("name", "AbtPublish");
2201
 
2202
                    target.addProperty("AbtPublishpackagevcstag", p.mVcsTag);
2203
                    target.addProperty("AbtPublishpackagename", p.mName);
2204
                    target.addProperty("AbtPublishpackageversion", p.mVersion);
2205
                    target.addProperty("AbtPublishpackageextension", p.mExtension);
2206
                    target.makePropertyTag("AbtPublishdirectchange", p.mDirectlyPlanned);
2207
                    target.makePropertyTag("AbtPublishdoesnotrequiresourcecontrolinteraction", ! p.mRequiresSourceControlInteraction);
2208
                    target.makePropertyTag("AbtPublishgeneric", p.isGeneric());
2209
                    target.addProperty("AbtPublishloc", getBuildLocation(p));
2210
 
2211
                    abt = target.addNewElement("abt").isExpanded();
2212
                    buildInfo(abt, p);
2213
 
2214
                }
2215
            }
2216
        }
2217
    }
2218
 
2219
    /** Extends the xml object. Adds ant default target for the current build iteration
2220
     * @param xml - The XmlBuilder Object to extend
2221
     * @param buildFile - The current build file level. This differs for Daemon and Escrow mode. In Daemon mode it will not be a '1' 
2222
     */
2223
    private void generateDefaultTarget(XmlBuilder xml, int buildFile)
2224
    {
2225
        mLogger.debug("generateDefaultTarget");
2226
 
2227
        XmlBuilder target = xml.addNewElement("target").isExpanded();
2228
        target.addAttribute("name", "fullstart");
2229
 
2230
        if (buildFile == 1)
2231
        {
2232
            antEcho(target, "${line.separator}" + mAnyBuildPlatforms + "${line.separator}${line.separator}");
7048 dpurdie 2233
            for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
6914 dpurdie 2234
            {
2235
                Package p = it.next();
2236
 
2237
                if ( p.mBuildFile == -1 )
2238
                {
2239
                    antEcho(target, "${line.separator}" + p.mAlias + "${line.separator}");
2240
                }
2241
            }
2242
 
2243
            antEcho(target, "${line.separator}" + mAssocBuildPlatforms + "${line.separator}${line.separator}");
7048 dpurdie 2244
            for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
6914 dpurdie 2245
            {
2246
                Package p = it.next();
2247
 
2248
                if ( p.mBuildFile == -2 )
2249
                {
2250
                    antEcho(target, "${line.separator}" + p.mAlias + "${line.separator}");
2251
                }
2252
            }
2253
 
2254
            antEcho(target, "${line.separator}" + mNotInBaseline + "${line.separator}${line.separator}");
7048 dpurdie 2255
            for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
6914 dpurdie 2256
            {
2257
                Package p = it.next();
2258
 
2259
                if ( p.mBuildFile == -4 )
2260
                {
2261
                    antEcho(target, "${line.separator}" + p.mAlias + "${line.separator}");
2262
                }
2263
            }
2264
 
2265
            antEcho(target, "${line.separator}" + mDependent + "${line.separator}${line.separator}");
7048 dpurdie 2266
            for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
6914 dpurdie 2267
            {
2268
                Package p = it.next();
2269
 
2270
                if ( p.mBuildFile == -5 )
2271
                {
2272
                    antEcho(target, "${line.separator}" + p.mAlias + "${line.separator}");
2273
                }
2274
            }
2275
        }
2276
 
2277
        if ( !mDaemon )
2278
        {
2279
            antEcho(target, "${line.separator}Build Started:${line.separator}${line.separator}");
2280
        }
2281
 
2282
        //
2283
        //	Create a comma separated list of all the required targets
2284
        //      Escrow : All packages
2285
        //      Daemon : Just the package being built
2286
        //
2287
        StringAppender dependList = new StringAppender(",");
2288
        dependList.append("fullstart");
7048 dpurdie 2289
        for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
6914 dpurdie 2290
        {
2291
            Package p = it.next();
2292
 
2293
            if ( ( p.mBuildFile > 0 ) && ( p.mBuildFile <= buildFile ) )
2294
            {
2295
                if ((mDaemon && p.mBuildFile == 1) || !mDaemon)
2296
                {
2297
                    dependList.append(p.mAlias);                    
2298
                }
2299
            }
2300
        }
2301
 
2302
        target = xml.addNewElement("target").isExpanded();
2303
        target.addAttribute("name", "full");
2304
        target.addAttribute("depends", dependList.toString());
2305
 
2306
        if ( !mDaemon )
2307
        {
2308
            antEcho(target, "${line.separator}Build Finished${line.separator}");
2309
        }
2310
    }
2311
 
2312
    /** Internal helper function to create an ant 'echo statement
2313
     *  Many of the parameters are fixed
2314
     */
2315
    private void antEcho( XmlBuilder xml, String message)
2316
    {
2317
        XmlBuilder msg = xml.addNewElement("echo");
2318
        msg.addAttribute("message", message);
2319
        msg.addAttribute("file", "publish.log");
2320
        msg.addAttribute("append", "true");
2321
    }
2322
 
2323
    /**sets the mIndirectlyPlanned true for the package and all dependent packages
2324
     */
2325
    private void rippleIndirectlyPlanned(Package p)
2326
    {
2327
        mLogger.debug("rippleIndirectlyPlanned");
2328
        if ( !p.mIndirectlyPlanned && p.mBuildFile == 0 )
2329
        {
2330
            p.mIndirectlyPlanned = true;
2331
 
7048 dpurdie 2332
            for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
6914 dpurdie 2333
            {
2334
                Package pkg = it.next();
2335
 
2336
                if ( pkg != p )
2337
                {
2338
                    for (Iterator<Package> it2 = pkg.mPackageDependencyCollection.iterator(); it2.hasNext(); )
2339
                    {
2340
                        Package dependency = it2.next();
2341
 
2342
                        if ( dependency == p )
2343
                        {
2344
                            rippleIndirectlyPlanned( pkg );
2345
                            break;
2346
                        }
2347
                    }
2348
                }
2349
            }
2350
        }
7048 dpurdie 2351
        mLogger.info("rippleIndirectlyPlanned set {} {}", p.mName, p.mIndirectlyPlanned);    
6914 dpurdie 2352
    }
2353
 
2354
    /**accessor method
2355
     */
2356
    public String getEscrowSetUp()
2357
    {
2358
        mLogger.debug("getEscrowSetUp");
2359
        String retVal = mEscrowSetup;
2360
 
7048 dpurdie 2361
        mLogger.debug("getEscrowSetUp returned {}", retVal);
6914 dpurdie 2362
        return retVal;
2363
    }
2364
 
2365
    /**accessor method
2366
     */
2367
    public String getRawData()
2368
    {
2369
        mLogger.debug("getRawData");
2370
        String retVal = mEscrowRawData;
2371
 
7048 dpurdie 2372
        mLogger.debug("getRawData returned {}", retVal);
6914 dpurdie 2373
        return retVal;
2374
    }
2375
 
2376
    /**Get the build loc (location)
2377
     * This is package specific and will depend on the build mode (Escrow/Daemon)
2378
     * 
2379
     * @param	p - Package being built
2380
     * @return A string that describes the build location
2381
     */
2382
    private String getBuildLocation(Package p)
2383
    {
2384
        mLogger.debug("locationProperty");
2385
        String location = "";
2386
 
2387
        if (mDaemon)
2388
        {
2389
            // Daemon: Start in root of view/workspace
2390
            location += mBaseline;
2391
        }
2392
        else
2393
        {
2394
            // Escrow: mAlias used with jats -extractfiles -view
2395
            location += p.mAlias;
2396
        }
2397
 
2398
        //
2399
        //  Always use '/' as a path separator - even if user has specified '\'
2400
        //  Ant can handle it.
2401
        //
2402
        location = location.replace('\\', '/');
2403
        return location;
2404
    }
2405
 
2406
    /**Adds package build into as XML elements 
2407
     * @param	  xml 	- Xml element to extend
2408
     * @param   p       - Package to process
2409
     */
2410
    private void buildInfo(XmlBuilder xml, Package p)
2411
    {
2412
        mLogger.debug("buildInfo");
2413
 
2414
        //
2415
        // Create the xml build information
2416
        // <platform gbe_machtype="linux_i386" type="jats" arg="all"/>
2417
        //
2418
        for (Iterator<BuildStandard> it = p.mBuildStandardCollection.iterator(); it.hasNext();)
2419
        {
2420
            BuildStandard bs = it.next();
2421
            bs.getBuildStandardXml(xml);
2422
        }
2423
    }
2424
 
2425
    /**returns the buildInfo as a single line of text
2426
     * Used for reporting purposes only
2427
     * @param   p       - Package to process
2428
     * 
2429
     */
2430
    public String buildInfoText(Package p)
2431
    {
2432
        mLogger.debug("buildInfoText");
2433
 
2434
        StringAppender result = new StringAppender (";");
2435
 
2436
        //
2437
        //  Create platform:standards
2438
        //      
2439
        for (Iterator<BuildStandard> it = p.mBuildStandardCollection.iterator(); it.hasNext(); )
2440
        {
2441
            BuildStandard bs = it.next();
2442
 
2443
            if ( bs.isActive() )
2444
            {
2445
                String info = bs.getBuildStandardText();
2446
                result.append(info);
2447
            }
2448
        }
2449
 
7048 dpurdie 2450
        mLogger.info("buildInfoText returned {}", result);
6914 dpurdie 2451
        return result.toString();
2452
    }
2453
 
2454
    /**prints to standard out in escrow mode only
2455
     * <br>Prints a title and information. The title is only printed once.
2456
     * 
2457
     * @param header - The message title to display, if printMessage is true
2458
     * @param text - A package name. Really just the 2nd line of the message
2459
     * @param printHeader -  Controls the printing of the message argument
2460
     */
2461
    private void standardOut(final String header, final String text, boolean printHeader)
2462
    {
2463
        mLogger.debug("standardOut");
2464
        if (!mDaemon)
2465
        {
2466
            if ( printHeader )
2467
            {
2468
                System.out.println(header);
2469
            }
2470
 
2471
            System.out.println(text);
2472
        }
2473
    }
2474
 
2475
 
2476
    /**
2477
     *  Email users about a rejected daemon instruction
2478
     *  @param  reason  - Reason for the rejection
2479
     *  @param  pkg     - Package to be affected by the instruction
2480
     *  
2481
     */
2482
    public void emailRejectedDaemonInstruction(String reason, Package p)
2483
    {
2484
        mLogger.debug("emailRejectedDaemonInstruction");
2485
 
2486
        //  Email Subject
2487
        String subject = "BUILD FAILURE of Daemon Instruction on package " + p.mAlias;
2488
 
2489
        // Email Body
2490
        String mailBody = "The build system reject the the Daemon Instruction";
2491
        mailBody += "<br>Reason: " + reason; 
2492
 
2493
        mailBody += "<p>Release: " + mBaselineName 
2494
                +  "<br>Package: " + p.mAlias 
2495
                +  "<br>Rm Ref: " + CreateUrls.generateRmUrl(getRtagId(), p.mId); 
2496
 
2497
        mailBody += "<p><hr>";
2498
 
2499
        String target = p.emailInfoNonAntTask(this);
2500
 
7048 dpurdie 2501
        mLogger.error("emailRejectedDaemonInstruction Server: {}", getMailServer());
2502
        mLogger.error("emailRejectedDaemonInstruction Sender: {}", getMailSender());
2503
        mLogger.error("emailRejectedDaemonInstruction Target: {}", target);
6914 dpurdie 2504
 
2505
        try
2506
        {
2507
            //    
7048 dpurdie 2508
            Smtpsend.send(getMailServer(),  // mailServer
2509
                    getMailSender(),        // source
2510
                    target,                 // target
2511
                    getMailSender(),        // cc
2512
                    null,                   // bcc
2513
                    subject,                // subject
2514
                    mailBody,               // body
2515
                    null                    // attachment
6914 dpurdie 2516
                    );
2517
        } catch (Exception e)
2518
        {
7048 dpurdie 2519
            mLogger.warn("Email Failure: emailRejectedDaemonInstruction:{}", e.getMessage());
6914 dpurdie 2520
        }
2521
    }
2522
 
2523
    /**
2524
     * @return the mMailServer
2525
     */
2526
    public String getMailServer() {
2527
        return mMailServer;
2528
    }
2529
 
2530
    /**
2531
     * @param mMailServer the mMailServer to set
2532
     */
2533
    public void setMailServer(String mMailServer) {
2534
        this.mMailServer = mMailServer;
2535
    }
2536
 
2537
    /**
2538
     * @return the mMailSender
2539
     */
2540
    public String getMailSender() {
2541
        return mMailSender;
2542
    }
2543
 
2544
    /**
2545
     * @param mMailSender the mMailSender to set
2546
     */
2547
    public void setMailSender(String mMailSender) {
2548
        this.mMailSender = mMailSender;
2549
    }
2550
 
2551
    /**
2552
     * @return the mMailGlobalTarget
2553
     */
2554
    public String getMailGlobalTarget() {
2555
        return mMailGlobalTarget;
2556
    }
2557
 
2558
    /**
2559
     * @param mMailGlobalTarget the mMailGlobalTarget to set
2560
     */
2561
    public void setMailGlobalTarget(String mMailGlobalTarget) {
2562
        this.mMailGlobalTarget = mMailGlobalTarget;
2563
    }
2564
 
2565
}