/**
   * Notify RM to create a new node.
   *
   * @param parentID The ID of the machine the node belongs to
   * @param name the name of the node
   * @param number the number of the node (rank)
   * @return the id of the new node
   */
  public String createNode(String parentID, String name, int number) {
    String id = generateID();
    ElementAttributeManager mgr = new ElementAttributeManager();
    AttributeManager attrMgr = new AttributeManager();
    attrMgr.addAttribute(
        NodeAttributes.getStateAttributeDefinition().create(NodeAttributes.State.UP));
    try {
      attrMgr.addAttribute(
          NodeAttributes.getNumberAttributeDefinition().create(new Integer(number)));
    } catch (IllegalValueException e) {
      /*
       * This exception is not possible, since number is always valid.
       */
      RMCorePlugin.log(e);
      assert false;
    }
    attrMgr.addAttribute(ElementAttributes.getNameAttributeDefinition().create(name));
    mgr.setAttributeManager(new RangeSet(id), attrMgr);
    fireRuntimeNewNodeEvent(eventFactory.newRuntimeNewNodeEvent(parentID, mgr));

    DebugUtil.trace(
        DebugUtil.RTS_TRACING,
        "RTS {0}: new node #{1}",
        getResourceManager().getConfiguration().getName(),
        id); //$NON-NLS-1$

    return id;
  }
  /**
   * Get environment to append
   *
   * @param configuration
   * @return
   * @throws CoreException
   * @since 3.0
   */
  protected static String[] getEnvironment(ILaunchConfiguration configuration)
      throws CoreException {
    Map<?, ?> defaultEnv = null;
    Map<?, ?> configEnv =
        configuration.getAttribute(ILaunchManager.ATTR_ENVIRONMENT_VARIABLES, defaultEnv);
    if (configEnv == null) {
      return null;
    }
    if (!configuration.getAttribute(ILaunchManager.ATTR_APPEND_ENVIRONMENT_VARIABLES, true)) {
      throw new CoreException(
          new Status(
              IStatus.ERROR,
              RMCorePlugin.getUniqueIdentifier(),
              Messages.AbstractToolRuntimeSystem_EnvNotSupported));
    }

    List<String> strings = new ArrayList<String>(configEnv.size());
    Iterator<?> iter = configEnv.entrySet().iterator();
    while (iter.hasNext()) {
      Entry<?, ?> entry = (Entry<?, ?>) iter.next();
      String key = (String) entry.getKey();
      String value = (String) entry.getValue();
      strings.add(key + "=" + value); // $NON-NLS-1$
    }
    return strings.toArray(new String[strings.size()]);
  }
 /*
  * (non-Javadoc)
  *
  * @see org.eclipse.ptp.remote.core.IRemoteConnectionChangeListener#
  * connectionChanged
  * (org.eclipse.ptp.remote.core.IRemoteConnectionChangeEvent)
  */
 public void connectionChanged(IRemoteConnectionChangeEvent event) {
   if (event.getType() == IRemoteConnectionChangeEvent.CONNECTION_ABORTED
       || event.getType() == IRemoteConnectionChangeEvent.CONNECTION_CLOSED) {
     try {
       getResourceManager().stop();
     } catch (CoreException e) {
       RMCorePlugin.log(e);
     }
   }
 }
 public void run() {
   DebugUtil.trace(
       DebugUtil.JOB_TRACING,
       "RTS {0}: started job thread",
       getResourceManager().getConfiguration().getName()); // $NON-NLS-1$
   try {
     while (connection != null) {
       Job job = pendingJobQueue.take();
       if (job instanceof IToolRuntimeSystemJob) {
         DebugUtil.trace(
             DebugUtil.JOB_TRACING,
             "RTS {0}: schedule job #{1}",
             getResourceManager().getConfiguration().getName(),
             ((IToolRuntimeSystemJob) job).getJobID()); // $NON-NLS-1$
       } else {
         DebugUtil.trace(
             DebugUtil.JOB_TRACING,
             "RTS {0}: schedule job #{1}",
             getResourceManager().getConfiguration().getName(),
             job.getName()); // $NON-NLS-1$
       }
       job.schedule();
     }
   } catch (InterruptedException e) {
     // Ignore
   } catch (Exception e) {
     DebugUtil.error(
         DebugUtil.JOB_TRACING,
         "RTS {0}: {1}",
         getResourceManager().getConfiguration().getName(),
         e); //$NON-NLS-1$
     RMCorePlugin.log(e);
   }
   DebugUtil.trace(
       DebugUtil.JOB_TRACING,
       "RTS {0}: terminated job thread",
       getResourceManager().getConfiguration().getName()); // $NON-NLS-1$
 }
  /*
   * (non-Javadoc)
   *
   * @see org.eclipse.ptp.rtsystem.IRuntimeSystem#shutdown()
   */
  public void shutdown() throws CoreException {
    DebugUtil.trace(
        DebugUtil.RTS_TRACING,
        "RTS {0}: shutdown",
        getResourceManager().getConfiguration().getName()); // $NON-NLS-1$

    /*
     * Remove listener to avoid re-entry
     */
    if (connection != null) {
      connection.removeConnectionChangeListener(fConnectionChangeHandler);
    }

    try {
      stopEvents();
    } catch (CoreException e) {
      // Ignore exception and shutdown anyway
      RMCorePlugin.log(e);
    }

    try {
      doShutdown();
    } finally {

      /*
       * Stop jobs that might be in the pending queue. Also stop the
       * thread that dispatches pending jobs.
       */
      if (jobQueueThread != null) {
        jobQueueThread.interrupt();
        for (Job job : pendingJobQueue) {
          job.cancel();
        }
      }

      /*
       * Stop jobs that are running or that already finished.
       */
      Iterator<Job> iterator = jobs.values().iterator();
      while (iterator.hasNext()) {
        Job job = iterator.next();
        job.cancel();
        iterator.remove();
      }

      synchronized (this) {
        if (startupMonitor != null) {
          startupMonitor.setCanceled(true);
        }
      }

      /*
       * Close the the connection.
       */
      if (connection != null) {
        connection.close();
      }

      jobQueueThread = null;
      fireRuntimeShutdownStateEvent(eventFactory.newRuntimeShutdownStateEvent());
    }
  }
  /**
   * Notify RM to create a new job.
   *
   * @param parentID parent element ID
   * @param jobID the ID of the job
   * @param attrMgr attributes from the job thread
   * @return job id of the newly created job
   */
  public String createJob(String parentID, AttributeManager attrMgr) throws CoreException {
    ElementAttributeManager mgr = new ElementAttributeManager();
    AttributeManager jobAttrMgr = new AttributeManager();

    /*
     * Add generated attributes.
     */
    String jobID = generateID().toString();
    jobAttrMgr.addAttribute(JobAttributes.getJobIdAttributeDefinition().create(jobID));
    jobAttrMgr.addAttribute(
        JobAttributes.getStatusAttributeDefinition()
            .create(MPIJobAttributes.Status.NORMAL.toString()));
    jobAttrMgr.addAttribute(
        JobAttributes.getUserIdAttributeDefinition().create(System.getenv("USER"))); // $NON-NLS-1$
    jobAttrMgr.addAttribute(
        ElementAttributes.getNameAttributeDefinition().create(generateJobName()));

    /*
     * Get mandatory launch attributes.
     */
    String subId = getAttributeValue(JobAttributes.getSubIdAttributeDefinition(), attrMgr);
    String execName =
        getAttributeValue(JobAttributes.getExecutableNameAttributeDefinition(), attrMgr);
    String execPath =
        getAttributeValue(JobAttributes.getExecutablePathAttributeDefinition(), attrMgr);
    String workDir =
        getAttributeValue(JobAttributes.getWorkingDirectoryAttributeDefinition(), attrMgr);
    Integer numProcs =
        getAttributeValue(JobAttributes.getNumberOfProcessesAttributeDefinition(), attrMgr);
    List<? extends String> progArgs =
        getAttributeValue(JobAttributes.getProgramArgumentsAttributeDefinition(), attrMgr);

    /*
     * Copy these relevant attributes to IPJob.
     */
    jobAttrMgr.addAttribute(JobAttributes.getSubIdAttributeDefinition().create(subId));
    jobAttrMgr.addAttribute(JobAttributes.getExecutableNameAttributeDefinition().create(execName));
    jobAttrMgr.addAttribute(JobAttributes.getExecutablePathAttributeDefinition().create(execPath));
    jobAttrMgr.addAttribute(JobAttributes.getWorkingDirectoryAttributeDefinition().create(workDir));
    try {
      jobAttrMgr.addAttribute(
          JobAttributes.getNumberOfProcessesAttributeDefinition().create(numProcs));
    } catch (IllegalValueException e) {
      RMCorePlugin.log(e);
    }
    jobAttrMgr.addAttribute(
        JobAttributes.getProgramArgumentsAttributeDefinition()
            .create(progArgs.toArray(new String[0])));

    /*
     * Copy optional attributes
     */
    BooleanAttribute debugAttr =
        attrMgr.getAttribute(JobAttributes.getDebugFlagAttributeDefinition());
    if (debugAttr != null) {
      jobAttrMgr.addAttribute(
          JobAttributes.getDebugFlagAttributeDefinition().create(debugAttr.getValue()));
    }

    /*
     * Notify RM.
     */
    mgr.setAttributeManager(new RangeSet(jobID), jobAttrMgr);
    fireRuntimeNewJobEvent(eventFactory.newRuntimeNewJobEvent(parentID, mgr));

    DebugUtil.trace(
        DebugUtil.RTS_TRACING,
        "RTS {0}: new job #{1}",
        getResourceManager().getConfiguration().getName(),
        jobID); //$NON-NLS-1$

    return jobID;
  }