43
43
import java .util .List ;
44
44
import java .util .Map ;
45
45
import java .util .Set ;
46
+ import java .util .concurrent .locks .Lock ;
47
+ import java .util .concurrent .locks .ReentrantLock ;
46
48
47
49
/**
48
50
* This class represents the DAO class related to assessing compliance of APIs
49
51
*/
50
52
public class ComplianceMgtDAOImpl implements ComplianceMgtDAO {
51
53
52
54
private static final Log log = LogFactory .getLog (ComplianceMgtDAOImpl .class );
55
+ private static final Lock resultWrtiteDelLock = new ReentrantLock ();
56
+
53
57
private ComplianceMgtDAOImpl () {
54
58
55
59
}
@@ -504,23 +508,25 @@ public void addComplianceEvalResults(String artifactRefId, ArtifactType artifact
504
508
Map <String , List <RuleViolation >> rulesetViolationsMap , String organization )
505
509
throws APIMGovernanceException {
506
510
507
- List < String > rulesetIds = new ArrayList <>( rulesetViolationsMap . keySet () );
511
+ resultWrtiteDelLock . lock ( );
508
512
try (Connection connection = APIMGovernanceDBUtil .getConnection ()) {
513
+ String artifactKey = getArtifactKey (connection , artifactRefId , artifactType , organization );
514
+ if (artifactKey == null ) {
515
+ throw new APIMGovernanceException (APIMGovExceptionCodes .ARTIFACT_NOT_FOUND , artifactRefId );
516
+ }
509
517
connection .setAutoCommit (false );
510
518
511
519
try {
512
- clearOldRuleViolations (connection , artifactRefId , artifactType , rulesetIds , organization );
513
- clearOldRulesetRuns (connection , artifactRefId , artifactType , rulesetIds , organization );
514
- clearOldPolicyRun (connection , artifactRefId , artifactType , policyId , organization );
515
- connection .commit ();
520
+ clearOldRuleViolations (connection , artifactKey , policyId );
521
+ clearOldRulesetRuns (connection , artifactKey , policyId );
522
+ clearOldPolicyRun (connection , artifactKey , policyId );
523
+ connection .commit (); // Required to release the locks and avoid deadlocks
516
524
517
- String artifactKey = getArtifactKey (connection , artifactRefId , artifactType , organization );
518
525
addPolicyRun (connection , artifactKey , policyId );
519
-
520
526
for (Map .Entry <String , List <RuleViolation >> entry : rulesetViolationsMap .entrySet ()) {
521
527
String rulesetId = entry .getKey ();
522
528
List <RuleViolation > ruleViolations = entry .getValue ();
523
- String rulesetResultId = addRulesetRuns (connection , artifactKey , rulesetId ,
529
+ String rulesetResultId = addRulesetRun (connection , artifactKey , rulesetId ,
524
530
ruleViolations .isEmpty ());
525
531
addRuleViolations (connection , rulesetResultId , ruleViolations );
526
532
}
@@ -534,82 +540,64 @@ public void addComplianceEvalResults(String artifactRefId, ArtifactType artifact
534
540
} catch (SQLException e ) {
535
541
throw new APIMGovernanceException (APIMGovExceptionCodes .ERROR_WHILE_SAVING_GOVERNANCE_RESULT ,
536
542
e , artifactRefId );
543
+ } finally {
544
+ resultWrtiteDelLock .unlock ();
537
545
}
538
546
}
539
547
540
548
/**
541
549
* Clear old policy run results for the artifact
542
550
*
543
- * @param connection Connection
544
- * @param artifactRefId Artifact Reference ID (ID of the artifact on APIM side)
545
- * @param artifactType Artifact Type
546
- * @param policyId Policy ID
547
- * @param organization Organization
551
+ * @param connection Connection
552
+ * @param artifactKey Artifact Key
553
+ * @param policyId Policy ID
548
554
* @throws SQLException If an error occurs while clearing the old policy result
549
555
*/
550
- private void clearOldPolicyRun (Connection connection , String artifactRefId , ArtifactType artifactType ,
551
- String policyId , String organization )
556
+ private void clearOldPolicyRun (Connection connection , String artifactKey , String policyId )
552
557
throws SQLException {
553
558
554
559
String sqlQuery = SQLConstants .DELETE_POLICY_RUN_FOR_ARTIFACT_AND_POLICY ;
555
560
try (PreparedStatement prepStmnt = connection .prepareStatement (sqlQuery )) {
556
- prepStmnt .setString (1 , artifactRefId );
557
- prepStmnt .setString (2 , String .valueOf (artifactType ));
558
- prepStmnt .setString (3 , organization );
559
- prepStmnt .setString (4 , policyId );
561
+ prepStmnt .setString (1 , artifactKey );
562
+ prepStmnt .setString (2 , policyId );
560
563
prepStmnt .executeUpdate ();
561
564
}
562
565
}
563
566
564
567
/**
565
568
* Clear old ruleset runs for the artifact
566
569
*
567
- * @param connection Connection
568
- * @param artifactRefId Artifact Reference ID (ID of the artifact on APIM side)
569
- * @param artifactType Artifact Type
570
- * @param rulesetIds List of Ruleset IDs
571
- * @param organization Organization
570
+ * @param connection Connection
571
+ * @param artifactKey Artifact Reference ID (ID of the artifact on APIM side)
572
+ * @param policyId Policy ID
572
573
* @throws SQLException If an error occurs while clearing the old ruleset results
573
574
*/
574
- private void clearOldRulesetRuns (Connection connection , String artifactRefId , ArtifactType artifactType ,
575
- List <String > rulesetIds , String organization ) throws SQLException {
575
+ private void clearOldRulesetRuns (Connection connection , String artifactKey , String policyId ) throws SQLException {
576
576
577
- String sqlQuery = SQLConstants .DELETE_RULESET_RUN_FOR_ARTIFACT_AND_RULESET ;
577
+ String sqlQuery = SQLConstants .DELETE_RULESET_RUN_FOR_ARTIFACT_AND_POLICY ;
578
578
try (PreparedStatement prepStmnt = connection .prepareStatement (sqlQuery )) {
579
- for (String rulesetId : rulesetIds ) {
580
- prepStmnt .setString (1 , artifactRefId );
581
- prepStmnt .setString (2 , String .valueOf (artifactType ));
582
- prepStmnt .setString (3 , organization );
583
- prepStmnt .setString (4 , rulesetId );
584
- prepStmnt .addBatch ();
585
- }
586
- prepStmnt .executeBatch ();
579
+ prepStmnt .setString (1 , artifactKey );
580
+ prepStmnt .setString (2 , policyId );
581
+ prepStmnt .executeUpdate ();
587
582
}
588
583
}
589
584
590
585
/**
591
586
* Clear rule violations for the artifact and rulesets
592
587
*
593
- * @param connection Connection
594
- * @param artifactRefId Artifact Reference ID (ID of the artifact on APIM side)
595
- * @param artifactType Artifact Type
596
- * @param rulesetIds List of Ruleset IDs
597
- * @param organization Organization
588
+ * @param connection Connection
589
+ * @param artifactKey Artifact Key
590
+ * @param policyId List of Ruleset IDs
598
591
* @throws SQLException If an error occurs while clearing the rule violations
599
592
*/
600
- private void clearOldRuleViolations (Connection connection , String artifactRefId , ArtifactType artifactType ,
601
- List < String > rulesetIds , String organization ) throws SQLException {
593
+ private void clearOldRuleViolations (Connection connection , String artifactKey ,
594
+ String policyId ) throws SQLException {
602
595
603
- String sqlQuery = SQLConstants .DELETE_RULE_VIOLATIONS_FOR_ARTIFACT_AND_RULESET ;
596
+ String sqlQuery = SQLConstants .DELETE_RULE_VIOLATIONS_FOR_ARTIFACT_AND_POLICY ;
604
597
try (PreparedStatement prepStmnt = connection .prepareStatement (sqlQuery )) {
605
- for (String rulesetId : rulesetIds ) {
606
- prepStmnt .setString (1 , artifactRefId );
607
- prepStmnt .setString (2 , String .valueOf (artifactType ));
608
- prepStmnt .setString (3 , organization );
609
- prepStmnt .setString (4 , rulesetId );
610
- prepStmnt .addBatch ();
611
- }
612
- prepStmnt .executeBatch ();
598
+ prepStmnt .setString (1 , artifactKey );
599
+ prepStmnt .setString (2 , policyId );
600
+ prepStmnt .execute ();
613
601
}
614
602
}
615
603
@@ -661,7 +649,7 @@ private void addPolicyRun(Connection connection, String artifactKey, String poli
661
649
}
662
650
663
651
/**
664
- * Add a ruleset compliance evaluation result
652
+ * Add or update a ruleset compliance evaluation result
665
653
*
666
654
* @param connection Connection
667
655
* @param artifactKey Artifact Key
@@ -670,8 +658,8 @@ private void addPolicyRun(Connection connection, String artifactKey, String poli
670
658
* @return Ruleset Result ID
671
659
* @throws SQLException If an error occurs while adding the ruleset compliance evaluation result
672
660
*/
673
- private String addRulesetRuns (Connection connection , String artifactKey , String rulesetId ,
674
- boolean isRulesetEvalSuccess ) throws SQLException {
661
+ private String addRulesetRun (Connection connection , String artifactKey , String rulesetId ,
662
+ boolean isRulesetEvalSuccess ) throws SQLException {
675
663
676
664
String sqlQuery = SQLConstants .ADD_RULESET_RUN ;
677
665
try (PreparedStatement prepStmnt = connection .prepareStatement (sqlQuery )) {
@@ -1104,6 +1092,8 @@ public List<String> getPendingPoliciesForArtifact(String artifactRefId,
1104
1092
@ Override
1105
1093
public void deleteArtifact (String artifactRefId , ArtifactType artifactType , String organization )
1106
1094
throws APIMGovernanceException {
1095
+
1096
+ resultWrtiteDelLock .lock ();
1107
1097
try (Connection connection = APIMGovernanceDBUtil .getConnection ()) {
1108
1098
connection .setAutoCommit (false );
1109
1099
@@ -1164,6 +1154,8 @@ public void deleteArtifact(String artifactRefId, ArtifactType artifactType, Stri
1164
1154
} catch (SQLException e ) {
1165
1155
throw new APIMGovernanceException (APIMGovExceptionCodes
1166
1156
.ERROR_WHILE_DELETING_GOVERNANCE_DATA , e , artifactRefId );
1157
+ } finally {
1158
+ resultWrtiteDelLock .unlock ();
1167
1159
}
1168
1160
}
1169
1161
}
0 commit comments