Skip to content

Conversation

@rogin
Copy link
Contributor

@rogin rogin commented Apr 19, 2025

Enhance the VMRouter using @tonygermano's existing code template.

Initial PR with @jonbartels feedback is here.

Note that I removed the boolean property that toggled its functionality, so it's always on now.

@pacmano1
Copy link
Contributor

Revised nextgenhealthcare/connect#5293, closed issue and renamed appending (fixed in OIE fork) to issue.

Signed-off-by: Richard Ogin <rogin@users.noreply.github.com>
@jonbartels jonbartels added this to the Technical Preview 2 milestone May 6, 2025
@kpalang kpalang requested review from a team, gibson9583, kayyagari, pacmano1 and ssrowe and removed request for a team September 8, 2025 15:25
@jonbartels jonbartels requested review from a team, gibson9583, kayyagari, kpalang, pacmano1, ssrowe and tonygermano and removed request for a team, gibson9583, kayyagari, pacmano1 and ssrowe September 8, 2025 15:26
@pacmano1 pacmano1 requested a review from Copilot December 12, 2025 18:30
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR enhances the VMRouter class to automatically track message routing chains, similar to how Channel Writer connectors work. The enhancement adds source channel IDs and message IDs to the sourceMap of routed messages, making it easier to trace message flow through multiple channels when using VMRouter programmatically instead of Channel Writer connectors.

Key Changes:

  • Added a new VMRouter constructor that accepts tracking information (channelId, messageId, sourceMap)
  • Introduced new overloaded routing methods that accept sourceMap and destinationSet parameters for enhanced control
  • Created a TrackingEnhancer inner class that enriches source maps with routing chain information

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 10 comments.

File Description
server/src/com/mirth/connect/server/util/javascript/JavaScriptScopeUtil.java Adds addRouterEnhancement method to create VMRouter instances with tracking capabilities and integrates them into all JavaScript execution scopes (preprocessor, postprocessor, filter/transformer, etc.). Also adds an unused doGetContext method.
server/src/com/mirth/connect/server/userutil/VMRouter.java Extends VMRouter with new constructor, overloaded routing methods for enhanced control, TrackingEnhancer inner class for automatic source tracking, createRawMessage helper method, and improved Javadoc with @link references.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +273 to +284
public RawMessage createRawMessage(Object message, Map<String, Object> sourceMap,
Collection<Number> destinationSet) {
if (trackingEnhancer != null) {
sourceMap = trackingEnhancer.enrich(sourceMap);
}

if (message instanceof byte[]) {
return new RawMessage((byte[]) message, destinationSet, sourceMap);
} else {
return new com.mirth.connect.donkey.model.message.RawMessage(message.getRawData(), message.getDestinationMetaDataIds(), message.getSourceMap());
return new RawMessage(message.toString(), destinationSet, sourceMap);
}
}
Copy link

Copilot AI Dec 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The createRawMessage method doesn't validate that the message parameter is non-null before calling message.toString() on line 282. If a null message is passed, this will throw a NullPointerException. Consider adding validation at the beginning of this method to throw a more meaningful exception or handle the null case appropriately.

Copilot uses AI. Check for mistakes.
Comment on lines +287 to +289
* Adds additional message tracking data.
*
* TrackingEnhancer
Copy link

Copilot AI Dec 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Javadoc comment "TrackingEnhancer" on line 289 is redundant as it simply restates the class name. Consider either removing this line or expanding it to provide a more meaningful description of what the TrackingEnhancer does, such as "Inner class that enriches source maps with message tracking information for routing chains."

Suggested change
* Adds additional message tracking data.
*
* TrackingEnhancer
* Inner class that enriches source maps with message tracking information for routing chains.
* Used to add channel and message identifiers to the source map for tracking purposes.

Copilot uses AI. Check for mistakes.
List<String> sourceChannelIds = lookupAsList("sourceChannelIds", "sourceChannelId");
List<String> sourceMessageIds = lookupAsList("sourceMessageIds", "sourceMessageId");

