Skip to content

Commit 43fd954

Browse files
gnodetclaude
andcommitted
CAMEL-23271: Use try-with-resources for CamelContext in CamelYamlParser
CamelContext implements AutoCloseable, so use try-with-resources instead of manual stop in finally block. Removes the need for @SuppressWarnings. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 17b6229 commit 43fd954

File tree

1 file changed

+3
-11
lines changed
  • dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/main/java/org/apache/camel/dsl/yaml/validator

1 file changed

+3
-11
lines changed

dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/main/java/org/apache/camel/dsl/yaml/validator/CamelYamlParser.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,10 @@
5050
public class CamelYamlParser {
5151

5252
public List<ValidationMessage> parse(File file) throws Exception {
53-
// CamelContext is stopped in finally block
54-
@SuppressWarnings("java:S2095")
55-
CamelContext camelContext = null;
56-
try {
57-
DefaultRegistry registry = new DefaultRegistry();
58-
registry.addBeanRepository(new StubBeanRepository("*"));
53+
DefaultRegistry registry = new DefaultRegistry();
54+
registry.addBeanRepository(new StubBeanRepository("*"));
5955

60-
camelContext = new DefaultCamelContext(registry);
56+
try (CamelContext camelContext = new DefaultCamelContext(registry)) {
6157
camelContext.setAutoStartup(false);
6258
camelContext.getCamelContextExtension().addContextPlugin(ComponentResolver.class,
6359
(name, context) -> new StubComponent());
@@ -103,10 +99,6 @@ public List<ValidationMessage> parse(File file) throws Exception {
10399
ValidationMessage vm = ValidationMessage.builder().type("parser")
104100
.messageSupplier(() -> e.getClass().getName() + ": " + e.getMessage()).build();
105101
return List.of(vm);
106-
} finally {
107-
if (camelContext != null) {
108-
camelContext.stop();
109-
}
110102
}
111103
}
112104

0 commit comments

Comments
 (0)