public byte[] digest(byte[] input) {
   String function = DigestFunction.class.getCanonicalName();
   String uniqueId = null;
   byte[] algoNameBytes = ALGO_NAME.getBytes();
   byte[] digestParms = new byte[input.length + algoNameBytes.length + 1];
   System.arraycopy(algoNameBytes, 0, digestParms, 0, algoNameBytes.length);
   digestParms[algoNameBytes.length] = '\0';
   System.arraycopy(input, 0, digestParms, algoNameBytes.length + 1, input.length);
   GearmanJob job = GearmanJobImpl.createJob(function, digestParms, uniqueId);
   Future<GearmanJobResult> f = client.submit(job);
   GearmanJobResult jr = null;
   try {
     jr = f.get();
   } catch (Exception e) {
     e.printStackTrace(); // NOPMD
   }
   if (jr == null) {
     return new byte[0];
   } else {
     if (jr.jobSucceeded()) {
       return jr.getResults();
     } else {
       System.err.println(ByteUtils.fromUTF8Bytes(jr.getExceptions())); // NOPMD
       return new byte[0];
     }
   }
 }
 private void addNewJob(GearmanSessionEvent event) {
   byte[] handle, data, functionNameBytes, unique;
   GearmanPacket p = event.getPacket();
   GearmanJobServerSession sess = event.getSession();
   String functionName;
   handle = p.getDataComponentValue(GearmanPacket.DataComponentName.JOB_HANDLE);
   functionNameBytes = p.getDataComponentValue(GearmanPacket.DataComponentName.FUNCTION_NAME);
   data = p.getDataComponentValue(GearmanPacket.DataComponentName.DATA);
   unique = p.getDataComponentValue(DataComponentName.UNIQUE_ID);
   functionName = ByteUtils.fromUTF8Bytes(functionNameBytes);
   FunctionDefinition def = functionMap.get(functionName);
   if (def == null) {
     GearmanTask gsr =
         new GearmanTask(
             new GearmanPacketImpl(GearmanPacketMagic.REQ, GearmanPacketType.WORK_FAIL, handle));
     sess.submitTask(gsr);
   } else {
     GearmanFunction function = def.getFactory().getFunction();
     function.setData(data);
     function.setJobHandle(handle);
     function.registerEventListener(sess);
     if (unique != null && unique.length > 0) {
       function.setUniqueId(unique);
     }
     functionList.add(function);
   }
 }
  public void handleSessionEvent(GearmanSessionEvent event)
      throws IllegalArgumentException, IllegalStateException {
    GearmanPacket p = event.getPacket();
    GearmanJobServerSession s = event.getSession();
    GearmanPacketType t = p.getPacketType();
    Map<JobHandle, GearmanJobImpl> jobsMaps = sessionJobsMap.get(s);

    switch (t) {
      case JOB_CREATED:
        if (jobAwatingCreation == null) {
          throw new IllegalStateException(
              "Recevied job creation " + "message but have not job awaiting submission.");
        }
        if (!jobAwatingCreation.isBackgroundJob()) {
          jobsMaps.put(new JobHandle(jobAwatingCreation), jobAwatingCreation);
        }
        break;
      case WORK_DATA:
      case WORK_STATUS:
      case WORK_WARNING:
      case WORK_COMPLETE:
      case WORK_FAIL:
      case WORK_EXCEPTION:
        JobHandle handle =
            new JobHandle(p.getDataComponentValue(GearmanPacket.DataComponentName.JOB_HANDLE));
        GearmanJobImpl job = jobsMaps.get(handle);
        if (job == null) {
          LOG.warn(
              "Client received packet from server"
                  + " for unknown job ( job_handle = "
                  + handle
                  + " packet = "
                  + t
                  + " )");
        } else {
          job.handleEvent(p);
          if (job.isDone()) {
            jobsMaps.remove(handle);
          }
        }
        break;
      case ERROR:
        String errCode =
            ByteUtils.fromUTF8Bytes(
                p.getDataComponentValue(GearmanPacket.DataComponentName.ERROR_CODE));
        String errMsg =
            ByteUtils.fromUTF8Bytes(
                p.getDataComponentValue(GearmanPacket.DataComponentName.ERROR_TEXT));
        LOG.warn(
            "Received error code "
                + errCode
                + "( "
                + errMsg
                + " )"
                + " from session "
                + s
                + ". Shutting session down");
        shutDownSession(s);
        if (sessionsMap.isEmpty()) {
          shutdown();
        }
        break;
      default:
        LOG.warn(
            "received un-expected packet from Job"
                + " Server Session: "
                + p
                + ". Shutting down session");
        shutDownSession(s);
        if (sessionsMap.isEmpty()) {
          shutdown();
        }
    }
  }