Subversion Repositories DevTools

Rev

Rev 7095 | Rev 7097 | 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
     */
7079 dpurdie 122
    private ArrayList<PlannedPackage> mBuildOrder = new ArrayList  <PlannedPackage>();
6914 dpurdie 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
 
7078 dpurdie 437
            if ( p.hasCircularDependency( mPackageCollectionAll ) )
6914 dpurdie 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
 
7079 dpurdie 444
                // Exclude the package
6914 dpurdie 445
                // max 50 chars
7079 dpurdie 446
                rippleBuildExclude(p, p.mId, "Package has circular dependency", -6);
6914 dpurdie 447
 
448
                // take the package out of the build
449
                standardOut(mCircularDependency, p.mAlias, mCircularDependencyFlag);
450
                mCircularDependencyFlag = false;
451
            }
452
        }
453
 
454
        // Scan for packages with missing dependencies
455
        //    ie: The dependent package is not in the package Collection
456
        mLogger.debug("planRelease use the fully built mPackageDependencyCollection");
7048 dpurdie 457
        for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
6914 dpurdie 458
        {
459
            Package p = it.next();
460
 
461
            if ( mDaemon )
462
            {
463
                //  Daemon Mode Only
464
                //  Not interested in the dependencies of a pegged package or a package provided from an SDK.
465
                //  Such packages will be deemed to not have missing dependencies
466
                if (p.mIsPegged || p.mIsSdk)
467
                {
468
                    continue;
469
                }
470
            }
471
 
472
            for (Iterator<String> it2 = p.mDependencyCollection.iterator(); it2.hasNext(); )
473
            {
474
                String alias = it2.next();
475
                Package dependency = findPackage(alias);
476
 
477
                if (dependency == ReleaseManager.NULL_PACKAGE)
478
                {
7048 dpurdie 479
                    mLogger.info("planRelease dependency is not in the baseline {}", alias);
7079 dpurdie 480
                    // Exclude the package
6914 dpurdie 481
                    // max 50 chars
7079 dpurdie 482
                    rippleBuildExclude(p, p.mId, "Package build dependency not in the release", -4);
6914 dpurdie 483
 
484
                    // take the package out of the build
485
                    standardOut(mNotInBaseline, p.mAlias, mNotInBaselineFlag);
486
                    mNotInBaselineFlag = false;
487
                    break;
488
                }
489
            }
490
        }
491
 
7048 dpurdie 492
        // Detect packages with no build standard and exclude them from the build
6914 dpurdie 493
        mLogger.debug("planRelease process packages which are not reproducible");
7048 dpurdie 494
        for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
6914 dpurdie 495
        {
496
            Package p = it.next();
497
 
498
            if (p.mBuildFile == 0)
499
            {
500
                if ( mDaemon )
501
                {
502
                    //  Daemon Mode Only
503
                    //  Not interested in the reproducibility of a pegged package or a package provided from an SDK.
504
                    //  Such packages will be deemed to be reproducible - but out side of this release
505
                    if (p.mIsPegged || p.mIsSdk)
506
                    {
507
                        continue;
508
                    }
509
                }
510
 
7048 dpurdie 511
                // Does the package have a build standard. If not then we can't reproduce it.
512
                // Escrow - Assume the package is provided
513
                // Daemon - Exclude this package, but not its consumers. If the package is available then we can use it.
6914 dpurdie 514
                if (!p.isReproducible())
515
                {
7048 dpurdie 516
                    mLogger.info("planRelease package not reproducible {}" ,p.mName);
7079 dpurdie 517
 
518
                    // Exclude the package
6914 dpurdie 519
                    // max 50 chars
7079 dpurdie 520
                    rippleBuildExclude(p, p.mId, "Package has no build environment", -1);
6914 dpurdie 521
 
522
                    // package is not reproducible, discard
523
                    standardOut(mAnyBuildPlatforms, p.mAlias, mAnyBuildPlatformsFlag);
524
                    mAnyBuildPlatformsFlag = false;
525
                }
526
            }
527
        }
528
 
7048 dpurdie 529
        //    Process packages which are not reproducible on the configured set of build machines.
6914 dpurdie 530
        //
531
        //    Test each package and determine if the package contains a buildStandard that
532
        //    can be processed by one of the machines in the build set
533
        //
534
        //    ie: Package: Win32:Production and we have a BM with a class of 'Win32'
535
        //
536
        //    Only exclude the failing package and not its dependents
537
        //    May be legitimate in the release
538
        //
539
        mLogger.debug("planRelease process packages which are not reproducible2");
7048 dpurdie 540
        for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
6914 dpurdie 541
        {
542
            Package p = it.next();
543
 
544
            if (p.mBuildFile == 0)
545
            {
546
                if ( mDaemon )
547
                {
548
                    //  Daemon Mode Only
549
                    //  Not interested in the reproducibility of a pegged package or a package provided from an SDK.
550
                    //  Such packages will be deemed to be reproducible - but out side of this release
551
                    if (p.mIsPegged || p.mIsSdk)
552
                    {
553
                        continue;
554
                    }
555
                }
556
 
557
                // package has yet to be processed
558
                // assume it does not need to be reproduced for this baseline
559
                //
560
                //    For each machineClass in the buildset
561
                boolean reproduce = false;
562
 
563
                for (Iterator<String> it2 = mReleaseManager.mReleaseConfigCollection.mMachineClasses.iterator(); it2.hasNext(); )
564
                {
565
                    String machineClass = it2.next();
566
 
567
                    if ( p.canBeBuildby(machineClass))
568
                    {
569
                        reproduce = true;
7048 dpurdie 570
                        mLogger.info("planRelease package built on {} {}", machineClass, p.mAlias );
6914 dpurdie 571
                        break;
572
                    }
573
                }
574
 
575
                if ( !reproduce )
576
                {
7048 dpurdie 577
                    mLogger.info("planRelease package not reproducible on the build platforms configured for this baseline {}", p.mName);
6914 dpurdie 578
 
7079 dpurdie 579
                    // Exclude the package
7048 dpurdie 580
                    // max 50 chars
7079 dpurdie 581
                    rippleBuildExclude(p, p.mId, "Package not built for configured platforms", -2);
6914 dpurdie 582
 
583
                    // package is not reproducible on the build platforms configured for this baseline, discard
584
                    standardOut(mAssocBuildPlatforms, p.mAlias, mAssocBuildPlatformsFlag);
585
                    mAssocBuildPlatformsFlag = false;
586
                }
587
            }
588
        }      
589
 
7079 dpurdie 590
        if (mDaemon )
6914 dpurdie 591
        {
592
 
593
            //  Daemon Mode Only
594
            //  Process packages which need to be ripple built
595
            mLogger.debug("planRelease process packages which need to be ripple built");
596
            for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
597
            {
598
                Package p = it.next();
599
 
600
                if (p.mBuildFile == 0)
601
                {
602
                    //  Not interested in a pegged package or a package provided from an SDK.
603
                    //  Such packages are not rippled
604
                    if (p.mIsPegged || p.mIsSdk)
605
                    {
606
                        continue;
607
                    }
608
 
7048 dpurdie 609
                    //    Examine this packages dependencies
610
                    //    If one of them does not exist in the 'official release' set then
611
                    //    the package needs to be built against the official release set
612
                    //    and we can't build any of its dependent packages
613
                    Iterator<Integer> it2 = p.mDependencyIDCollection.iterator();
614
                    Iterator<Package> it3 = p.mPackageDependencyCollection.iterator();
615
                    while ( it2.hasNext() && it3.hasNext() )
6914 dpurdie 616
                    {
7048 dpurdie 617
                        Integer dpvId = it2.next();
618
                        Package dependency = it3.next();
619
 
620
                        if ( !dependency.mAdvisoryRipple )
6914 dpurdie 621
                        {
7048 dpurdie 622
                            // not advisory, ie: has ripple build impact
623
                            if ( !isInRelease(dpvId) )
6914 dpurdie 624
                            {
7048 dpurdie 625
                                // the package is out of date
626
                                // exclude all dependent package versions
627
                                mLogger.info("planRelease package out of date {}", p.mName);
628
                                p.mBuildReason = BuildReason.Ripple;
629
                                rippleIndirectlyPlanned(p);
630
 
631
                                //  This package needs to be rippled
632
                                //  If this package has a rippleStop marker of 's', then we cannot
633
                                //  build this package at the moment.
634
                                //  Packages that depend on this package have been excluded
635
                                //  We need to exclude this one too
636
                                //
637
                                if (p.mRippleStop == 's' || p.mRippleStop == 'w') 
6914 dpurdie 638
                                {
7048 dpurdie 639
                                    // Package marked as a rippleStop
640
                                    // max 50 chars
7079 dpurdie 641
                                    rippleBuildExclude(p, -2, "Ripple Required." + " Waiting for user", -11);
7048 dpurdie 642
 
643
                                    if (p.mRippleStop == 's' ) {
644
                                        // Need to flag to users that the package build is waiting user action
645
                                        mLogger.info("planRelease Ripple Required. Stopped by flag {}", p.mName);
646
                                        mReleaseManager.setRippleStopWait(mRtagId,p);
6914 dpurdie 647
                                    }
648
                                }
7048 dpurdie 649
 
650
                                break;
6914 dpurdie 651
                            }
652
                        }
653
                    }
654
                }
655
            }
656
 
657
            //  Daemon Mode Only
658
            //  Process packages which do not exist in the archive
659
            //  For unit test purposes, assume all packages exist in the archive if released
660
            mLogger.debug("planRelease process packages which do not exist in the archive");
7048 dpurdie 661
            if ( mReleaseManager.mUseDatabase )
6914 dpurdie 662
            {
663
                for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
664
                {
665
                    Package p = it.next();
666
 
667
                    if (p.mBuildFile == 0)
668
                    {
669
                        // package has yet to be processed
670
                        if (!p.mDirectlyPlanned && !p.mIndirectlyPlanned && p.mForcedRippleInstruction == 0)
671
                        {
672
                            // check package version archive existence
673
                            if (!p.existsInDpkgArchive())
674
                            {
675
                                if (! p.mIsBuildable)
676
                                {
677
                                    //  Package does not exist in dpkg_archive and it has been flagged as unbuildable
678
                                    //  This may be because is Unbuildable or Manually built
7048 dpurdie 679
                                    mLogger.info("planRelease Unbuildable package not found in archive {}", p.mName);
7079 dpurdie 680
                                    // Exclude the package
6914 dpurdie 681
                                    // max 50 chars
7079 dpurdie 682
                                    rippleBuildExclude(p, p.mId, "Unbuildable" + " package not found in archive", -10);
6914 dpurdie 683
 
684
                                }
685
                                //  Not interested in a pegged package or a package provided from an SDK.
686
                                //  Such packages are not rippled
687
                                else if (p.mIsPegged || p.mIsSdk)
688
                                {
689
                                    String reason;
690
                                    reason = (p.mIsPegged) ? "Pegged" : "SDK";
691
 
692
                                    //  Pegged packages or packages provided from an SDK MUST exist in dpkg_archive
693
                                    //  They will not be built within the context of this release. It is the responsibility
694
                                    //  of another release to build them.
7048 dpurdie 695
                                    mLogger.info("planRelease {} package not found in archive {}", reason, p.mName);
7079 dpurdie 696
                                    // Exclude the package
6914 dpurdie 697
                                    // max 50 chars
7079 dpurdie 698
                                    rippleBuildExclude(p, p.mId, reason + " package not found in archive", -7);
6914 dpurdie 699
                                }
700
                                else if (p.mForcedRippleInstruction == 0)
701
                                {
702
                                    //  [JATS-331] Unable to rebuild package with Advisory Ripple dependencies
703
                                    //    Examine this packages dependencies
704
                                    //    If one of them does not exist in the 'official release' set then
705
                                    //    the package cannot be rebuilt in this release.
706
                                    for (Iterator<Integer> it2 = p.mDependencyIDCollection.iterator() ; it2.hasNext() ;)
707
                                    {
708
                                        Integer dpvId = it2.next();
709
                                        if ( !isInRelease(dpvId) )
710
                                        {
711
                                            // This package cannot be rebuilt as one of its dependents is NOT in this release
712
                                            // exclude all dependent package versions
713
 
7048 dpurdie 714
                                            mLogger.info("planRelease package not found in archive. Cannot be rebuilt due to {}", p.mName);
7079 dpurdie 715
                                            // Exclude the package
6914 dpurdie 716
                                            // max 50 chars
7079 dpurdie 717
                                            rippleBuildExclude(p, p.mId, "Package cannot be rebuilt in this release", -4);
6914 dpurdie 718
                                            break;
719
                                        }
720
                                    }
721
 
722
                                    //  The package has not been excluded from the build
723
                                    if (p.mBuildFile == 0)
724
                                    {
7048 dpurdie 725
                                        mLogger.info("planRelease package not found in archive {}", p.mName);
6914 dpurdie 726
                                        // DEVI 47395 the cause of this build is not WIP or ripple induced,
727
                                        // it simply does not exist in the archive (has been removed)
728
                                        // prevent source control interaction
729
                                        p.mRequiresSourceControlInteraction = false;
730
                                        p.mBuildReason = BuildReason.Restore;
731
                                        rippleIndirectlyPlanned(p);
732
                                    }
733
                                }
734
                            }
735
                        }
736
                    }
737
                }
738
            }
739
 
740
            //  Daemon Mode Only
7048 dpurdie 741
            //  Detect bad forced ripples requests and reject them
6914 dpurdie 742
            mLogger.debug("planRelease process forced ripples");
7048 dpurdie 743
            for (Iterator<Package> it = mPackageCollectionRipple.iterator(); it.hasNext(); )
6914 dpurdie 744
            {
745
                Package p = it.next();
746
 
747
                if (p.mBuildFile == 0)
748
                {
7048 dpurdie 749
                    //
750
                    //    Cannot force a ripple on an SDK based package or a Pegged Package
751
                    //        Remove the daemon instruction from the database
752
                    //        Send a warning email
753
                    //
754
                    if(p.mIsPegged || p.mIsSdk)
6914 dpurdie 755
                    {
7048 dpurdie 756
                        String reason;
757
                        reason = (p.mIsPegged) ? "Pegged" : "SDK Based";
6914 dpurdie 758
 
7048 dpurdie 759
                        mLogger.error("planRelease Daemon Instruction of {} package deleted: {}", reason, p.mName);
760
                        mReleaseManager.markDaemonInstCompleted( p.mForcedRippleInstruction );
761
                        emailRejectedDaemonInstruction("Cannot 'Ripple' a " + reason + " package",p);
6914 dpurdie 762
 
7048 dpurdie 763
                        p.mBuildFile = -8; 
6914 dpurdie 764
                    }
765
                }
766
            }
767
 
768
            //  Daemon Mode Only
769
            //  Mark Pegged and SDK packages as not to be built
7078 dpurdie 770
            //  Mark RippleStoped packages as not to be build
6914 dpurdie 771
            //  Have previously detected conflicts between pegged/sdk packages and daemon instructions
772
            //
773
            mLogger.debug("planRelease remove pegged and SDK packages from the build set");
774
            for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
775
            {
776
                Package p = it.next();
777
 
778
                if (p.mBuildFile == 0)
779
                {
7078 dpurdie 780
                    String reason = null;
781
 
782
                    //  Not interested in a pegged package or a package provided from an SDK or packages with an active RippleStop
6914 dpurdie 783
                    //  Such packages are not built
7078 dpurdie 784
                    if (p.mIsPegged ) {
785
                        reason = "Pegged";
786
                        p.mBuildFile = -8;
787
                    } else if (p.mIsSdk) {
788
                        reason = "SDK";
789
                        p.mBuildFile = -8;
790
                    } else if (p.mRippleStop == 's' || p.mRippleStop == 'w') {
791
                        reason = "RippleStop";
792
                        p.mBuildFile = -11;
793
                    }
794
 
795
                    if (reason != null)
6914 dpurdie 796
                    {
7048 dpurdie 797
                        mLogger.info("planRelease {} not built in this release {}", reason, p.mName);
6914 dpurdie 798
                    }
799
                }
800
            }
801
 
802
            //  Daemon Mode Only
803
            //  Locate Test Build Requests that cannot be satisfied - will not be built due to
804
            //  errors in dependent packages. Report the error to the user and remove the request
805
            //
7048 dpurdie 806
            for (Iterator<Package> it = mPackageCollectionTest.iterator(); it.hasNext(); )
6914 dpurdie 807
            {
808
                Package p = it.next();
809
 
7048 dpurdie 810
                if (p.mBuildFile < 0)
6914 dpurdie 811
                {
7048 dpurdie 812
                    String reason;
813
                    switch (p.mBuildFile)
6914 dpurdie 814
                    {
7048 dpurdie 815
                    case -1:  reason = "Not reproducible"; break;
816
                    case -2:  reason = "Not reproducible on configured build platforms"; break;
817
                    case -3:  reason = "Marked as 'Do not ripple'"; break;
818
                    case -4:  reason = "Dependent on a package not in the release"; break;
819
                    case -5:  reason = "Indirectly dependent on a package not reproducible in the release"; break;
820
                    case -6:  reason = "Has a circular dependency"; break;
821
                    case -7:  reason = "Pegged or SDK package not in dpkg_archive"; break;
822
                    case -8:  reason = "Is a Pegged or SDK package"; break;
823
                    case -9:  reason = "Rejected Daemon Instruction"; break;
824
                    case -10: reason = "Unbuildable package not in dpkg_archive"; break;
825
                    case -11: reason = "Marked as 'RippleStop'"; break;
826
                    default:  reason = "Unknown reason. Code:" + p.mBuildFile; break;
6914 dpurdie 827
                    }
7048 dpurdie 828
                    mLogger.error("planRelease Test Build of an unbuildable of package deleted: {}", p.mName);
829
                    mReleaseManager.markDaemonInstCompleted( p.mTestBuildInstruction );
830
                    emailRejectedDaemonInstruction(reason,p);
6914 dpurdie 831
                }
832
            }
833
        }
