@Subscribe
  public void inputCreated(InputCreated inputCreatedEvent) {
    LOG.debug("Input created/changed: " + inputCreatedEvent.id());
    final Input input;
    try {
      input = inputService.find(inputCreatedEvent.id());
    } catch (NotFoundException e) {
      LOG.warn("Received InputCreated event but could not find Input: ", e);
      return;
    }

    final IOState<MessageInput> inputState = inputRegistry.getInputState(inputCreatedEvent.id());
    if (inputState != null) {
      inputRegistry.remove(inputState);
    }

    if (!input.isGlobal() && !this.nodeId.toString().equals(input.getNodeId())) {
      return;
    }

    final MessageInput messageInput;
    try {
      messageInput = inputService.getMessageInput(input);
      messageInput.initialize();
    } catch (NoSuchInputTypeException e) {
      LOG.warn("Newly created input is of invalid type: " + input.getType(), e);
      return;
    }
    final IOState<MessageInput> newInputState = inputLauncher.launch(messageInput);
    inputRegistry.add(newInputState);
  }
 @Subscribe
 public void inputDeleted(InputDeleted inputDeletedEvent) {
   LOG.debug("Input deleted: " + inputDeletedEvent.id());
   final IOState<MessageInput> inputState = inputRegistry.getInputState(inputDeletedEvent.id());
   if (inputState != null) {
     inputRegistry.remove(inputState);
   }
 }
  @Override
  protected void shutDown() throws Exception {
    LOG.debug("Stopping InputSetupService");
    eventBus.unregister(this);

    for (InputState state : inputRegistry.getRunningInputs()) {
      MessageInput input = state.getMessageInput();

      LOG.info(
          "Attempting to close input <{}> [{}].", input.getUniqueReadableId(), input.getName());

      Stopwatch s = Stopwatch.createStarted();
      try {
        input.stop();

        LOG.info(
            "Input <{}> closed. Took [{}ms]",
            input.getUniqueReadableId(),
            s.elapsed(TimeUnit.MILLISECONDS));
      } catch (Exception e) {
        LOG.error(
            "Unable to stop input <{}> [{}]: " + e.getMessage(),
            input.getUniqueReadableId(),
            input.getName());
      } finally {
        s.stop();
      }
    }
    LOG.debug("Stopped InputSetupService");
  }
  @POST
  @Timed
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  @ApiOperation(value = "Add an extractor to an input", response = ExtractorCreated.class)
  @ApiResponses(
      value = {
        @ApiResponse(code = 404, message = "No such input on this node."),
        @ApiResponse(code = 400, message = "No such extractor type."),
        @ApiResponse(code = 400, message = "Field the extractor should write on is reserved."),
        @ApiResponse(code = 400, message = "Missing or invalid configuration.")
      })
  public Response create(
      @ApiParam(name = "inputId", required = true) @PathParam("inputId") String inputId,
      @ApiParam(name = "JSON body", required = true) @Valid @NotNull CreateExtractorRequest cer)
      throws NotFoundException {
    checkPermission(RestPermissions.INPUTS_EDIT, inputId);

    final MessageInput input = inputs.getRunningInput(inputId);
    if (input == null) {
      LOG.error("Input <{}> not found.", inputId);
      throw new javax.ws.rs.NotFoundException();
    }

    final Input mongoInput = inputService.find(input.getPersistId());
    final String id = new com.eaio.uuid.UUID().toString();
    final Extractor extractor = buildExtractorFromRequest(cer, id);

    try {
      inputService.addExtractor(mongoInput, extractor);
    } catch (ValidationException e) {
      LOG.error("Extractor persist validation failed.", e);
      throw new BadRequestException(e);
    }

    final String msg =
        "Added extractor <"
            + id
            + "> of type ["
            + cer.extractorType()
            + "] to input <"
            + inputId
            + ">.";
    LOG.info(msg);
    activityWriter.write(new Activity(msg, ExtractorsResource.class));

    final ExtractorCreated result = ExtractorCreated.create(id);
    final URI extractorUri =
        getUriBuilderToSelf().path(ExtractorsResource.class).path("{inputId}").build(input.getId());

    return Response.created(extractorUri).entity(result).build();
  }
  @Override
  protected void run() throws Exception {
    // prevent launching persisted inputs too early.
    LOG.debug("Delaying lauching persisted inputs until the node is in RUNNING state.");
    Uninterruptibles.awaitUninterruptibly(startLatch);

    if (previousLifecycle.get() == Lifecycle.RUNNING) {
      LOG.debug("Launching persisted inputs now.");
      inputRegistry.launchAllPersisted();
    } else {
      LOG.error("Not starting any inputs because lifecycle is: {}", previousLifecycle.get());
    }

    // next, simply block until we are asked to shutdown, even though we are consuming a thread this
    // way.
    Uninterruptibles.awaitUninterruptibly(stopLatch);
  }