Search in sources :

Example 1 with IDataIterator

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);
            }
        }
    }
}
Also used : IDataIterator(nl.nn.adapterframework.core.IDataIterator) TimeOutException(nl.nn.adapterframework.core.TimeOutException) TransformerException(javax.xml.transform.TransformerException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) DomBuilderException(nl.nn.adapterframework.util.DomBuilderException) IOException(java.io.IOException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) TimeOutException(nl.nn.adapterframework.core.TimeOutException) SenderException(nl.nn.adapterframework.core.SenderException)

Aggregations

IOException (java.io.IOException)1 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)1 TransformerException (javax.xml.transform.TransformerException)1 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)1 IDataIterator (nl.nn.adapterframework.core.IDataIterator)1 SenderException (nl.nn.adapterframework.core.SenderException)1 TimeOutException (nl.nn.adapterframework.core.TimeOutException)1 DomBuilderException (nl.nn.adapterframework.util.DomBuilderException)1