834
        else
835
        {
836
            // escrow reporting only
837
            // Report packages that are not reproducible
838
            //
839
            //  Note: I don't believe this code can be executed
840
            //        The -3 is only set in daemon mode
841
            for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
842
            {
843
                Package p = it.next();
844
 
845
                if (p.mBuildFile == -3)
846
                {
847
                    standardOut(mDependent, p.mAlias, mDependentFlag);
848
                    mDependentFlag = false;
849
                }
850
            }
851
        }
852
    }
7079 dpurdie 853
 
854
    /** Plan the build order.
855
         *  Assumes that a great deal of work has been done.
856
         *  This is a stand alone method to contain the work
857
         *  
858
         * @throws Exception
859
         * @throws SQLException
860
         */
861
        private void planBuildOrder() throws Exception, SQLException 
862
        {
863
 
864
    //TODO - Planning is not working well
865
    /**
866
     * Current status
867
     * The selection of the package to build next is deeply flawed
868
     * Currently it will select a directly planned package over a ripple - (perhaps that is good)
869
     * 
870
     * Currently on the buildPlan of the 'selected' package is included in the BuildOrder
871
     * 
872
     * The current algorithm is based on time - but all packages have zero build time ( in UTF )
873
     * Even if this is fixed the algorithm is broken as we will select the complete build with the shortest time
874
     * which will be the ripple of a leaf package ( perhaps that is good )
875
     * 
876
     * Also:
877
     *      [Done] Need to include all packages that we can now build into the final build plan, not just the first one and its build tree
878
     * 
879
     *      [Done-ish]Need to generate a single package set - so that WIPS and others replace those in the final build set
880
     *      [Done]Remove duplicates from the complete package set
881
     *      
882
     *      [Being Done] Need more test cases
883
     *      
884
     *      [Done, in the UTF] Need to report all build options, not just the selected one
885
     *                         Data to RM may need some more info
886
     *      
887
     *      Cleanup the ANT build file - don't list all the release dependencies, just those needed to build the current package
888
     *      Chances are - all we need is the new package version number
889
     *      
890
     *      [Fixed] If a WIP on a package that has circular dependencies is added, then the WIP will be excluded. This prevents the problem being fixed.
891
     *              Should not exclude a WIP of a package with a circular dependency
892
     */
893
 
894
            // Process remaining packages which are need to be reproduced for this baseline.
895
            //    Determine the build file for each package
896
            //    For daemon builds:
897
            //      Determine the next package that can be built now
898
            //          Sounds simple - doesn't it
899
            //      Set its mBuildNumber to 1, all remaining reproducible packages to 2
900
            //    For escrow builds:
901
            //      Determine the package versions that can be built in the build iteration
902
            //      Set their mBuildNumber to the build iteration
903
            //      Increment the build iteration and repeat until all package versions 
904
            //      that need to be reproduced have been assigned a build iteration        
905
 
906
            //
907
            //  Generate a plan for the packages that are in the current release
908
            //  These may be the result of a ripple going through the system 
909
            //  or a rebuild of a missing package or the result of a merge.
910
            //
911
            //  This will provide our basic plan, before we consider adding new packages to the
912
            //  mix, such as those from user WIPS and RIPPLE requests.
913
            //  
914
            PlanResults basicPlan = planCollection("Basic", mPackageCollection, true);
915
 
916
            //  Have not considered any of the WIP or RIPPLE packages
917
            //  If any of them are in the basic plan, then we can ignore them as we will build them
918
            //  when we build the basic plan
919
            //
920
            //  Determine a set of packages that will not be included if we build the basic plan
921
            //
922
            //  At the moment - lets assume there are none
923
            //  What do we do ?
924
            //      Select the first request
925
            //      Replace the package in the released collection
926
            //      Run the plan algorithm
927
            //      Restore the release collection
928
            //
929
            if ( mDaemon )
930
            {
931
                //
932
                //  Need to consider the TEST requests
933
                //      - Valid requests will be placed first on the build order
934
                //
935
                mBuildOrder.clear();
936
                for (Iterator<Package> it = mPackageCollectionTest.iterator(); it.hasNext(); )
937
                {
938
                    Package p = it.next();
939
                    if (p.mBuildFile >= 0)
940
                    {
941
                        mBuildOrder.add( new PlannedPackage(p,0 ) );
942
                    }
943
                }
944
 
945
                //  First attempt
946
                //  Insert all the WIPs and RIPPLES into the buildset. 
947
                //  Generate a plan and see how much the time is extended.
948
                //
949
                //  Don't modify mPackageCollection, although data in the 'Package' in the underlying packages may change
950
 
7080 dpurdie 951
                PlanResults fullPlan = postRipplePlan("Full", basicPlan, true);
7079 dpurdie 952
 
953
                //  Decide with plan to use
954
                //  At the moment we have, at most two.
955
                //      - (basic) Current Release ripples
956
                //      - (full) Current Release + All WIPS and RIPPLES
957
                //
958
                //  If we have both then if the full plan is < 20% longer than the basic plan, use the full plan
959
                //  Otherwise we use basic plan FOLLOWED by the a MODIFIED full plan (one without the natural ripples)
960
                //  Add the planned packages into the buildOrder
961
 
7080 dpurdie 962
                if( !basicPlan.planCollection.isEmpty() && !fullPlan.planCollection.isEmpty() )
7079 dpurdie 963
                {
7093 dpurdie 964
                    mLogger.error("Two plan selection: {} {}, {} <= {}", basicPlan.planTime,fullPlan.planTime, basicPlan.planTime * 12, fullPlan.planTime * 10);
7079 dpurdie 965
                    if ( (basicPlan.planTime * 12) <= (fullPlan.planTime * 10) )
966
                    {
967
                        //  Use the basic plan FOLLOWED by a plan that does not include
968
                        //      Ripples done by the basic plan
969
                        //      WIPs/RIPPLES done in the course of doing the basic plan
7093 dpurdie 970
                        mLogger.error("Use basic plan, then modified full plan");
7079 dpurdie 971
                        mBuildOrder.addAll(basicPlan.planCollection);
972
                        fullPlan = postRipplePlan("Full-Ripples", basicPlan, false);
973
                        mBuildOrder.addAll(fullPlan.planCollection);
974
 
975
                    } else {
976
                        //  Use the full plan
7093 dpurdie 977
                        mLogger.error("Use full plan");
7079 dpurdie 978
                        mBuildOrder.addAll(fullPlan.planCollection);
979
                    }
980
                } else if (!basicPlan.planCollection.isEmpty() ) {
981
                    // Use the basic plan
7093 dpurdie 982
                    mLogger.error("Use basic plan");
7079 dpurdie 983
                    mBuildOrder.addAll(basicPlan.planCollection);
984
 
7080 dpurdie 985
                } else if ( !fullPlan.planCollection.isEmpty() ) {
986
                    // Use the full plan
7093 dpurdie 987
                    mLogger.error("Use full plan");
7081 dpurdie 988
                    mBuildOrder.addAll(fullPlan.planCollection);
7079 dpurdie 989
 
990
                } else {
991
                    // Do not have a plan
992
                    // May have tests requests
7093 dpurdie 993
                    mLogger.error("Use NO plan");
7079 dpurdie 994
                }
995
 
7096 dpurdie 996
                //
997
                //  Now have a build order
998
                //  Allocate new version numbers
999
                //      Examine packages with a buidLevel of zero
1000
                //  If we fail all of them then we can't build anything
1001
                //
1002
                mLogger.error("Determine new version numbers");
1003
                Package build = ReleaseManager.NULL_PACKAGE;
1004
                for (Iterator<PlannedPackage> it = mBuildOrder.iterator(); it.hasNext(); )
1005
                {
1006
                    PlannedPackage pkg = it.next();
1007
                    if (pkg.mBuildLevel != 0)
1008
                    {
1009
                        continue;
1010
                    }
1011
 
1012
                    //
1013
                    //  Attempt to allocate a new version number
1014
                    //  If we can't generate a new version number, then this is considered to be a build failure
1015
                    //  The package will be excluded and the user will be emailed
1016
                    //
1017
 
1018
 
1019
                    Package p = pkg.mPkg;
1020
 
1021
                    int pvApplied = p.applyPV(mReleaseManager);
1022
 
1023
                    if ( pvApplied == 0)
1024
                    {
1025
                        build = p;
1026
                        break;
1027
                    }
1028
                    else if ( pvApplied == 1 )
1029
                    {
1030
                        // max 50 chars
1031
                        rippleBuildExclude(p, p.mId, "Package has non standard versioning", -12);
1032
                    }
1033
                    else if ( pvApplied == 2 )
1034
                    {
1035
                        // max 50 chars
1036
                        rippleBuildExclude(p, p.mId, "Package has reached ripple field limitations", -12);
1037
                    }
1038
                    else if ( pvApplied == 3 )
1039
                    {
1040
                        // max 50 chars
1041
                        rippleBuildExclude(p, p.mId, "Package has invalid change type", -12);
1042
                    }
1043
                    else
1044
                    {
1045
                        // max 50 chars
1046
                        // Bad programming - should not get here
1047
                        rippleBuildExclude(p, p.mId, "Unable to calculate next version", -12);
1048
                    }
1049
                }
1050
 
1051
 
7079 dpurdie 1052
                //  Now have an mBuildOrder
7096 dpurdie 1053
                //  Mark the selected package in the build order as the one to be built
1054
                //  May need to process its it a bit more
7094 dpurdie 1055
                //  
7079 dpurdie 1056
                if (!mBuildOrder.isEmpty()) {
1057
                    build.mBuildFile = 1;
1058
 
1059
                    if ( build.mForcedRippleInstruction > 0 )
1060
                    {
1061
                        mReleaseManager.markDaemonInstCompleted( build.mForcedRippleInstruction );
1062
                    }
1063
 
1064
                    if ( build.mTestBuildInstruction > 0 )
1065
                    {
1066
                        mReleaseManager.markDaemonInstInProgress( build.mTestBuildInstruction );
1067
                    }
1068
 
1069
                    //  Now that we know which package we are building
1070
                    //      Set the previously calculated nextVersion as the packages version number
1071
                    //      Claim the version number to prevent other builds from using it. Even if doing a test build
1072
                    //
7093 dpurdie 1073
                    mLogger.error("Update mVersion: {}", build);
7079 dpurdie 1074
                    if (build.mNextVersion != null)
1075
                    {
1076
                        mReleaseManager.claimVersion(build.mPid, build.mNextVersion + build.mExtension, mBaseline);
1077
                        build.mVersion = build.mNextVersion;
7093 dpurdie 1078
                        mLogger.error("Update mVersion: {} to {}", build, build.mVersion);
7079 dpurdie 1079
                    }
1080
                }
1081
 
1082
                //
1083
                //  Massage the package collection
1084
                //      Replace WIP/TEST/RIPPLE so that the mPackageCollection is a collection of packages we would like in the release
1085
                //      Don't replace those that we can't build due to errors
1086
                //  
6914 dpurdie 1087
 
7079 dpurdie 1088
                //         Update packages in the basicPlan
1089
                //         Update / Add packages to the fullPlan
1090
                //
1091
                ArrayList<Package> buildCandidates = new ArrayList<Package>();
1092
                buildCandidates.addAll(mPackageCollectionTest);
1093
                buildCandidates.addAll(mPackageCollectionWip);
1094
                buildCandidates.addAll(mPackageCollectionRipple);
1095
 
1096
                for (Iterator<Package> it = buildCandidates.iterator(); it.hasNext(); )
1097
                {
1098
                    Package p = it.next();
1099
                    if ( p.mBuildFile >= 0 )
1100
                    {
1101
                        Package pReleased = findPackage(p.mAlias);
1102
                        if ( pReleased != ReleaseManager.NULL_PACKAGE)
1103
                        {
1104
                            int index = mReleaseManager.findPackageLastIndex;
1105
                            mPackageCollection.set(index, p);
1106
                            p.mIsNotReleased = false;
1107
                        }
1108
                        else
1109
                        {
1110
                            mPackageCollection.add(p);
1111
                            p.mIsNotReleased = false;
1112
                        }
1113
                    }
1114
                }
1115
 
1116
                //
1117
                //  Report packages that are indirectly excluded
7081 dpurdie 1118
                //  ie: They will not be built because one, or more, of there dependents is not buildable
7079 dpurdie 1119
                //
1120
                mLogger.debug("planBuildOrder process packages which are not ripple buildable");
1121
 
1122
                 for (ListIterator<BuildExclusion> it = mBuildExclusionCollection.listIterator(); it.hasNext(); )
1123
                 {
1124
                     BuildExclusion be = it.next();
1125
 
1126
                     for (Iterator<Package> it1 = mPackageCollection.iterator(); it1.hasNext(); )
1127
                     {
1128
                         Package p = it1.next();
1129
 
1130
                         // ensure only root cause, non test build, build exclusions are excluded
1131
                         // mBuildExclusionCollection is at this point based on
1132
                         // relevant (direct and indirect) excluded pv's in the database
7081 dpurdie 1133
 
7079 dpurdie 1134
                         if ( be.compare(p.mId) && be.isARootCause() && p.mTestBuildInstruction == 0 && p.mForcedRippleInstruction == 0 )
1135
                         {
1136
                             // package is not reproducible, discard it and its consumers
1137
                             //
7081 dpurdie 1138
                             mLogger.info("Excluded Package {}, {}", p.mAlias, be );                                     
7079 dpurdie 1139
 
1140
                             ArrayList<Package> toExclude = new ArrayList<Package>();
1141
                             toExclude.addAll(usedByAnyPackages(p, mPackageCollection ));
1142
 
1143
                             //  Process packages that we need to exclude indirectly
1144
                             //
1145
                             while ( ! toExclude.isEmpty())
1146
                             {
1147
                                 Package pkg = toExclude.remove(0);
1148
 
7081 dpurdie 1149
                                 // If this package has not been excluded (for whatever reason), than add it
7079 dpurdie 1150
                                 boolean found = false;
1151
                                 for (Iterator<BuildExclusion> it2 = mBuildExclusionCollection.iterator(); it2.hasNext(); )
1152
                                 {
1153
                                     BuildExclusion buildExclusion = it2.next();
1154
                                     if (buildExclusion.compare(pkg.mId) )
1155
                                     {
1156
                                         found = true;
7081 dpurdie 1157
                                         if  ( buildExclusion.isImported() )
1158
                                         {
1159
                                             // An exclusion for this package already exists
1160
                                             // If it was 'imported' from the database then 
1161
                                             // mark it as processed so that it will be persisted
1162
                                             //
1163
                                             // Otherwise it will be a new one
1164
                                             //
1165
                                             buildExclusion.setProcessed();
1166
                                         }
7079 dpurdie 1167
                                         break;
1168
                                     }
1169
                                 }
1170
 
1171
                                 if (!found)
1172
                                 {
1173
                                     BuildExclusion buildExclusion = new BuildExclusion(pkg.mId, p.mId, null, p.mTestBuildInstruction);
1174
                                     it.add(buildExclusion);
7096 dpurdie 1175
                                     mLogger.info("Indirectly Excluded Package {}", pkg.mAlias);
1176
                                     pkg.mBuildFile = -5; 
7079 dpurdie 1177
 
7094 dpurdie 1178
                                     ArrayList<Package> usedBy = usedByAnyPackages(pkg, mPackageCollectionAll );
7079 dpurdie 1179
                                     toExclude.addAll(usedBy);
1180
                                 }
1181
                             }
1182
                         }
1183
                     }
1184
                 }
7096 dpurdie 1185
 
1186
                 //
1187
                 //  Examine the build order and 'delete' entries with a -ve mBuildFile
1188
                 //  These will be the product of pvApply errors and the packages that depend on them
1189
 
1190
                 for (Iterator<PlannedPackage> it = mBuildOrder.iterator(); it.hasNext(); )
1191
                 {
1192
                     PlannedPackage pkg = it.next();
1193
                     if (pkg.mPkg.mBuildFile < 0)
1194
                     {
1195
                         it.remove();
1196
                         mLogger.error("Purge mBuildOrder {}", pkg.mPkg);
1197
                     }
1198
                 }
1199
 
1200
 
7079 dpurdie 1201
 
1202
                //
1203
                //  To fit in with the old algorithm ( ie: could be improved )
1204
                //  Insert marks into all packages
1205
                //  Not sure exactly why - Its used in the generation of the ant build file
1206
                //                     Want to set mNoBuildReason, mBuildFile
1207
                //
1208
                //      Package we have selected to build: 0     , 1
1209
                //      Package we could have built      : 0     , 2
1210
                //      Packages we can't build          : reason, 3
1211
                //      Packages that are OK             : 3     , 3
1212
                //      ????                             : 0     , 3
1213
 
1214
                for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
1215
                {
1216
                    Package p = it.next();
1217
                    if (p == build ) {
1218
                        p.mNoBuildReason = 0;
1219
                        p.mBuildFile = 1;
1220
                    } 
1221
                    else if ( p.mBuildFile < 0 )
1222
                    {
1223
                        p.mNoBuildReason = p.mBuildFile;
1224
                        p.mBuildFile = 3;
1225
                    }
1226
                    else if (p.mBuildReason != null)
1227
                    {
1228
                        p.mNoBuildReason = 0;
1229
                        p.mBuildFile = 2;
1230
                    }
1231
                    else
1232
                    {
1233
                        p.mNoBuildReason = 0;
1234
                        p.mBuildFile = 3;
1235
                    }
1236
                }
1237
 
1238
            }
1239
            else
1240
            {
1241
                //  Escrow
1242
                //  The basic plan is the escrow build order
1243
                mBuildOrder = basicPlan.planCollection;
1244
            }
1245
 
1246
            mLogger.error("Final Plan");
1247
            for (Iterator<PlannedPackage> it = mBuildOrder.iterator(); it.hasNext(); )
1248
            {
1249
                PlannedPackage p = it.next();
7080 dpurdie 1250
                mLogger.error("Plan: {} {} {}", p.mBuildLevel, p.mPkg.mAlias, p.mPkg.mId);
7079 dpurdie 1251
            }
1252
 
1253
 
1254
        }
