Subversion Repositories DevTools

Rev

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