Search in sources :

Example 6 with ManagedRouteMBean

use of org.apache.camel.api.management.mbean.ManagedRouteMBean in project camel by apache.

the class RouteCoverageXmlParser method parseXml.

/**
     * Parses the XML.
     *
     * @param camelContext the CamelContext
     * @param is           the XML content as an input stream
     * @return the DOM model of the routes with coverage information stored as attributes
     * @throws Exception is thrown if error parsing
     */
public static Document parseXml(final CamelContext camelContext, final InputStream is) throws Exception {
    final SAXParserFactory factory = SAXParserFactory.newInstance();
    final SAXParser parser = factory.newSAXParser();
    final DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    final DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    final Document doc = docBuilder.newDocument();
    final Stack<Element> elementStack = new Stack<Element>();
    final StringBuilder textBuffer = new StringBuilder();
    final DefaultHandler handler = new DefaultHandler() {

        @Override
        public void setDocumentLocator(final Locator locator) {
        // noop
        }

        @Override
        public void startElement(final String uri, final String localName, final String qName, final Attributes attributes) throws SAXException {
            addTextIfNeeded();
            final Element el = doc.createElement(qName);
            // add other elements
            for (int i = 0; i < attributes.getLength(); i++) {
                el.setAttribute(attributes.getQName(i), attributes.getValue(i));
            }
            String id = el.getAttribute("id");
            if (id != null) {
                try {
                    if ("route".equals(qName)) {
                        ManagedRouteMBean route = camelContext.getManagedRoute(id, ManagedRouteMBean.class);
                        if (route != null) {
                            long total = route.getExchangesTotal();
                            el.setAttribute("exchangesTotal", "" + total);
                            long totalTime = route.getTotalProcessingTime();
                            el.setAttribute("totalProcessingTime", "" + totalTime);
                        }
                    } else if ("from".equals(qName)) {
                    // TODO: include the stats from the route mbean as that would be the same
                    } else {
                        ManagedProcessorMBean processor = camelContext.getManagedProcessor(id, ManagedProcessorMBean.class);
                        if (processor != null) {
                            long total = processor.getExchangesTotal();
                            el.setAttribute("exchangesTotal", "" + total);
                            long totalTime = processor.getTotalProcessingTime();
                            el.setAttribute("totalProcessingTime", "" + totalTime);
                        }
                    }
                } catch (Exception e) {
                // ignore
                }
            }
            // we do not want customId in output
            el.removeAttribute("customId");
            elementStack.push(el);
        }

        @Override
        public void endElement(final String uri, final String localName, final String qName) {
            addTextIfNeeded();
            final Element closedEl = elementStack.pop();
            if (elementStack.isEmpty()) {
                // is this the root element?
                doc.appendChild(closedEl);
            } else {
                final Element parentEl = elementStack.peek();
                parentEl.appendChild(closedEl);
            }
        }

        @Override
        public void characters(final char[] ch, final int start, final int length) throws SAXException {
            textBuffer.append(ch, start, length);
        }

        /**
             * outputs text accumulated under the current node
             */
        private void addTextIfNeeded() {
            if (textBuffer.length() > 0) {
                final Element el = elementStack.peek();
                final Node textNode = doc.createTextNode(textBuffer.toString());
                el.appendChild(textNode);
                textBuffer.delete(0, textBuffer.length());
            }
        }
    };
    parser.parse(is, handler);
    return doc;
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) ManagedRouteMBean(org.apache.camel.api.management.mbean.ManagedRouteMBean) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) Attributes(org.xml.sax.Attributes) ManagedProcessorMBean(org.apache.camel.api.management.mbean.ManagedProcessorMBean) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) Stack(java.util.Stack) DefaultHandler(org.xml.sax.helpers.DefaultHandler) Locator(org.xml.sax.Locator) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParser(javax.xml.parsers.SAXParser) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Aggregations

ManagedRouteMBean (org.apache.camel.api.management.mbean.ManagedRouteMBean)6 ArrayList (java.util.ArrayList)3 MBeanServer (javax.management.MBeanServer)3 ManagedProcessorMBean (org.apache.camel.api.management.mbean.ManagedProcessorMBean)3 ObjectName (javax.management.ObjectName)2 Route (org.apache.camel.Route)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Stack (java.util.Stack)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 SAXParser (javax.xml.parsers.SAXParser)1 SAXParserFactory (javax.xml.parsers.SAXParserFactory)1 CamelContext (org.apache.camel.CamelContext)1 Endpoint (org.apache.camel.Endpoint)1 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)1 Test (org.junit.Test)1 Document (org.w3c.dom.Document)1