RemoteControlReader.java

    1/*******************************************************************************
    2 * Copyright (c) 2009, 2010 Mountainminds GmbH & Co. KG and Contributors
    3 * All rights reserved. This program and the accompanying materials
    4 * are made available under the terms of the Eclipse Public License v1.0
    5 * which accompanies this distribution, and is available at
    6 * http://www.eclipse.org/legal/epl-v10.html
    7 *
    8 * Contributors:
    9 *    Marc R. Hoffmann - initial API and implementation
   10 *    
   11 *******************************************************************************/
   12package org.jacoco.core.runtime;
   13
   14import java.io.IOException;
   15import java.io.InputStream;
   16
   17import org.jacoco.core.data.ExecutionDataReader;
   18
   19/**
   20 * {@link ExecutionDataReader} with commands added for runtime remote control.
   21 * 
   22 * @author Marc R. Hoffmann
   23 * @version 0.4.1.20101007204400
   24 */
   25public class RemoteControlReader extends ExecutionDataReader {
   26
   27    private IRemoteCommandVisitor remoteCommandVisitor;
   28
   29    /**
   30     * Create a new read based on the given input stream.
   31     * 
   32     * @param input
   33     *            input stream to read commands from
   34     * @throws IOException
   35     *             if the stream does not have a valid header
   36     */
   37    public RemoteControlReader(final InputStream input) throws IOException {
   38        super(input);
   39    }
   40
   41    @Override
   42    protected boolean readBlock(final byte blockid) throws IOException {
   43        switch (blockid) {
   44        case RemoteControlWriter.BLOCK_CMDDUMP:
   45            readDumpCommand();
   46            return true;
   47        case RemoteControlWriter.BLOCK_CMDOK:
   48            return false;
   49        default:
   50            return super.readBlock(blockid);
   51        }
   52    }
   53
   54    /**
   55     * Sets an listener for agent commands.
   56     * 
   57     * @param visitor
   58     */
   59    public void setRemoteCommandVisitor(final IRemoteCommandVisitor visitor) {
   60        this.remoteCommandVisitor = visitor;
   61    }
   62
   63    private void readDumpCommand() throws IOException {
   64        if (remoteCommandVisitor == null) {
   65            throw new IOException("No remote command visitor.");
   66        }
   67        final boolean dump = in.readBoolean();
   68        final boolean reset = in.readBoolean();
   69        remoteCommandVisitor.visitDumpCommand(dump, reset);
   70    }
   71
   72}