Skip to content

Commit

Permalink
Adapt to Camel 4.2.0
Browse files Browse the repository at this point in the history
* Adapt to API changes
* ExchangeProperties moved to Message/ExchangeProperties.
ExchangeProperties from 4.1- are no more provided
* setSourceLocationEnabled must be explicitly set starting with Camel
4.2 when using Java DSL and in the tests despite the camel-debug jar
being on the classpath

Signed-off-by: Aurélien Pupier <[email protected]>
  • Loading branch information
apupier committed Nov 21, 2023
1 parent 43ed795 commit 21e5f38
Show file tree
Hide file tree
Showing 22 changed files with 125 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ public boolean attach(Map<String, Object> args, IDebugProtocolClient client) {
ObjectName debuggerMBeanObjectName = names.iterator().next();
backlogDebugger = JMX.newMBeanProxy(mbeanConnection, debuggerMBeanObjectName,
ManagedBacklogDebuggerMBean.class);
try {
backlogDebugger.setIncludeExchangeProperties(true);
} catch(Exception ex) {
// Ignore, we might be connected to pre 4.2 Camel version
System.out.println(ex);
}
backlogDebugger.enableDebugger();
routesDOMDocument = retrieveRoutesWithSourceLineNumber(jmxAddress);

Expand Down Expand Up @@ -194,7 +200,7 @@ private void handleSuspendedBreakpoint(String nodeId) {
if (!isStepping && !notifiedSuspendedBreakpointIds.contains(nodeId)) {
StoppedEventArguments stoppedEventArgs = new StoppedEventArguments();
stoppedEventArgs.setReason(StoppedEventArgumentsReason.BREAKPOINT);
String xml = backlogDebugger.dumpTracedMessagesAsXml(nodeId, true);
String xml = backlogDebugger.dumpTracedMessagesAsXml(nodeId);
EventMessage eventMessage = new UnmarshallerEventMessage().getUnmarshalledEventMessage(xml);
Optional<CamelThread> thread = camelThreads.stream().filter(camelThread -> camelThread.getExchangeId().equals(eventMessage.getExchangeId())).findAny();
if(thread.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@
import com.github.cameltooling.dap.internal.IdUtils;
import com.github.cameltooling.dap.internal.model.CamelScope;
import com.github.cameltooling.dap.internal.model.CamelStackFrame;
import com.github.cameltooling.dap.internal.model.variables.exchange.ExchangePropertiesVariable;
import com.github.cameltooling.dap.internal.types.EventMessage;
import com.github.cameltooling.dap.internal.types.UnmarshallerEventMessage;

public class CamelExchangeScope extends CamelScope {

public static final String NAME = "Exchange";
private ExchangePropertiesVariable exchangeVariable;

public CamelExchangeScope(CamelStackFrame stackframe) {
super(NAME, stackframe.getName(), IdUtils.getPositiveIntFromHashCode((stackframe.getId()+"@Exchange@" + stackframe.getName()).hashCode()));
Expand All @@ -44,35 +42,19 @@ public CamelExchangeScope(CamelStackFrame stackframe) {
public Set<Variable> createVariables(int variablesReference, ManagedBacklogDebuggerMBean debugger) {
Set<Variable> variables = new HashSet<>();
if (variablesReference == getVariablesReference()) {
String xml = debugger.dumpTracedMessagesAsXml(getBreakpointId(), true);
String xml = debugger.dumpTracedMessagesAsXml(getBreakpointId());
EventMessage eventMessage = new UnmarshallerEventMessage().getUnmarshalledEventMessage(xml);
if (eventMessage != null) {
variables.add(createVariable("ID", eventMessage.getExchangeId()));
variables.add(createVariable("To node", eventMessage.getToNode()));
variables.add(createVariable("Route ID", eventMessage.getRouteId()));
exchangeVariable = new ExchangePropertiesVariable(variablesReference, eventMessage.getExchangeProperties(), getBreakpointId());
variables.add(exchangeVariable);
}
} else {
if (exchangeVariable != null && variablesReference == exchangeVariable.getVariablesReference()) {
variables.addAll(exchangeVariable.createVariables());
}
}
return variables;
}

@Override
public SetVariableResponse setVariableIfInScope(SetVariableArguments args, ManagedBacklogDebuggerMBean backlogDebugger) {
if (getVariablesReference() == args.getVariablesReference()) {
throw new UnsupportedOperationException("Not supported");
} else if (exchangeVariable == null) {
return null;
}
return exchangeVariable.setVariableIfInScope(args, backlogDebugger);
return null;
}

public ExchangePropertiesVariable getExchangePropertiesVariable() {
return exchangeVariable;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.github.cameltooling.dap.internal.model.CamelScope;
import com.github.cameltooling.dap.internal.model.CamelStackFrame;
import com.github.cameltooling.dap.internal.model.variables.message.MessageBodyCamelVariable;
import com.github.cameltooling.dap.internal.model.variables.message.MessageExchangePropertiesVariable;
import com.github.cameltooling.dap.internal.model.variables.message.MessageHeadersVariable;
import com.github.cameltooling.dap.internal.types.EventMessage;
import com.github.cameltooling.dap.internal.types.UnmarshallerEventMessage;
Expand All @@ -37,6 +38,7 @@ public class CamelMessageScope extends CamelScope {
public static final String NAME = "Message";
private MessageBodyCamelVariable messageBody;
private MessageHeadersVariable headersVariable;
private MessageExchangePropertiesVariable exchangePropertiesVariable;

public CamelMessageScope(CamelStackFrame stackframe) {
super(NAME, stackframe.getName(), IdUtils.getPositiveIntFromHashCode((stackframe.getId()+"@Message@" + stackframe.getName()).hashCode()));
Expand All @@ -46,19 +48,24 @@ public CamelMessageScope(CamelStackFrame stackframe) {
public Set<Variable> createVariables(int variablesReference, ManagedBacklogDebuggerMBean debugger) {
Set<Variable> variables = new HashSet<>();
if (variablesReference == getVariablesReference()) {
String xml = debugger.dumpTracedMessagesAsXml(getBreakpointId(), true);
String xml = debugger.dumpTracedMessagesAsXml(getBreakpointId());
EventMessage eventMessage = new UnmarshallerEventMessage().getUnmarshalledEventMessage(xml);
if(eventMessage != null) {
variables.add(createVariable("Exchange ID", eventMessage.getExchangeId()));
messageBody = new MessageBodyCamelVariable(getBreakpointId(), eventMessage.getMessage().getBody());
variables.add(messageBody);
headersVariable = new MessageHeadersVariable(variablesReference, eventMessage.getMessage().getHeaders(), getBreakpointId());
variables.add(headersVariable);
exchangePropertiesVariable = new MessageExchangePropertiesVariable(variablesReference, eventMessage.getMessage().getExchangeProperties(), getBreakpointId());
variables.add(getExchangePropertiesVariable());
}
} else {
if (headersVariable != null && variablesReference == headersVariable.getVariablesReference()) {
variables.addAll(headersVariable.createVariables());
}
if (getExchangePropertiesVariable() != null && variablesReference == getExchangePropertiesVariable().getVariablesReference()) {
variables.addAll(getExchangePropertiesVariable().createVariables());
}
}
return variables;
}
Expand All @@ -74,14 +81,25 @@ public SetVariableResponse setVariableIfInScope(SetVariableArguments args, Manag
} else {
throw new UnsupportedOperationException("Not supported");
}
} else if (headersVariable == null) {
return null;
}
return headersVariable.setVariableIfInScope(args, debugger);
if (headersVariable != null) {
SetVariableResponse response = headersVariable.setVariableIfInScope(args, debugger);
if (response != null) {
return response;
}
}
if (getExchangePropertiesVariable() != null) {
return getExchangePropertiesVariable().setVariableIfInScope(args, debugger);
}
return null;
}

public MessageHeadersVariable getHeadersVariable() {
return headersVariable;
}

public MessageExchangePropertiesVariable getExchangePropertiesVariable() {
return exchangePropertiesVariable;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.cameltooling.dap.internal.model.variables.exchange;
package com.github.cameltooling.dap.internal.model.variables.message;

import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -28,12 +28,15 @@
import com.github.cameltooling.dap.internal.IdUtils;
import com.github.cameltooling.dap.internal.types.ExchangeProperty;

public class ExchangePropertiesVariable extends Variable {
/**
* This is used pre-Camel 4.2 only (not included)
*/
public class MessageExchangePropertiesVariable extends Variable {

private final List<ExchangeProperty> exchangeProperties;
private final String breakpointId;

public ExchangePropertiesVariable(int parentVariablesReference, List<ExchangeProperty> exchangeProperties, String breakpointId) {
public MessageExchangePropertiesVariable(int parentVariablesReference, List<ExchangeProperty> exchangeProperties, String breakpointId) {
this.exchangeProperties = exchangeProperties;
this.breakpointId = breakpointId;
setName("Properties");
Expand All @@ -46,7 +49,7 @@ public Collection<Variable> createVariables() {
Collection<Variable> variables = new ArrayList<>();
if (exchangeProperties != null) {
for (ExchangeProperty exchangeProperty : exchangeProperties) {
variables.add(createVariable(exchangeProperty.getName(), exchangeProperty.getContent()));
variables.add(createVariable(exchangeProperty.getKey(), exchangeProperty.getContent()));
}
}
return variables;
Expand All @@ -64,7 +67,7 @@ public SetVariableResponse setVariableIfInScope(SetVariableArguments args, Manag
debugger.setExchangePropertyOnBreakpoint(breakpointId, args.getName(), args.getValue());
if (exchangeProperties != null) {
for (ExchangeProperty exchangeProperty : exchangeProperties) {
if (exchangeProperty.getName().equals(args.getName())) {
if (exchangeProperty.getKey().equals(args.getName())) {
exchangeProperty.setContent(args.getValue());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
*/
package com.github.cameltooling.dap.internal.types;

import java.util.List;
import java.util.Map;

import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlElementWrapper;
import jakarta.xml.bind.annotation.XmlRootElement;

import org.apache.camel.spi.BacklogTracerEventMessage;
Expand All @@ -39,7 +37,6 @@ public class EventMessage implements BacklogTracerEventMessage {
private String toNode;
private String exchangeId;
private Message message;
private List<ExchangeProperty> exchangeProperties;

@XmlElement(name = "uid")
public long getUid() {
Expand Down Expand Up @@ -89,15 +86,6 @@ public void setMessage(Message message) {
this.message = message;
}

@XmlElementWrapper(name = "exchangeProperties")
@XmlElement(name = "exchangeProperty")
public List<ExchangeProperty> getExchangeProperties() {
return exchangeProperties;
}
public void setExchangeProperties(List<ExchangeProperty> exchangeProperties) {
this.exchangeProperties = exchangeProperties;
}

@Override
public String getMessageAsXml() {
throw new UnsupportedOperationException("This class is used only to read message sent from Camel server through JMX");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ public class ExchangeProperty implements Serializable {

private static final long serialVersionUID = 7994857995065989510L;

private String name;
private String key;
private String type;
private String content;

@XmlAttribute(name = "name")
public String getName() {
return name;
@XmlAttribute(name = "key")
public String getKey() {
return key;
}

public void setName(String name) {
this.name = name;
public void setKey(String key) {
this.key = key;
}

@XmlAttribute(name = "type")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class Message implements Serializable {
private String exchangeId;
private List<Header> headers;
private String body;
private List<ExchangeProperty> exchangeProperties;

@XmlAttribute(name = "exchangeId")
public String getExchangeId() {
Expand Down Expand Up @@ -60,4 +61,13 @@ public String getBody() {
public void setBody(String body) {
this.body = body;
}

@XmlElementWrapper(name = "exchangeProperties")
@XmlElement(name = "exchangeProperty")
public List<ExchangeProperty> getExchangeProperties() {
return exchangeProperties;
}
public void setExchangeProperties(List<ExchangeProperty> exchangeProperties) {
this.exchangeProperties = exchangeProperties;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@

import com.github.cameltooling.dap.internal.telemetry.TelemetryEvent;

//@SetSystemProperty(key = "camel.debug.enabled", value = "true")
public abstract class BaseTest {

protected static final int DEFAULT_VARIABLES_NUMBER = 18;
protected static final int DEFAULT_VARIABLES_NUMBER = 16;
protected CamelDebugAdapterServer server;
protected DummyCamelDebugClient clientProxy;
protected CamelContext context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ abstract class BasicDebugFlowTest extends BaseTest {
@Test
void testBasicFlow() throws Exception {
context = new DefaultCamelContext();
context.setSourceLocationEnabled(true);
String routeId = "a-route-id";
String logEndpointId = "testBasicFlow-log-id";
registerRouteToTest(context, routeId, logEndpointId);
Expand Down Expand Up @@ -97,7 +98,7 @@ void testBasicFlow() throws Exception {
await().untilAsserted(() -> assertThat(stackAndData.getScopes()).hasSize(5));
await("handling of stop event response is finished")
.atMost(Duration.ofSeconds(60))
.untilAsserted(() -> assertThat(stackAndData.getVariables()).hasSize(22));
.untilAsserted(() -> assertThat(stackAndData.getVariables()).hasSize(21));
ManagedBacklogDebuggerMBean debugger = server.getConnectionManager().getBacklogDebugger();
List<Variable> variables = stackAndData.getVariables();
assertThat(variables)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ void testInitialize() throws Exception {
@Test
void testAttachToCamelWithPid() throws Exception {
context = new DefaultCamelContext();
context.setSourceLocationEnabled(true);
startBasicRoute(context);
attachWithPid(server);
checkConnectionEstablished();
Expand All @@ -44,6 +45,7 @@ void testAttachToCamelWithPid() throws Exception {
@Test
void testAttachToCamelWithDefaultJMX() throws Exception {
context = new DefaultCamelContext();
context.setSourceLocationEnabled(true);
startBasicRoute(context);
attach(server);
checkConnectionEstablished();
Expand All @@ -52,6 +54,7 @@ void testAttachToCamelWithDefaultJMX() throws Exception {
@Test
void testAttachToCamelWithProvidedJMXURL() throws Exception {
context = new DefaultCamelContext();
context.setSourceLocationEnabled(true);
startBasicRoute(context);
attachWithJMXURL(server, BacklogDebuggerConnectionManager.DEFAULT_JMX_URI);
checkConnectionEstablished();
Expand All @@ -60,6 +63,7 @@ void testAttachToCamelWithProvidedJMXURL() throws Exception {
@Test
void testFailToAttach() throws Exception {
context = new DefaultCamelContext();
context.setSourceLocationEnabled(true);
startBasicRoute(context);
server.attach(Collections.singletonMap(BacklogDebuggerConnectionManager.ATTACH_PARAM_JMX_URL, "invalidUrl")).get();
assertThat(clientProxy.getOutputEventArguments().get(0).getOutput()).contains("Please check that the Camel application under debug has the following requirements:");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class ConditionalBreakpointTest extends BaseTest {
@Test
void testCondition() throws Exception {
context = new DefaultCamelContext();
context.setSourceLocationEnabled(true);
String routeId = "a-route-id";
String startEndpointUri = "direct:testConditionalBreakpoint";
context.addRoutes(new RouteBuilder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class MultipleThreadsTest extends BaseTest {
@Test
void test2SuspendedBreakpointsCreates2Threads() throws Exception {
context = new DefaultCamelContext();
context.setSourceLocationEnabled(true);
String startEndpointUri1 = "direct:testThreads1";
String startEndpointUri2 = "direct:testThreads2";
context.addRoutes(new RouteBuilder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class NextStepTest extends BaseTest {
@Test
void testSteppingInsideRoute() throws Exception {
context = new DefaultCamelContext();
context.setSourceLocationEnabled(true);
String routeId = "a-route-id";
String startEndpointUri = "direct:testResume";
context.addRoutes(new RouteBuilder() {
Expand Down Expand Up @@ -88,6 +89,7 @@ public void configure() throws Exception {
@Test
void testSteppingAtEndOfRoute() throws Exception {
context = new DefaultCamelContext();
context.setSourceLocationEnabled(true);
String routeId = "a-route-id";
String startEndpointUri = "direct:testResume";
context.addRoutes(new RouteBuilder() {
Expand Down Expand Up @@ -133,6 +135,7 @@ public void configure() throws Exception {
@Test
void testSteppingWithExistingBreakpoint() throws Exception {
context = new DefaultCamelContext();
context.setSourceLocationEnabled(true);
String routeId = "a-route-id";
String startEndpointUri = "direct:testResume";
context.addRoutes(new RouteBuilder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class RemoveBreakpointTest extends BaseTest {
@Test
void testRemoveOneBreakpoint() throws Exception {
context = new DefaultCamelContext();
context.setSourceLocationEnabled(true);
String fromUri = "direct:testRemoveBreakpoint";
context.addRoutes(new RouteBuilder() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class ResumeAllTest extends BaseTest {
@Test
void testResumeAndSecondBreakpointHitOfAnotherRouteInstance() throws Exception {
context = new DefaultCamelContext();
context.setSourceLocationEnabled(true);
String routeId = "a-route-id";
String startEndpointUri = "direct:testResume";
context.addRoutes(new RouteBuilder() {
Expand Down Expand Up @@ -93,6 +94,7 @@ private Optional<Variable> findBodyVariableAtIndex(int index) {
@Test
void testResumeAndSecondBreakpointHitOnSameRouteInstance() throws Exception {
context = new DefaultCamelContext();
context.setSourceLocationEnabled(true);
String routeId = "a-route-id";
String startEndpointUri = "direct:testResume";
context.addRoutes(new RouteBuilder() {
Expand Down
Loading

0 comments on commit 21e5f38

Please sign in to comment.