Search in sources :

Example 1 with CamelMessageInfo

use of com.github.cameltooling.idea.runner.debugger.stack.CamelMessageInfo in project camel-idea-plugin by camel-tooling.

the class CamelDebuggerRunner method attachVM.

private RunContentDescriptor attachVM(final RunProfileState state, @NotNull final ExecutionEnvironment env, RemoteConnection connection, boolean pollConnection) throws ExecutionException {
    LOG.debug("Attaching VM...");
    DefaultDebugEnvironment environment = new DefaultDebugEnvironment(env, state, connection, pollConnection);
    final DebuggerSession debuggerSession = DebuggerManagerEx.getInstanceEx(env.getProject()).attachVirtualMachine(environment);
    final CamelDebuggerSession camelDebuggerSession = new CamelDebuggerSession();
    if (debuggerSession == null) {
        return null;
    } else {
        final DebugProcessImpl debugProcess = debuggerSession.getProcess();
        if (!debugProcess.isDetached() && !debugProcess.isDetaching()) {
            if (environment.isRemote()) {
                debugProcess.putUserData(BatchEvaluator.REMOTE_SESSION_KEY, Boolean.TRUE);
            }
            return XDebuggerManager.getInstance(env.getProject()).startSession(env, new XDebugProcessStarter() {

                @NotNull
                public XDebugProcess start(@NotNull XDebugSession session) {
                    final XDebugSessionImpl sessionImpl = (XDebugSessionImpl) session;
                    final ExecutionResult executionResult = debugProcess.getExecutionResult();
                    final Map<String, XDebugProcess> context = new HashMap<>();
                    final ContextAwareDebugProcess contextAwareDebugProcess = new ContextAwareDebugProcess(session, executionResult.getProcessHandler(), context, JAVA_CONTEXT);
                    camelDebuggerSession.addMessageReceivedListener(new MessageReceivedListener() {

                        @Override
                        public void onNewMessageReceived(CamelMessageInfo camelMessageInfo) {
                            contextAwareDebugProcess.setContext(CAMEL_CONTEXT);
                        }
                    });
                    debuggerSession.getContextManager().addListener((newContext, event) -> contextAwareDebugProcess.setContext(JAVA_CONTEXT));
                    // Init Java Debug Process
                    sessionImpl.addExtraActions(executionResult.getActions());
                    if (executionResult instanceof DefaultExecutionResult) {
                        sessionImpl.addRestartActions(((DefaultExecutionResult) executionResult).getRestartActions());
                    // sessionImpl.addExtraStopActions(((DefaultExecutionResult) executionResult).getAdditionalStopActions());
                    }
                    final JavaDebugProcess javaDebugProcess = JavaDebugProcess.create(session, debuggerSession);
                    // Init Camel Debug Process
                    final CamelDebugProcess camelDebugProcess = new CamelDebugProcess(session, camelDebuggerSession, javaDebugProcess.getProcessHandler());
                    // Register All Processes
                    context.put(JAVA_CONTEXT, javaDebugProcess);
                    context.put(CAMEL_CONTEXT, camelDebugProcess);
                    return contextAwareDebugProcess;
                }
            }).getRunContentDescriptor();
        } else {
            debuggerSession.dispose();
            camelDebuggerSession.dispose();
            return null;
        }
    }
}
Also used : XDebugSession(com.intellij.xdebugger.XDebugSession) XDebugProcess(com.intellij.xdebugger.XDebugProcess) CamelMessageInfo(com.github.cameltooling.idea.runner.debugger.stack.CamelMessageInfo) DefaultExecutionResult(com.intellij.execution.DefaultExecutionResult) HashMap(java.util.HashMap) JavaDebugProcess(com.intellij.debugger.engine.JavaDebugProcess) ExecutionResult(com.intellij.execution.ExecutionResult) DefaultExecutionResult(com.intellij.execution.DefaultExecutionResult) NotNull(org.jetbrains.annotations.NotNull) DefaultDebugEnvironment(com.intellij.debugger.DefaultDebugEnvironment) DebuggerSession(com.intellij.debugger.impl.DebuggerSession) DebugProcessImpl(com.intellij.debugger.engine.DebugProcessImpl) XDebugProcessStarter(com.intellij.xdebugger.XDebugProcessStarter) XDebugSessionImpl(com.intellij.xdebugger.impl.XDebugSessionImpl)

Example 2 with CamelMessageInfo

use of com.github.cameltooling.idea.runner.debugger.stack.CamelMessageInfo in project camel-idea-plugin by camel-tooling.

the class CamelDebuggerSession method stepOut.

