Andrea Cosentino created CAMEL-27933:
----------------------------------------
Summary: camel-dapr - load the workflow class via the CamelContext
ClassResolver
Key: CAMEL-27933
URL: https://issues.apache.org/jira/browse/CAMEL-27933
Project: Camel
Issue Type: Improvement
Components: camel-dapr
Reporter: Andrea Cosentino
Assignee: Andrea Cosentino
Fix For: 4.25.0
h3. Background
The {{camel-dapr}} workflow producer schedules a new workflow from the
configured workflow class (the {{workflowClass}} endpoint option).
{{DaprWorkflowHandler.resolveWorkflowClass(String)}} turns that class name into
a {{Class<? extends Workflow>}}, and it is called both while validating the
exchange and while scheduling the workflow.
h3. Current behaviour
{{resolveWorkflowClass}} resolves the name with {{Class.forName(className)}}:
{code:java}
private Class<? extends Workflow> resolveWorkflowClass(String className) throws
ClassNotFoundException {
Class<?> rawClass = Class.forName(className);
if (!Workflow.class.isAssignableFrom(rawClass)) {
throw new IllegalArgumentException(className + " does not extend
Workflow");
}
return (Class<? extends Workflow>) rawClass;
}
{code}
This has two drawbacks:
* {{Class.forName(String)}} resolves against the class loader that loaded the
dapr component, not the class loader the running application uses. In
deployments with isolated or custom class loaders (for example Quarkus) a
workflow class that is visible to the application can fail to resolve.
* {{Class.forName(String)}} also eagerly initializes the resolved class as a
side effect of loading it; turning a configured name into a {{Class}} reference
should not require initializing the class.
h3. Behaviour after the change
{{resolveWorkflowClass}} resolves the name through the CamelContext
{{ClassResolver}} instead of {{Class.forName}}:
{code:java}
Class<?> rawClass =
endpoint.getCamelContext().getClassResolver().resolveClass(className);
{code}
The {{ClassResolver}} delegates to the application and container class loaders,
so isolated-class-loader deployments (Quarkus) resolve the workflow class
correctly. It also resolves the class without eagerly initializing it. The
{{Workflow.class.isAssignableFrom(...)}} type check is kept unchanged.
{{ClassResolver.resolveClass(String)}} returns {{null}} for an unknown name
rather than throwing {{ClassNotFoundException}}, so the two callers are
adjusted to treat a {{null}} result the way they currently treat a missing
class:
* {{validateConfiguration(Exchange)}} (the {{scheduleNew}} branch) reports the
existing {{IllegalArgumentException(className + " not found")}} when the name
does not resolve.
* {{scheduleNewWorkflow(Exchange, DaprWorkflowClient)}} tolerates an unresolved
name exactly as the current {{catch (ClassNotFoundException)}} does.
A valid {{Workflow}} subtype is scheduled exactly as before; only the
resolution mechanism changes.
h3. Classes involved
* {{org.apache.camel.component.dapr.operations.DaprWorkflowHandler}} --
{{resolveWorkflowClass}} and its two callers, {{validateConfiguration}} and
{{scheduleNewWorkflow}}.
* {{org.apache.camel.spi.ClassResolver}}, obtained via
{{org.apache.camel.CamelContext#getClassResolver()}} -- the replacement
resolution path.
h3. Tests
New tests in {{org.apache.camel.component.dapr.operations.DaprWorkflowTest}}
(JUnit + Mockito, matching the existing tests in the module):
* a name that does not resolve to a {{Workflow}} is rejected with
{{IllegalArgumentException}} ("does not extend Workflow");
* class resolution does not trigger class initialization, verified with a small
helper class that records whether it has been initialized and asserting it
stays uninitialized after a resolve-and-reject.
h3. Related
CAMEL-27850 lists this among several camel-dapr follow-ups (item 6: the
workflow class is loaded with {{Class.forName}} rather than the CamelContext
{{ClassResolver}}).
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)