HashMap<String, Object> newSourceMap = new HashMap<String, Object>(messageSourceMap);
Copy link

Copilot AI Dec 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The HashMap is constructed with an initial capacity based on messageSourceMap size, but then 4 additional entries are added. To avoid potential resizing, consider using new HashMap<>(messageSourceMap.size() + 4) or similar to ensure adequate initial capacity.

Suggested change
HashMap<String, Object> newSourceMap = new HashMap<String, Object>(messageSourceMap);
HashMap<String, Object> newSourceMap = new HashMap<String, Object>(messageSourceMap.size() + 4);
newSourceMap.putAll(messageSourceMap);

Copilot uses AI. Check for mistakes.
Comment on lines +355 to +357
((Collection<?>) primaryValue).stream().map(i -> i.toString()).forEach(result::add);
} else if (primaryValue instanceof Object[]) {
Arrays.stream((Object[]) primaryValue).map(i -> i.toString()).forEach(result::add);
Copy link

Copilot AI Dec 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lambda expression i -> i.toString() can be simplified to a method reference: Object::toString. Similarly on line 357. This improves code readability and is a common Java best practice.

Suggested change
((Collection<?>) primaryValue).stream().map(i -> i.toString()).forEach(result::add);
} else if (primaryValue instanceof Object[]) {
Arrays.stream((Object[]) primaryValue).map(i -> i.toString()).forEach(result::add);
((Collection<?>) primaryValue).stream().map(Object::toString).forEach(result::add);
} else if (primaryValue instanceof Object[]) {
Arrays.stream((Object[]) primaryValue).map(Object::toString).forEach(result::add);

Copilot uses AI. Check for mistakes.
/**
* Enrich the given source map with additional message tracking data.
*
* @param messageSourceMap
Copy link

Copilot AI Dec 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The @param documentation for messageSourceMap is missing. While parameters in private methods don't strictly require documentation, it would improve maintainability to document what this parameter represents (e.g., "the source map provided by the caller, may be null").

Suggested change
* @param messageSourceMap
* @param messageSourceMap the source map provided by the caller, may be null

Copilot uses AI. Check for mistakes.
/**
* Look up a value from the environment's {@link SourceMap}
*
* @param key
Copy link

Copilot AI Dec 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The @param documentation for key is missing. Adding a brief description (e.g., "the key to look up in the source map") would improve code documentation consistency.

Suggested change
* @param key
* @param key the key to look up in the source map

Copilot uses AI. Check for mistakes.
Comment on lines +85 to +88
public Context doGetContext(ContextFactory contextFactory) {
return JavaScriptScopeUtil.getContext(contextFactory);
}

Copy link

Copilot AI Dec 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This public method doGetContext appears to be unused throughout the codebase. It's a non-static wrapper around the static getContext method but serves no clear purpose in this PR. Consider removing it unless there's a specific planned use case.

Suggested change
public Context doGetContext(ContextFactory contextFactory) {
return JavaScriptScopeUtil.getContext(contextFactory);
}

Copilot uses AI. Check for mistakes.
*
* TrackingEnhancer
*/
private class TrackingEnhancer {
Copy link

Copilot AI Dec 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TrackingEnhancer should be made static, since the enclosing instance is not used.

Suggested change
private class TrackingEnhancer {
private static class TrackingEnhancer {

Copilot uses AI. Check for mistakes.
*
* @see #routeMessageByChannelId(String, Object, Map, Collection)
*/
public Response routeMessageByChannelId(String channelId, Object message) {
Copy link

Copilot AI Dec 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method VMRouter.routeMessageByChannelId(..) could be confused with overloaded method routeMessageByChannelId, since dispatch depends on static types.

Copilot uses AI. Check for mistakes.
* @return The {@link Response} object returned by the channel, if its source
* connector is configured to return one.
*/
public Response routeMessageByChannelId(String channelId, RawMessage rawMessage) {
Copy link

Copilot AI Dec 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method VMRouter.routeMessageByChannelId(..) could be confused with overloaded method routeMessageByChannelId, since dispatch depends on static types.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants