Search in sources :

Example 1 with CamelBreakpoint

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

the class CamelDebuggerSession method getCamelBreakpointById.

@Nullable
private CamelBreakpoint getCamelBreakpointById(String id) throws Exception {
    String path = "//*[@id='" + id + "']";
    // Find node with this ID in the document
    XPath xPath = XPathFactory.newInstance().newXPath();
    Node tagNode = (Node) xPath.evaluate(path, routesDOMDocument, XPathConstants.NODE);
    if (tagNode == null) {
        return null;
    }
    Element tag = (Element) tagNode;
    String filePath = tag.getAttribute("sourceLocation");
    String lineNumber = tag.getAttribute("sourceLineNumber");
    if (StringUtils.isEmpty(filePath)) {
        return null;
    }
    if (StringUtils.isEmpty(lineNumber) || "-1".equals(lineNumber.trim())) {
        return null;
    }
    String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
    if (!filePath.startsWith("file:") && !filePath.startsWith("classpath:")) {
        // This is Java class
        PsiClass psiClass = JavaPsiFacade.getInstance(project).findClass(fileName, GlobalSearchScope.everythingScope(project));
        VirtualFile virtualFile = psiClass.getContainingFile().getVirtualFile();
        XSourcePosition position = XDebuggerUtil.getInstance().createPosition(virtualFile, new Integer(lineNumber) - 1);
        Map<String, PsiElement> breakpointElement = createBreakpointElementFromPosition(position);
        return new CamelBreakpoint(id, breakpointElement.get(id), position);
    } else {
        PsiFile[] psiFiles = FilenameIndex.getFilesByName(project, fileName, GlobalSearchScope.everythingScope(project));
        if (psiFiles == null || psiFiles.length < 1) {
            return null;
        }
        for (PsiFile psiFile : psiFiles) {
            VirtualFile virtualFile = psiFile.getVirtualFile();
            String url = virtualFile.getPresentableUrl();
            if (virtualFile.isInLocalFileSystem()) {
                // TODO - we need a better way to match source to target
                // file:/absolute/path/to/file.xml
                url = "file:" + url.replace("src/main/resources", "target/classes");
            } else {
                // Then it must be a Jar
                url = "classpath:" + url.substring(url.lastIndexOf("!") + 2);
            }
            if (filePath.equals(url)) {
                // We found our file, let's get a source position
                XSourcePosition position = XDebuggerUtil.getInstance().createPosition(virtualFile, new Integer(lineNumber) - 1);
                Map<String, PsiElement> breakpointElement = createBreakpointElementFromPosition(position);
                return new CamelBreakpoint(id, breakpointElement.get(id), position);
            }
        }
    }
    return null;
}
Also used : XPath(javax.xml.xpath.XPath) VirtualFile(com.intellij.openapi.vfs.VirtualFile) CamelBreakpoint(com.github.cameltooling.idea.runner.debugger.breakpoint.CamelBreakpoint) Node(org.w3c.dom.Node) PsiElement(com.intellij.psi.PsiElement) Element(org.w3c.dom.Element) PsiClass(com.intellij.psi.PsiClass) PsiFile(com.intellij.psi.PsiFile) XSourcePosition(com.intellij.xdebugger.XSourcePosition) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with CamelBreakpoint

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

the class CamelDebuggerSession method toggleBreakpoint.

private boolean toggleBreakpoint(@NotNull XLineBreakpoint<XBreakpointProperties> xBreakpoint, boolean toggleOn) {
    XSourcePosition position = xBreakpoint.getSourcePosition();
    Map<String, PsiElement> breakpointElement = createBreakpointElementFromPosition(position);
    if (breakpointElement != null) {
        String breakpointId = breakpointElement.keySet().iterator().next();
        PsiElement psiElement = breakpointElement.get(breakpointId);
        if (toggleOn) {
            XExpression condition = xBreakpoint.getConditionExpression();
            if (condition == null) {
                backlogDebugger.addBreakpoint(breakpointId);
            } else {
                backlogDebugger.addConditionalBreakpoint(breakpointId, condition.getLanguage().getID(), condition.getExpression());
            }
            explicitBreakpointIDs.add(breakpointId);
        } else {
            backlogDebugger.removeBreakpoint(breakpointId);
            explicitBreakpointIDs.remove(breakpointId);
        }
        breakpoints.put(breakpointId, new CamelBreakpoint(breakpointId, psiElement, position));
        return true;
    }
    // Breakpoint is invalid
    xDebugSession.setBreakpointInvalid(xBreakpoint, "Camel EIP ID not found");
    return false;
}
Also used : CamelBreakpoint(com.github.cameltooling.idea.runner.debugger.breakpoint.CamelBreakpoint) XExpression(com.intellij.xdebugger.XExpression) XSourcePosition(com.intellij.xdebugger.XSourcePosition) PsiElement(com.intellij.psi.PsiElement)

Example 3 with CamelBreakpoint

use of com.github.cameltooling.idea.runner.debugger.breakpoint.CamelBreakpoint 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 CamelBreakpoint

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

the class CamelDebuggerSession method nextStep.

private void nextStep(XSourcePosition position, boolean isOver) {
    Map<String, PsiElement> breakpointElement = createBreakpointElementFromPosition(position);
    if (breakpointElement == null) {
        // TODO Log warning
        return;
    }
    String breakpointId = breakpointElement.keySet().iterator().next();
    PsiElement breakpointTag = breakpointElement.get(breakpointId);
    breakpoints.put(breakpointId, new CamelBreakpoint(breakpointId, breakpointTag, position));
    String name = breakpointTag instanceof XmlTag ? ((XmlTag) breakpointTag).getLocalName() : breakpointTag.getText();
    if (isOver && ("to".equals(name) || "toD".equals(name))) {
        String newTemporaryBreakpointId = getSiblingId(breakpointId);
        if (newTemporaryBreakpointId != null) {
            // Add temporary breakpoint
            backlogDebugger.addBreakpoint(newTemporaryBreakpointId);
            // Run to that breakpoint
            backlogDebugger.resumeBreakpoint(breakpointId);
            if (temporaryBreakpointId != null && !explicitBreakpointIDs.contains(temporaryBreakpointId) && !newTemporaryBreakpointId.equals(temporaryBreakpointId)) {
                // Remove previous temporary breakpoint
                backlogDebugger.removeBreakpoint(temporaryBreakpointId);
            }
            temporaryBreakpointId = newTemporaryBreakpointId;
        } else {
            // This was the last one
            resume();
        }
    } else {
        backlogDebugger.stepBreakpoint(breakpointId);
    }
}
Also used : CamelBreakpoint(com.github.cameltooling.idea.runner.debugger.breakpoint.CamelBreakpoint) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 5 with CamelBreakpoint

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

the class CamelDebuggerSession method runToPosition.

public void runToPosition(XSourcePosition fromPosition, XSourcePosition toPosition) {
    Map<String, PsiElement> fromBreakpointElement = createBreakpointElementFromPosition(fromPosition);
    Map<String, PsiElement> toBreakpointElement = createBreakpointElementFromPosition(toPosition);
    if (toBreakpointElement == null) {
        // this is not a tag
        return;
    }
    String toBreakpointId = toBreakpointElement.keySet().iterator().next();
    String fromBreakpointId = fromBreakpointElement.keySet().iterator().next();
    breakpoints.put(toBreakpointId, new CamelBreakpoint(toBreakpointId, toBreakpointElement.get(toBreakpointId), toPosition));
    backlogDebugger.addBreakpoint(toBreakpointId);
    // Run to that breakpoint
    backlogDebugger.resumeBreakpoint(fromBreakpointId);
    if (temporaryBreakpointId != null && !explicitBreakpointIDs.contains(temporaryBreakpointId) && !toBreakpointId.equals(temporaryBreakpointId)) {
        // Remove previous temporary breakpoint
        backlogDebugger.removeBreakpoint(temporaryBreakpointId);
    }
    temporaryBreakpointId = toBreakpointId;
}
Also used : CamelBreakpoint(com.github.cameltooling.idea.runner.debugger.breakpoint.CamelBreakpoint) PsiElement(com.intellij.psi.PsiElement)

Aggregations

CamelBreakpoint (com.github.cameltooling.idea.runner.debugger.breakpoint.CamelBreakpoint)6 PsiElement (com.intellij.psi.PsiElement)5 CamelMessageInfo (com.github.cameltooling.idea.runner.debugger.stack.CamelMessageInfo)2 XSourcePosition (com.intellij.xdebugger.XSourcePosition)2 Element (org.w3c.dom.Element)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiClass (com.intellij.psi.PsiClass)1 PsiFile (com.intellij.psi.PsiFile)1 XmlTag (com.intellij.psi.xml.XmlTag)1 XExpression (com.intellij.xdebugger.XExpression)1 XLineBreakpoint (com.intellij.xdebugger.breakpoints.XLineBreakpoint)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 MBeanException (javax.management.MBeanException)1 ReflectionException (javax.management.ReflectionException)1 XPath (javax.xml.xpath.XPath)1 XPathExpressionException (javax.xml.xpath.XPathExpressionException)1 Nullable (org.jetbrains.annotations.Nullable)1