Search in sources :

Example 21 with NodeList

use of org.w3c.dom.NodeList in project hadoop by apache.

the class HostsFileReader method readXmlFileToMapWithFileInputStream.

public static void readXmlFileToMapWithFileInputStream(String type, String filename, InputStream fileInputStream, Map<String, Integer> map) throws IOException {
    Document dom;
    DocumentBuilderFactory builder = DocumentBuilderFactory.newInstance();
    try {
        DocumentBuilder db = builder.newDocumentBuilder();
        dom = db.parse(fileInputStream);
        // Examples:
        // <host><name>host1</name></host>
        // <host><name>host2</name><timeout>123</timeout></host>
        // <host><name>host3</name><timeout>-1</timeout></host>
        // <host><name>host4, host5,host6</name><timeout>1800</timeout></host>
        Element doc = dom.getDocumentElement();
        NodeList nodes = doc.getElementsByTagName("host");
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element e = (Element) node;
                // Support both single host and comma-separated list of hosts.
                String v = readFirstTagValue(e, "name");
                String[] hosts = StringUtils.getTrimmedStrings(v);
                String str = readFirstTagValue(e, "timeout");
                Integer timeout = (str == null) ? null : Integer.parseInt(str);
                for (String host : hosts) {
                    map.put(host, timeout);
                    LOG.info("Adding a node \"" + host + "\" to the list of " + type + " hosts from " + filename);
                }
            }
        }
    } catch (IOException | SAXException | ParserConfigurationException e) {
        LOG.fatal("error parsing " + filename, e);
        throw new RuntimeException(e);
    } finally {
        fileInputStream.close();
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 22 with NodeList

use of org.w3c.dom.NodeList in project hadoop by apache.

the class TestAMWebServicesJobs method verifyAMJobXML.

public void verifyAMJobXML(NodeList nodes, AppContext appContext) {
    assertEquals("incorrect number of elements", 1, nodes.getLength());
    for (int i = 0; i < nodes.getLength(); i++) {
        Element element = (Element) nodes.item(i);
        Job job = appContext.getJob(MRApps.toJobID(WebServicesTestUtils.getXmlString(element, "id")));
        assertNotNull("Job not found - output incorrect", job);
        verifyAMJobGeneric(job, WebServicesTestUtils.getXmlString(element, "id"), WebServicesTestUtils.getXmlString(element, "user"), WebServicesTestUtils.getXmlString(element, "name"), WebServicesTestUtils.getXmlString(element, "state"), WebServicesTestUtils.getXmlLong(element, "startTime"), WebServicesTestUtils.getXmlLong(element, "finishTime"), WebServicesTestUtils.getXmlLong(element, "elapsedTime"), WebServicesTestUtils.getXmlInt(element, "mapsTotal"), WebServicesTestUtils.getXmlInt(element, "mapsCompleted"), WebServicesTestUtils.getXmlInt(element, "reducesTotal"), WebServicesTestUtils.getXmlInt(element, "reducesCompleted"), WebServicesTestUtils.getXmlFloat(element, "reduceProgress"), WebServicesTestUtils.getXmlFloat(element, "mapProgress"));
        // restricted access fields - if security and acls set
        verifyAMJobGenericSecure(job, WebServicesTestUtils.getXmlInt(element, "mapsPending"), WebServicesTestUtils.getXmlInt(element, "mapsRunning"), WebServicesTestUtils.getXmlInt(element, "reducesPending"), WebServicesTestUtils.getXmlInt(element, "reducesRunning"), WebServicesTestUtils.getXmlBoolean(element, "uberized"), WebServicesTestUtils.getXmlString(element, "diagnostics"), WebServicesTestUtils.getXmlInt(element, "newReduceAttempts"), WebServicesTestUtils.getXmlInt(element, "runningReduceAttempts"), WebServicesTestUtils.getXmlInt(element, "failedReduceAttempts"), WebServicesTestUtils.getXmlInt(element, "killedReduceAttempts"), WebServicesTestUtils.getXmlInt(element, "successfulReduceAttempts"), WebServicesTestUtils.getXmlInt(element, "newMapAttempts"), WebServicesTestUtils.getXmlInt(element, "runningMapAttempts"), WebServicesTestUtils.getXmlInt(element, "failedMapAttempts"), WebServicesTestUtils.getXmlInt(element, "killedMapAttempts"), WebServicesTestUtils.getXmlInt(element, "successfulMapAttempts"));
        Map<JobACL, AccessControlList> allacls = job.getJobACLs();
        if (allacls != null) {
            for (Map.Entry<JobACL, AccessControlList> entry : allacls.entrySet()) {
                String expectName = entry.getKey().getAclName();
                String expectValue = entry.getValue().getAclString();
                Boolean found = false;
                // make sure ws includes it
                NodeList id = element.getElementsByTagName("acls");
                if (id != null) {
                    for (int j = 0; j < id.getLength(); j++) {
                        Element aclElem = (Element) id.item(j);
                        if (aclElem == null) {
                            fail("should have acls in the web service info");
                        }
                        if (expectName.matches(WebServicesTestUtils.getXmlString(aclElem, "name"))) {
                            found = true;
                            WebServicesTestUtils.checkStringMatch("value", expectValue, WebServicesTestUtils.getXmlString(aclElem, "value"));
                        }
                    }
                } else {
                    fail("should have acls in the web service info");
                }
                assertTrue("acl: " + expectName + " not found in webservice output", found);
            }
        }
    }
}
Also used : AccessControlList(org.apache.hadoop.security.authorize.AccessControlList) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Job(org.apache.hadoop.mapreduce.v2.app.job.Job) JobACL(org.apache.hadoop.mapreduce.JobACL) Map(java.util.Map)

Example 23 with NodeList

use of org.w3c.dom.NodeList in project hadoop by apache.

the class TestAMWebServicesJobs method verifyAMJobCountersXML.

public void verifyAMJobCountersXML(NodeList nodes, Job job) {
    for (int i = 0; i < nodes.getLength(); i++) {
        Element element = (Element) nodes.item(i);
        assertNotNull("Job not found - output incorrect", job);
        WebServicesTestUtils.checkStringMatch("id", MRApps.toString(job.getID()), WebServicesTestUtils.getXmlString(element, "id"));
        // just do simple verification of fields - not data is correct
        // in the fields
        NodeList groups = element.getElementsByTagName("counterGroup");
        for (int j = 0; j < groups.getLength(); j++) {
            Element counters = (Element) groups.item(j);
            assertNotNull("should have counters in the web service info", counters);
            String name = WebServicesTestUtils.getXmlString(counters, "counterGroupName");
            assertTrue("name not set", (name != null && !name.isEmpty()));
            NodeList counterArr = counters.getElementsByTagName("counter");
            for (int z = 0; z < counterArr.getLength(); z++) {
                Element counter = (Element) counterArr.item(z);
                String counterName = WebServicesTestUtils.getXmlString(counter, "name");
                assertTrue("counter name not set", (counterName != null && !counterName.isEmpty()));
                long mapValue = WebServicesTestUtils.getXmlLong(counter, "mapCounterValue");
                assertTrue("mapCounterValue not >= 0", mapValue >= 0);
                long reduceValue = WebServicesTestUtils.getXmlLong(counter, "reduceCounterValue");
                assertTrue("reduceCounterValue  >= 0", reduceValue >= 0);
                long totalValue = WebServicesTestUtils.getXmlLong(counter, "totalCounterValue");
                assertTrue("totalCounterValue  >= 0", totalValue >= 0);
            }
        }
    }
}
Also used : Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList)