1255
 
7078 dpurdie 1256
    /** Internal class to contain intermediate results
6914 dpurdie 1257
     */
7078 dpurdie 1258
    class PlanResults {
1259
        int planTime = 0;
7079 dpurdie 1260
        ArrayList<PlannedPackage> planCollection = new ArrayList<PlannedPackage>(); 
7078 dpurdie 1261
    }
1262
 
1263
    /**
1264
     * Process a collection of packages and generate a collection of package plans
1265
     * A package plan is a collection of packages that can be built
1266
     *      The first item in the list is a package that can be built right now
1267
     *      Other items in the list will be packages that can be built now or will need to be built as the result
1268
     *      of a ripple
1269
     *      
1270
     * The method will be called multiple times so that we can evaluate different plans
1271
     * The underling packages will have data and flags that will need to be rested before a calculation      
1272
     *
1273
     * @param name - Name of the plan
1274
     * @param packageCollection - Base collection of packages to plan
1275
     * @param mode - true: Include 'Ripples'
1276
     * 
1277
     * @return
1278
     */
1279
    private PlanResults planCollection(String name, ArrayList<Package> packageCollection, boolean mode)
7070 dpurdie 1280
    {
7094 dpurdie 1281
        Phase phase = new Phase("PlanCollection"); 
7079 dpurdie 1282
        ArrayList<PlannedPackage> ripplePlan = new ArrayList<PlannedPackage>();
7078 dpurdie 1283
        PlanResults results = new PlanResults();
6914 dpurdie 1284
 
7078 dpurdie 1285
        //  Reset flags used in the calculations
1286
        Package.resetProcessed(packageCollection);
6914 dpurdie 1287
 
7078 dpurdie 1288
        //  Exclude all packages that cannot be built and all packages that depend on them
1289
        //      First find packages to be directly excluded
1290
        ArrayList<Package>exclude = new ArrayList<Package>();
7094 dpurdie 1291
        phase.setPhase("Exclude Unbuildable");
7078 dpurdie 1292
        for (Iterator<Package> it = packageCollection.iterator(); it.hasNext(); )
1293
        {
1294
            Package p = it.next();
7081 dpurdie 1295
            if ( p.mBuildFile == -8 ) {
1296
 
1297
                //  Mark SDK or Pegged so that we don't build them, but can build their dependents
1298
                p.mProcessed = true;
1299
                p.mIsProcessed = true;
1300
                mLogger.error("SDK/Peg Exclude: {}", p);
1301
 
1302
            } else if (p.mBuildFile < 0 ) {
7078 dpurdie 1303
                exclude.add(p);
7081 dpurdie 1304
                mLogger.error("Direct Exclude: {}", p);
1305
 
7078 dpurdie 1306
            }
1307
        }
1308
 
7080 dpurdie 1309
        //  Exclude packages that have failed to be built
1310
        //      These are recorded in mBuildExclusionCollection
7094 dpurdie 1311
        phase.setPhase("Exclude Build Failures");
7080 dpurdie 1312
        for (ListIterator<BuildExclusion> it = mBuildExclusionCollection.listIterator(); it.hasNext(); )
1313
        {
1314
            BuildExclusion be = it.next();
7094 dpurdie 1315
            mLogger.error("BE: {}", be);
1316
            if ( !be.isAIndirectError() )
7080 dpurdie 1317
            {
1318
                for (Iterator<Package> it1 = packageCollection.iterator(); it1.hasNext(); )
1319
                {
1320
                    Package p = it1.next();
7081 dpurdie 1321
                    if (p.mBuildFile >= 0 && p.mId == be.mId) {
7080 dpurdie 1322
                        p.mBuildFile  = -3;
7094 dpurdie 1323
                        mLogger.error("AddExclude {}",p);
7080 dpurdie 1324
                        exclude.add(p);
1325
                        break;
1326
                    }
1327
                }
1328
            }
1329
        }
1330
 
1331
 
7078 dpurdie 1332
        //  Process the list of packages to be excluded
1333
        //  Add to the list packages that depend on the excluded package that have not already been excluded
7094 dpurdie 1334
        phase.setPhase("ExcludeAllUsed");
7078 dpurdie 1335
        while( !exclude.isEmpty() )
1336
        {
1337
            Package p = exclude.remove(0);
1338
            p.mProcessed = true;
1339
            p.mIsProcessed = true;
7081 dpurdie 1340
            mLogger.error("planCollection package not buildable {}", p.mName);
7078 dpurdie 1341
 
1342
            for (Iterator<Package> it1 = packageCollection.iterator(); it1.hasNext(); )
1343
            {
1344
                Package pkg = it1.next();
1345
                if (pkg.mProcessed) {
1346
                    continue;
1347
                }
1348
 
7080 dpurdie 1349
                for (Iterator<String> it = pkg.mDependencyCollection.iterator(); it.hasNext(); )
7078 dpurdie 1350
                {
1351
                    String alias = it.next();
1352
                    if ( p.mAlias.compareTo( alias ) == 0 ) {
1353
                        exclude.add(pkg);
1354
                    }
1355
                }
1356
            }
1357
        }
1358
 
1359
        //  Now have a collection of packages with those that we cannot build have been marked
1360
        //  Process the list and determine packages that we can build right now
1361
        //      ie: they have not been excluded, have not already been discovered and do not depend on any package in the collection
1362
        //      Continue to do this until all packages have been extracted
1363
        //      Allocate build levels as we go
1364
        //
7094 dpurdie 1365
        phase.setPhase("Determine Build Order");
6914 dpurdie 1366
        boolean allProcessed = false;
7078 dpurdie 1367
        int buildLevel = 1;
6914 dpurdie 1368
 
1369
        do
1370
        {
7048 dpurdie 1371
            //
1372
            //  Create the mBuildOrder collection
1373
            //      Same for both Daemon and Escrow modes
1374
            //      Will have package build order and build level
6914 dpurdie 1375
            boolean allDependenciesProcessed = true;
1376
            do
1377
            {
1378
                allDependenciesProcessed = true;
7078 dpurdie 1379
                for (Iterator<Package> it = packageCollection.iterator(); it.hasNext(); )
6914 dpurdie 1380
                {
1381
                    Package p = it.next();
7078 dpurdie 1382
 
1383
                    //  Skip packages that have been processed - have determined build order, or cannot build
1384
                    if (p.mIsProcessed) {
1385
                        continue;
1386
                    }
6914 dpurdie 1387
 
7078 dpurdie 1388
                    // Package yet to be processed and
1389
                    //      Daemon mode: Could be built
1390
                    //      Escrow mode: Can be reproduced
1391
                    boolean canBeBuiltNow = true;
1392
                    boolean allDependenciesForThisPackageProcessed = true;
1393
 
1394
                    //  Scan the packages dependencies
1395
                    for ( Iterator<Package> it2 = p.mPackageDependencyCollection.iterator(); it2.hasNext(); )
6914 dpurdie 1396
                    {
7078 dpurdie 1397
                        Package dependency = it2.next();
6914 dpurdie 1398
 
7078 dpurdie 1399
                        if ( !dependency.mProcessed )
6914 dpurdie 1400
                        {
7078 dpurdie 1401
                            //  If all the dependencies have been processed, then we can potentially build this package
1402
                            //  If any of them have not been processed, then cannot calculate canBeBuiltNow until this 
1403
                            //  dependency has been processed
1404
                            allDependenciesForThisPackageProcessed = false;
1405
                            allDependenciesProcessed = false;
6914 dpurdie 1406
                        }
7078 dpurdie 1407
                        else if (  (  mDaemon && (   dependency.mBuildFile == 0 ) ||  (dependency.mBuildFile == buildLevel)) 
1408
                                || ( !mDaemon && ( ( dependency.mBuildFile == 0 ) ||
1409
                                                       ( dependency.mBuildFile == buildLevel &&
1410
                                                       ( !p.haveSameBuildStandards(dependency)  ) ) ) ) )
1411
                        {
1412
                            // Can be built - yet
1413
                            // Daemon mode:
1414
                            //    This processed dependency has not been assigned to a build iteration
1415
                            //    This processed dependency has been assigned to this build iteration
1416
                            // Escrow mode: This package cannot be built now if
1417
                            //    This processed dependency has not been assigned to a build iteration or
1418
                            //    This processed dependency has been assigned to this build iteration, but the 
1419
                            //    build standards of the package and this dependency prevent the package being
1420
                            //    built in this iteration.   
1421
                            canBeBuiltNow = false;
1422
                            mLogger.info("planRelease package cannot be built in this iteration {}", p.mName);
1423
                            break;
1424
                        }
1425
                    }
6914 dpurdie 1426
 
7078 dpurdie 1427
                    //  All dependencies have been processed. May be able to build this package
1428
                    //      Add the package to the build order list
1429
                    //
1430
                    if (allDependenciesForThisPackageProcessed)
1431
                    {
1432
                        p.mProcessed = true;
1433
                        if ( canBeBuiltNow )
6914 dpurdie 1434
                        {
7079 dpurdie 1435
                            results.planCollection.add( new PlannedPackage(p, buildLevel));
7078 dpurdie 1436
                            p.mBuildFile = buildLevel;
1437
                            p.mIsProcessed = true;
1438
                            mLogger.info("planRelease set mBuildFile to {} for package {}",buildLevel, p.mAlias );
6914 dpurdie 1439
                        }
1440
                    }
1441
                }
1442
            } while( !allDependenciesProcessed );
7048 dpurdie 1443
 
7095 dpurdie 1444
            //  Have process all the packages that we can build at the moment
7048 dpurdie 1445
            //      Examine all the packages in the collection to see if there are more that need to be extracted
1446
            //      These will be done at a different build-level
1447
            //  
1448
            allProcessed = true;
7078 dpurdie 1449
            for (Iterator<Package> it = packageCollection.iterator(); it.hasNext(); )
7048 dpurdie 1450
            {
1451
                Package p = it.next();
7078 dpurdie 1452
                if ( !  p.mIsProcessed )
7048 dpurdie 1453
                {
1454
                    // more build files are required
1455
                    allProcessed = false;
1456
                    mLogger.info("planRelease more build files are required for {}", p.mName);
1457
                    break;
1458
                }
1459
            }
6914 dpurdie 1460
 
7078 dpurdie 1461
            buildLevel++;
1462
            if (buildLevel % 500 == 0 ) {
1463
                mLogger.error("planRelease. Too many build levels {}", buildLevel);
7048 dpurdie 1464
            }
1465
        } while( !allProcessed );
7078 dpurdie 1466
 
1467
        //  Now we have a collection packages that we can build right now
1468
        //      The packages currently have a build level set into mBuildFile
1469
        //
1470
        if (mDaemon)
7048 dpurdie 1471
        {
7094 dpurdie 1472
            phase.setPhase("Calc Build Order");
1473
            //
7078 dpurdie 1474
            //  Determine if we have a reason to build anything in this collection of buildable packages
7095 dpurdie 1475
            //      Reset the calculated buildLevel - It will be calculated again
1476
            //      Reset the mProcessed - it will be used to detect  that we have processed a package
1477
 
7079 dpurdie 1478
            ArrayList<PlannedPackage> toBuild = new ArrayList<PlannedPackage>();
1479
            for (Iterator<PlannedPackage> it = results.planCollection.iterator(); it.hasNext(); )
6914 dpurdie 1480
            {
7079 dpurdie 1481
                PlannedPackage p = it.next();
1482
                p.mBuildLevel = 0;
7095 dpurdie 1483
                p.mPkg.mCheckedCircularDependency = false;
7079 dpurdie 1484
 
1485
                if (p.mPkg.mBuildReason != null) {
1486
                    if (mode || ( !mode && p.mPkg.mBuildReason != BuildReason.Ripple ))
7048 dpurdie 1487
                    {
7078 dpurdie 1488
                        toBuild.add(p);
7048 dpurdie 1489
                    }
1490
                }
1491
            }
1492
 
7078 dpurdie 1493
            //  Need (would like to) build stuff
1494
            //  Determine the packages that we need to build and the time that it will take
7070 dpurdie 1495
            //
7078 dpurdie 1496
            if ( !toBuild.isEmpty() )
7048 dpurdie 1497
            {
7096 dpurdie 1498
                //  Locate the packages that we need to build
1499
                //  The build order cannot be correctly determined at this time
1500
                //      Need to add elements to the end of the list while processing
1501
                //      Sum the buildTimes of the packages that we add to the list
7078 dpurdie 1502
                while ( ! toBuild.isEmpty())
7048 dpurdie 1503
                {
7079 dpurdie 1504
                    PlannedPackage pkg = toBuild.remove(0);
7078 dpurdie 1505
 
7095 dpurdie 1506
                    if (pkg.mBuildLevel > 400 ) {
7078 dpurdie 1507
                        mLogger.error("Circular dependency detected - not handled well");
1508
                        break;
7048 dpurdie 1509
                    }
1510
 
7095 dpurdie 1511
                    if ( ! pkg.mPkg.mCheckedCircularDependency )
7048 dpurdie 1512
                    {
7078 dpurdie 1513
                        ripplePlan.add(pkg);
7079 dpurdie 1514
                        results.planTime += pkg.mPkg.mBuildTime;
7095 dpurdie 1515
                        pkg.mPkg.mCheckedCircularDependency = true;
1516
 
1517
                        ArrayList<PlannedPackage> usedBy = usedByPackages(pkg, results.planCollection);
1518
                        toBuild.addAll(usedBy);
7048 dpurdie 1519
                    }
6914 dpurdie 1520
                }
7079 dpurdie 1521
 
7095 dpurdie 1522
                //
7096 dpurdie 1523
                //  Now we have a collection of packages to build ( and only the packages to build)
7095 dpurdie 1524
                //  Need to determine the order in which it can be done and assign build levels
7096 dpurdie 1525
                //  Basically:
1526
                //      Scan the collection looking for any package whose dependencies do not exist in the collection
1527
                //      or they have been allocated a build level.
7095 dpurdie 1528
                //
7096 dpurdie 1529
                //      Need to do this in waves so that we can correctly determine the build level.
1530
                //  
1531
                //
7095 dpurdie 1532
                for (Iterator<PlannedPackage> it = ripplePlan.iterator(); it.hasNext(); )
1533
                {
1534
                    PlannedPackage p = it.next();
1535
                    p.mPkg.mCheckedCircularDependency = true;
1536
                    p.mBuildLevel = 0;
1537
                }
1538
 
1539
                buildLevel = 0;
1540
                boolean more = true;
1541
                do  {
1542
                    ArrayList<PlannedPackage> thisLevel = new ArrayList<PlannedPackage>();
1543
                    for (Iterator<PlannedPackage> it = ripplePlan.iterator(); it.hasNext(); )
1544
                    {
1545
                        PlannedPackage p = it.next();
1546
                        if ( p.mPkg.mCheckedCircularDependency )
1547
                        {
1548
                            boolean found = false;
1549
                            for (Iterator<String> it2 = p.mPkg.mDependencyCollection.iterator(); !found && it2.hasNext(); )
1550
                            {
1551
                                String alias = it2.next();
1552
                                for (Iterator<PlannedPackage> it3 = ripplePlan.iterator(); !found && it3.hasNext(); )
1553
                                {
1554
                                    PlannedPackage pkg = it3.next();
1555
                                    if (pkg.mPkg.mAlias.compareTo( alias ) == 0)
1556
                                    {
1557
                                        found = pkg.mPkg.mCheckedCircularDependency ;
1558
                                        break;
1559
                                    }
1560
                                }
1561
                            }
1562
 
1563
                            if (!found)
1564
                            {
1565
                                //  None of this packages dependencies can be found in the collection of packages to build
1566
                                //  Thus we can build it - at the current level
1567
                                p.mBuildLevel = buildLevel;
1568
                                thisLevel.add(p);
1569
                            }
1570
                        }
1571
                    }
1572
 
1573
                    //  Mark all the packages at this level as have been processed
1574
                    //  Cannot do this while we are determining the packages in the level 
1575
                    for (Iterator<PlannedPackage> it = thisLevel.iterator(); it.hasNext(); )
1576
                    {
1577
                        PlannedPackage p = it.next();
1578
                        p.mPkg.mCheckedCircularDependency = false;
1579
                    }
1580
 
1581
                    more = !thisLevel.isEmpty(); 
1582
                    buildLevel++;
1583
 
1584
                } while (more);
1585
 
7078 dpurdie 1586
                //  Sort the packages by buildOrder and buildTime
7095 dpurdie 1587
                phase.setPhase("Sort Plan");
7079 dpurdie 1588
                Collections.sort(ripplePlan, PlannedPackage.BuildOrderComparitor);
7078 dpurdie 1589
 
7094 dpurdie 1590
                phase.setPhase("Display Build Order");
7093 dpurdie 1591
                mLogger.error("Plan Build {} Time: {}", name, results.planTime);
7079 dpurdie 1592
                for (Iterator<PlannedPackage> it = ripplePlan.iterator(); it.hasNext(); )
7078 dpurdie 1593
                {
7079 dpurdie 1594
                    PlannedPackage p = it.next();
7093 dpurdie 1595
                    mLogger.error("Plan: {} {} t:{}", p.mBuildLevel, p.mPkg.mAlias, p.mPkg.mBuildTime);
7078 dpurdie 1596
                }
6914 dpurdie 1597
            }
7048 dpurdie 1598
 
7078 dpurdie 1599
            //  Daemon mode: Returning calculated plan
1600
            results.planCollection = ripplePlan;
1601
        }
7094 dpurdie 1602
 
1603
        phase.setPhase("EndPlanCollection");
7078 dpurdie 1604
        return results;
1605
    }