public void stepOut(XSourcePosition position) {
    try {
        Map<String, PsiElement> breakpointElement = createBreakpointElementFromPosition(position);
        if (breakpointElement == null) {
            // TODO Log warning
            return;
        }
        String breakpointId = breakpointElement.keySet().iterator().next();
        // Get the route of the current tag
        Element routeElement = getParentRouteId(breakpointId);
        String routeId = routeElement.getAttribute("id");
        // Get current stack and find the caller in the stack
        List<CamelMessageInfo> stack = getStack(breakpointId, backlogDebugger.dumpTracedMessagesAsXml(breakpointId));
        CamelMessageInfo callerStackFrame = stack.stream().filter(info -> !info.getRouteId().equals(routeId) && info.getProcessorId().startsWith("to")).findFirst().orElse(null);
        if (callerStackFrame == null) {
            // This is the top route
            resume();
        } else {
            String newTemporaryBreakpointId = getSiblingId(callerStackFrame.getProcessorId());
            if (newTemporaryBreakpointId != null) {
                // Add temporary breakpoint
                backlogDebugger.addBreakpoint(newTemporaryBreakpointId);
                // Run to that breakpoint
                backlogDebugger.resumeBreakpoint(breakpointId);
                if (temporaryBreakpointId != null && !explicitBreakpointIDs.contains(temporaryBreakpointId) && !temporaryBreakpointId.equals(newTemporaryBreakpointId)) {
                    // Remove previous temporary breakpoint
                    backlogDebugger.removeBreakpoint(temporaryBreakpointId);
                }
                temporaryBreakpointId = newTemporaryBreakpointId;
            } else {
                // This was the last one
                resume();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : CamelMessageInfo(com.github.cameltooling.idea.runner.debugger.stack.CamelMessageInfo) PsiElement(com.intellij.psi.PsiElement) Element(org.w3c.dom.Element) PsiElement(com.intellij.psi.PsiElement) XPathExpressionException(javax.xml.xpath.XPathExpressionException) ReflectionException(javax.management.ReflectionException) MBeanException(javax.management.MBeanException)

Example 3 with CamelMessageInfo

use of com.github.cameltooling.idea.runner.debugger.stack.CamelMessageInfo in project camel-idea-plugin by camel-tooling.

the class CamelDebuggerSession method checkSuspendedBreakpoints.

private void checkSuspendedBreakpoints() {
    while (isConnected()) {
        try {
            Collection<String> suspendedBreakpointIDs = (Collection<String>) serverConnection.invoke(this.debuggerMBeanObjectName, "getSuspendedBreakpointNodeIds", new Object[] {}, new String[] {});
            if (suspendedBreakpointIDs != null && !suspendedBreakpointIDs.isEmpty()) {
                // Fire notifications here, we need to display the exchange, stack etc
                for (String id : suspendedBreakpointIDs) {
                    String xml = backlogDebugger.dumpTracedMessagesAsXml(id);
                    try {
                        // If the Camel version is 3.15 or later, the exchange properties are included
                        xml = (String) serverConnection.invoke(this.debuggerMBeanObjectName, "dumpTracedMessagesAsXml", new Object[] { id, true }, new String[] { "java.lang.String", "boolean" });
                    } catch (Exception e) {
                    // TODO log this or display warning
                    }
                    final String suspendedMessage = xml;
                    ApplicationManager.getApplication().runReadAction(() -> {
                        for (MessageReceivedListener listener : messageReceivedListeners) {
                            try {
                                CamelBreakpoint breakpoint = breakpoints.get(id);
                                if (breakpoint == null) {
                                    // find tag and source position based on ID
                                    breakpoint = getCamelBreakpointById(id);
                                    breakpoints.put(id, breakpoint);
                                }
                                List<CamelMessageInfo> stack = getStack(id, suspendedMessage);
                                // We only need stack for the top frame
                                CamelMessageInfo info = stack.get(0);
                                info.setStack(stack);
                                listener.onNewMessageReceived(info);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
            }
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
            } finally {
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Also used : CamelMessageInfo(com.github.cameltooling.idea.runner.debugger.stack.CamelMessageInfo) CamelBreakpoint(com.github.cameltooling.idea.runner.debugger.breakpoint.CamelBreakpoint) Collection(java.util.Collection) XPathExpressionException(javax.xml.xpath.XPathExpressionException) ReflectionException(javax.management.ReflectionException) MBeanException(javax.management.MBeanException)

Example 4 with CamelMessageInfo

use of com.github.cameltooling.idea.runner.debugger.stack.CamelMessageInfo in project camel-idea-plugin by camel-tooling.

the class CamelDebugProcess method init.

public void init() {
    camelDebuggerSession.setXDebugSession(this.getSession());
    camelDebuggerSession.connect(javaProcessHandler);
    camelDebuggerSession.addMessageReceivedListener(new MessageReceivedListener() {

        @Override
        public void onNewMessageReceived(CamelMessageInfo camelMessageInfo) {
            XSourcePosition topPosition = getSession().getTopFramePosition();
            if (forceRefresh || topPosition == null || !topPosition.equals(camelMessageInfo.getXSourcePosition())) {
                forceRefresh = false;
                // List frames
                List<CamelStackFrame> stackFrames = new ArrayList<CamelStackFrame>();
                for (CamelMessageInfo info : camelMessageInfo.getStack()) {
                    CamelStackFrame nextFrame = new CamelStackFrame(getSession().getProject(), camelDebuggerSession, info);
                    stackFrames.add(nextFrame);
                }
                getSession().positionReached(new CamelSuspendContext(stackFrames.toArray(new CamelStackFrame[stackFrames.size()])));
            }
        }
    });
}
Also used : CamelMessageInfo(com.github.cameltooling.idea.runner.debugger.stack.CamelMessageInfo) ArrayList(java.util.ArrayList) List(java.util.List) XSourcePosition(com.intellij.xdebugger.XSourcePosition) CamelStackFrame(com.github.cameltooling.idea.runner.debugger.stack.CamelStackFrame)

Example 5 with CamelMessageInfo

use of com.github.cameltooling.idea.runner.debugger.stack.CamelMessageInfo in project camel-idea-plugin by camel-tooling.

the class CamelDebuggerSession method getStack.

private List<CamelMessageInfo> getStack(String breakpointId, String suspendedMessage) throws Exception {
    List<CamelMessageInfo> stack = new ArrayList<>();
    // Use new operation to retrieve message history
    String messageHistory = (String) serverConnection.invoke(this.debuggerMBeanObjectName, "messageHistoryOnBreakpointAsXml", new Object[] { breakpointId }, new String[] { "java.lang.String" });
    InputStream targetStream = new ByteArrayInputStream(messageHistory.getBytes());
    Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(targetStream);
    NodeList historyEntries = document.getElementsByTagName("messageHistoryEntry");
    for (int i = 0; i < historyEntries.getLength(); i++) {
        Element nextEntry = (Element) historyEntries.item(i);
        String routeId = nextEntry.getAttribute("routeId");
        String processorId = nextEntry.getAttribute("processorId");
        String processor = nextEntry.getAttribute("processor");
        CamelBreakpoint breakpoint = breakpoints.get(processorId);
        if (breakpoint == null) {
            // find tag and source position based on ID
            breakpoint = getCamelBreakpointById(processorId);
        }
        if (breakpoint != null) {
            breakpoints.put(processorId, breakpoint);
            CamelMessageInfo info = new CamelMessageInfo(suspendedMessage, breakpoint.getXSourcePosition(), breakpoint.getBreakpointTag(), routeId, processorId, processor, null);
            stack.add(info);
        }
    }
    Collections.reverse(stack);
    return stack;
}
Also used : CamelMessageInfo(com.github.cameltooling.idea.runner.debugger.stack.CamelMessageInfo) ByteArrayInputStream(java.io.ByteArrayInputStream) CamelBreakpoint(com.github.cameltooling.idea.runner.debugger.breakpoint.CamelBreakpoint) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) PsiElement(com.intellij.psi.PsiElement) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) XLineBreakpoint(com.intellij.xdebugger.breakpoints.XLineBreakpoint) CamelBreakpoint(com.github.cameltooling.idea.runner.debugger.breakpoint.CamelBreakpoint)

Aggregations

CamelMessageInfo (com.github.cameltooling.idea.runner.debugger.stack.CamelMessageInfo)5 CamelBreakpoint (com.github.cameltooling.idea.runner.debugger.breakpoint.CamelBreakpoint)2 PsiElement (com.intellij.psi.PsiElement)2 ArrayList (java.util.ArrayList)2 MBeanException (javax.management.MBeanException)2 ReflectionException (javax.management.ReflectionException)2 XPathExpressionException (javax.xml.xpath.XPathExpressionException)2 Element (org.w3c.dom.Element)2 CamelStackFrame (com.github.cameltooling.idea.runner.debugger.stack.CamelStackFrame)1 DefaultDebugEnvironment (com.intellij.debugger.DefaultDebugEnvironment)1 DebugProcessImpl (com.intellij.debugger.engine.DebugProcessImpl)1 JavaDebugProcess (com.intellij.debugger.engine.JavaDebugProcess)1 DebuggerSession (com.intellij.debugger.impl.DebuggerSession)1 DefaultExecutionResult (com.intellij.execution.DefaultExecutionResult)1 ExecutionResult (com.intellij.execution.ExecutionResult)1 XDebugProcess (com.intellij.xdebugger.XDebugProcess)1 XDebugProcessStarter (com.intellij.xdebugger.XDebugProcessStarter)1 XDebugSession (com.intellij.xdebugger.XDebugSession)1 XSourcePosition (com.intellij.xdebugger.XSourcePosition)1 XLineBreakpoint (com.intellij.xdebugger.breakpoints.XLineBreakpoint)1