Skip to content

Commit

Permalink
Remove PipelinesCdiContext.java bean cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
carter-cundiff committed Oct 10, 2024
1 parent 40e7ccb commit f745adf
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 190 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ public class SparkPipelineMessagingMigration extends AbstractPomMigration {
private static final String SMALLRYE_REACTIVE_MESSAGING_GROUP_ID = "io.smallrye.reactive";
private static final String SMALLRYE_REACTIVE_MESSAGING_ARTIFACT_ID = "smallrye-reactive-messaging";
private static final String SMALLRYE_REACTIVE_MESSAGING_KAFKA_ARTIFACT_ID = "smallrye-reactive-messaging-kafka";
private static final String SMALLRYE_REACTIVE_CDI_EVENTS_IMPORT = "import io.smallrye.reactive.messaging.kafka.KafkaCDIEvents;";
private static final String SMALLRYE_REACTIVE_CDI_EVENTS_OBJECT = "KafkaCDIEvents.class";
private static final String SMALLRYE_REACTIVE_KAFKA_CONNECTOR_IMPORT = "import io.smallrye.reactive.messaging.kafka.KafkaConnector;";
private static final String SMALLRYE_REACTIVE_KAFKA_CONNECTOR_OBJECT = "KafkaConnector.class";

private static final String AISSEMBLE_GROUP_ID = "com.boozallen.aissemble";
private static final String AISSEMBLE_MESSAGING_KAFKA_ARTIFACT_ID = "extensions-messaging-kafka";
Expand All @@ -68,12 +64,8 @@ protected boolean shouldExecuteOnFile(File pomFile) {
boolean addKafkaCdiContext = this.hasDependency(model, SMALLRYE_REACTIVE_MESSAGING_GROUP_ID, SMALLRYE_REACTIVE_MESSAGING_KAFKA_ARTIFACT_ID);

if (addKafkaCdiContext) {
// Get the PipelinesCdiContext.java file
File pipelinesCdiContextFile = searchForFileInCdi(new File(pomFile.getParentFile(), "src/main/java"), "PipelinesCdiContext.java");

// Check if any of the CdiContainerFactory.java, PipelinesCdiContext.java, or pom.xml needs to be migrated
return shouldMigratePipelinesCdiContextFile(pipelinesCdiContextFile) || shouldMigratePomFile(model) ||
shouldMigrateCdiContainerFactoryFile(cdiContainerFactoryFile, addKafkaCdiContext);
// Check if any of the CdiContainerFactory.java or pom.xml needs to be migrated
return shouldMigratePomFile(model) || shouldMigrateCdiContainerFactoryFile(cdiContainerFactoryFile, addKafkaCdiContext);
}

// Check if only the CdiContainerFactory.java needs to be migrated
Expand Down Expand Up @@ -112,21 +104,9 @@ protected boolean performMigration(File pomFile) {
logger.info("Skipping file as CDI messaging dependency is already present");
pomFileMigrationSuccess = true;
}

// Get the PipelinesCdiContext.java file
File pipelinesCdiContextFile = searchForFileInCdi(new File(pomFile.getParentFile(), "src/main/java"), "PipelinesCdiContext.java");

boolean pipelinesCdiContextFileMigrationSuccess = false;
if (shouldMigratePipelinesCdiContextFile(pipelinesCdiContextFile)) {
// Remove the old kafka beans from the file
pipelinesCdiContextFileMigrationSuccess = this.migratePipelinesCdiContextFile(pipelinesCdiContextFile);
} else {
logger.info("Skipping migration as Kafka beans/imports are already removed");
pipelinesCdiContextFileMigrationSuccess = true;
}

// Return the results of all three file migrations (pom.xml, CdiContainerFactory.java, PipelinesCdiContext.java)
return pomFileMigrationSuccess && cdiContainerFactoryFileMigrationSuccess && pipelinesCdiContextFileMigrationSuccess;
// Return the results of both file migrations (pom.xml, CdiContainerFactory.java)
return pomFileMigrationSuccess && cdiContainerFactoryFileMigrationSuccess;
}

// Return the results of the only file migration (CdiContainerFactory.java)
Expand Down Expand Up @@ -203,53 +183,6 @@ private boolean migrateCdiContainerFactoryFile(File file, boolean addKafkaCdiCon
}
}

