/**
   * Launch the specified call list.
   *
   * @param callList launch the call list
   * @return {@code true} in case the launch was successful
   */
  private boolean launchCallList(final List<String> callList) {
    try {
      final ProcessBuilder pBuilder = new ProcessBuilder(callList);

      File workingDirectory = DirectoryManager.getInstance().getWorkingDirectory();
      if (workingDirectory != null) {
        pBuilder.directory(workingDirectory);
      }
      pBuilder.redirectErrorStream(true);
      final Process proc = pBuilder.start();
      proc.getOutputStream().close();

      final StringBuilder outputBuffer = new StringBuilder();
      outputReader = new BufferedReader(new InputStreamReader(proc.getInputStream()));

      launchTimer.start();
      cancelExecution = false;

      while (true) {
        if (cancelExecution) {
          throw new IOException("Response Timeout.");
        }
        final String line = outputReader.readLine();
        if (line == null) {
          errorData = outputBuffer.toString().trim();
          return false;
        }
        if (line.endsWith("Startup done.")) {
          outputReader.close();
          return true;
        }
        outputBuffer.append(line);
        outputBuffer.append('\n');
      }
    } catch (@Nonnull final Exception e) {
      final StringWriter sWriter = new StringWriter();
      final PrintWriter writer = new PrintWriter(sWriter);
      e.printStackTrace(writer);
      writer.flush();
      errorData = sWriter.toString();
      return false;
    } finally {
      if (outputReader != null) {
        try {
          outputReader.close();
        } catch (@Nonnull final IOException e) {
          // nothing
        }
      }
      outputReader = null;
    }
  }
  /** Construct a new launcher and set the classpath and the class to launch. */
  @SuppressWarnings("nls")
  public JavaLauncher(final boolean snapshot) {
    this.snapshot = snapshot;

    launchTimer =
        new Timer(
            10000,
            new Runnable() {
              @Override
              public void run() {
                cancelExecution = true;
                if (outputReader != null) {
                  try {
                    outputReader.close();
                  } catch (IOException ignored) {
                  }
                }
              }
            });
    launchTimer.stop();
    launchTimer.setRepeats(false);
  }
Beispiel #3
0
  /**
   * Disconnect the client-server connection and shut the socket along with all threads for sending
   * and receiving down.
   */
  @SuppressWarnings("nls")
  public void disconnect() {
    try {
      if (keepAliveTimer != null) {
        keepAliveTimer.stop();
        keepAliveTimer = null;
      }
      // stop threads
      if (sender != null) {
        sender.saveShutdown();
        sender = null;
      }

      if (inputThread != null) {
        inputThread.saveShutdown();
        inputThread = null;
      }

      if (messageHandler != null) {
        messageHandler.saveShutdown();
        messageHandler = null;
      }

      // wait for threads to react
      try {
        Thread.sleep(THREAD_WAIT_TIME);
      } catch (@Nonnull final InterruptedException e) {
        LOGGER.warn("Disconnecting wait got interrupted.");
      }

      inputQueue.clear();
      outputQueue.clear();

      // close connection
      if (socket != null) {
        socket.close();
        socket = null;
      }
    } catch (@Nonnull final IOException e) {
      LOGGER.warn("Disconnecting failed.", e);
    }
  }