public boolean executionFailedOnWorker(
      WorkerEntry workerEntry,
      GridProcessErrorTypes type,
      GridProcess execution,
      List<IResponseTO> responses) {

    if (workerEntry != null && type != null) {
      if (type.blackListError()) {

        int taskid = execution.getTaskId();

        responses.add(
            new LoggerResponseTO(
                "Adding to blacklist. Task: " + taskid + ", Worker: " + workerEntry.getWorkerID(),
                LoggerResponseTO.DEBUG));

        workerEntry.addBlacklistedTask(taskid);

        // a sabotage error causes a immediately job blacklist entry
        if (type.equals(GridProcessErrorTypes.SABOTAGE_ERROR)) {
          saboteurs.add(workerEntry);
        }
        return true;
      }
    }
    return false;
  }
  public void disposeWorker(WorkerEntry workerEntry, List<IResponseTO> responses) {
    ServiceID serviceID = workerEntry.getServiceID();

    responses.add(new LoggerResponseTO("Worker dispose: " + serviceID, LoggerResponseTO.DEBUG));

    workerEntry.dispose();

    WorkerDAO workerDAO = BrokerDAOFactory.getInstance().getWorkerDAO();
    String wAddress = StringUtil.deploymentIDToAddress(workerEntry.getWorkerID());

    DisposeWorkerResponseTO disposeTO = new DisposeWorkerResponseTO();
    disposeTO.setPeerAddress(StringUtil.deploymentIDToAddress(workerEntry.getPeerID()));
    disposeTO.setWorkerAddress(wAddress);
    disposeTO.setWorkerPublicKey(workerDAO.getWorkerPublicKey(wAddress));
    responses.add(disposeTO);

    responses.add(new LoggerResponseTO("Stub to be released: " + wAddress, LoggerResponseTO.INFO));

    ReleaseResponseTO releaseTO = new ReleaseResponseTO();
    releaseTO.setStubAddress(wAddress);
    responses.add(releaseTO);

    workerDAO.removeWorker(wAddress);
    WorkerInfo.getInstance().removeWorker(workerEntry.getServiceID().getContainerID().toString());
  }
  /**
   * @param job
   * @param workerEntry
   */
  private void unwantWorker(Job job, WorkerEntry workerEntry, List<IResponseTO> responses) {

    responses.add(
        new LoggerResponseTO(
            "Worker unwanted: " + workerEntry.getWorkerID(), LoggerResponseTO.DEBUG));

    job.unwantWorker(workerEntry);
    workerEntry.unwant();

    RequestSpecification spec = workerEntry.getRequestSpecification();

    String peerAddress = StringUtil.deploymentIDToAddress(workerEntry.getPeerID());
    String workerAddress = StringUtil.deploymentIDToAddress(workerEntry.getWorkerID());
    Integer jobId = Integer.parseInt("" + spec.getJobId());

    JobSpecification jobSpec = BrokerDAOFactory.getInstance().getJobDAO().getJobSpec(jobId);

    UnwantWorkerResponseTO unwantWorkerTO = new UnwantWorkerResponseTO();
    unwantWorkerTO.setJobID(jobId);
    unwantWorkerTO.setJobSpec(jobSpec);
    unwantWorkerTO.setMaxFails(spec.getMaxFails());
    unwantWorkerTO.setMaxReplicas(spec.getMaxReplicas());
    unwantWorkerTO.setPeerAddress(peerAddress);
    unwantWorkerTO.setRequestID(spec.getRequestId());
    unwantWorkerTO.setRequiredWorkers(spec.getRequiredWorkers());

    unwantWorkerTO.setWorkerAddress(workerAddress);

    WorkerDAO workerDAO = BrokerDAOFactory.getInstance().getWorkerDAO();

    String workerPublicKey = workerDAO.getWorkerPublicKey(workerAddress);
    unwantWorkerTO.setWorkerPublicKey(workerPublicKey);

    responses.add(unwantWorkerTO);

    ReleaseResponseTO releaseTO = new ReleaseResponseTO();
    releaseTO.setStubAddress(workerAddress);

    responses.add(releaseTO);

    workerDAO.removeWorker(workerAddress);

    WorkerInfo.getInstance().removeWorker(workerEntry.getServiceID().getContainerID().toString());
  }
  public JobWorkerStatus getCompleteStatus() {

    Map<Integer, Job> jobsMap = JobInfo.getInstance().getJobs();
    Map<Integer, Set<WorkerEntry>> workersByJob = CommonUtils.createMap();

    JobStatusInfo jobInfo = null;
    List<TaskStatusInfo> tasksList = null;
    Map<Integer, JobStatusInfo> jobs = CommonUtils.createSerializableMap();

    // Jobs
    for (Job job : jobsMap.values()) {
      Set<WorkerEntry> workers = new LinkedHashSet<WorkerEntry>();
      tasksList = new ArrayList<TaskStatusInfo>();

      for (Task task : job.getTasks()) {
        tasksList.add(fillTask(task));
        for (GridProcess gridProcess : task.getGridProcesses()) {
          if (gridProcess.getState() == GridProcessState.RUNNING)
            workers.add(gridProcess.getWorkerEntry());
        }
      }

      jobInfo =
          new JobStatusInfo(
              job.getJobId(),
              job.getSpec(),
              UtilConverter.getJobState(job.getState()),
              tasksList,
              job.getCreationTime(),
              job.getFinalizationTime());

      jobs.put(jobInfo.getJobId(), jobInfo);

      if (job.isRunning()) {
        workersByJob.put(job.getJobId(), workers);
      }
    }

    Map<Integer, WorkerStatusInfo[]> workers = CommonUtils.createSerializableMap();

    WorkerStatusInfo[] workerList = null;
    for (Entry<Integer, Set<WorkerEntry>> entry : workersByJob.entrySet()) {

      workerList = workers.get(entry.getKey());
      if (workerList == null) {
        workerList = new WorkerStatusInfo[entry.getValue().size()];
        workers.put(entry.getKey(), workerList);
      }

      int i = 0;
      for (WorkerEntry workerEntry : entry.getValue()) {

        GridProcessHandle handle = null;
        String state = null;

        if (workerEntry.getGridProcess() != null) {
          handle = workerEntry.getGridProcess().getHandle();
          state = workerEntry.getGridProcess().getState().toString();
        }

        workerList[i] =
            new WorkerStatusInfo(
                workerEntry.getWorkerSpecification(), handle, workerEntry.getWorkerID(), state);
        i++;
      }
    }

    JobWorkerStatus status = new JobWorkerStatus(jobs, workers);

    return status;
  }