Search in sources :

Example 1 with DecodingStream

use of hudson.util.DecodingStream in project hudson-2.x by hudson.

the class ExternalRun method acceptRemoteSubmission.

/**
     * Instead of performing a build, accept the log and the return code
     * from a remote machine.
     *
     * <p>
     * The format of the XML is:
     *
     * <pre><xmp>
     * <run>
     *  <log>...console output...</log>
     *  <result>exit code</result>
     * </run>
     * </xmp></pre>
     */
@SuppressWarnings({ "Since15" })
@IgnoreJRERequirement
public void acceptRemoteSubmission(final Reader in) throws IOException {
    final long[] duration = new long[1];
    run(new Runner() {

        private String elementText(XMLStreamReader r) throws XMLStreamException {
            StringBuilder buf = new StringBuilder();
            while (true) {
                int type = r.next();
                if (type == CHARACTERS || type == CDATA)
                    buf.append(r.getTextCharacters(), r.getTextStart(), r.getTextLength());
                else
                    return buf.toString();
            }
        }

        public Result run(BuildListener listener) throws Exception {
            PrintStream logger = new PrintStream(new DecodingStream(listener.getLogger()));
            XMLInputFactory xif = XMLInputFactory.newInstance();
            XMLStreamReader p = xif.createXMLStreamReader(in);
            // get to the <run>
            p.nextTag();
            // get to the <log>
            p.nextTag();
            charset = p.getAttributeValue(null, "content-encoding");
            while (p.next() != END_ELEMENT) {
                int type = p.getEventType();
                if (type == CHARACTERS || type == CDATA)
                    logger.print(p.getText());
            }
            // get to <result>
            p.nextTag();
            Result r = Integer.parseInt(elementText(p)) == 0 ? Result.SUCCESS : Result.FAILURE;
            // get to <duration> (optional)
            p.nextTag();
            if (p.getEventType() == START_ELEMENT && p.getLocalName().equals("duration")) {
                duration[0] = Long.parseLong(elementText(p));
            }
            return r;
        }

        public void post(BuildListener listener) {
        // do nothing
        }

        public void cleanUp(BuildListener listener) {
        // do nothing
        }
    });
    if (duration[0] != 0) {
        super.duration = duration[0];
        // save the updated duration
        save();
    }
}
Also used : PrintStream(java.io.PrintStream) XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) DecodingStream(hudson.util.DecodingStream) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) XMLInputFactory(javax.xml.stream.XMLInputFactory) IgnoreJRERequirement(org.jvnet.animal_sniffer.IgnoreJRERequirement)

Aggregations

DecodingStream (hudson.util.DecodingStream)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 XMLInputFactory (javax.xml.stream.XMLInputFactory)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 IgnoreJRERequirement (org.jvnet.animal_sniffer.IgnoreJRERequirement)1