/*
* Determine if the PipelinesCdiContext.java needs to be migrated
*/
private boolean shouldMigratePipelinesCdiContextFile(File file) {
if (file != null) {
try {
// Check if the unecessary imports still exist
boolean hasKafkaCdiEventsImport = Files.readString(file.toPath()).contains(SMALLRYE_REACTIVE_CDI_EVENTS_IMPORT);
boolean hasKafkaConnectorImport = Files.readString(file.toPath()).contains(SMALLRYE_REACTIVE_KAFKA_CONNECTOR_IMPORT);

return hasKafkaCdiEventsImport || hasKafkaConnectorImport;
} catch (IOException e) {
throw new BatonException("Failed to read pipeline CDI context file: " + file.getAbsolutePath(), e);
}
} else {
logger.warn("Failed to find '{}' for migration", file);
return false;
}
}

/*
* Migrate the PipelinesCdiContext.java to remove the Kafka beans and imports as they are no longer necessary
*/
private boolean migratePipelinesCdiContextFile(File file) {
logger.info("Removing unnecessary Kafka beans and imports from the file: {}", file.getAbsolutePath());
try {
List<String> newFileContents = new ArrayList<>();
List<String> originalFile = FileUtils.readAllFileLines(file);

// Iterate through the file
for (String line : originalFile) {

// Add the line if it's not one unecessary beans/imports
if (!line.contains(SMALLRYE_REACTIVE_CDI_EVENTS_IMPORT) && !line.contains(SMALLRYE_REACTIVE_CDI_EVENTS_OBJECT) &&
!line.contains(SMALLRYE_REACTIVE_KAFKA_CONNECTOR_IMPORT) && !line.contains(SMALLRYE_REACTIVE_KAFKA_CONNECTOR_OBJECT)) {
newFileContents.add(line);
}
}

FileUtils.writeFile(file, newFileContents);
return true;
}
catch (IOException e) {
throw new BatonException("Failed to remove Kafka beans/imports from the file: " + file.getAbsolutePath(), e);
}
}

/*
* Determine if the pom.xml needs to be migrated
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,6 @@ public void the_CDI_container_factory_is_updated_with_the_messaging_and_kafka_co
assertTestFileMatchesExpectedFile("CDI container factory was not updated with the new kafka and messaging context objects");
}

@Then("the pipeline CDI context has the unecessary Kafka beans and imports removed")
public void the_pipeline_CDI_context_has_the_unecessary_Kafka_beans_and_imports_removed() {
this.testFile = getTestPom("smallrye-kafka/src/main/java/test/path/cdi/PipelinesCdiContext.java");
assertTestFileMatchesExpectedFile("pipeline CDI context did not have the unecessary Kafka beans and imports removed");
}

@Then("the CDI container factory is updated with the messaging context object")
public void the_CDI_container_factory_is_updated_with_the_messaging_context_object() {
this.testFile = getTestPom("smallrye-non-kafka/src/main/java/test/path/cdi/CdiContainerFactory.java");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ Feature: Migrate a Spark pipeline module with the new CDI classes to ensure mess
When the Spark Pipeline Messaging migration executes
Then the aissemble kafka messaging dependency is added to the POM
And the CDI container factory is updated with the messaging and kafka context objects
And the pipeline CDI context has the unecessary Kafka beans and imports removed

Scenario: A Spark pipeline module with a different smallrye dependency is migrated to include the messaging CDI context
Given a POM with the "jar" packaging type
Expand Down

This file was deleted.

This file was deleted.

0 comments on commit f745adf

Please sign in to comment.