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;
}
}
Aggregations