Subversion Repositories DevTools

Rev

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