6914 dpurdie 1606
 
7078 dpurdie 1607
    /** Generate a plan based on a modified set of packages
1608
     * 
1609
     * @param name      - Name of the plan
1610
     * @param basicPlan - basicPlan
1611
     * @param mode      - True: Include natural ripples and WIPS/RIPPLES that would have been done
1612
     *                          as a part of the basic plan
1613
     * @return
1614
     */
1615
    private PlanResults postRipplePlan(String name, PlanResults basicPlan, boolean mode) {
1616
 
1617
        ArrayList<Package> fullPlanCollection = new ArrayList<Package>(mPackageCollection);
1618
        ArrayList<Package> buildCandidates = new ArrayList<Package>();
1619
        buildCandidates.addAll(mPackageCollectionWip);
1620
        buildCandidates.addAll(mPackageCollectionRipple);
1621
 
1622
        if ( !buildCandidates.isEmpty() )
1623
        {
7048 dpurdie 1624
            for (Iterator<Package> it = buildCandidates.iterator(); it.hasNext(); )
1625
            {
1626
                Package p = it.next();
7078 dpurdie 1627
                if (p.mBuildFile >= 0)
6914 dpurdie 1628
                {
7078 dpurdie 1629
                    if (!mode)
6914 dpurdie 1630
                    {
7078 dpurdie 1631
                        //  Exclude packages that would have been processed in the basicPlan
7079 dpurdie 1632
                        for (Iterator<PlannedPackage> it1 = basicPlan.planCollection.iterator(); it1.hasNext(); )
7048 dpurdie 1633
                        {
7079 dpurdie 1634
                            PlannedPackage pkg = it1.next();
1635
                            if (pkg.mPkg == p)
7078 dpurdie 1636
                            {
7093 dpurdie 1637
                                mLogger.error("Test Plan without {}", p.mAlias);
7078 dpurdie 1638
                                continue;
1639
                            }
7048 dpurdie 1640
                        }
6914 dpurdie 1641
                    }
7078 dpurdie 1642
 
7093 dpurdie 1643
                    mLogger.error("Test Plan with {}", p.mAlias);
7078 dpurdie 1644
                    Package foundInRelease = mReleaseManager.findPackage(p.mAlias, fullPlanCollection);
1645
                    int foundIndex = mReleaseManager.findPackageLastIndex;
1646
                    if (foundInRelease == ReleaseManager.NULL_PACKAGE)
1647
                    {
1648
                        fullPlanCollection.add(p);
1649
                    }
1650
                    else
1651
                    {
1652
                        fullPlanCollection.set(foundIndex, p);
1653
                    }
1654
 
1655
                    Package.resetCircularDependency (fullPlanCollection);
1656
                    if ( p.hasCircularDependency( fullPlanCollection ) )
1657
                    {
1658
                        mLogger.info("planRelease circular dependency detected {}", p.mAlias);
1659
 
1660
                        //  Force this package to be marked as having a circular dependency - even if its been excluded
1661
                        p.mBuildFile = 0;
1662
 
7079 dpurdie 1663
                        // Exclude the package
7078 dpurdie 1664
                        // max 50 chars
7079 dpurdie 1665
                        rippleBuildExclude(p, p.mId, "Package has circular dependency", -6);
7078 dpurdie 1666
                    }
6914 dpurdie 1667
                }
1668
            }
7048 dpurdie 1669
 
7078 dpurdie 1670
        }
1671
        return planCollection(name, fullPlanCollection, mode);
1672
    }
