Search in sources :

Example 1 with CloseableIterator

use of org.springframework.integration.util.CloseableIterator in project spring-integration by spring-projects.

the class FileSplitter method splitMessage.

@Override
protected Object splitMessage(final Message<?> message) {
    Object payload = message.getPayload();
    Reader reader = null;
    final String filePath;
    if (payload instanceof String) {
        try {
            reader = new FileReader((String) payload);
            filePath = (String) payload;
        } catch (FileNotFoundException e) {
            throw new MessageHandlingException(message, "failed to read file [" + payload + "]", e);
        }
    } else if (payload instanceof File) {
        try {
            if (this.charset == null) {
                reader = new FileReader((File) payload);
            } else {
                reader = new InputStreamReader(new FileInputStream((File) payload), this.charset);
            }
            filePath = ((File) payload).getAbsolutePath();
        } catch (FileNotFoundException e) {
            throw new MessageHandlingException(message, "failed to read file [" + payload + "]", e);
        }
    } else if (payload instanceof InputStream) {
        if (this.charset == null) {
            reader = new InputStreamReader((InputStream) payload);
        } else {
            reader = new InputStreamReader((InputStream) payload, this.charset);
        }
        filePath = buildPathFromMessage(message, ":stream:");
    } else if (payload instanceof Reader) {
        reader = (Reader) payload;
        filePath = buildPathFromMessage(message, ":reader:");
    } else {
        return message;
    }
    final BufferedReader bufferedReader = new BufferedReader(reader) {

        @Override
        public void close() throws IOException {
            try {
                super.close();
            } finally {
                Closeable closeableResource = StaticMessageHeaderAccessor.getCloseableResource(message);
                if (closeableResource != null) {
                    closeableResource.close();
                }
            }
        }
    };
    String firstLineAsHeader;
    if (this.firstLineHeaderName != null) {
        try {
            firstLineAsHeader = bufferedReader.readLine();
        } catch (IOException e) {
            throw new MessageHandlingException(message, "IOException while reading first line", e);
        }
    } else {
        firstLineAsHeader = null;
    }
    Iterator<Object> iterator = new CloseableIterator<Object>() {

        boolean markers = FileSplitter.this.markers;

        boolean sof = this.markers;

        boolean eof;

        boolean done;

        String line;

        long lineCount;

        boolean hasNextCalled;

        @Override
        public boolean hasNext() {
            this.hasNextCalled = true;
            try {
                if (!this.done && this.line == null) {
                    this.line = bufferedReader.readLine();
                }
                boolean ready = !this.done && this.line != null;
                if (!ready) {
                    if (this.markers) {
                        this.eof = true;
                        if (this.sof) {
                            this.done = true;
                        }
                    }
                    bufferedReader.close();
                }
                return this.sof || ready || this.eof;
            } catch (IOException e) {
                try {
                    this.done = true;
                    bufferedReader.close();
                } catch (IOException e1) {
                // ignored
                }
                throw new MessageHandlingException(message, "IOException while iterating", e);
            }
        }

        @Override
        public Object next() {
            if (!this.hasNextCalled) {
                hasNext();
            }
            this.hasNextCalled = false;
            if (this.sof) {
                this.sof = false;
                return markerToReturn(new FileMarker(filePath, Mark.START, 0));
            }
            if (this.eof) {
                this.eof = false;
                this.markers = false;
                this.done = true;
                return markerToReturn(new FileMarker(filePath, Mark.END, this.lineCount));
            }
            if (this.line != null) {
                String line = this.line;
                this.line = null;
                this.lineCount++;
                AbstractIntegrationMessageBuilder<String> messageBuilder = getMessageBuilderFactory().withPayload(line);
                if (firstLineAsHeader != null) {
                    messageBuilder.setHeader(FileSplitter.this.firstLineHeaderName, firstLineAsHeader);
                }
                return messageBuilder;
            } else {
                this.done = true;
                throw new NoSuchElementException(filePath + " has been consumed");
            }
        }

        private AbstractIntegrationMessageBuilder<Object> markerToReturn(FileMarker fileMarker) {
            Object payload;
            if (FileSplitter.this.markersJson) {
                try {
                    payload = objectMapper.toJson(fileMarker);
                } catch (Exception e) {
                    throw new MessageHandlingException(message, "Failed to convert marker to JSON", e);
                }
            } else {
                payload = fileMarker;
            }
            return getMessageBuilderFactory().withPayload(payload).setHeader(FileHeaders.MARKER, fileMarker.mark.name());
        }

        @Override
        public void close() {
            try {
                this.done = true;
                bufferedReader.close();
            } catch (IOException e) {
            // ignored
            }
        }
    };
    if (this.iterator) {
        return iterator;
    } else {
        List<Object> lines = new ArrayList<Object>();
        while (iterator.hasNext()) {
            lines.add(iterator.next());
        }
        return lines;
    }
}
Also used : CloseableIterator(org.springframework.integration.util.CloseableIterator) InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Closeable(java.io.Closeable) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) IOException(java.io.IOException) MessageHandlingException(org.springframework.messaging.MessageHandlingException) FileInputStream(java.io.FileInputStream) MessageHandlingException(org.springframework.messaging.MessageHandlingException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

BufferedReader (java.io.BufferedReader)1 Closeable (java.io.Closeable)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 ArrayList (java.util.ArrayList)1 NoSuchElementException (java.util.NoSuchElementException)1 CloseableIterator (org.springframework.integration.util.CloseableIterator)1 MessageHandlingException (org.springframework.messaging.MessageHandlingException)1