protected void setEnvironmentVariable(
      CloudFoundryApplicationModule appModule,
      CloudFoundryServer cloudServer,
      int remoteDebugPort,
      IProgressMonitor monitor)
      throws CoreException {

    ApplicationDeploymentInfo info = appModule.getDeploymentInfo();
    List<EnvironmentVariable> vars = info.getEnvVariables();
    EnvironmentVariable javaOpts = getDebugEnvironment(info);

    IModule[] mod = new IModule[] {appModule.getLocalModule()};

    boolean restart = CloudFoundryProperties.isModuleStopped.testProperty(mod, cloudServer);

    if (!containsDebugOption(javaOpts)) {

      if (javaOpts == null) {
        javaOpts = new EnvironmentVariable();
        javaOpts.setVariable(JAVA_OPTS);
        vars.add(javaOpts);
      }

      String debugOpts =
          "-Xdebug -Xrunjdwp:server=y,transport=dt_socket,address="
              + remoteDebugPort //$NON-NLS-1$
              + ",suspend=n"; //$NON-NLS-1$
      javaOpts.setValue(debugOpts);

      cloudServer
          .getBehaviour()
          .operations()
          .environmentVariablesUpdate(
              appModule.getLocalModule(), appModule.getDeployedApplicationName(), vars)
          .run(monitor);

      restart = true;
    }

    if (restart) {
      printToConsole(
          appModule,
          cloudServer,
          NLS.bind(
              Messages.SshDebugLaunchConfigDelegate_RESTARTING_APP,
              appModule.getDeployedApplicationName()),
          false);

      cloudServer
          .getBehaviour()
          .operations()
          .applicationDeployment(mod, ApplicationAction.START, false)
          .run(monitor);
    }
  }
  protected JarPackageData getJarPackageData(
      IPackageFragmentRoot[] roots, IType mainType, IProgressMonitor monitor) throws CoreException {

    String filePath = getTempJarPath(appModule.getLocalModule());

    if (filePath == null) {
      handleApplicationDeploymentFailure();
    }

    IPath location = new Path(filePath);

    // Note that if no jar builder is specified in the package data
    // then a default one is used internally by the data that does NOT
    // package any jar dependencies.
    JarPackageData packageData = new JarPackageData();

    packageData.setJarLocation(location);

    // Don't create a manifest. A repackager should determine if a generated
    // manifest is necessary
    // or use a user-defined manifest.
    packageData.setGenerateManifest(false);

    // Since user manifest is not used, do not save to manifest (save to
    // manifest saves to user defined manifest)
    packageData.setSaveManifest(false);

    packageData.setManifestMainClass(mainType);
    packageData.setElements(roots);
    return packageData;
  }
 protected void handleApplicationDeploymentFailure(String errorMessage) throws CoreException {
   if (errorMessage == null) {
     errorMessage = Messages.JavaCloudFoundryArchiver_ERROR_CREATE_PACKAGED_FILE;
   }
   throw CloudErrorUtil.toCoreException(
       errorMessage
           + " - " //$NON-NLS-1$
           + appModule.getDeployedApplicationName()
           + ". Unable to package application for deployment."); //$NON-NLS-1$
 }
  public InstanceState track(IProgressMonitor monitor) throws CoreException {

    InstanceState runState = null;

    long currentTime = System.currentTimeMillis();

    long totalTime = currentTime + timeout;

    CloudFoundryApplicationModule appModule =
        cloudServer.getBehaviour().updateCloudModuleWithInstances(appName, monitor);

    ApplicationStats stats = getStats(monitor);

    printlnToConsole(
        NLS.bind(Messages.ApplicationInstanceStartingTracker_STARTING_TRACKING, appName),
        appModule);

    while (runState != InstanceState.RUNNING
        && runState != InstanceState.FLAPPING
        && runState != InstanceState.CRASHED
        && currentTime < totalTime) {

      runState = getRunState(stats, appModule.getApplication());
      try {
        Thread.sleep(WAIT_TIME);
      } catch (InterruptedException e) {

      }

      stats = getStats(monitor);

      currentTime = System.currentTimeMillis();
    }

    String runningStateMessage =
        runState == InstanceState.RUNNING
            ? NLS.bind(Messages.ApplicationInstanceStartingTracker_APPLICATION_IS_RUNNING, appName)
            : NLS.bind(
                Messages.ApplicationInstanceStartingTracker_APPLICATION_IS_NOT_RUNNING, appName);
    printlnToConsole(runningStateMessage, appModule);

    return runState;
  }
  protected ICloudFoundryOperation getUnbindServiceOp(
      CloudFoundryApplicationModule appModule, CFServiceInstance service) throws Exception {
    CloudApplication updatedApplication =
        getUpdatedApplication(appModule.getDeployedApplicationName());
    List<String> boundServices = updatedApplication.getServices();
    List<String> servicesToUpdate = new ArrayList<String>();

    // Must iterate rather than passing to constructor or using
    // addAll, as some
    // of the entries in existing services may be null
    for (String existingService : boundServices) {
      if (existingService != null) {
        servicesToUpdate.add(existingService);
      }
    }

    if (servicesToUpdate.contains(service.getName())) {
      servicesToUpdate.remove(service.getName());
    }
    return serverBehavior.operations().bindServices(appModule, servicesToUpdate);
  }
 public ApplicationInstanceRunningTracker(
     CloudFoundryApplicationModule appModule, CloudFoundryServer cloudServer) {
   this.cloudServer = cloudServer;
   this.appName = appModule.getDeployedApplicationName();
   this.timeout = TIMEOUT;
 }
  protected DebugConnectionDescriptor getSshConnectionDescriptor(
      CloudFoundryApplicationModule appModule,
      CloudFoundryServer cloudServer,
      int appInstance,
      int remoteDebugPort,
      IProgressMonitor monitor)
      throws CoreException {

    CFInfo cloudInfo = cloudServer.getBehaviour().getCloudInfo();
    if (cloudInfo instanceof CloudInfoSsh) {
      ISshClientSupport ssh = cloudServer.getBehaviour().getSshClientSupport(monitor);
      if (ssh == null) {
        return null;
      }
      try {
        printToConsole(
            appModule,
            cloudServer,
            NLS.bind(
                Messages.SshDebugLaunchConfigDelegate_CONNECTING_FOR_USER,
                appModule.getDeployedApplicationName()),
            false);

        Session session =
            ssh.connect(
                appModule.getDeployedApplicationName(),
                appInstance,
                cloudServer.getServer(),
                monitor);

        printToConsole(
            appModule,
            cloudServer,
            NLS.bind(
                Messages.SshDebugLaunchConfigDelegate_CONNECTION_SUCCESSFUL,
                appModule.getDeployedApplicationName()),
            false);

        int localDebuggerPort =
            session.setPortForwardingL(0, "localhost", remoteDebugPort); // $NON-NLS-1$

        printToConsole(
            appModule,
            cloudServer,
            NLS.bind(
                Messages.SshDebugLaunchConfigDelegate_PORT_FORWARDING_SUCCESSFUL,
                remoteDebugPort,
                localDebuggerPort),
            false);

        return new DebugConnectionDescriptor("localhost", localDebuggerPort); // $NON-NLS-1$

      } catch (JSchException e) {
        throw CloudErrorUtil.toCoreException(
            "SSH connection error " + e.getMessage()); // $NON-NLS-1$
      }
    } else {
      throw CloudErrorUtil.toCoreException(
          "Unable to resolve SSH connection information from the Cloud Foundry target. Please ensure SSH is supported."); //$NON-NLS-1$
    }
  }