public SeleniumGlobalConfiguration getConfiguration(String name) {
   for (SeleniumGlobalConfiguration c : configurations) {
     if (name.equals(c.getName())) {
       return c;
     }
   }
   return null;
 }
 public List<SeleniumGlobalConfiguration> getGlobalConfigurationForComputer(Computer computer) {
   List<SeleniumGlobalConfiguration> confs = new ArrayList<SeleniumGlobalConfiguration>();
   for (SeleniumGlobalConfiguration c : PluginImpl.getPlugin().getGlobalConfigurations()) {
     if (c.getMatcher().match(computer.getNode())) {
       confs.add(c);
     }
   }
   return confs;
 }
  @Override
  public void stop() throws Exception {
    for (Computer c : Hudson.getInstance().getComputers()) {
      for (SeleniumGlobalConfiguration cfg : configurations) {
        cfg.stop(c);
      }
    }

    this.listener.closeQuietly();
    channel.close();
  }
  /**
   * @param req StaplerRequest
   * @param rsp StaplerResponse to redirect with
   * @throws IOException if redirection goes wrong
   */
  public void doCreate(StaplerRequest req, StaplerResponse rsp) throws Exception {
    validateAdmin();
    SeleniumGlobalConfiguration conf =
        req.bindJSON(SeleniumGlobalConfiguration.class, req.getSubmittedForm());
    if (null == conf.getName() || conf.getName().trim().equals("")) {
      throw new Failure("You must specify a name for the configuration");
    }

    if (PluginImpl.getPlugin().hasGlobalConfiguration(conf.getName())) {
      throw new Failure(
          "The configuration name you have chosen is already taken, please choose a unique name.");
    }

    PluginImpl.getPlugin().getGlobalConfigurations().add(conf);
    PluginImpl.getPlugin().save();
    rsp.sendRedirect2("configurations");
  }
  /**
   * @param name
   * @param save
   * @throws IOException
   */
  private void removeGlobalConfigurations(String name, boolean save) throws IOException {
    Iterator<SeleniumGlobalConfiguration> it = configurations.iterator();
    while (it.hasNext()) {
      SeleniumGlobalConfiguration conf = it.next();
      if (conf.getName().equals(name)) {
        it.remove();
        for (Computer c : Hudson.getInstance().getComputers()) {
          conf.remove(c);
        }
        if (save) {
          save();
        }

        // there should only be one config with that name
        return;
      }
    }
  }
  public static void startSeleniumNode(Computer c, TaskListener listener, String conf)
      throws IOException, InterruptedException {
    LOGGER.fine("Examining if we need to start Selenium Grid Node");

    final PluginImpl p = Hudson.getInstance().getPlugin(PluginImpl.class);

    final String exclusions = p.getExclusionPatterns();
    List<String> exclusionPatterns = new ArrayList<String>();
    if (StringUtils.hasText(exclusions)) {
      exclusionPatterns = Arrays.asList(exclusions.split(SEPARATOR));
    }
    if (exclusionPatterns.size() > 0) {
      // loop over all the labels and check if we need to exclude a node
      // based on the exlusionPatterns
      for (Label label : c.getNode().getAssignedLabels()) {
        for (String pattern : exclusionPatterns) {
          if (label.toString().matches(pattern)) {
            LOGGER.fine(
                "Node "
                    + c.getNode().getDisplayName()
                    + " is excluded from Selenium Grid because its label '"
                    + label
                    + "' matches exclusion pattern '"
                    + pattern
                    + "'");
            return;
          }
        }
      }
    }

    final String masterName = PluginImpl.getMasterHostName();
    if (masterName == null) {
      listener
          .getLogger()
          .println(
              "Unable to determine the host name of the master. Skipping Selenium execution. "
                  + "Please "
                  + HyperlinkNote.encodeTo("/configure", "configure the Jenkins URL")
                  + " from the system configuration screen.");
      return;
    }

    // make sure that Selenium Hub is started before we start RCs.
    try {
      p.waitForHubLaunch();
    } catch (ExecutionException e) {
      throw new IOException2("Failed to wait for the Hub launch to complete", e);
    }

    List<SeleniumGlobalConfiguration> confs = getPlugin().getGlobalConfigurationForComputer(c);
    if (confs == null || confs.size() == 0) {
      LOGGER.fine(
          "There is no matching configurations for that computer. Skipping selenium execution.");
      return;
    }

    String nodehost = c.getHostName();
    if (nodehost == null) {
      LOGGER.warning("Unable to determine node's hostname. Skipping");
      return;
    }

    listener
        .getLogger()
        .println(
            "Starting Selenium nodes on " + ("".equals(c.getName()) ? "(master)" : c.getName()));

    for (SeleniumGlobalConfiguration config : confs) {
      if ((conf != null && config.getName().equals(conf)) || conf == null) {
        try {
          config.start(c, listener);
        } catch (ExecutionException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    }
  }