1673
 
7079 dpurdie 1674
    /** 
1675
     *  Calculate a collection of packages that actively use the named package
1676
     *  A consumer package does NOT use the named package,
1677
     *      If the consumer is an SDK or is Pegged
1678
     *      If the consumer is marked as advisoryRipple
1679
     *      If the consumer cannot be built
1680
     *   
1681
     * @param pkg - Package to process
1682
     * @param planCollection - collection of packages to scan
1683
     *
1684
     * @return A collection of packages that actively 'use' the specified package
1685
 
7078 dpurdie 1686
     */
7095 dpurdie 1687
    private ArrayList<PlannedPackage> usedByPackages(PlannedPackage pkg, ArrayList<PlannedPackage> planCollection) {
7078 dpurdie 1688
 
7079 dpurdie 1689
        ArrayList<PlannedPackage> usedBy = new ArrayList<PlannedPackage>();
7078 dpurdie 1690
 
7079 dpurdie 1691
        for (Iterator<PlannedPackage> it = planCollection.iterator(); it.hasNext(); )
7078 dpurdie 1692
        {
7079 dpurdie 1693
            PlannedPackage p = it.next();
7070 dpurdie 1694
 
7079 dpurdie 1695
            //  Is this package 'actively used' in the current build
1696
            if (p.mPkg.mBuildFile >= 0)
7070 dpurdie 1697
            {
7079 dpurdie 1698
                for (Iterator<String> it2 = p.mPkg.mDependencyCollection.iterator(); it2.hasNext(); )
7070 dpurdie 1699
                {
7079 dpurdie 1700
                    String alias = it2.next();
1701
                    if (  pkg.mPkg.mAlias.compareTo( alias ) == 0  ) {
1702
                        //  Have found a consumer of 'pkg'
1703
                        usedBy.add(p);
1704
                        break;
7070 dpurdie 1705
                    }
1706
                }
1707
            }
7048 dpurdie 1708
        }
7079 dpurdie 1709
 
1710
        return usedBy;
7048 dpurdie 1711
    }
7079 dpurdie 1712
 
7048 dpurdie 1713
    /** 
7079 dpurdie 1714
     *  Calculate a collection of packages that use the named package
7048 dpurdie 1715
     *   
1716
     * @param pkg - Package to process
7070 dpurdie 1717
     * @param pkgCollection - collection of packages to scan
7078 dpurdie 1718
     *
7048 dpurdie 1719
     * @return A collection of packages that actively 'use' the specified package
1720
 
1721
     */
7079 dpurdie 1722
    private ArrayList<Package> usedByAnyPackages(Package pkg, ArrayList<Package> pkgCollection) {
7048 dpurdie 1723
 
1724
        ArrayList<Package> usedBy = new ArrayList<Package>();
1725
 
7070 dpurdie 1726
        for (Iterator<Package> it = pkgCollection.iterator(); it.hasNext(); )
7048 dpurdie 1727
        {
1728
            Package p = it.next();
1729
 
7079 dpurdie 1730
            for (Iterator<String> it2 = p.mDependencyCollection.iterator(); it2.hasNext(); )
7048 dpurdie 1731
            {
7079 dpurdie 1732
                String alias = it2.next();
1733
                if (  pkg.mAlias.compareTo( alias ) == 0  ) {
1734
                    usedBy.add(p);
1735
                    break;
6914 dpurdie 1736
                }
1737
            }
7048 dpurdie 1738
        }
1739
 
1740
        return usedBy;
6914 dpurdie 1741
    }
1742
 
1743
    /** Determine if a given PVID is a member of the current release.
1744
     *  Used to determine if a package dependency is out of date
1745
     * 
1746
     * @param dpvId
1747
     * @return true - specified pvid is a full member of the Release
1748
     */
