use of nl.nn.adapterframework.core.IDataIterator in project iaf by ibissource.
the class IteratingPipe method sendMessage.
protected String sendMessage(Object input, IPipeLineSession session, String correlationID, ISender sender, Map threadContext) throws SenderException, TimeOutException {
// sendResult has a messageID for async senders, the result for sync senders
boolean keepGoing = true;
IDataIterator it = null;
try {
ItemCallback callback = new ItemCallback(session, correlationID, sender);
it = getIterator(input, session, correlationID, threadContext);
if (it == null) {
iterateInput(input, session, correlationID, threadContext, callback);
} else {
String nextItemStored = null;
while (keepGoing && (it.hasNext() || nextItemStored != null)) {
if (Thread.currentThread().isInterrupted()) {
throw new TimeOutException("Thread has been interrupted");
}
StringBuffer items = new StringBuffer();
if (getBlockSize() > 0) {
items.append(getBlockPrefix());
for (int i = 0; i < getBlockSize() && it.hasNext(); i++) {
String item = (String) it.next();
items.append(getLinePrefix());
items.append(item);
items.append(getLineSuffix());
}
items.append(getBlockSuffix());
keepGoing = callback.handleItem(items.toString());
} else {
if (getStartPosition() >= 0 && getEndPosition() > getStartPosition()) {
items.append(getBlockPrefix());
String keyPreviousItem = null;
boolean sameKey = true;
while (sameKey && (it.hasNext() || nextItemStored != null)) {
String item;
if (nextItemStored == null) {
item = (String) it.next();
} else {
item = nextItemStored;
nextItemStored = null;
}
String key;
if (getEndPosition() >= item.length()) {
key = item.substring(getStartPosition());
} else {
key = item.substring(getStartPosition(), getEndPosition());
}
if (keyPreviousItem == null || key.equals(keyPreviousItem)) {
items.append(getLinePrefix());
items.append(item);
items.append(getLineSuffix());
if (keyPreviousItem == null) {
keyPreviousItem = key;
}
} else {
sameKey = false;
nextItemStored = item;
}
}
items.append(getBlockSuffix());
keepGoing = callback.handleItem(items.toString());
} else {
String item = getItem(it);
items.append(getLinePrefix());
items.append(item);
items.append(getLineSuffix());
keepGoing = callback.handleItem(item);
}
}
}
}
String results = "";
if (isCollectResults()) {
StringBuffer callbackResults = callback.getResults();
callbackResults.insert(0, "<results count=\"" + callback.getCount() + "\">\n");
callbackResults.append("</results>");
results = callbackResults.toString();
} else {
results = "<results count=\"" + callback.getCount() + "\"/>";
}
return results;
} finally {
if (it != null) {
try {
if (isCloseIteratorOnExit()) {
it.close();
}
} catch (Exception e) {
log.warn("Exception closing iterator", e);
}
}
}
}
Aggregations