davsclaus opened a new pull request, #30592:
URL: https://github.com/apache/camel/pull/30592

   JIRA: [CAMEL-27364](https://issues.apache.org/jira/browse/CAMEL-27364)
   
   ## What was wrong
   
   With the Camel CLI, a route that uses a Kamelet cannot refer to a class that 
only a dependency of that Kamelet provides. For example, a `doCatch` on 
`org.apache.cxf.binding.soap.SoapFault` around `kamelet:soap-action` (a Kamelet 
that declares `camel:cxf-soap`) fails at startup:
   
   ```
   FailedToCreateRouteException: Failed to create route: r1 at: >>> 
DoTry[[to[kamelet:my-soap-action], 
DoCatch[org.apache.cxf.binding.soap.SoapFault -> ...
   Caused by: java.lang.ClassNotFoundException: 
org.apache.cxf.binding.soap.SoapFault
   ```
   
   It works with `--dep=camel:cxf-soap`. The cause is the order of startup:
   
   - `DependencyDownloaderKamelet` downloads the `spec.dependencies` of a 
Kamelet when the kamelet component loads the template.
   - The kamelet component defers creating the routes of Kamelets until the 
CamelContext is initialized (`LifecycleHandler.onContextInitialized`). That is 
after all the user routes are created.
   - So when the `doCatch` of the user route is created, nothing has downloaded 
`camel-cxf-soap` yet.
   
   ## The fix
   
   `DependencyDownloaderKamelet` (in `camel-kamelet-main`, used by the Camel 
CLI only) is now also a `ModelLifecycleStrategy`. When a route definition is 
added to the model, which happens before the route is created, it does three 
things:
   
   1. It collects the `kamelet:` uris of the route: the `from`, and every 
`EndpointRequiredDefinition` in the outputs, which includes `doTry`/`doCatch` 
and `onException` blocks.
   2. It finds the Kamelet file in the Kamelet location the same way as 
`RouteTemplateHelper`. The location is the `location` option of the endpoint, 
otherwise the one of the kamelet component, which includes 
`--local-kamelet-dir`.
   3. It downloads the dependencies of that Kamelet with the existing 
downloader.
   
   Some uris are skipped: `kamelet:source`, `kamelet:sink`, and names that are 
placeholders or expressions. Each Kamelet is looked up once. If it fails, the 
result is the same as before: the dependencies are downloaded later, when the 
template is loaded. The Kamelet's route is still created when the CamelContext 
is initialized, so the kamelet component itself is unchanged.
   
   ## Tests
   
   - New `DependencyDownloaderKameletTest`, with a downloader that only records 
what is asked:
     - A Kamelet in a temp directory declares `camel:cxf-soap`. When a route 
that uses it is added to a CamelContext that is not started, `camel-cxf-soap` 
is asked for. This fails without the fix.
     - Unit test of the Kamelet name taken from the uri: `kamelet://name?..`, 
`name/routeId`, `source`/`sink`, `{{name}}` and `${header.name}`.
   - Ran the whole `camel-kamelet-main` test suite: 101 tests, 0 failures, 7 
skipped (manual tests).
   - Reproduced end to end:
     - With the installed Camel CLI (`camel run route.camel.yaml 
--local-kamelet-dir=...`): `ClassNotFoundException: 
org.apache.cxf.binding.soap.SoapFault`.
     - With a temporary test (not committed) that runs `KameletMain` with 
downloads on, a local Kamelet that declares `camel:cxf-soap`, and the route 
with the `doCatch`. Without the fix: `FailedToCreateRouteException ... 
ClassNotFoundException`. With the fix: it starts, with 2 routes.
   
   _Claude Code on behalf of davsclaus_
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to