1749
    private boolean isInRelease(Integer dpvId) {
1750
 
7048 dpurdie 1751
        boolean inRelease = false;
6914 dpurdie 1752
 
7048 dpurdie 1753
        for ( Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
6914 dpurdie 1754
        {
7048 dpurdie 1755
            Package p = it.next();
6914 dpurdie 1756
 
7048 dpurdie 1757
            if ( p.mId == dpvId )
6914 dpurdie 1758
            {
7048 dpurdie 1759
                inRelease = ! p.mIsNotReleased;
6914 dpurdie 1760
                break;
1761
            }
1762
        }
7048 dpurdie 1763
        return inRelease;
6914 dpurdie 1764
    }
1765
 
1766
 
7081 dpurdie 1767
    /** Reports what change in build exceptions happens as part of planRelease
1768
     * 
1769
     *  There are three types of exceptions
1770
     *      PlanError - These may be removed, if the error was not seen in the last plan
1771
     *      BuildErrors - These we persist
1772
     *      IndirectErrors - Packages that depend on a Plan or Build errors
6914 dpurdie 1773
     */
1774
    public void reportChange() throws SQLException, Exception
1775
    {
1776
        int counter = 0;
1777
        for (Iterator<BuildExclusion> it = mBuildExclusionCollection.iterator(); it.hasNext(); )
1778
        {
1779
            BuildExclusion buildExclusion = it.next();
1780
 
7081 dpurdie 1781
            //  Skip 'Processed' entries
1782
            //  These will be the result of a PlanError that we have seen again
1783
            //
1784
            //  PlanErrors - Add/Remove as detected
1785
            //  BuildErrors - Persist. 
1786
            //  IndirectErrors - Add/Remove as detected
1787
            //  
1788
 
1789
            if ( !buildExclusion.isABuildError() )
6914 dpurdie 1790
            {
7081 dpurdie 1791
                if ( !buildExclusion.isProcessed() )
1792
                {
1793
                    if (buildExclusion.isImported() && !buildExclusion.isARootCause() ) {
1794
                        // Remove from the exclusion list
1795
                        buildExclusion.includeToBuild(mReleaseManager, mBaseline);
1796
                        mLogger.error("reportChange remove unused exclusion: {}", buildExclusion );
1797
                    } else {
1798
                        // Exclude and notify
1799
                        buildExclusion.excludeFromBuild(mReleaseManager, mBaseline);
1800
                        buildExclusion.email(this, mPackageCollectionAll);
1801
                        counter++;
1802
                    }
7032 dpurdie 1803
                }
6914 dpurdie 1804
            }
1805
        }
7048 dpurdie 1806
        mLogger.error("reportChange exclusion count: {}", counter);
6914 dpurdie 1807
    }
1808
 
1809
    /**reports the build plan
1810
     */
1811
    public void reportPlan() throws SQLException, Exception
1812
    {
1813
        mReleaseManager.reportPlan(mRtagId, mBuildOrder);
1814
    }
1815
 
1816
    /**Returns the first build file from the collection
1817
     * The build file will be flagged as empty if none exists
1818
     */
1819
    public BuildFile getFirstBuildFileContent()
1820
    {
1821
        mLogger.debug("getFirstBuildFileContent");
1822
 
1823
        mBuildIndex = -1;
1824
        return getNextBuildFileContent();
1825
    }
1826
 
1827
    /**Returns next build file from the collection
1828
     * The build file will be flagged as empty if none exists
1829
     */
1830
    public BuildFile getNextBuildFileContent()
1831
    {
1832
        mLogger.debug("getNextBuildFileContent");
1833
        BuildFile retVal = null;
1834
 
1835
        try
1836
        {
1837
            mBuildIndex++;
1838
            retVal = mBuildCollection.get( mBuildIndex );
1839
        }
1840
        catch( IndexOutOfBoundsException e )
1841
        {
1842
            // Ignore exception. retVal is still null.
1843
        }
1844
 
1845
        if (retVal == null)
1846
        {
1847
            retVal = new BuildFile();
1848
        }
1849
 
7048 dpurdie 1850
        mLogger.debug("getNextBuildFileContent returned {}", retVal.state.name() );
6914 dpurdie 1851
        return retVal;
1852
    }
1853
 
1854
 
1855
    /**collects meta data associated with the baseline
1856
     * this is sufficient to send an indefinite pause email notification 
1857
     *  
1858
     * Escrow: Used once to collect information about the SBOM and associated Release 
1859
     *         mBaseline is an SBOMID 
1860
     * Daemon: Used each build cycle to refresh the information 
1861
     *          mBaseline is an RTAGID
1862
     */
1863
    public void collectMetaData() throws SQLException, Exception
1864
    {
1865
        Phase phase = new Phase("cmd");
7048 dpurdie 1866
        mLogger.debug("collectMetaData mDaemon {}", mDaemon);
6914 dpurdie 1867
 
1868
        try
1869
        {
1870
            phase.setPhase("connect");
1871
            mReleaseManager.connect();
1872
 
1873
            if (! mDaemon)
1874
            {
1875
                mSbomId = mBaseline;
1876
                phase.setPhase("queryRtagIdForSbom");
1877
                mRtagId = mReleaseManager.queryRtagIdForSbom(mBaseline);
1878
                if (mRtagId == 0)
1879
                {
7033 dpurdie 1880
                    mLogger.error("SBOM does not have a matching Release. Cannot be used as a base for an Escrow"); 
6914 dpurdie 1881
                    throw new Exception("rtagIdForSbom show stopper. SBOM does not have an associated Release");
1882
                }
1883
            }
1884
 
1885
            phase.setPhase("queryReleaseConfig");
1886
            mReleaseManager.queryReleaseConfig(mRtagId);
1887
 
1888
            if (mDaemon)
1889
            {
1890
                phase.setPhase("queryMailServer");
1891
                setMailServer(mReleaseManager.queryMailServer());
1892
                phase.setPhase("queryMailSender");
1893
                setMailSender(mReleaseManager.queryMailSender());
1894
                phase.setPhase("queryGlobalAddresses");
1895
                setMailGlobalTarget(mReleaseManager.queryGlobalAddresses());
1896
                phase.setPhase("queryProjectEmail");
1897
                mMailGlobalCollection = mReleaseManager.queryProjectEmail(mBaseline);
1898
                phase.setPhase("mMailGlobalTarget");
1899
                mMailGlobalCollection.add(0,getMailGlobalTarget());
1900
            }
1901
            phase.setPhase("queryBaselineName");
1902
            mBaselineName = mReleaseManager.queryBaselineName(mBaseline);
1903
            phase.setPhase("Done");
1904
        }
1905
        finally
1906
        {
1907
            // this block is executed regardless of what happens in the try block
1908
            // even if an exception is thrown
1909
            // ensure disconnect
1910
            mReleaseManager.disconnect();
1911
        }
1912
    }
1913
 
1914
    /**
1915
     * Find Package by package alias
7070 dpurdie 1916
     * Searches the released package collection
6914 dpurdie 1917
     * @param   alias               - alias of package to locate
1918
     * @return  Package with the matching mAlias or NULL_PACKAGE if no package has the mAlias
1919
     */
1920
    public Package findPackage(String alias)
1921
    {
1922
        mLogger.debug("findPackage");
1923
 
1924
        Package retVal = mReleaseManager.findPackage(alias, mPackageCollection);
1925
 
7048 dpurdie 1926
        mLogger.info("findPackage returned {}", retVal.mName);
6914 dpurdie 1927
        return retVal;
1928
    }
7070 dpurdie 1929
 
6914 dpurdie 1930
    /**
7079 dpurdie 1931
     * Sets the mBuildFile to the specified value for the package
1932
     * Does not handle dependent packages - this will be done later  
7048 dpurdie 1933
     *  
7032 dpurdie 1934
     * @param p             The package being excluded 
1935
     * @param rootPvId      The PVID of the package that is causing the exclusion. Null or -ve values are special
1936
     *                      This package is the root cause, -2: Excluded by Ripple Stop 
1937
     * @param rootCause     Text message. Max 50 characters imposed by RM database 
7079 dpurdie 1938
     * @param reason        New value for mBuildFile
6914 dpurdie 1939
     */
7079 dpurdie 1940
    private void rippleBuildExclude(Package p, int rootPvId, String rootCause, int reason )
6914 dpurdie 1941
    {
1942
        mLogger.debug("rippleBuildExclude");
7048 dpurdie 1943
 
7096 dpurdie 1944
        if ( p.mBuildFile >= 0 )
6914 dpurdie 1945
        {
7079 dpurdie 1946
            p.mBuildFile = reason;
7096 dpurdie 1947
            mLogger.info("rippleBuildExclude set mBuildFile to {} for package {}", reason, p.mAlias );
7079 dpurdie 1948
 
1949
            //  Scan the complete collection looking for a matching item
7081 dpurdie 1950
            //  If found then assume that this error is a a PlanError that is still present
1951
            //      Mark it as Processed to indicate that its still present
7079 dpurdie 1952
            //  If found, process it, else add it (unprocessed)
1953
            boolean found = false;
1954
            for (Iterator<BuildExclusion> it = mBuildExclusionCollection.iterator(); it.hasNext(); )
1955
            {
1956
                BuildExclusion buildExclusion = it.next();
6914 dpurdie 1957
 
7079 dpurdie 1958
                if ( buildExclusion.compare(p.mId, rootPvId, rootCause))
1959
                {
1960
                    buildExclusion.setProcessed();
1961
                    found = true;
1962
                    break;
1963
                }
6914 dpurdie 1964
            }
7079 dpurdie 1965
 
1966
            if (!found)
6914 dpurdie 1967
            {
7081 dpurdie 1968
                // Entry not found in the mBuildExclusionCollection. Its a new error
1969
                // 
7079 dpurdie 1970
                // Mark all occurrences for this package as processed
1971
                // These will be superseded by a new build exclusion entry
6914 dpurdie 1972
                for (Iterator<BuildExclusion> it = mBuildExclusionCollection.iterator(); it.hasNext(); )
1973
                {
1974
                    BuildExclusion buildExclusion = it.next();
1975
 
7079 dpurdie 1976
                    if ( buildExclusion.compare(p.mId))
6914 dpurdie 1977
                    {
7032 dpurdie 1978
                        buildExclusion.setProcessed();
6914 dpurdie 1979
                    }
1980
                }
1981
 
7081 dpurdie 1982
                //  Add the new build exclusion to a list
7079 dpurdie 1983
                BuildExclusion buildExclusion = new BuildExclusion(p.mId, rootPvId, rootCause, p.mTestBuildInstruction);
1984
                mBuildExclusionCollection.add(buildExclusion);
6914 dpurdie 1985
            }
7079 dpurdie 1986
        }
6914 dpurdie 1987
 
7079 dpurdie 1988
        mLogger.info("rippleBuildExclude set {} {}", p.mAlias, p.mBuildFile);
6914 dpurdie 1989
    }
1990
 
1991
    /**Simple XML string escaping
1992
     * 
1993
     * @param xml		- String to escape
1994
     * @return		- A copy of the string with XML-unfriendly characters escaped 
1995
     */
1996
    public static String escapeXml( String xml )
1997
    {
1998
        xml = xml.replaceAll("&", "&amp;");
1999
        xml = xml.replaceAll("<", "&lt;");
2000
        xml = xml.replaceAll(">", "&gt;");
2001
        xml = xml.replaceAll("\"","&quot;");
2002
        xml = xml.replaceAll("'", "&apos;");
2003
        xml = xml.replaceAll("\\$", "\\$\\$");
2004
 
2005
        return xml;
2006
    }
2007
 
2008
    /** Quote a string or a string pair
2009
     *  If two strings are provided, then they will be joined with a comma.
2010
     *   
2011
     * @param text		- First string to quote
2012
     * @param text2		- Optional, second string
2013
     * @return A string of the form 'text','text2'
2014
     */
2015
    public static String quoteString(String text, String text2)
2016
    {
2017
        String result;
2018
        result =  "\'" + text + "\'";
2019
        if (text2 != null )
2020
        {
2021
            result +=  ",\'" + text2 + "\'";  
2022
        }
2023
        return result;
2024
    }
2025
 
2026
    /** Generate build file information
2027
     * 
2028
     */
2029
    private void generateBuildFiles() 
2030
    {
2031
 
2032
        // persist the build files
2033
        boolean allProcessed = false;
2034
        int buildFile = 1;
2035
        StringBuilder rawData = new StringBuilder();
2036
        StringBuilder setUp = new StringBuilder();
2037
 
2038
        mLogger.debug("generateBuildFiles");
2039
 
2040
        if ( mDaemon )
2041
        {
2042
            // all interesting packages in daemon mode match the following filter
2043
            buildFile = 3;
2044
        }
2045
 
2046
        //-----------------------------------------------------------------------
2047
        //    Generate the build file
2048
        do
2049
        {
2050
            BuildFile buildEntry = new  BuildFile();
2051
            buildEntry.state = BuildFileState.Dummy;
2052
            XmlBuilder xml = generateBuildFileHeader();
2053
 
2054
 
2055
            //	Generate properties for each package in this build level or lower build levels
2056
            //	The properties link the packageAlias to the PackageName and PackageVersion
2057
            //
2058
            //	[DEVI 54816] In escrow mode all unreproducible packages are included 
7070 dpurdie 2059
            for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
6914 dpurdie 2060
            {
2061
                Package p = it.next();
2062
 
2063
                if ( ( ( p.mBuildFile > 0 ) && ( p.mBuildFile <= buildFile ) ) || ( !mDaemon && p.mBuildFile == -2 ) )
2064
                {
2065
                    xml.addProperty(p.mAlias, p.mName + " " + p.mVersion + p.mExtension);
2066
                }
2067
            }
2068
 
2069
            //	UTF Support
2070
            //	Insert additional info into the build file to provide extra checking
2071
            //
7048 dpurdie 2072
            if ( ! mReleaseManager.mUseDatabase )
6914 dpurdie 2073
            {
2074
                // UTF Support
2075
                // Insert per-package planning information
2076
                //
7070 dpurdie 2077
                xml.addComment("mPackageCollection");
2078
                for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
6914 dpurdie 2079
                {
2080
                    Package p = it.next();
2081
                    generatePackageInfo(xml, p);
2082
                }
2083
 
7070 dpurdie 2084
                xml.addComment("mPackageCollectionWip");
2085
                for (Iterator<Package> it = mPackageCollectionWip.iterator(); it.hasNext(); )
2086
                {
2087
                    Package p = it.next();
2088
                    if (p.mIsNotReleased )
2089
                        generatePackageInfo(xml, p);
2090
                }
2091
 
2092
                xml.addComment("mPackageCollectionTest");
2093
                for (Iterator<Package> it = mPackageCollectionTest.iterator(); it.hasNext(); )
2094
                {
2095
                    Package p = it.next();
2096
                    if (p.mIsNotReleased )
2097
                        generatePackageInfo(xml, p);
2098
                }
2099
 
2100
                xml.addComment("mPackageCollectionTestRipple");
2101
                for (Iterator<Package> it = mPackageCollectionRipple.iterator(); it.hasNext(); )
2102
                {
2103
                    Package p = it.next();
2104
                    if (p.mIsNotReleased )
2105
                        generatePackageInfo(xml, p);
2106
                }
2107
 
6914 dpurdie 2108
                // UTF Support
2109
                // Insert build exclusion information
2110
                xml.addComment("mBuildExclusionCollection");
2111
                for (Iterator<BuildExclusion> it = mBuildExclusionCollection.iterator(); it.hasNext(); )
2112
                {
2113
                    BuildExclusion buildExclusion = it.next();
2114
                    {
7081 dpurdie 2115
                        xml.addComment(buildExclusion.toString());
6914 dpurdie 2116
                    }
2117
                }
7080 dpurdie 2118
            }
2119
 
2120
            // UTF Support (Also while trailing the changes)
2121
            // Insert build Plan
2122
            if (mDaemon)
2123
            {
2124
                xml.addComment("mBuildOrder");
2125
                for (Iterator<PlannedPackage> it = mBuildOrder.iterator(); it.hasNext(); )
6914 dpurdie 2126
                {
7080 dpurdie 2127
                    PlannedPackage p = it.next();
2128
                    String comment =
2129
                            "pvid="+ p.mPkg.mId +
2130
                            " order=" + p.mBuildLevel +
2131
                            " time=" + p.mPkg.mBuildTime +
2132
                            " name=\"" + p.mPkg.mAlias + "\"";
2133
                    xml.addComment(comment);
6914 dpurdie 2134
                }
2135
            }
2136
 
2137
            //  Generate Taskdef information
2138
            generateTaskdef(xml);
2139
 
2140
            //
2141
            //  Insert known Machine Information
2142
            //  Escrow usage: 
2143
            //      Map machType to machClass
2144
            //  Also serves as a snapshot of the required build configuration
2145
            //  ie: machine types and buildfilters
2146
            //
2147
            if (!mDaemon)
2148
            {
2149
                generateMachineInfo(xml, null);
2150
            }
2151
 
2152
            //
2153
            //	Generate target rules for each package to be built within the current build file
2154
            //
2155
            boolean daemonHasTarget = false;
2156
 
7048 dpurdie 2157
            for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
6914 dpurdie 2158
            {
2159
                Package p = it.next();
2160
 
2161
                if ( p.mBuildFile > 0 && p.mBuildFile <= buildFile )
2162
                {
2163
                    generateTarget(xml, buildEntry, p, buildFile);
2164
 
2165
                    if ( p.mBuildFile == 1 )
2166
                    {
2167
                        daemonHasTarget = true;
2168
 
2169
                        // Retain information about the target package
2170
                        buildEntry.mPkgId = p.mPid;
2171
                        buildEntry.mPvId = p.mId;
2172
                    }
2173
                }
2174
 
2175
                //	Generate escrow extraction commands
2176
                //	Only done on the first pass though the packages
2177
                //
2178
                if ( !mDaemon && buildFile == 1 )
2179
                {
2180
                    setUp.append("jats jats_vcsrelease -extractfiles"
2181
                                + " \"-label=" + p.mVcsTag + "\""
2182
                                + " \"-view=" + p.mAlias + "\""
2183
                                + " -root=. -noprefix"
2184
                                + mlf );
2185
                }
2186
 
2187
                //	Generate escrow raw CSV data
2188
                //	Note: I don't think this data is used at all
2189
 
2190
                if ( !mDaemon && ( p.mBuildFile == buildFile))
2191
                {
2192
                    StringAppender machines = new StringAppender(",");
2193
                    for (Iterator<BuildStandard> it1 = p.mBuildStandardCollection.iterator(); it1.hasNext();)
2194
                    {
2195
                        BuildStandard bs = it1.next();
2196
                        machines.append(bs.mMachClass);
2197
                    }
2198
 
2199
                    rawData.append(p.mAlias + "," +
2200
                                    buildFile + "," +
2201
                                    machines +
2202
                                    mlf);
2203
                }
2204
            }
2205
 
2206
            if ( mDaemon && !daemonHasTarget )
2207
            {
2208
                // must have AbtTestPath, AbtSetUp, AbtTearDown, and AbtPublish targets
2209
                XmlBuilder target = xml.addNewElement("target");
2210
                target.addAttribute("name", "AbtTestPath");
2211
 
2212
                target = xml.addNewElement("target");
2213
                target.addAttribute("name", "AbtSetUp");
2214
 
2215
                target = xml.addNewElement("target");
2216
                target.addAttribute("name", "AbtTearDown");
2217
 
2218
                target = xml.addNewElement("target");
2219
                target.addAttribute("name", "AbtPublish");
2220
            }
2221
 
2222
            generateDefaultTarget( xml, buildFile);
2223
 
2224
            //	Convert the Xml structure into text and save it in the build file
2225
            //	Add this build file to the mBuildCollection
2226
            buildEntry.content = mXmlHeader + xml.toString();
2227
            mBuildCollection.add(buildEntry);
2228
 
2229
            // are more build files required
2230
            allProcessed = true;
2231
 
2232
            if (!mDaemon)
2233
            {
2234
                // this is escrow mode centric
7048 dpurdie 2235
                for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
6914 dpurdie 2236
                {
2237
                    Package p = it.next();
2238
 
2239
                    if ( p.mBuildFile > buildFile )
2240
                    {
2241
                        // more build files are required
2242
                        allProcessed = false;
7048 dpurdie 2243
                        mLogger.info("planRelease reiterating package has no build requirement {} {} {}",p.mName, p.mBuildFile,  buildFile);
6914 dpurdie 2244
                        break;
2245
                    }
2246
                } 
2247
 
2248
                buildFile++;
2249
            }
2250
 
2251
        } while( !allProcessed );
2252
 
2253
        //	Save additional escrow data
2254
        if ( !mDaemon )
2255
        {
2256
            mEscrowSetup = setUp.toString();
2257
            mEscrowRawData = rawData.toString();
2258
        }
2259
    }
2260
 
2261
    /**returns a build file header for the mBaseline
2262
     */
2263
    private XmlBuilder generateBuildFileHeader()
2264
    {
2265
        mLogger.debug("generateBuildFileHeader");
2266
        XmlBuilder element = new XmlBuilder("project");
2267
 
2268
        element.addAttribute("name", "mass");
2269
        element.addAttribute("default", "full");
2270
        element.addAttribute("basedir", ".");
2271
 
2272
        if ( mDaemon )
2273
        {
2274
            element.addProperty("abt_mail_server", getMailServer());
2275
            element.addProperty("abt_mail_sender", getMailSender()); 
2276
            element.addProperty("abt_rtag_id", mBaseline);
2277
            element.addProperty("abt_daemon", mReleaseManager.currentTimeMillis());
2278
            element.makePropertyTag("abt_packagetarball", true);
2279
            element.makePropertyTag("abt_usetestarchive", !ReleaseManager.getUseMutex());
2280
 
7048 dpurdie 2281
            for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
6914 dpurdie 2282
            {
2283
                Package p = it.next();
2284
 
2285
                if ( p.mBuildFile == 1 )
2286
                {
2287
                    element.addProperty("abt_package_name", p.mName);
2288
                    element.addProperty("abt_package_version", p.mVersion + p.mExtension);
2289
                    element.addProperty("abt_package_extension", p.mExtension);
2290
                    element.addProperty("abt_package_location", getBuildLocation(p));
2291
                    element.addProperty("abt_package_ownerlist", p.emailInfoNonAntTask(this));
2292
                    element.addProperty("abt_package_build_info", buildInfoText(p));
2293
 
2294
                    // depends in the form 'cs','25.1.0000.cr';'Dinkumware_STL','1.0.0.cots'
2295
                    StringAppender depends = new StringAppender(";");
2296
 
2297
                    for (Iterator<Package> it3 = p.mPackageDependencyCollection.iterator(); it3.hasNext(); )
2298
                    {
2299
                        Package depend = it3.next();
2300
                        depends.append( quoteString(depend.mName, depend.mVersion + depend.mExtension) );
2301
                    }
2302
 
2303
                    element.addProperty("abt_package_depends", depends.toString());
2304
                    element.addProperty("abt_is_ripple", p.mDirectlyPlanned ? "0" : "1");
2305
                    element.addProperty("abt_build_reason", p.mBuildReason.toString());
2306
                    element.addProperty("abt_package_version_id", p.mId);
2307
                    element.addProperty("abt_does_not_require_source_control_interaction", ! p.mRequiresSourceControlInteraction ? "true" : "false" );
2308
                    element.addProperty("abt_test_build_instruction", p.mTestBuildInstruction);
2309
                }
2310
            }
2311
        }
2312
        else
2313
        {
2314
            //    Escrow Mode
2315
            element.addProperty("abt_rtag_id", mRtagId);
2316
            element.addProperty("abt_sbom_id", mSbomId);
2317
        }
2318
 
2319
        element.addProperty("abt_release", escapeXml(mBaselineName));
2320
        element.addProperty("abt_buildtool_version", mReleaseManager.getMajorVersionNumber() );
2321
 
2322
        element.addNewElement("condition")
2323
        .addAttribute("property", "abt_family")
2324
        .addAttribute("value", "windows")
2325
        .addNewElement("os")
2326
        .addAttribute("family", "windows");
2327
        element.addProperty("abt_family", "unix");
2328
 
2329
        return element;
2330
    }
2331
 
2332
    /** Add task def XML items taskdef for the abt ant task
2333
     * @param xml 	- xml item to extend
2334
     */
2335
    private void generateTaskdef(XmlBuilder xml)
2336
    {
2337
        mLogger.debug("generateTaskdef");
2338
        xml.addNewElement("taskdef")
2339
        .addAttribute("name", "abt")
2340
        .addAttribute("classname", "com.erggroup.buildtool.abt.ABT");
2341
 
2342
        xml.addNewElement("taskdef")
2343
        .addAttribute("name", "abtdata")
2344
        .addAttribute("classname", "com.erggroup.buildtool.abt.ABTData");
2345
    }
2346
 
2347
    /** returns the command abtdata items
2348
     *  <br>Common machine information
2349
     *  <br>Common email address
2350
     *  
2351
     *  
2352
     *  @param	xml - Xml element to extend   
2353
     *  @param    p - Package (May be null)
2354
     */
2355
    private void generateMachineInfo(XmlBuilder xml, Package p)
2356
    {
2357
        XmlBuilder element = xml.addNewElement("abtdata");
2358
        element.addAttribute("id", "global-abt-data");
2359
 
2360
        //
2361
        //  Iterate over all the machines and create a nice entry
2362
        //
2363
        for (Iterator<ReleaseConfig> it = mReleaseManager.mReleaseConfigCollection.mReleaseConfig.iterator(); it.hasNext(); )
2364
        {
2365
            ReleaseConfig rc = it.next();
2366
            element.addElement(rc.getMachineEntry());
2367
        }
2368
 
2369
        //
2370
        //  Now the email information
2371
        //
2372
        if ( p != null)
2373
        {
2374
            p.emailInfo(this, element);
2375
        }
2376
    }
2377
 
2378
    /** Generate package information
2379
     *  Only when running unit tests
2380
     *  
2381
     * @param xml	- An xml element to append data to
2382
     * @param p	- Package to process
2383
     */
2384
    private void generatePackageInfo (XmlBuilder xml, Package p)
2385
    {
7071 dpurdie 2386
        StringBuilder comment = new StringBuilder();
2387
        StringBuilder deps = new StringBuilder();
2388
 
7051 dpurdie 2389
        String joiner = "";
2390
        for (Iterator<String> it2 = p.mDependencyCollection.iterator(); it2.hasNext(); )
2391
        {
2392
            String alias = it2.next();
7071 dpurdie 2393
            deps.append(joiner).append(alias); 
7051 dpurdie 2394
            joiner = ",";
2395
        }
2396
 
7071 dpurdie 2397
        comment.append("pvid=").append(p.mId);
2398
        comment.append(" name=").append('"').append(p.mAlias).append('"');
2399
        comment.append(" reason=").append(p.mNoBuildReason);
2400
        comment.append(" buildFile=").append(p.mBuildFile);
2401
        comment.append(" directlyPlanned=").append(p.mDirectlyPlanned);
2402
        comment.append(" indirectlyPlanned=").append(p.mIndirectlyPlanned);
2403
        comment.append(" depends=[").append(deps).append("]");
6914 dpurdie 2404
 
7071 dpurdie 2405
        xml.addComment(comment.toString());            
6914 dpurdie 2406
    }
2407
 
2408
    /**returns an ant target for the passed Package
2409
     * in daemon mode:
2410
     *  packages are categorized with one of three mBuildFile values:
2411
     *   1 the package to be built by this buildfile
2412
     *   2 the packages with a future build requirement
2413
     *   3 the packages with no build requirement
2414
     *  the returned target depends on this categorization and will have
2415
     *   1 full abt info
2416
     *   2 full dependency info to determine future build ordering but no abt info (will not build this package)
2417
     *   3 only a name attribute (will not build this package)
2418
     * in escrow mode:
2419
     *  if the passed Package's mBuildFile is different (less than) the passed build file,
2420
     *  the returned target have only a name attribute (will not build this package)
2421
     *   
2422
     * @param xml - Xml element to extend
2423
     * @param buildEntry - Record build type (dummy/generic/not generic)
2424
     * @param p - Package to process
2425
     * @param buildFile - buildfile level being processed
2426
     */
2427
    private void generateTarget(XmlBuilder xml, BuildFile buildEntry, Package p, int buildFile)
2428
    {
2429
        mLogger.debug("generateTarget");
2430
 
2431
        //---------------------------------------------------------------------
2432
        //  Generate the AbtData - common machine and email information
2433
        //  Only used by the daemon builds
2434
        //
2435
        if ( mDaemon && p.mBuildFile == 1 )
2436
        {
2437
            generateMachineInfo(xml, p );
2438
        }
2439
 
2440
        //-------------------------------------------------------------------------
2441
        //  Generate the <target name=... /> construct
2442
        //  There are two types
2443
        //      1) Simple dummy place holder. Just has the PackageName.PackageExt
2444
        //      2) Full target. Has all information to build the package including
2445
        //              AbtSetUp, AbtTearDown and AbtPublish targets
2446
        //
2447
 
2448
        if ( !mDaemon && ( p.mBuildFile < buildFile ) )
2449
        {
2450
            XmlBuilder target = xml.addNewElement("target");
2451
            target.addAttribute("name", p.mAlias);
2452
        }
2453
        else
2454
        {
2455
            if (!mDaemon) 
2456
            {
2457
                //  Escrow Only:
2458
                //  Generate the 'wrapper' target
2459
                //  This is used to ensure that the required dependencies have been built - I think
2460
                //
2461
                StringAppender dependList = new StringAppender(",");
2462
                if ( !p.mPackageDependencyCollection.isEmpty() )
2463
                {
2464
                    for (Iterator<Package> it = p.mPackageDependencyCollection.iterator(); it.hasNext(); )
2465
                    {
2466
                        Package dependency = it.next();
2467
                        if ( !mDaemon && dependency.mBuildFile == -2 )
2468
                        {
2469
                            // ignore targets which build in foreign environments in escrow mode
2470
                            continue;
2471
                        }
2472
                        dependList.append(dependency.mAlias);
2473
                    }
2474
                }
2475
 
2476
                XmlBuilder target = xml.addNewElement("target").isExpanded();
2477
                target.addAttribute("name", p.mAlias + ".wrap");
2478
 
2479
                if (dependList.length() > 0)
2480
                {
2481
                    target.addAttribute("depends", dependList.toString() );
2482
                }
2483
 
2484
                if ( !mDaemon )
2485
                {
2486
                    boolean hasDependenciesBuiltInThisIteration = false;
2487
                    if ( ( !p.mPackageDependencyCollection.isEmpty()) )
2488
                    {
2489
                        for (Iterator<Package> it = p.mPackageDependencyCollection.iterator(); it.hasNext(); )
2490
                        {
2491
                            Package dependency = it.next();
2492
 
2493
                            if ( dependency.mBuildFile == buildFile )
2494
                            {
2495
                                hasDependenciesBuiltInThisIteration = true;
2496
                                break;
2497
                            }
2498
                        }
2499
                    }
2500
 
2501
                    if ( hasDependenciesBuiltInThisIteration )
2502
                    {
2503
                        XmlBuilder condition = target.addNewElement("condition");
2504
                        condition.addAttribute("property",  p.mAlias + ".build");
2505
 
2506
                        XmlBuilder and = condition.addNewElement("and");
2507
 
2508
                        for (Iterator<Package> it = p.mPackageDependencyCollection.iterator(); it.hasNext(); )
2509
                        {
2510
                            Package dependency = it.next();
2511
 
2512
                            if ( dependency.mBuildFile == buildFile )
2513
                            {
2514
                                XmlBuilder or = and.addNewElement("or");
2515
 
2516
                                XmlBuilder equal1 = or.addNewElement("equals");
2517
                                equal1.addAttribute("arg1", "${" + dependency.mAlias + ".res}");
2518
                                equal1.addAttribute("arg2", "0");
2519
 
2520
                                XmlBuilder equal2 = or.addNewElement("equals");
2521
                                equal2.addAttribute("arg1", "${" + dependency.mAlias + ".res}");
2522
                                equal2.addAttribute("arg2", "257");
2523
                            }
2524
                        }
2525
                    }
2526
                    else
2527
                    {
2528
                        target.addProperty(p.mAlias + ".build", "");
2529
                    }
2530
                }
2531
            }
2532
 
2533
 
2534
            //
2535
            //  Generate the 'body' of the target package
2536
            //  Escrow Mode: Always
2537
            //  Daemon Mode: Only for the one target we are building
2538
            //                  Simplifies the XML
2539
            //                  Reduces noise in the logs
2540
            //              Don't add target dependencies. 
2541
            //                  We are only building one target and the 
2542
            //                  dependency management has been done way before now.
2543
            //                  All it does is makes the logs noisy.
2544
            //
2545
            if ( ( mDaemon && p.mBuildFile == 1 ) || !mDaemon )
2546
            {
2547
                XmlBuilder target = xml.addNewElement("target").isExpanded();
2548
                target.addAttribute("name", p.mAlias);
2549
 
2550
                if ( !mDaemon )
2551
                {
2552
                    target.addAttribute("depends",  p.mAlias + ".wrap");
2553
                    target.addAttribute("if",p.mAlias + ".build");
2554
                }
2555
 
2556
                if ( mDaemon && p.mBuildFile == 1 )
2557
                {
2558
                    target.addProperty(p.mAlias + "pkg_id",p.mPid);
2559
                    target.addProperty(p.mAlias + "pv_id",p.mId);
2560
                }
2561
 
2562
                target.addProperty(p.mAlias + "packagename",p.mName);        		
2563
                target.addProperty(p.mAlias + "packageversion",p.mVersion);
2564
                target.addProperty(p.mAlias + "packageextension",p.mExtension);
2565
                target.addProperty(p.mAlias + "packagevcstag",p.mVcsTag);
2566
 
2567
                target.makePropertyTag(p.mAlias + "directchange", p.mDirectlyPlanned); 
2568
                target.makePropertyTag(p.mAlias + "doesnotrequiresourcecontrolinteraction", ! p.mRequiresSourceControlInteraction);
2569
 
2570
                buildEntry.state = BuildFile.BuildFileState.NonGeneric;
2571
                if ( p.isGeneric() )
2572
                {
2573
                    buildEntry.state = BuildFile.BuildFileState.Generic;
2574
                    target.makePropertyTag(p.mAlias + "generic", true); 
2575
                }
2576
 
2577
                target.addProperty(p.mAlias + "loc", getBuildLocation(p));
2578
                target.makePropertyTag(p.mAlias + "unittests", p.mHasAutomatedUnitTests && mDaemon);
2579
 
2580
                //    Add our own task and associated information
2581
                //
2582
                XmlBuilder abt = target.addNewElement("abt").isExpanded();
2583
 
2584
                for (Iterator<Package> it = p.mPackageDependencyCollection.iterator(); it.hasNext(); )
2585
                {
2586
                    Package dependency = it.next();
2587
                    XmlBuilder depend = abt.addNewElement("depend");
2588
                    depend.addAttribute("package_alias", "${" + dependency.mAlias + "}");
2589
                }
2590
 
2591
                buildInfo(abt,p);
2592
 
2593
                if ( mDaemon && p.mBuildFile == 1 )
2594
                {
2595
                    //    AbtTestPath
2596
                    target = xml.addNewElement("target").isExpanded();
2597
                    target.addAttribute("name", "AbtTestPath");
2598
                    target.addProperty("AbtTestPathpackagevcstag", p.mVcsTag);
2599
                    target.addProperty("AbtTestPathpackagename", p.mName);
2600
                    abt = target.addNewElement("abt").isExpanded();
2601
                    buildInfo(abt, p);
2602
 
2603
 
2604
                    //    AbtSetUp
2605
                    target = xml.addNewElement("target").isExpanded();
2606
                    target.addAttribute("name", "AbtSetUp");
2607
                    target.addProperty("AbtSetUppackagevcstag", p.mVcsTag);
2608
                    target.addProperty("AbtSetUppackagename", p.mName);
2609
 
2610
                    abt = target.addNewElement("abt").isExpanded();
2611
                    buildInfo(abt, p);
2612
 
2613
                    //    AbtTearDown
2614
                    target = xml.addNewElement("target").isExpanded();
2615
                    target.addAttribute("name", "AbtTearDown");
2616
                    target.addProperty("AbtTearDownpackagevcstag", p.mVcsTag);
2617
                    target.addProperty("AbtTearDownpackagename", p.mName);
2618
                    target.addProperty("AbtTearDownpackageversion", p.mVersion);
2619
                    target.addProperty("AbtTearDownpackageextension", p.mExtension);
2620
                    target.makePropertyTag(p.mAlias + "generic", p.isGeneric());
2621
 
2622
                    abt = target.addNewElement("abt").isExpanded();
2623
                    buildInfo(abt, p);
2624
 
2625
 
2626
                    //  AbtPublish
2627
                    target = xml.addNewElement("target").isExpanded();
2628
                    target.addAttribute("name", "AbtPublish");
2629
 
2630
                    target.addProperty("AbtPublishpackagevcstag", p.mVcsTag);
2631
                    target.addProperty("AbtPublishpackagename", p.mName);
2632
                    target.addProperty("AbtPublishpackageversion", p.mVersion);
2633
                    target.addProperty("AbtPublishpackageextension", p.mExtension);
2634
                    target.makePropertyTag("AbtPublishdirectchange", p.mDirectlyPlanned);
2635
                    target.makePropertyTag("AbtPublishdoesnotrequiresourcecontrolinteraction", ! p.mRequiresSourceControlInteraction);
2636
                    target.makePropertyTag("AbtPublishgeneric", p.isGeneric());
2637
                    target.addProperty("AbtPublishloc", getBuildLocation(p));
2638
 
2639
                    abt = target.addNewElement("abt").isExpanded();
2640
                    buildInfo(abt, p);
2641
 
2642
                }
2643
            }
2644
        }
2645
    }
2646
 
2647
    /** Extends the xml object. Adds ant default target for the current build iteration
2648
     * @param xml - The XmlBuilder Object to extend
2649
     * @param buildFile - The current build file level. This differs for Daemon and Escrow mode. In Daemon mode it will not be a '1' 
2650
     */
2651
    private void generateDefaultTarget(XmlBuilder xml, int buildFile)
2652
    {
2653
        mLogger.debug("generateDefaultTarget");
2654
 
2655
        XmlBuilder target = xml.addNewElement("target").isExpanded();
2656
        target.addAttribute("name", "fullstart");
2657
 
2658
        if (buildFile == 1)
2659
        {
2660
            antEcho(target, "${line.separator}" + mAnyBuildPlatforms + "${line.separator}${line.separator}");
7048 dpurdie 2661
            for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
6914 dpurdie 2662
            {
2663
                Package p = it.next();
2664
 
2665
                if ( p.mBuildFile == -1 )
2666
                {
2667
                    antEcho(target, "${line.separator}" + p.mAlias + "${line.separator}");
2668
                }
2669
            }
2670
 
2671
            antEcho(target, "${line.separator}" + mAssocBuildPlatforms + "${line.separator}${line.separator}");
7048 dpurdie 2672
            for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
6914 dpurdie 2673
            {
2674
                Package p = it.next();
2675
 
2676
                if ( p.mBuildFile == -2 )
2677
                {
2678
                    antEcho(target, "${line.separator}" + p.mAlias + "${line.separator}");
2679
                }
2680
            }
2681
 
2682
            antEcho(target, "${line.separator}" + mNotInBaseline + "${line.separator}${line.separator}");
7048 dpurdie 2683
            for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
6914 dpurdie 2684
            {
2685
                Package p = it.next();
2686
 
2687
                if ( p.mBuildFile == -4 )
2688
                {
2689
                    antEcho(target, "${line.separator}" + p.mAlias + "${line.separator}");
2690
                }
2691
            }
2692
 
2693
            antEcho(target, "${line.separator}" + mDependent + "${line.separator}${line.separator}");
7048 dpurdie 2694
            for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
6914 dpurdie 2695
            {
2696
                Package p = it.next();
2697
 
2698
                if ( p.mBuildFile == -5 )
2699
                {
2700
                    antEcho(target, "${line.separator}" + p.mAlias + "${line.separator}");
2701
                }
2702
            }
2703
        }
2704
 
2705
        if ( !mDaemon )
2706
        {
2707
            antEcho(target, "${line.separator}Build Started:${line.separator}${line.separator}");
2708
        }
2709
 
2710
        //
2711
        //	Create a comma separated list of all the required targets
2712
        //      Escrow : All packages
2713
        //      Daemon : Just the package being built
2714
        //
2715
        StringAppender dependList = new StringAppender(",");
2716
        dependList.append("fullstart");
7048 dpurdie 2717
        for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
6914 dpurdie 2718
        {
2719
            Package p = it.next();
2720
 
2721
            if ( ( p.mBuildFile > 0 ) && ( p.mBuildFile <= buildFile ) )
2722
            {
2723
                if ((mDaemon && p.mBuildFile == 1) || !mDaemon)
2724
                {
2725
                    dependList.append(p.mAlias);                    
2726
                }
2727
            }
2728
        }
2729
 
2730
        target = xml.addNewElement("target").isExpanded();
2731
        target.addAttribute("name", "full");
2732
        target.addAttribute("depends", dependList.toString());
2733
 
2734
        if ( !mDaemon )
2735
        {
2736
            antEcho(target, "${line.separator}Build Finished${line.separator}");
2737
        }
2738
    }
2739
 
2740
    /** Internal helper function to create an ant 'echo statement
2741
     *  Many of the parameters are fixed
2742
     */
2743
    private void antEcho( XmlBuilder xml, String message)
2744
    {
2745
        XmlBuilder msg = xml.addNewElement("echo");
2746
        msg.addAttribute("message", message);
2747
        msg.addAttribute("file", "publish.log");
2748
        msg.addAttribute("append", "true");
2749
    }
2750
 
2751
    /**sets the mIndirectlyPlanned true for the package and all dependent packages
2752
     */
2753
    private void rippleIndirectlyPlanned(Package p)
2754
    {
2755
        mLogger.debug("rippleIndirectlyPlanned");
2756
        if ( !p.mIndirectlyPlanned && p.mBuildFile == 0 )
2757
        {
2758
            p.mIndirectlyPlanned = true;
2759
 
7048 dpurdie 2760
            for (Iterator<Package> it = mPackageCollectionAll.iterator(); it.hasNext(); )
6914 dpurdie 2761
            {
2762
                Package pkg = it.next();
2763
 
2764
                if ( pkg != p )
2765
                {
2766
                    for (Iterator<Package> it2 = pkg.mPackageDependencyCollection.iterator(); it2.hasNext(); )
2767
                    {
2768
                        Package dependency = it2.next();
2769
 
2770
                        if ( dependency == p )
2771
                        {
2772
                            rippleIndirectlyPlanned( pkg );
2773
                            break;
2774
                        }
2775
                    }
2776
                }
2777
            }
2778
        }
7048 dpurdie 2779
        mLogger.info("rippleIndirectlyPlanned set {} {}", p.mName, p.mIndirectlyPlanned);    
6914 dpurdie 2780
    }
2781
 
2782
    /**accessor method
2783
     */
2784
    public String getEscrowSetUp()
2785
    {
2786
        mLogger.debug("getEscrowSetUp");
2787
        String retVal = mEscrowSetup;
2788
 
7048 dpurdie 2789
        mLogger.debug("getEscrowSetUp returned {}", retVal);
6914 dpurdie 2790
        return retVal;
2791
    }
2792
 
2793
    /**accessor method
2794
     */
2795
    public String getRawData()
2796
    {
2797
        mLogger.debug("getRawData");
2798
        String retVal = mEscrowRawData;
2799
 
7048 dpurdie 2800
        mLogger.debug("getRawData returned {}", retVal);
6914 dpurdie 2801
        return retVal;
2802
    }
2803
 
2804
    /**Get the build loc (location)
2805
     * This is package specific and will depend on the build mode (Escrow/Daemon)
2806
     * 
2807
     * @param	p - Package being built
2808
     * @return A string that describes the build location
2809
     */
2810
    private String getBuildLocation(Package p)
2811
    {
2812
        mLogger.debug("locationProperty");
2813
        String location = "";
2814
 
2815
        if (mDaemon)
2816
        {
2817
            // Daemon: Start in root of view/workspace
2818
            location += mBaseline;
2819
        }
2820
        else
2821
        {
2822
            // Escrow: mAlias used with jats -extractfiles -view
2823
            location += p.mAlias;
2824
        }
2825
 
2826
        //
2827
        //  Always use '/' as a path separator - even if user has specified '\'
2828
        //  Ant can handle it.
2829
        //
2830
        location = location.replace('\\', '/');
2831
        return location;
2832
    }
2833
 
2834
    /**Adds package build into as XML elements 
2835
     * @param	  xml 	- Xml element to extend
2836
     * @param   p       - Package to process
2837
     */
2838
    private void buildInfo(XmlBuilder xml, Package p)
2839
    {
2840
        mLogger.debug("buildInfo");
2841
 
2842
        //
2843
        // Create the xml build information
2844
        // <platform gbe_machtype="linux_i386" type="jats" arg="all"/>
2845
        //
2846
        for (Iterator<BuildStandard> it = p.mBuildStandardCollection.iterator(); it.hasNext();)
2847
        {
2848
            BuildStandard bs = it.next();
2849
            bs.getBuildStandardXml(xml);
2850
        }
2851
    }
2852
 
2853
    /**returns the buildInfo as a single line of text
2854
     * Used for reporting purposes only
2855
     * @param   p       - Package to process
2856
     * 
2857
     */
2858
    public String buildInfoText(Package p)
2859
    {
2860
        mLogger.debug("buildInfoText");
2861
 
2862
        StringAppender result = new StringAppender (";");
2863
 
2864
        //
2865
        //  Create platform:standards
2866
        //      
2867
        for (Iterator<BuildStandard> it = p.mBuildStandardCollection.iterator(); it.hasNext(); )
2868
        {
2869
            BuildStandard bs = it.next();
2870
 
2871
            if ( bs.isActive() )
2872
            {
2873
                String info = bs.getBuildStandardText();
2874
                result.append(info);
2875
            }
2876
        }
2877
 
7048 dpurdie 2878
        mLogger.info("buildInfoText returned {}", result);
6914 dpurdie 2879
        return result.toString();
2880
    }
2881
 
2882
    /**prints to standard out in escrow mode only
2883
     * <br>Prints a title and information. The title is only printed once.
2884
     * 
2885
     * @param header - The message title to display, if printMessage is true
2886
     * @param text - A package name. Really just the 2nd line of the message
2887
     * @param printHeader -  Controls the printing of the message argument
2888
     */
2889
    private void standardOut(final String header, final String text, boolean printHeader)
2890
    {
2891
        mLogger.debug("standardOut");
2892
        if (!mDaemon)
2893
        {
2894
            if ( printHeader )
2895
            {
2896
                System.out.println(header);
2897
            }
2898
 
2899
            System.out.println(text);
2900
        }
2901
    }
2902
 
2903
 
2904
    /**
2905
     *  Email users about a rejected daemon instruction
2906
     *  @param  reason  - Reason for the rejection
2907
     *  @param  pkg     - Package to be affected by the instruction
2908
     *  
2909
     */
2910
    public void emailRejectedDaemonInstruction(String reason, Package p)
2911
    {
2912
        mLogger.debug("emailRejectedDaemonInstruction");
2913
 
2914
        //  Email Subject
2915
        String subject = "BUILD FAILURE of Daemon Instruction on package " + p.mAlias;
2916
 
2917
        // Email Body
2918
        String mailBody = "The build system reject the the Daemon Instruction";
2919
        mailBody += "<br>Reason: " + reason; 
2920
 
2921
        mailBody += "<p>Release: " + mBaselineName 
2922
                +  "<br>Package: " + p.mAlias 
2923
                +  "<br>Rm Ref: " + CreateUrls.generateRmUrl(getRtagId(), p.mId); 
2924
 
2925
        mailBody += "<p><hr>";
2926
 
2927
        String target = p.emailInfoNonAntTask(this);
2928
 
7048 dpurdie 2929
        mLogger.error("emailRejectedDaemonInstruction Server: {}", getMailServer());
2930
        mLogger.error("emailRejectedDaemonInstruction Sender: {}", getMailSender());
2931
        mLogger.error("emailRejectedDaemonInstruction Target: {}", target);
6914 dpurdie 2932
 
2933
        try
2934
        {
2935
            //    
7048 dpurdie 2936
            Smtpsend.send(getMailServer(),  // mailServer
2937
                    getMailSender(),        // source
2938
                    target,                 // target
2939
                    getMailSender(),        // cc
2940
                    null,                   // bcc
2941
                    subject,                // subject
2942
                    mailBody,               // body
2943
                    null                    // attachment
6914 dpurdie 2944
                    );
2945
        } catch (Exception e)
2946
        {
7048 dpurdie 2947
            mLogger.warn("Email Failure: emailRejectedDaemonInstruction:{}", e.getMessage());
6914 dpurdie 2948
        }
2949
    }
2950
 
2951
    /**
2952
     * @return the mMailServer
2953
     */
2954
    public String getMailServer() {
2955
        return mMailServer;
2956
    }
2957
 
2958
    /**
2959
     * @param mMailServer the mMailServer to set
2960
     */
2961
    public void setMailServer(String mMailServer) {
2962
        this.mMailServer = mMailServer;
2963
    }
2964
 
2965
    /**
2966
     * @return the mMailSender
2967
     */
2968
    public String getMailSender() {
2969
        return mMailSender;
2970
    }
2971
 
2972
    /**
2973
     * @param mMailSender the mMailSender to set
2974
     */
2975
    public void setMailSender(String mMailSender) {
2976
        this.mMailSender = mMailSender;
2977
    }
2978
 
2979
    /**
2980
     * @return the mMailGlobalTarget
2981
     */
2982
    public String getMailGlobalTarget() {
2983
        return mMailGlobalTarget;
2984
    }
2985
 
2986
    /**
2987
     * @param mMailGlobalTarget the mMailGlobalTarget to set
2988
     */
2989
    public void setMailGlobalTarget(String mMailGlobalTarget) {
2990
        this.mMailGlobalTarget = mMailGlobalTarget;
2991
    }
2992
 
2993
}