Esempio n. 1
0
  /**
   * This gets a String to be split in the message object.It splits the string into substrings and
   * then stores them in the message object to be apssed to the next processors individually. This
   * class expects at least a single next processor hence it return a null if there are no next
   * processors.
   *
   * @param msgObj The MesageObject containg the string.
   * @return NVPair[] Name-Value pair array of next processor name and the MessageObject
   * @exception ProcessingException Thrown if processing fails
   */
  public NVPair[] process(MessageProcessorContext context, MessageObject msgObj)
      throws MessageException, ProcessingException {

    if (Debug.isLevelEnabled(Debug.BENCHMARK))
      Debug.log(Debug.BENCHMARK, "Reached Splitter process method..");

    // Debug.log(null, Debug.UNIT_TEST,"MESSAGE OBJECT RECEIVED:--->"+msgObj.describe());

    if (msgObj == null) {
      return null;
    } else if (toProcessorNames == null) {
      Debug.log(Debug.ALL_WARNINGS, "Splitter: No next processors available. Hence exiting.");
    } else if (!(msgObj.get() instanceof String)) {
      if (Debug.isLevelEnabled(Debug.UNIT_TEST))
        Debug.log(Debug.UNIT_TEST, "SPLITTER : UNIT_TEST: " + ((msgObj.get()).getClass()));

      throw new ProcessingException("ERROR: Splitter: " + "The msg Object must be a String.");
    }

    if (Debug.isLevelEnabled(Debug.UNIT_TEST))
      Debug.log(Debug.UNIT_TEST, "MESSAGE OBJECT RECEIVED:--->" + msgObj.describe());

    String batchedRecords = msgObj.getString();

    List singleRecord;
    String subStr = "";
    // to take care empty batch files that are valid files in some cases
    if (!StringUtils.hasValue(batchedRecords)) {
      Debug.log(Debug.ALL_WARNINGS, "MESSAGE CONTENT IS NULL");
      return null;
    }

    if (!StringUtils.hasValue(fileSeparator)) // if fileseparator is false...
    {
      if (batchedRecords.length() < splitLength) {
        throw new MessageException(
            "ERROR: Splitter: " + "Batched String length is less than " + splitLength + "bytes.");
      }

      float tempSize = (float) (batchedRecords.length()) / splitLength;
      int size = (batchedRecords.length()) / splitLength;

      // check for length of string
      if (size != tempSize) {
        throw new MessageException(
            "ERROR: Splitter: " + "Record received is not of correct length");
      }
      singleRecord = new Vector(size);
      int i = 0;
      for (i = 0; i < size; i++) {

        subStr = batchedRecords.substring(i * splitLength, (i + 1) * splitLength);
        singleRecord.add(i, subStr);
      }
    } else // if fileseparator is true...
    {
      singleRecord = new Vector();
      StringTokenizer st = new StringTokenizer(batchedRecords, fileSeparator);
      while (st.hasMoreTokens()) {
        subStr = st.nextToken();
        if (subStr.length() != splitLength) {
          throw new MessageException(
              "ERROR: Splitter: Record "
                  + "received after removing the fileseparator is not of correct length .");
        }
        singleRecord.add(subStr);

        if (Debug.isLevelEnabled(Debug.UNIT_TEST))
          Debug.log(
              Debug.UNIT_TEST,
              "\nDATA\n" + "********************************************" + subStr);
      }
    }

    if (getBoolean(truncHeaderFooter)) // if truncHeaderFooter is true...
    {
      singleRecord.remove((singleRecord.size() - 1));
      singleRecord.remove(0);
    }
    // else continue...
    int batchedSize = singleRecord.size();

    if (Debug.isLevelEnabled(Debug.UNIT_TEST))
      Debug.log(Debug.UNIT_TEST, "BATCHED SIZE--->" + batchedSize);

    int processorsNo = toProcessorNames.length;
    NVPair[] contents = new NVPair[batchedSize * processorsNo];
    int i = 0;

    for (i = 0; i < batchedSize; i++) {
      MessageObject mObj = new MessageObject();
      mObj.set(singleRecord.get(i));

      // Sending the message objects to the next processors.
      for (int j = 0; j < processorsNo; j++) {

        contents[(i * (processorsNo)) + j] = new NVPair(toProcessorNames[j], mObj);

        if (Debug.isLevelEnabled(Debug.UNIT_TEST))
          Debug.log(
              Debug.UNIT_TEST,
              "NEXT PROCESSOR-->"
                  + toProcessorNames[j]
                  + "\n"
                  + "MESSAGE OBJECT CONTENT------------>"
                  + mObj.describe());
      }
    }

    if (Debug.isLevelEnabled(Debug.UNIT_TEST))
      Debug.log(Debug.UNIT_TEST, "SPLITTER : CONTEXT---->" + context.describe());

    return contents;
  }