Example 24 with NodeList

use of org.w3c.dom.NodeList in project hadoop by apache.

the class TestAMWebServicesAttempts method verifyAMTaskCountersXML.

public void verifyAMTaskCountersXML(NodeList nodes, TaskAttempt att) {
    for (int i = 0; i < nodes.getLength(); i++) {
        Element element = (Element) nodes.item(i);
        WebServicesTestUtils.checkStringMatch("id", MRApps.toString(att.getID()), WebServicesTestUtils.getXmlString(element, "id"));
        // just do simple verification of fields - not data is correct
        // in the fields
        NodeList groups = element.getElementsByTagName("taskAttemptCounterGroup");
        for (int j = 0; j < groups.getLength(); j++) {
            Element counters = (Element) groups.item(j);
            assertNotNull("should have counters in the web service info", counters);
            String name = WebServicesTestUtils.getXmlString(counters, "counterGroupName");
            assertTrue("name not set", (name != null && !name.isEmpty()));
            NodeList counterArr = counters.getElementsByTagName("counter");
            for (int z = 0; z < counterArr.getLength(); z++) {
                Element counter = (Element) counterArr.item(z);
                String counterName = WebServicesTestUtils.getXmlString(counter, "name");
                assertTrue("counter name not set", (counterName != null && !counterName.isEmpty()));
                long value = WebServicesTestUtils.getXmlLong(counter, "value");
                assertTrue("value not >= 0", value >= 0);
            }
        }
    }
}
Also used : Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList)

Example 25 with NodeList

use of org.w3c.dom.NodeList in project hadoop by apache.

the class TestAMWebServices method verifyBlacklistedNodesInfoXML.

public void verifyBlacklistedNodesInfoXML(String xml, AppContext ctx) throws JSONException, Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(xml));
    Document dom = db.parse(is);
    NodeList infonodes = dom.getElementsByTagName("blacklistednodesinfo");
    assertEquals("incorrect number of elements", 1, infonodes.getLength());
    NodeList nodes = dom.getElementsByTagName("blacklistedNodes");
    Set<String> blacklistedNodes = ctx.getBlacklistedNodes();
    assertEquals("incorrect number of elements", blacklistedNodes.size(), nodes.getLength());
    for (int i = 0; i < nodes.getLength(); i++) {
        Element element = (Element) nodes.item(i);
        assertTrue(blacklistedNodes.contains(element.getFirstChild().getNodeValue()));
    }
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) StringReader(java.io.StringReader) Document(org.w3c.dom.Document)

Aggregations

NodeList (org.w3c.dom.NodeList)1806 Node (org.w3c.dom.Node)1059 Element (org.w3c.dom.Element)902 Document (org.w3c.dom.Document)636 ArrayList (java.util.ArrayList)314 DocumentBuilder (javax.xml.parsers.DocumentBuilder)268 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)208 IOException (java.io.IOException)183 NamedNodeMap (org.w3c.dom.NamedNodeMap)144 InputSource (org.xml.sax.InputSource)131 HashMap (java.util.HashMap)121 Test (org.junit.Test)117 SAXException (org.xml.sax.SAXException)117 StringReader (java.io.StringReader)106 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)100 XPath (javax.xml.xpath.XPath)99 Attr (org.w3c.dom.Attr)80 XPathExpressionException (javax.xml.xpath.XPathExpressionException)76 File (java.io.File)64 HashSet (java.util.HashSet)59