public void handleMessage(Connection connection, InputStream dataStream) throws IOException {
      final int workingVersion;
      final ManagementRequestHeader requestHeader;
      final ManagementOperationHandler handler;
      ByteDataInput input = null;
      try {
        input = new SimpleByteDataInput(dataStream);

        // Start by reading the request header
        requestHeader = new ManagementRequestHeader(input);

        // Work with the lowest protocol version
        workingVersion = Math.min(ManagementProtocol.VERSION, requestHeader.getVersion());

        byte handlerId = requestHeader.getOperationHandlerId();
        if (handlerId == -1) {
          throw new IOException("Management request failed.  Invalid handler id");
        }
        handler = handlers.get(handlerId);
        if (handler == null) {
          String msg = "Management request failed.  No handler found for id " + handlerId;
          throw new IOException(msg);
        }
        connection.setMessageHandler(handler);
      } catch (IOException e) {
        throw e;
      } catch (Throwable t) {
        throw new IOException("Failed to read request header", t);
      } finally {
        safeClose(input);
        safeClose(dataStream);
      }

      OutputStream dataOutput = null;
      ByteDataOutput output = null;
      try {
        dataOutput = connection.writeMessage();
        output = new SimpleByteDataOutput(dataOutput);

        // Now write the response header
        final ManagementResponseHeader responseHeader =
            new ManagementResponseHeader(workingVersion, requestHeader.getRequestId());
        responseHeader.write(output);

        output.close();
        dataOutput.close();
      } catch (IOException e) {
        throw e;
      } catch (Throwable t) {
        throw new IOException("Failed to write management response headers", t);
      } finally {
        safeClose(output);
        safeClose(dataOutput);
      }
    }
 public void handleFailure(final Connection connection, final IOException e) throws IOException {
   connection.close();
 }
 public void handleShutdown(final Connection connection) throws IOException {
   connection.shutdownWrites();
 }