@Override
  protected void init() {
    super.init();

    if (process.isTerminated()) {
      if (shellConnection != null) {
        try {
          shellConnection.disconnect();
        } catch (final IOException e) {
          KarafUIPluginActivator.getLogger().error("Unable to disconnect from SSH server", e);
        }
      }
    } else {
      DebugPlugin.getDefault().addDebugEventListener(this);
    }
  }
  @Override
  public void handleDebugEvents(final DebugEvent[] events) {
    for (final DebugEvent event : events) {
      if (event.getSource().equals(process)) {
        if (event.getKind() == DebugEvent.TERMINATE) {
          if (shellConnection != null) {
            try {
              shellConnection.disconnect();
            } catch (final IOException e) {
              KarafUIPluginActivator.getLogger().error("Unable to disconnect from SSH server", e);
            }
          }
          DebugPlugin.getDefault().removeDebugEventListener(this);

          resetName();
        }
      }
    }
  }
  public KarafRemoteConsole(
      final IProcess process,
      final KarafSshConnectionUrl connectionUrl,
      final KarafSshShellConnection.Credentials credentials,
      final IConsoleColorProvider colorProvider,
      final String name,
      final String encoding) {
    super(
        name,
        KARAF_REMOTE_CONSOLE_TYPE,
        KarafUIPluginActivator.getDefault()
            .getImageRegistry()
            .getDescriptor(KarafUIPluginActivator.LOGO_16X16_IMG),
        encoding,
        true);

    this.process = process;
    this.inputStream = getInputStream();
    this.colorProvider = colorProvider;

    final Color color = this.colorProvider.getColor(IDebugUIConstants.ID_STANDARD_INPUT_STREAM);
    this.inputStream.setColor(color);

    final InputStream noAvailableInputStream =
        new FilterInputStream(inputStream) {
          @Override
          public int available() throws IOException {
            return 0;
          }
        };

    setName(computeName());
    final IOConsoleOutputStream outputStream = newOutputStream();

    boolean remoteShellEnabled = false;
    try {
      final ILaunchConfiguration configuration = process.getLaunch().getLaunchConfiguration();
      remoteShellEnabled =
          configuration.getAttribute(
              KarafLaunchConfigurationConstants.KARAF_LAUNCH_START_REMOTE_CONSOLE, false);
    } catch (final CoreException e) {
      return;
    }

    if (remoteShellEnabled) {
      shellConnection =
          new KarafSshShellConnection(
              connectionUrl, credentials, noAvailableInputStream, outputStream, outputStream);

      final KarafRemoteShellConnectJob job = new KarafRemoteShellConnectJob(name, shellConnection);
      job.addJobChangeListener(
          new JobChangeAdapter() {
            @Override
            public void done(final IJobChangeEvent event) {
              if (!event.getResult().isOK()) {
                final Throwable t = event.getResult().getException();
                writeTo(
                    outputStream,
                    "Unable to connect to SSH server: "
                        + (t != null ? t.getLocalizedMessage() : "Unknown error"));
              }
            }
          });

      DebugPlugin.getDefault()
          .addDebugEventListener(
              new IDebugEventSetListener() {

                @Override
                public void handleDebugEvents(final DebugEvent[] events) {

                  for (final DebugEvent event : events) {
                    if (process != null
                        && process.equals(event.getSource())
                        && event.getKind() == DebugEvent.TERMINATE) {
                      job.cancel();
                    }
                  }
                }
              });

      job.schedule(15 * 1000);
    } else {
      writeTo(
          outputStream,
          "The Karaf remote shell is disabled. Enable it in the launch configuration dialog.");
    }
  }